Exploring MVVM-C

52d6b8fd288036707ddd50eb6f595cb7?s=47 Aydar
April 12, 2018

Exploring MVVM-C

Presentation for the Munich iOS Developers Meetup

52d6b8fd288036707ddd50eb6f595cb7?s=128

Aydar

April 12, 2018
Tweet

Transcript

  1. 2.
  2. 3.
  3. 4.
  4. 5.
  5. 8.

    • MVVM • From MVC to MVVM • Closer look

    at MVVM • Coordinator • Tasks • Classic solution • Solution with Coordinators • MVVM + Coordinator Agenda 8
  6. 9.

    • MVVM • From MVC to MVVM • Closer look

    at MVVM • Coordinator • Tasks • Classic solution • Solution with Coordinators • MVVM + Coordinator 9
  7. 12.

    UIViewController is a “God” class Business logic is mixed with

    UI Hard to reuse Hard to implement tests From MVC to MVVM MVC 12
  8. 16.

    • MVVM • From MVC to MVVM • Closer look

    at MVVM • Coordinator • Tasks • Classic solution • Solution with Coordinators • MVVM + Coordinator 16
  9. 20.

    private(set) var dataSource: ListDataSource { didSet { uiDelegate?.didUpdateDataSource() } }

    // ... func refresh() { uiDelegate?.didStartLoading() provider.loadPlanets { [weak self] planets in self?.uiDelegate?.didFinishLoading() self?.dataSource = ListDataSource(planets: planets) } } ViewModel Communications Closer look at MVVM 20
  10. 21.
  11. 22.

    • Draws UI with provided data • Notifies ViewModel about

    user actions View Responsibilities Closer look at MVVM 22
  12. 23.

    • Handles user actions from View • Gets data from

    Model • Updates data in Model • Contains business logic • Provides updated/processed data to View ViewModel Responsibilities Closer look at MVVM 23
  13. 24.

    Business logic is decoupled from UI Easy to maintain Easy

    to test Easy to reuse components Advantages Closer look at MVVM 24
  14. 25.

    25

  15. 26.

    • MVVM • From MVC to MVVM • Closer look

    at MVVM • Coordinator • Tasks • Classic solution • Solution with Coordinators • MVVM + Coordinator 26
  16. 30.

    E new D E new new D C A B

    C visible Push next A B visible +1 +n 30 Tasks
  17. 31.

    D E C E D A B C visible Go

    back A B visible -1 -n 31 Tasks
  18. 33.

    • MVVM • From MVC to MVVM • Closer look

    at MVVM • Coordinator • Tasks • Classic solution • Solution with Coordinators • MVVM + Coordinator 33
  19. 38.

    D Classic solution ... func showNext() { let vc: UIViewController

    if isOneFlow { vc = E() } else { vc = P() } push(vc) } ... Chained navigation 38
  20. 44.

    • MVVM • From MVC to MVVM • Closer look

    at MVVM • Coordinator • Tasks • Classic solution • Solution with Coordinators • MVVM + Coordinator 44
  21. 47.

    Solution with Coordinators E D A B C Coordinator 1.

    done 2. init push 3. done 4. init push 47
  22. 48.

    Solution with Coordinators E D A B C Coordinator 1.

    done 2. init push 3. done 4. init push 48
  23. 49.

    Solution with Coordinators B Coordinator private let onDone: () ->

    () ... func donePressed() { onDone() } ... private func configuredA() -> A { let a = A(onDone: { self.navigationController? .push(configuredB()) }) return a } private func configuredB() -> B { let b = B(onDone: { self.navigationController? .push(configuredC()) }) return b } ... 49
  24. 50.

    Solution with Coordinators E D A B C Coordinator 1.

    done 2. init push 3. done 4. init push 50
  25. 52.

    Solution with Coordinators Coordinator init(navigationController: UINavigationController, onFinish: @escaping () ->

    ()) { self.navigationController = navigationController self.onFinish = onFinish } func start() -> UIViewController { return configuredA() } private func configuredA() -> A { // … } // … 52
  26. 56.

    Solution with Coordinators Coordinator private func startChildCoordinator() { let coord

    = ChildCoordinator(<…>, onFinish: { self.navigationController?.dismiss() }) navigationController?.present(coord.start()) } init(<…>, onFinish: @escaping () -> ()) { self.onFinish = onFinish <…> } private func configuredI() -> I { let i = I(onDone: { self.onFinish() }) return i } Child coordinator 56
  27. 60.

    Solution with Coordinators +1 +n E D A B C

    Coord A onDone() push(e) visible -1 60
  28. 62.

    Solution with Coordinators E D A B C Coord A

    P M N O D Coord B +1 +n -1 62
  29. 66.

    Solution with Coordinators Coordinator private func startChildCoordinator() { let topVC

    = navigationController!.topViewController let coord = ChildCoordinator(onFinish: { self.navigationController?.pop(to: topVC) }) navigationController?.push(coord.start()) } -n 66
  30. 68.

    Solution with Coordinators +1 +n -1 -n Chained navigation Coordinators

    OK, but dependencies OK, but dependencies OK NOT OK OK OK OK OK 68
  31. 69.

    • MVVM • From MVC to MVVM • Closer look

    at MVVM • Coordinator • Tasks • Classic solution • Solution with Coordinators • MVVM + Coordinator 69
  32. 72.

    MVVM stack 72 ViewModel Coordinator ViewController Model 1. Build stack

    2. Push viewController 3. Call injected callback 4. Push next | go back | etc MVVM + Coordinator
  33. 73.
  34. 74.

    Thank you! 74 aydar.mukh aydar.mukh@gmail.com aydarin Resources: • Steve "Scotty"

    Scott – MVVM-C In Practice: • https://www.youtube.com/watch?v=9VojuJpUuE8 • objc.io App Architecture book: • https://www.objc.io/books/app-architecture/ • Krzysztof Zabłocki - MVVM vs. MVC vs. VIPER: • https://academy.realm.io/posts/krzysztof-zablocki-mDevCamp-ios-architecture-mvvm-mvc-viper/
  35. 75.
  36. 76.
  37. 77.