Skip to content

A simple and flexible implementation of the Coordinator pattern. Written in Swift.

Notifications You must be signed in to change notification settings

mugx/Koordinator

Repository files navigation

Koordinator

Koordinator is a simple and flexible implementation of the Coordinator pattern.

Overview

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.

Example

  • Let's say we want to push a view controller into a navigation stack, just need to subclass a BaseCoordinator and call the start 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 simple UIViewController:
let myNavigation: UINavigationController!

...

let coordinator = DemoCoordinator(presenter: myNavigation.visibleViewController)
coordinator.start()

Framework

The framework offers the following protocols and helper classes:

  1. 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)
}
  1. Presenter: it's the entity that shows the view controller.
    If the presenter is a:
    • UIViewController: the view controller is presented
    • UINavigationViewController: the view controller is pushed
    • UITabBarController: the view controller is appended as a new tab
    • UIWindow: the view controller becomes the root
protocol Presenter: class {
    func show(_ viewController: UIViewController)
}
  1. BaseCoordinator: Koordinator: plug and play implementation of the coordinator

  2. BaseViewController: UIViewController: helps to manage pop/dismiss callbacks to the BaseCoordinator

About

A simple and flexible implementation of the Coordinator pattern. Written in Swift.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages