Skip to content

Instantly share code, notes, and snippets.

@AndresR173
Created November 20, 2019 15:38
Show Gist options
  • Save AndresR173/8d9a61c42821d6246767cf1ace3fe1fc to your computer and use it in GitHub Desktop.
Save AndresR173/8d9a61c42821d6246767cf1ace3fe1fc to your computer and use it in GitHub Desktop.
Coordinator Pattern
//
// Coordinator.swift
// CoordinatorPatternDemo
//
// Created by Andres Rojas on 18/11/19.
// Copyright © 2019 Andres Rojas. All rights reserved.
//
import UIKit
protocol Coordinator: AnyObject {
var childCoordinators: [Coordinator] { get set }
var navigationController: UINavigationController { get set }
func start()
}
//
// MainCoordinator.swift
// CoordinatorPatternDemo
//
// Created by Andres Rojas on 18/11/19.
// Copyright © 2019 Andres Rojas. All rights reserved.
//
import UIKit
class MainCoordinator: Coordinator {
// MARK: - Properties
var childCoordinators: [Coordinator] = [Coordinator]()
var navigationController: UINavigationController
// MARK: - LifeCycle
init(navigationController: UINavigationController) {
self.navigationController = navigationController
}
// MARK: - Helpers
func start() {
let viewController = ViewController()
viewController.coordinator = self
navigationController.pushViewController(viewController, animated: false)
}
func childDidFinish(_ child: Coordinator) {
for (index, coordinator) in childCoordinators.enumerated() {
if coordinator === child {
childCoordinators.remove(at: index)
break
}
}
}
}
// MARK: - ViewController Coordinator Delegate
extension MainCoordinator: ViewControllerCoordinatorDelegate {
func didShowBuyScreen(_ item: Item) {
let child = BuyCoordinator(navigationController: navigationController, item: item)
child.parentCoordinator = self
childCoordinators.append(child)
child.start()
}
func didShowCreateAccountScreen() {
let vc = CreateAccountViewController()
vc.coordinator = self
navigationController.pushViewController(vc, animated: true)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment