aboutsummaryrefslogtreecommitdiff
path: root/foray/Coordinators/Coordinator.swift
blob: 68585760452336cf259ca3eed3a5f94a3b57a07c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//
//  Coordinator.swift
//  foray
//
//  Created by Nicholas Tay on 21/3/2022.
//  Based on code from Paul Hudson on Hacking with Swift
//  (https://www.hackingwithswift.com/articles/71/how-to-use-the-coordinator-pattern-in-ios-apps)
//

import Foundation
import UIKit

protocol Coordinator {
    var childCoordinators: [Coordinator] { get set }
    var navigationController: UINavigationController { get set }
    
    func start()
}

protocol Coordinated: UIViewController {
    associatedtype CoordinatorType: Coordinator
    var coordinator: CoordinatorType? { get set }
}

extension Coordinator {
    func push<T: Coordinated>(vc: T, animated: Bool = true) {
        vc.coordinator = self as? T.CoordinatorType
        navigationController.pushViewController(vc, animated: animated)
    }
}