Skip to content

Instantly share code, notes, and snippets.

@caldofran
Created November 20, 2019 15:33
Show Gist options
  • Select an option

  • Save caldofran/a7f6ef90ab4d98da1ae20bf4dd75d5f9 to your computer and use it in GitHub Desktop.

Select an option

Save caldofran/a7f6ef90ab4d98da1ae20bf4dd75d5f9 to your computer and use it in GitHub Desktop.

Revisions

  1. caldofran created this gist Nov 20, 2019.
    97 changes: 97 additions & 0 deletions whole_navigation.swift
    Original 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")
    }
    }