Skip to content

Instantly share code, notes, and snippets.

@yoni-g
Last active November 15, 2020 16:11
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 yoni-g/61bbe41e1de15e574fb7b7cf4d822344 to your computer and use it in GitHub Desktop.
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]
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 }
}
//
// 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)
}
}
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)
}
@yoni-g
Copy link
Author

yoni-g commented Nov 15, 2020

// apps' views -> we can also split into separate enums for each child coordinator
enum AppScreen {
    case loginScreen
    case agreementScreen
    
    case mainScreen
    case navigationScreen
    case callScreen
    case radioScreen
//    case aboutScreen
//    case messagesScreen
    
    case settingsListScreen
    case settingScreen(_ type: SettingType)
    case searchScreen(_ settingItems: [SettingsPresentable])
    case aboutScreen
//    case messagesSettings
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment