Created
November 20, 2019 15:38
-
-
Save AndresR173/8d9a61c42821d6246767cf1ace3fe1fc to your computer and use it in GitHub Desktop.
Coordinator Pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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