Koordinator
is a simple and flexible implementation of the Coordinator pattern.
The Coordinator pattern can be used to solve the problem of orchestrating the iOS navigation.
This framework standardizes the navigation entities behaviour.
It also helps to manage the back button and the iOS 13 swipe down behaviour (isModalInPresentation
),
so that a coordinator is automatically notified if its view controller gets dismiss/pop.
- Let's say we want to push a view controller into a navigation stack, just need to subclass a
BaseCoordinator
and call thestart
function:
let myNavigation: UINavigationController!
...
let coordinator = DemoCoordinator(presenter: myNavigation)
coordinator.start()
The a sub class of the BaseCoordinator
could be simply defined as:
class DemoCoordinator: BaseCoordinator {
override func start() {
let someViewController = UIViewController()
show(someViewController)
}
}
- If, instead, we want to present modally a view controller, make sure the
Presenter
is a simpleUIViewController
:
let myNavigation: UINavigationController!
...
let coordinator = DemoCoordinator(presenter: myNavigation.visibleViewController)
coordinator.start()
The framework offers the following protocols and helper classes:
Koordinator
: it's the navigation orchestrator, can create children coordinators, can make/show view controllers, it essentially manages their life cycle.
protocol Koordinator {
associatedtype Koordinator: Hashable
var children: Set<Koordinator> { get set }
var presenter: Presenter? { get set }
func start()
func start(child: Koordinator)
func show(_ viewController: UIViewController)
}
Presenter
: it's the entity that shows the view controller.
If the presenter is a:UIViewController
: the view controller is presentedUINavigationViewController
: the view controller is pushedUITabBarController
: the view controller is appended as a new tabUIWindow
: the view controller becomes the root
protocol Presenter: class {
func show(_ viewController: UIViewController)
}
-
BaseCoordinator: Koordinator
: plug and play implementation of the coordinator -
BaseViewController: UIViewController
: helps to manage pop/dismiss callbacks to theBaseCoordinator