Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // AppNavigationController.swift
- // SAL Pay
- //
- // Created by Zennon Jean S. Gosalvez on 2016-03-15.
- // Copyright © 2016 SALARIUM PTE. LTD. All rights reserved.
- //
- import Alamofire
- import AFBlurSegue
- import KeychainSwift
- import UIKit
- import Toast
- /**
- This is the primary class for handling view controllers.
- */
- class MainNavigationController: NavigationController, LockScreenNavigationControllerDelegate, SecureScreenNavigationControllerDelegate,PhoneNumberVerificationNavigationControllerDelegate {
- // MARK: - Properties:
- // MARK: Stored
- var lockScreenNavigationController: LockScreenNavigationController?
- var offlineScreenViewController: OfflineViewController?
- var phoneNumberVerificationNavigationController : PhoneNumberVerificationNavigationController?
- var phoneNumberVerificationViewController : PhoneNumberVerificationViewController?
- var secureScreenNavigationController: SecureScreenNavigationController?
- var networkManager: NetworkReachabilityManager!
- var isReachable = false {
- didSet {
- if isReachable {
- if let offlineScreenViewController = offlineScreenViewController
- where nil == lockScreenNavigationController {
- offlineScreenViewController.dismissViewControllerAnimated(true, completion: {
- [weak self] in
- self?.offlineScreenViewController = nil
- self?.showSecureScreen()
- })
- }
- } else {
- showOfflineScreen()
- }
- }
- }
- // MARK: - Methods:
- // MARK: Instance
- func setMain() {
- guard let mainViewController = viewControllers.first as? MainViewController else {
- return
- }
- mainViewController.isAvailable = true
- }
- func showLockScreen(animated: Bool) {
- if nil != lockScreenNavigationController {
- return
- }
- sideNavigationController?.closeLeftView()
- let storyboard = UIStoryboard(name: "LockScreen", bundle: nil)
- lockScreenNavigationController = LockScreenNavigationController(rootViewController: storyboard.instantiateInitialViewController()!)
- lockScreenNavigationController!.lockScreenDelegate = self
- var presenter: UIViewController!
- if let offlineScreenViewController = offlineScreenViewController {
- presenter = offlineScreenViewController
- } else if let presentedViewController = presentedViewController {
- presenter = presentedViewController
- } else {
- presenter = topViewController
- }
- presenter.presentViewController(lockScreenNavigationController!, animated: animated, completion: nil)
- }
- func showLoanRequestForm() {
- guard let mainViewController = viewControllers.first as? MainViewController else {
- return
- }
- mainViewController.isAvailable = true
- mainViewController.showLoanRequestForm()
- }
- func showOffers(){
- guard let mainViewController = viewControllers.first as? MainViewController else {
- return
- }
- mainViewController.isAvailable = true
- mainViewController.showOffers()
- }
- func showOffer(application_id:String, bid_id:String) {
- guard let mainViewController = viewControllers.first as? MainViewController else {
- return
- }
- mainViewController.isAvailable = true
- mainViewController.showOffer(application_id, bid_id: bid_id)
- }
- func showOfflineScreen() {
- if nil != offlineScreenViewController || nil != lockScreenNavigationController {
- return
- }
- let storyboard = UIStoryboard(name: "OfflineScreen", bundle: nil)
- offlineScreenViewController = (storyboard.instantiateInitialViewController() as! OfflineViewController)
- offlineScreenViewController!.modalTransitionStyle = .CrossDissolve
- var presenter: UIViewController!
- if let presentedViewController = presentedViewController {
- presenter = presentedViewController
- } else {
- presenter = self
- }
- let blurSegue = AFBlurSegue(identifier: nil, source: presenter, destination: offlineScreenViewController!)
- prepareForSegue(blurSegue, sender: self)
- blurSegue.perform()
- }
- func showPhoneVerificationScreen() {
- if nil != phoneNumberVerificationNavigationController {
- return
- }
- sideNavigationController?.closeLeftView()
- let storyboard = UIStoryboard(name: "Main", bundle: nil)
- let vc = storyboard.instantiateViewControllerWithIdentifier("PhoneNumberVerificationViewController") as? PhoneNumberVerificationViewController
- phoneNumberVerificationNavigationController = PhoneNumberVerificationNavigationController(rootViewController:vc! )
- phoneNumberVerificationNavigationController!.verificationDelegate = self
- var presenter: UIViewController!
- if let verifyViewController = phoneNumberVerificationViewController {
- presenter = verifyViewController
- } else if let presentedViewController = presentedViewController {
- presenter = presentedViewController
- } else {
- presenter = topViewController
- }
- presenter.presentViewController(phoneNumberVerificationNavigationController!, animated: true, completion: nil)
- }
- /**
- When appropriate, starts `GetPersonalAccount` request to check if the
- account is locked or not.
- - seealso: `Router` enumeration.
- */
- func showSecureScreen() {
- if nil != secureScreenNavigationController || nil != lockScreenNavigationController {
- return
- }
- view.makeToastActivity(CSToastPositionCenter)
- view.userInteractionEnabled = false
- sideNavigationController?.enabled = false
- Alamofire.request(Router.GetPersonalAccount())
- .responseJSON {
- [weak self] response in
- self?.view.hideToastActivity()
- self?.view.userInteractionEnabled = true
- self?.sideNavigationController?.enabled = true
- if let error = response.result.error {
- self?.view.makeToastError(error)
- return
- }
- guard let statusCode = response.response?.statusCode else {
- self?.view.makeToast("No response.")
- return
- }
- switch statusCode {
- case 200:
- if let JSON = (response.result.value as? [[NSObject: AnyObject]])?.first?["is_locked"] as? Bool
- where JSON {
- guard let this = self else {
- return
- }
- let storyboard = UIStoryboard(name: "SecureScreen", bundle: nil)
- let secureScreenViewController = (storyboard.instantiateInitialViewController() as! SecureScreenViewController)
- this.secureScreenNavigationController = SecureScreenNavigationController(rootViewController: secureScreenViewController)
- this.secureScreenNavigationController!.modalTransitionStyle = .CrossDissolve
- this.secureScreenNavigationController!.secureScreenDelegate = this
- let blurSegue = AFBlurSegue(identifier: nil, source: this.topViewController!, destination: this.secureScreenNavigationController!)
- this.prepareForSegue(blurSegue, sender: self)
- blurSegue.perform()
- } else {
- self?.setMain()
- }
- default:
- let HTTPAlertController = UIAlertController(HTTPStatusCode: statusCode, JSON: response.result.value)
- self?.presentViewController(HTTPAlertController, animated: true, completion: nil)
- }
- }
- }
- // MARK: Instance Overriding
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do any additional setup after loading the view.
- let navigationBar = self.navigationBar as! NavigationBar
- navigationBar.tintColor = MaterialColor.lightText.primary
- navigationBar.backButtonImage = UIImage(named: "Material/Navigation/ArrowBack")
- navigationBar.backgroundColor = AppColor.grey.base
- navigationBar.depth = .Depth2
- navigationBar.navigationBarStyle = .Medium
- navigationBar.statusBarStyle = .LightContent
- networkManager = NetworkReachabilityManager(host: Router.baseURL.host!)
- networkManager.listener = {
- [weak self] status in
- guard let this = self else {
- return
- }
- this.isReachable = status != NetworkReachabilityManager.NetworkReachabilityStatus.NotReachable
- }
- networkManager.startListening()
- }
- // MARK: - LockScreenNavigationControllerDelegate Protocol:
- // MARK: Method Protocol Requirements
- func lockScreenDidSignIn() {
- lockScreenNavigationController?.dismissViewControllerAnimated(true, completion: {
- [weak self] _ in
- guard let this = self else {
- return
- }
- this.lockScreenNavigationController = nil
- if this.isReachable {
- if let offlineScreenViewController = this.offlineScreenViewController {
- offlineScreenViewController.dismissViewControllerAnimated(true, completion: {
- [weak self] in
- self?.offlineScreenViewController = nil
- self?.showSecureScreen()
- })
- } else {
- let keychain = KeychainSwift()
- if keychain.get("mobile_number") == nil {
- self?.showPhoneVerificationScreen()
- }else{
- this.showSecureScreen()
- }
- }
- } else {
- this.showOfflineScreen()
- }
- })
- }
- // MARK: - LockScreenNavigationControllerDelegate Protocol:
- // MARK: Method Protocol Requirements
- func secureScreenDidUnlock() {
- secureScreenNavigationController?.dismissViewControllerAnimated(true, completion: {
- [weak self] _ in
- guard let this = self else {
- return
- }
- this.secureScreenNavigationController = nil
- if this.isReachable {
- this.setMain()
- } else {
- this.showOfflineScreen()
- }
- })
- }
- // MARK: - PhoneNumberVerificationNavigationontrollerDelegate Protocol:
- // MARK: Method Protocol Requirements
- func PhoneVerificationDidDismiss() {
- self.phoneNumberVerificationNavigationController?.dismissViewControllerAnimated(true, completion: {
- [weak self] _ in
- guard let this = self else {
- return
- }
- this.phoneNumberVerificationNavigationController = nil
- if this.isReachable {
- this.setMain()
- } else {
- this.showOfflineScreen()
- }
- })
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement