Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Data driven view controllers

Data driven view controllers

Originally posted here: https://speakerdeck.com/vmalakhovskiy/data-driven-view-controllers

Github link: https://github.com/vmalakhovskiy/data-driven-vc

Vitaly about his workshop:
"I will show how to unify/simplify view-presenter communication, using data-driven approauch. Of cource a bit about functional programming, code generation, comparison with other architectures and more."

This workshop was made for CocoaHeads Kyiv #14 which took place Oct 6 2018.

CocoaHeads Ukraine

October 06, 2018
Tweet

More Decks by CocoaHeads Ukraine

Other Decks in Programming

Transcript

  1. Plan » what is data driven view controllers? » basic

    examples » code » home work: github Vitalii Malakhovskyi, BetterMe 3
  2. Doesn't know about » entities » managed objects » models

    » services » etc Vitalii Malakhovskyi, BetterMe 8
  3. May contain: » string, number, bool » struct, enum, array

    » nested » closure <- for output Vitalii Malakhovskyi, BetterMe 10
  4. struct Props { let title: String let journeys: [ViewData.Journey] struct

    Journey { let name: String let description: String let image: UIImage let progress: String let isLocked: Bool let onSelect: () -> () } } Vitalii Malakhovskyi, BetterMe 16
  5. struct Props { let title: String let journeys: [ViewData.Journey] struct

    Journey { let name: String let description: String let image: UIImage let progress: String let isLocked: Bool let onSelect: () -> () } } Vitalii Malakhovskyi, BetterMe 17
  6. wrong struct Interface { let programs: [Program] let temperatures: [Temperature]

    let RPMs: [RPM] enum Program {} enum Temperature {} enum RPM {} } Vitalii Malakhovskyi, BetterMe 21
  7. enum Props { case loading([Loading]) case error(title: String, message: String,

    onReload: () -> ()) case data([Data]) struct Data {} struct Loading {} } Vitalii Malakhovskyi, BetterMe 24
  8. func render(props: Props) { self.props = props view.setNeedsLayout() } func

    viewWillLayoutSubviews() { super.viewWillLayoutSubviews() /// redraw view state } Vitalii Malakhovskyi, BetterMe 28
  9. class ViewController: UIViewController { func viewDidLoad() { viewModel.viewData.producer.startWithValues { _

    in view.setNeedsLayout() } } func viewWillLayoutSubviews() { switch viewModel.viewData.value { ... } } } Vitalii Malakhovskyi, BetterMe 34
  10. how to achieve this? » reactive cocoa » rxswift »

    handmade observations » KVO Vitalii Malakhovskyi, BetterMe 35
  11. /// View -> Presenter protocol ViewOutput { func viewIsReady() }

    /// Presenter -> View protocol PresenterOutput: { func receiveUpdate(with data: ViewData) } Vitalii Malakhovskyi, BetterMe 39
  12. class ViewController: UIViewController { struct ViewData {} func render(viewData: ViewData)

    { self.viewData = viewData view.setNeedsLayout() } } struct Presenter { let render: (ViewData) -> () func handleNewAppState(...) // calls render } Vitalii Malakhovskyi, BetterMe 43
  13. * simple input * input + validation * multistate screen

    * animation * MVVM vc DD MVVM * VIPER vc DD VIPER * MVC vc DD MVC Vitalii Malakhovskyi, BetterMe 47
  14. * Data-Driven View Controllers. Tips and Tricks * github *

    Redux Vitalii Malakhovskyi, BetterMe 50