Skip to content

Instantly share code, notes, and snippets.

@caldofran
Created November 20, 2019 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caldofran/a7f6ef90ab4d98da1ae20bf4dd75d5f9 to your computer and use it in GitHub Desktop.
Save caldofran/a7f6ef90ab4d98da1ae20bf4dd75d5f9 to your computer and use it in GitHub Desktop.
Whole navigation implementation: Navigation + Navigator
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")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment