Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.15 KB | None | 0 0
  1. //
  2. // AppNavigationController.swift
  3. // SAL Pay
  4. //
  5. // Created by Zennon Jean S. Gosalvez on 2016-03-15.
  6. // Copyright © 2016 SALARIUM PTE. LTD. All rights reserved.
  7. //
  8.  
  9. import Alamofire
  10. import AFBlurSegue
  11. import KeychainSwift
  12. import UIKit
  13. import Toast
  14.  
  15. /**
  16. This is the primary class for handling view controllers.
  17. */
  18. class MainNavigationController: NavigationController, LockScreenNavigationControllerDelegate, SecureScreenNavigationControllerDelegate,PhoneNumberVerificationNavigationControllerDelegate {
  19.  
  20. // MARK: - Properties:
  21. // MARK: Stored
  22.  
  23. var lockScreenNavigationController: LockScreenNavigationController?
  24. var offlineScreenViewController: OfflineViewController?
  25. var phoneNumberVerificationNavigationController : PhoneNumberVerificationNavigationController?
  26. var phoneNumberVerificationViewController : PhoneNumberVerificationViewController?
  27. var secureScreenNavigationController: SecureScreenNavigationController?
  28. var networkManager: NetworkReachabilityManager!
  29. var isReachable = false {
  30. didSet {
  31. if isReachable {
  32. if let offlineScreenViewController = offlineScreenViewController
  33. where nil == lockScreenNavigationController {
  34. offlineScreenViewController.dismissViewControllerAnimated(true, completion: {
  35. [weak self] in
  36. self?.offlineScreenViewController = nil
  37. self?.showSecureScreen()
  38. })
  39. }
  40. } else {
  41. showOfflineScreen()
  42. }
  43. }
  44. }
  45.  
  46.  
  47. // MARK: - Methods:
  48. // MARK: Instance
  49.  
  50. func setMain() {
  51. guard let mainViewController = viewControllers.first as? MainViewController else {
  52. return
  53. }
  54.  
  55. mainViewController.isAvailable = true
  56. }
  57.  
  58. func showLockScreen(animated: Bool) {
  59. if nil != lockScreenNavigationController {
  60. return
  61. }
  62.  
  63. sideNavigationController?.closeLeftView()
  64.  
  65. let storyboard = UIStoryboard(name: "LockScreen", bundle: nil)
  66. lockScreenNavigationController = LockScreenNavigationController(rootViewController: storyboard.instantiateInitialViewController()!)
  67. lockScreenNavigationController!.lockScreenDelegate = self
  68.  
  69. var presenter: UIViewController!
  70.  
  71. if let offlineScreenViewController = offlineScreenViewController {
  72. presenter = offlineScreenViewController
  73. } else if let presentedViewController = presentedViewController {
  74. presenter = presentedViewController
  75. } else {
  76. presenter = topViewController
  77. }
  78.  
  79. presenter.presentViewController(lockScreenNavigationController!, animated: animated, completion: nil)
  80. }
  81.  
  82.  
  83. func showLoanRequestForm() {
  84. guard let mainViewController = viewControllers.first as? MainViewController else {
  85. return
  86. }
  87.  
  88. mainViewController.isAvailable = true
  89. mainViewController.showLoanRequestForm()
  90. }
  91.  
  92. func showOffers(){
  93. guard let mainViewController = viewControllers.first as? MainViewController else {
  94. return
  95. }
  96.  
  97. mainViewController.isAvailable = true
  98. mainViewController.showOffers()
  99. }
  100.  
  101. func showOffer(application_id:String, bid_id:String) {
  102. guard let mainViewController = viewControllers.first as? MainViewController else {
  103. return
  104. }
  105.  
  106. mainViewController.isAvailable = true
  107. mainViewController.showOffer(application_id, bid_id: bid_id)
  108. }
  109.  
  110. func showOfflineScreen() {
  111. if nil != offlineScreenViewController || nil != lockScreenNavigationController {
  112. return
  113. }
  114.  
  115. let storyboard = UIStoryboard(name: "OfflineScreen", bundle: nil)
  116. offlineScreenViewController = (storyboard.instantiateInitialViewController() as! OfflineViewController)
  117. offlineScreenViewController!.modalTransitionStyle = .CrossDissolve
  118.  
  119. var presenter: UIViewController!
  120.  
  121. if let presentedViewController = presentedViewController {
  122. presenter = presentedViewController
  123. } else {
  124. presenter = self
  125. }
  126.  
  127. let blurSegue = AFBlurSegue(identifier: nil, source: presenter, destination: offlineScreenViewController!)
  128.  
  129. prepareForSegue(blurSegue, sender: self)
  130.  
  131. blurSegue.perform()
  132. }
  133.  
  134. func showPhoneVerificationScreen() {
  135.  
  136. if nil != phoneNumberVerificationNavigationController {
  137. return
  138. }
  139.  
  140. sideNavigationController?.closeLeftView()
  141.  
  142. let storyboard = UIStoryboard(name: "Main", bundle: nil)
  143. let vc = storyboard.instantiateViewControllerWithIdentifier("PhoneNumberVerificationViewController") as? PhoneNumberVerificationViewController
  144. phoneNumberVerificationNavigationController = PhoneNumberVerificationNavigationController(rootViewController:vc! )
  145. phoneNumberVerificationNavigationController!.verificationDelegate = self
  146.  
  147. var presenter: UIViewController!
  148.  
  149. if let verifyViewController = phoneNumberVerificationViewController {
  150. presenter = verifyViewController
  151. } else if let presentedViewController = presentedViewController {
  152. presenter = presentedViewController
  153. } else {
  154. presenter = topViewController
  155. }
  156.  
  157. presenter.presentViewController(phoneNumberVerificationNavigationController!, animated: true, completion: nil)
  158.  
  159. }
  160.  
  161.  
  162. /**
  163. When appropriate, starts `GetPersonalAccount` request to check if the
  164. account is locked or not.
  165.  
  166. - seealso: `Router` enumeration.
  167. */
  168. func showSecureScreen() {
  169. if nil != secureScreenNavigationController || nil != lockScreenNavigationController {
  170. return
  171. }
  172.  
  173. view.makeToastActivity(CSToastPositionCenter)
  174. view.userInteractionEnabled = false
  175. sideNavigationController?.enabled = false
  176.  
  177. Alamofire.request(Router.GetPersonalAccount())
  178. .responseJSON {
  179. [weak self] response in
  180. self?.view.hideToastActivity()
  181. self?.view.userInteractionEnabled = true
  182. self?.sideNavigationController?.enabled = true
  183.  
  184. if let error = response.result.error {
  185. self?.view.makeToastError(error)
  186. return
  187. }
  188.  
  189. guard let statusCode = response.response?.statusCode else {
  190. self?.view.makeToast("No response.")
  191. return
  192. }
  193.  
  194. switch statusCode {
  195. case 200:
  196. if let JSON = (response.result.value as? [[NSObject: AnyObject]])?.first?["is_locked"] as? Bool
  197. where JSON {
  198. guard let this = self else {
  199. return
  200. }
  201.  
  202. let storyboard = UIStoryboard(name: "SecureScreen", bundle: nil)
  203. let secureScreenViewController = (storyboard.instantiateInitialViewController() as! SecureScreenViewController)
  204.  
  205. this.secureScreenNavigationController = SecureScreenNavigationController(rootViewController: secureScreenViewController)
  206. this.secureScreenNavigationController!.modalTransitionStyle = .CrossDissolve
  207. this.secureScreenNavigationController!.secureScreenDelegate = this
  208.  
  209. let blurSegue = AFBlurSegue(identifier: nil, source: this.topViewController!, destination: this.secureScreenNavigationController!)
  210.  
  211. this.prepareForSegue(blurSegue, sender: self)
  212.  
  213. blurSegue.perform()
  214. } else {
  215. self?.setMain()
  216. }
  217.  
  218. default:
  219. let HTTPAlertController = UIAlertController(HTTPStatusCode: statusCode, JSON: response.result.value)
  220. self?.presentViewController(HTTPAlertController, animated: true, completion: nil)
  221. }
  222. }
  223. }
  224.  
  225.  
  226. // MARK: Instance Overriding
  227.  
  228. override func viewDidLoad() {
  229. super.viewDidLoad()
  230.  
  231. // Do any additional setup after loading the view.
  232.  
  233. let navigationBar = self.navigationBar as! NavigationBar
  234. navigationBar.tintColor = MaterialColor.lightText.primary
  235. navigationBar.backButtonImage = UIImage(named: "Material/Navigation/ArrowBack")
  236. navigationBar.backgroundColor = AppColor.grey.base
  237. navigationBar.depth = .Depth2
  238. navigationBar.navigationBarStyle = .Medium
  239. navigationBar.statusBarStyle = .LightContent
  240.  
  241. networkManager = NetworkReachabilityManager(host: Router.baseURL.host!)
  242.  
  243. networkManager.listener = {
  244. [weak self] status in
  245. guard let this = self else {
  246. return
  247. }
  248.  
  249. this.isReachable = status != NetworkReachabilityManager.NetworkReachabilityStatus.NotReachable
  250. }
  251.  
  252. networkManager.startListening()
  253. }
  254.  
  255.  
  256. // MARK: - LockScreenNavigationControllerDelegate Protocol:
  257. // MARK: Method Protocol Requirements
  258.  
  259. func lockScreenDidSignIn() {
  260. lockScreenNavigationController?.dismissViewControllerAnimated(true, completion: {
  261. [weak self] _ in
  262. guard let this = self else {
  263. return
  264. }
  265.  
  266. this.lockScreenNavigationController = nil
  267.  
  268. if this.isReachable {
  269. if let offlineScreenViewController = this.offlineScreenViewController {
  270. offlineScreenViewController.dismissViewControllerAnimated(true, completion: {
  271. [weak self] in
  272. self?.offlineScreenViewController = nil
  273.  
  274. self?.showSecureScreen()
  275.  
  276.  
  277. })
  278. } else {
  279. let keychain = KeychainSwift()
  280. if keychain.get("mobile_number") == nil {
  281. self?.showPhoneVerificationScreen()
  282. }else{
  283. this.showSecureScreen()
  284. }
  285.  
  286. }
  287. } else {
  288. this.showOfflineScreen()
  289. }
  290. })
  291. }
  292.  
  293. // MARK: - LockScreenNavigationControllerDelegate Protocol:
  294. // MARK: Method Protocol Requirements
  295.  
  296. func secureScreenDidUnlock() {
  297. secureScreenNavigationController?.dismissViewControllerAnimated(true, completion: {
  298. [weak self] _ in
  299. guard let this = self else {
  300. return
  301. }
  302.  
  303. this.secureScreenNavigationController = nil
  304.  
  305. if this.isReachable {
  306. this.setMain()
  307. } else {
  308. this.showOfflineScreen()
  309. }
  310. })
  311. }
  312.  
  313. // MARK: - PhoneNumberVerificationNavigationontrollerDelegate Protocol:
  314. // MARK: Method Protocol Requirements
  315.  
  316.  
  317. func PhoneVerificationDidDismiss() {
  318. self.phoneNumberVerificationNavigationController?.dismissViewControllerAnimated(true, completion: {
  319. [weak self] _ in
  320. guard let this = self else {
  321. return
  322. }
  323.  
  324. this.phoneNumberVerificationNavigationController = nil
  325.  
  326. if this.isReachable {
  327. this.setMain()
  328. } else {
  329. this.showOfflineScreen()
  330. }
  331. })
  332. }
  333. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement