guoyingtao / CoordinatorPatternExample

My practice of applying coordinator pattern in an iOS project

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CoordinatorPatternExample

My practice of applying coordinator pattern in an iOS project

Granularity

  • App coordinator
  • Main coordinator
  • Other child coordinators

Usually, nagivation includes

  1. using navigation controller to push a view controller to its child view controllers stack
  2. preseting another view controller

So bedisde Coordinator protocol, I make PresentingCoordinator and NavigationCoordinator too. They both conform to Coordinator protocol but have their own navigation functions.

Also, to make things easier, I made three base classes

  1. class BasePresentingCoordinator: PresentingCoordinator
  2. class BaseNavigationCoordinator: NavigationCoordinator
  3. class BaseVersatileCoordinator: VersatileCoordinator (VersatileCoordinator:PresentingCoordinator, NavigationCoordinator)

For a child coordinator, if the view controller it manages is being pushed by a navigation controller, choose NavigationCoordinator as its parent class and use navigate(:::) to show the view controller.
if the view controller it manages is being presented, choose PresentingCoordinator as its parent class and use present(:::) to show the view controller.
If the view controller can be pushed and presented, then use BaseVersatileCoordinator as its parent class.

How to handle backward events

Implementing a BackwardConscious protocol to make a view controller to know then it is being pushed back or dismissed.

How to pass value between coordinators

From parent to child,

use initial functions

From child to parent, use closure when handling backward events.

  • Make the view controller conforms BackwardConscious protocol
  • Implement getPassingInfo()
  • Set removeChildHandler inside the coordinator
removeChildHandler = { [weak vc] child, info in
            if child is OperationCoordinator, let result = info?["result"] as? Int  {
                vc?.setResult(result)
                return
            }
            
            if child is SettingsCoordinator, let number = info?["initialNumber"] as? Int {
                vc?.setInitialNumber(number)
                return
            }            
        }

About

My practice of applying coordinator pattern in an iOS project

License:MIT License


Languages

Language:Swift 100.0%