Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Router.swift
- //
- // Created by Maksim Petrenko on 22.10.16.
- // Copyright © 2017 MaxPayne. All rights reserved.
- //
- import UIKit
- let appRouter = Router()
- public struct Screen {
- var name: String
- var viewControllerIdentifier: String
- var storyboardName: String
- var resourceID: String?
- }
- extension Screen: Equatable {
- public static func ==(lhs: Screen, rhs: Screen) -> Bool {
- let areEqual = lhs.name == rhs.name &&
- lhs.viewControllerIdentifier == rhs.viewControllerIdentifier &&
- lhs.storyboardName == rhs.storyboardName &&
- lhs.resourceID == rhs.resourceID
- return areEqual
- }
- }
- public final class Router {
- public static let MAIN_STORYBOARD_NAME = "Main"
- public static let MAIN_SCREEN_NAME = "Main"
- public static let MAIN_VIEW_CONTROLLER_ID = "mainViewController"
- public static let OPTIONS_SCREEN_NAME = "Options"
- public static let OPTIONS_STORYBOARD_NAME = "Options"
- public static let OPTIONS_VIEW_CONTROLLER_ID = "optionsViewController"
- public static let PROFILE_SCREEN_NAME = "Profile"
- public static let PROFILE_STORYBOARD_NAME = "Profile"
- public static let PROFILE_VIEW_CONTROLLER_ID = "profileViewController"
- private var screens = Array<Screen>()
- public func register(screen: Screen) {
- if !self.screens.contains(screen) {
- self.screens.append(screen)
- }
- }
- public func openScreen(withName name: String, andResourceID resourceID: String?, sourceVC: UIViewController?, modalMode: Bool = false) {
- if let mainNavController = (UIApplication.shared.delegate as! AppDelegate).window?.rootViewController as? UINavigationController {
- if let currentVC = mainNavController.visibleViewController {
- if let screen = self.getScreen(byName: name, andResourceID: resourceID) {
- if let destinationViewController = self.getViewController(from: screen) {
- destinationViewController.assembleModule!(forResourceID: resourceID, sourceVC: sourceVC)
- let destVC = destinationViewController as! UIViewController
- if modalMode {
- let modalNavigationController = UINavigationController(rootViewController: destVC)
- modalNavigationController.navigationBar.isTranslucent = false
- currentVC.present(modalNavigationController, animated: true, completion: nil)
- } else {
- currentVC.navigationController?.pushViewController(destVC, animated: true)
- }
- }
- }
- }
- }
- }
- public func openScreen(withName name: String, andResource resource: AnyObject?, sourceVC: UIViewController?, modalMode: Bool = false) {
- if let mainNavController = (UIApplication.shared.delegate as! AppDelegate).window?.rootViewController as? UINavigationController {
- if let currentVC = mainNavController.visibleViewController {
- if let screen = self.getScreen(byName: name, andResourceID: nil) {
- if let destinationViewController = self.getViewController(from: screen) {
- destinationViewController.assembleModule!(forResource: resource, sourceVC: sourceVC)
- let destVC = destinationViewController as! UIViewController
- if modalMode {
- let modalNavigationController = UINavigationController(rootViewController: destVC)
- modalNavigationController.navigationBar.isTranslucent = false
- currentVC.present(modalNavigationController, animated: true, completion: nil)
- } else {
- currentVC.navigationController?.pushViewController(destVC, animated: true)
- }
- }
- }
- }
- }
- }
- public func openScreen(withName name: String, modalMode: Bool = false) {
- if let mainNavController = (UIApplication.shared.delegate as! AppDelegate).window?.rootViewController as? UINavigationController {
- if let currentVC = mainNavController.visibleViewController {
- if let screen = self.getScreen(byName: name, andResourceID: nil) {
- if let destinationViewController = self.getViewController(from: screen) {
- destinationViewController.assembleModule!()
- let destVC = destinationViewController as! UIViewController
- if modalMode {
- let modalNavigationController = UINavigationController(rootViewController: destVC)
- modalNavigationController.navigationBar.isTranslucent = false
- currentVC.present(modalNavigationController, animated: true, completion: nil)
- } else {
- currentVC.navigationController?.pushViewController(destVC, animated: true)
- }
- }
- }
- }
- }
- }
- public func setRootScreen(withName name: String) {
- if let mainNavController = (UIApplication.shared.delegate as! AppDelegate).window?.rootViewController as? UINavigationController {
- if let screen = self.getScreen(byName: name, andResourceID: nil) {
- if let destinationViewController = self.getViewController(from: screen) {
- destinationViewController.assembleModule?()
- mainNavController.viewControllers = [destinationViewController as! UIViewController]
- }
- }
- }
- }
- public func getViewController(byScreenName screenName: String) -> UIViewController? {
- if let screen = self.getScreen(byName: screenName, andResourceID: nil) {
- if let destinationViewController = self.getViewController(from: screen) {
- destinationViewController.assembleModule?()
- return destinationViewController as? UIViewController
- }
- }
- return nil
- }
- public func getMainMenuViewController() -> UITabBarController? {
- if let mainNavigationController = self.getMainNavigationController() {
- if let mainMenuViewController = mainNavigationController.viewControllers.first(where: { (vc) -> Bool in
- return vc is UITabBarController
- }) {
- return mainMenuViewController as? UITabBarController
- }
- }
- return nil
- }
- public func getMainNavigationController() -> UINavigationController? {
- return (UIApplication.shared.delegate as! AppDelegate).window?.rootViewController as? UINavigationController
- }
- private func getScreen(byName name: String, andResourceID resourceID: String?) -> Screen? {
- return self.screens.first(where: { (screen) -> Bool in
- return name == screen.name && resourceID == screen.resourceID
- })
- }
- private func getViewController(from screen: Screen) -> ModuleViewController? {
- let storyboard = UIStoryboard(name: screen.storyboardName, bundle: Bundle.main)
- return storyboard.instantiateViewController(withIdentifier: screen.viewControllerIdentifier) as? ModuleViewController
- }
- }
Add Comment
Please, Sign In to add comment