Last active
November 15, 2020 16:11
-
-
Save yoni-g/61bbe41e1de15e574fb7b7cf4d822344 to your computer and use it in GitHub Desktop.
A coordinator implementation I use In my projects [Used for one main coordinator, you can change it to work with multiple coordinators]
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 characters
import UIKit | |
protocol Coordinator { | |
var navigationController: UINavigationController? { get set } | |
// config funcs | |
func setNavControler(_ navCtrl: UINavigationController) -> Self | |
func start() | |
// display funcs | |
func show(view: AppScreen) | |
func showMainViewCtrl(animated: Bool) | |
func back() | |
var activeViewController: AppScreen { get set } | |
} |
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 characters
// | |
// MainCoordinator.swift | |
// InprisWay | |
// | |
// Created by Yonathan Goriachnick on 13/10/2020. | |
// Copyright © 2020 200apps. All rights reserved. | |
// | |
import Foundation | |
import UIKit | |
class MainCoordinator: Coordinator{ | |
internal var navigationController: UINavigationController? | |
internal var activeViewController: AppScreen = .mainScreen | |
class func shared() -> MainCoordinator { | |
return sharedManager | |
} | |
private static var sharedManager = MainCoordinator() | |
private init() {} | |
func start(){ | |
showMainViewCtrl(animated: false) | |
} | |
// set the nav bar | |
func setNavControler(_ navCtrl: UINavigationController) -> Self{ | |
self.navigationController = navCtrl | |
return self | |
} | |
func show(view: AppScreen){ | |
activeViewController = view | |
switch view { | |
case .settingsListScreen: | |
let vc = SettingsListViewController.instantiate() | |
vc.settingsViewModal = SettingsListViewModal() | |
push(vc) | |
case .aboutScreen: | |
let vc = AboutViewController.instantiate() | |
push(vc) | |
case .mainScreen: | |
showMainViewCtrl(animated: false) | |
case .settingScreen(let type): | |
let vc = SettingViewController.instantiate() | |
vc.viewModal = SettingViewModal(type) | |
push(vc) | |
case .searchScreen(let settingItems): | |
let vc = SearchableListViewController.instantiate() | |
vc.settingItems = settingItems | |
vc.modalPresentationStyle = .pageSheet | |
navigationController?.topViewController?.show(vc, sender: true) | |
default: | |
break | |
} | |
} | |
internal func showMainViewCtrl(animated: Bool){ | |
activeViewController = .mainScreen | |
let vc = MainViewController.instantiate() | |
vc.viewModal = MainViewCtrlViewModal() | |
navigationController?.pushViewController(vc, animated: animated) | |
} | |
func back(){ | |
navigationController?.popViewController(animated: true) | |
} | |
var topViewController: UIViewController?{ | |
return navigationController?.topViewController | |
} | |
func push(_ vc: UIViewController){ | |
navigationController?.pushViewController(vc, animated: true) | |
} | |
} |
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 characters
class Usage{ | |
// setup coordinator in AppDelegate or SceneDelegate | |
let navController = UINavigationController() | |
MainCoordinator.shared().setNavControler(navController).start() | |
// AppDelegate or SceneDelegate setting | |
// config window | |
let window = UIWindow(windowScene: windowScene) | |
window.rootViewController = navController | |
self.window = window | |
window.makeKeyAndVisible() | |
// show screen | |
MainCoordinator.shared().show(view: .aboutScreen) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.