Created
November 20, 2019 15:33
-
-
Save caldofran/a7f6ef90ab4d98da1ae20bf4dd75d5f9 to your computer and use it in GitHub Desktop.
Revisions
-
caldofran created this gist
Nov 20, 2019 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,97 @@ import UIKit enum Navigation { case section(Section) case modal(Screen) case push(Screen) } enum Section: Int { case home case messages case notifications case profile } struct Screen { let viewController: () -> UIViewController } extension Screen { static func offer(with id: String) -> Self { return .init() { OfferViewController(id: id) } } } extension Screen { static func job(with id: String) -> Self { return .init() { JobViewController(id: id) } } } extension Screen { static func document(with url: URL) -> Self { return .init() { DocumentViewController(url: url) } } } class Navigator { var tabBarController: UITabBarController! var navigationController: UINavigationController! func handle(navigation: Navigation, animated: Bool = true) { switch navigation { case .section(let section): tabBarController.selectedIndex = section.rawValue case .modal(let screen): tabBarController.present(screen.viewController(), animated: animated) case .push(let screen): navigationController.pushViewController(screen.viewController(), animated: animated) } } } let navigator = Navigator() navigator.handle(navigation: .section(.home)) navigator.handle(navigation: .modal(.offer(with: "1"))) navigator.handle(navigation: .push(.job(with: "1"))) class OfferViewController: UIViewController { private let id: String init(id: String) { self.id = id super.init(nibName: nil, bundle: nil) } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } } class JobViewController: UIViewController { private let id: String init(id: String) { self.id = id super.init(nibName: nil, bundle: nil) } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } } class DocumentViewController: UIViewController { private let url: URL init(url: URL) { self.url = url super.init(nibName: nil, bundle: nil) } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } }