Guest User

Untitled

a guest
Oct 23rd, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.16 KB | None | 0 0
  1. //
  2. // Router.swift
  3. //
  4. // Created by Maksim Petrenko on 22.10.16.
  5. // Copyright © 2017 MaxPayne. All rights reserved.
  6. //
  7.  
  8. import UIKit
  9.  
  10. let appRouter = Router()
  11.  
  12. public struct Screen {
  13. var name: String
  14. var viewControllerIdentifier: String
  15. var storyboardName: String
  16. var resourceID: String?
  17. }
  18.  
  19. extension Screen: Equatable {
  20. public static func ==(lhs: Screen, rhs: Screen) -> Bool {
  21. let areEqual = lhs.name == rhs.name &&
  22. lhs.viewControllerIdentifier == rhs.viewControllerIdentifier &&
  23. lhs.storyboardName == rhs.storyboardName &&
  24. lhs.resourceID == rhs.resourceID
  25. return areEqual
  26. }
  27. }
  28.  
  29. public final class Router {
  30.  
  31. public static let MAIN_STORYBOARD_NAME = "Main"
  32. public static let MAIN_SCREEN_NAME = "Main"
  33. public static let MAIN_VIEW_CONTROLLER_ID = "mainViewController"
  34.  
  35. public static let OPTIONS_SCREEN_NAME = "Options"
  36. public static let OPTIONS_STORYBOARD_NAME = "Options"
  37. public static let OPTIONS_VIEW_CONTROLLER_ID = "optionsViewController"
  38.  
  39. public static let PROFILE_SCREEN_NAME = "Profile"
  40. public static let PROFILE_STORYBOARD_NAME = "Profile"
  41. public static let PROFILE_VIEW_CONTROLLER_ID = "profileViewController"
  42.  
  43.  
  44. private var screens = Array<Screen>()
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51. public func register(screen: Screen) {
  52. if !self.screens.contains(screen) {
  53. self.screens.append(screen)
  54. }
  55. }
  56.  
  57. public func openScreen(withName name: String, andResourceID resourceID: String?, sourceVC: UIViewController?, modalMode: Bool = false) {
  58.  
  59. if let mainNavController = (UIApplication.shared.delegate as! AppDelegate).window?.rootViewController as? UINavigationController {
  60. if let currentVC = mainNavController.visibleViewController {
  61. if let screen = self.getScreen(byName: name, andResourceID: resourceID) {
  62. if let destinationViewController = self.getViewController(from: screen) {
  63. destinationViewController.assembleModule!(forResourceID: resourceID, sourceVC: sourceVC)
  64. let destVC = destinationViewController as! UIViewController
  65. if modalMode {
  66. let modalNavigationController = UINavigationController(rootViewController: destVC)
  67. modalNavigationController.navigationBar.isTranslucent = false
  68. currentVC.present(modalNavigationController, animated: true, completion: nil)
  69. } else {
  70. currentVC.navigationController?.pushViewController(destVC, animated: true)
  71. }
  72. }
  73. }
  74. }
  75. }
  76. }
  77.  
  78. public func openScreen(withName name: String, andResource resource: AnyObject?, sourceVC: UIViewController?, modalMode: Bool = false) {
  79.  
  80. if let mainNavController = (UIApplication.shared.delegate as! AppDelegate).window?.rootViewController as? UINavigationController {
  81. if let currentVC = mainNavController.visibleViewController {
  82. if let screen = self.getScreen(byName: name, andResourceID: nil) {
  83. if let destinationViewController = self.getViewController(from: screen) {
  84. destinationViewController.assembleModule!(forResource: resource, sourceVC: sourceVC)
  85. let destVC = destinationViewController as! UIViewController
  86. if modalMode {
  87. let modalNavigationController = UINavigationController(rootViewController: destVC)
  88. modalNavigationController.navigationBar.isTranslucent = false
  89. currentVC.present(modalNavigationController, animated: true, completion: nil)
  90. } else {
  91. currentVC.navigationController?.pushViewController(destVC, animated: true)
  92. }
  93. }
  94. }
  95. }
  96. }
  97. }
  98.  
  99. public func openScreen(withName name: String, modalMode: Bool = false) {
  100.  
  101. if let mainNavController = (UIApplication.shared.delegate as! AppDelegate).window?.rootViewController as? UINavigationController {
  102. if let currentVC = mainNavController.visibleViewController {
  103. if let screen = self.getScreen(byName: name, andResourceID: nil) {
  104. if let destinationViewController = self.getViewController(from: screen) {
  105. destinationViewController.assembleModule!()
  106. let destVC = destinationViewController as! UIViewController
  107. if modalMode {
  108. let modalNavigationController = UINavigationController(rootViewController: destVC)
  109. modalNavigationController.navigationBar.isTranslucent = false
  110. currentVC.present(modalNavigationController, animated: true, completion: nil)
  111. } else {
  112. currentVC.navigationController?.pushViewController(destVC, animated: true)
  113. }
  114. }
  115. }
  116. }
  117. }
  118. }
  119.  
  120. public func setRootScreen(withName name: String) {
  121. if let mainNavController = (UIApplication.shared.delegate as! AppDelegate).window?.rootViewController as? UINavigationController {
  122. if let screen = self.getScreen(byName: name, andResourceID: nil) {
  123. if let destinationViewController = self.getViewController(from: screen) {
  124. destinationViewController.assembleModule?()
  125. mainNavController.viewControllers = [destinationViewController as! UIViewController]
  126. }
  127. }
  128. }
  129. }
  130.  
  131. public func getViewController(byScreenName screenName: String) -> UIViewController? {
  132. if let screen = self.getScreen(byName: screenName, andResourceID: nil) {
  133. if let destinationViewController = self.getViewController(from: screen) {
  134. destinationViewController.assembleModule?()
  135. return destinationViewController as? UIViewController
  136. }
  137. }
  138. return nil
  139. }
  140.  
  141.  
  142. public func getMainMenuViewController() -> UITabBarController? {
  143. if let mainNavigationController = self.getMainNavigationController() {
  144. if let mainMenuViewController = mainNavigationController.viewControllers.first(where: { (vc) -> Bool in
  145. return vc is UITabBarController
  146. }) {
  147. return mainMenuViewController as? UITabBarController
  148. }
  149. }
  150. return nil
  151. }
  152.  
  153.  
  154. public func getMainNavigationController() -> UINavigationController? {
  155. return (UIApplication.shared.delegate as! AppDelegate).window?.rootViewController as? UINavigationController
  156. }
  157.  
  158.  
  159.  
  160. private func getScreen(byName name: String, andResourceID resourceID: String?) -> Screen? {
  161. return self.screens.first(where: { (screen) -> Bool in
  162. return name == screen.name && resourceID == screen.resourceID
  163. })
  164. }
  165.  
  166. private func getViewController(from screen: Screen) -> ModuleViewController? {
  167. let storyboard = UIStoryboard(name: screen.storyboardName, bundle: Bundle.main)
  168. return storyboard.instantiateViewController(withIdentifier: screen.viewControllerIdentifier) as? ModuleViewController
  169. }
  170.  
  171. }
Add Comment
Please, Sign In to add comment