$30 off During Our Annual Pro Sale. View Details »

Exploring MVVM-C

Aydar
April 12, 2018

Exploring MVVM-C

Presentation for the Munich iOS Developers Meetup

Aydar

April 12, 2018
Tweet

More Decks by Aydar

Other Decks in Programming

Transcript

  1. • MVVM • From MVC to MVVM • Closer look

    at MVVM • Coordinator • Tasks • Classic solution • Solution with Coordinators • MVVM + Coordinator Agenda 8
  2. • MVVM • From MVC to MVVM • Closer look

    at MVVM • Coordinator • Tasks • Classic solution • Solution with Coordinators • MVVM + Coordinator 9
  3. UIViewController is a “God” class Business logic is mixed with

    UI Hard to reuse Hard to implement tests From MVC to MVVM MVC 12
  4. • MVVM • From MVC to MVVM • Closer look

    at MVVM • Coordinator • Tasks • Classic solution • Solution with Coordinators • MVVM + Coordinator 16
  5. 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
  6. • Draws UI with provided data • Notifies ViewModel about

    user actions View Responsibilities Closer look at MVVM 22
  7. • 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
  8. Business logic is decoupled from UI Easy to maintain Easy

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

  10. • MVVM • From MVC to MVVM • Closer look

    at MVVM • Coordinator • Tasks • Classic solution • Solution with Coordinators • MVVM + Coordinator 26
  11. E new D E new new D C A B

    C visible Push next A B visible +1 +n 30 Tasks
  12. D E C E D A B C visible Go

    back A B visible -1 -n 31 Tasks
  13. • MVVM • From MVC to MVVM • Closer look

    at MVVM • Coordinator • Tasks • Classic solution • Solution with Coordinators • MVVM + Coordinator 33
  14. D Classic solution ... func showNext() { let vc: UIViewController

    if isOneFlow { vc = E() } else { vc = P() } push(vc) } ... Chained navigation 38
  15. • MVVM • From MVC to MVVM • Closer look

    at MVVM • Coordinator • Tasks • Classic solution • Solution with Coordinators • MVVM + Coordinator 44
  16. Solution with Coordinators E D A B C Coordinator 1.

    done 2. init push 3. done 4. init push 47
  17. Solution with Coordinators E D A B C Coordinator 1.

    done 2. init push 3. done 4. init push 48
  18. 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
  19. Solution with Coordinators E D A B C Coordinator 1.

    done 2. init push 3. done 4. init push 50
  20. 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
  21. 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
  22. Solution with Coordinators +1 +n E D A B C

    Coord A onDone() push(e) visible -1 60
  23. Solution with Coordinators E D A B C Coord A

    P M N O D Coord B +1 +n -1 62
  24. 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
  25. Solution with Coordinators +1 +n -1 -n Chained navigation Coordinators

    OK, but dependencies OK, but dependencies OK NOT OK OK OK OK OK 68
  26. • MVVM • From MVC to MVVM • Closer look

    at MVVM • Coordinator • Tasks • Classic solution • Solution with Coordinators • MVVM + Coordinator 69
  27. 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
  28. Thank you! 74 aydar.mukh [email protected] 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/