Last active
April 28, 2020 23:52
-
-
Save rorodriguez116/b5f748582cf8b914381999724941f59d to your computer and use it in GitHub Desktop.
Quick example of Module Provider
This file contains 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 ModuleProvider | |
import UIKit | |
import Foundation | |
enum Module { | |
case none | |
case messages(YourConfigObject) | |
case feed | |
case profile | |
} | |
class ModuleProvider { | |
let shared = ModuleProvider() | |
private let rootNavigationController: UINavigationController | |
private let rootViewController: UIViewController | |
private var activeModule: Module = .none | |
var root: UIViewController { | |
self.rootViewController | |
} | |
private init() { | |
self.rootViewController = HomeScreenViewController() | |
self.rootNavigationController = UINavigationController(rootViewController: self.rootViewController) | |
NotificationCenter.default.publisher(for: .init(rawValue: "moduleChanged")).sink { [weak self] (notification) in | |
guard let userData = notification.userInfo as? [String: Any], let newActiveModule = userData["val"] as? ActiveModule else { return } | |
self?.activeModule = newActiveModule | |
self?.updateHierarchy() | |
} | |
} | |
func updateHierarchy() { | |
switch activeModule { | |
case messages(let settings): | |
let vc = MessagesViewController() | |
controller.yourCustomSettings = settings | |
self.rootNavigationController.popToRootViewController(animated: true) | |
self.rootViewController.push(vc) | |
case .none: | |
self.rootNavigationController.popToRootViewController(animated: true) | |
default: print("add code to launch this module...") | |
} | |
} | |
} | |
class SceneDelegate: UIResponder, UIWindowSceneDelegate { | |
var window: UIWindow? | |
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { | |
let rootViewController = ModuleProvider.shared.root() | |
if let windowScene = scene as? UIWindowScene { | |
let window = UIWindow(windowScene: windowScene) | |
window.rootViewController = rootViewController | |
self.window = window | |
window.makeKeyAndVisible() | |
} | |
} | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment