You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
5.7 KiB

3 years ago
3 years ago
4 years ago
4 years ago
5 years ago
5 years ago
3 years ago
  1. //
  2. // LauncherScreenWireframe.swift
  3. // GME Remit
  4. //
  5. // Created by InKwon James Kim on 22/07/2019.
  6. //Copyright © 2019 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import UIKit
  9. import LGSideMenuController
  10. class LauncherScreenWireframe {
  11. weak var view: UIViewController!
  12. private var biometricViewController: UIViewController?
  13. }
  14. extension LauncherScreenWireframe: LauncherScreenWireframeInput {
  15. var storyboardName: String {return "LauncherScreen"}
  16. func getMainView() -> UIViewController {
  17. let service = LauncherScreenService()
  18. let interactor = LauncherScreenInteractor(service: service)
  19. let presenter = LauncherScreenPresenter()
  20. let viewController = viewControllerFromStoryboard(of: LauncherScreenViewController.self)
  21. viewController.presenter = presenter
  22. interactor.output = presenter
  23. presenter.interactor = interactor
  24. presenter.wireframe = self
  25. presenter.view = viewController
  26. view = viewController
  27. return viewController
  28. }
  29. func goSplashScreen() {
  30. let splashWireframe = SplashScreenWireframe()
  31. let splashViewController = splashWireframe.getMainView()
  32. let nav = UINavigationController(rootViewController: splashViewController)
  33. window?.rootViewController = nav
  34. }
  35. func openAuthenticationScreen() {
  36. let biometricAuthenticationWireframe = BiometricAuthenticationWireframe()
  37. biometricAuthenticationWireframe.openWithDelegate(
  38. on: view,
  39. delegate: self
  40. )
  41. }
  42. }
  43. // MARK: - BiometricAuthenticationViewControllerDelegate
  44. extension LauncherScreenWireframe: BiometricAuthenticationViewControllerDelegate {
  45. func viewController(
  46. _ viewController: BiometricAuthenticationViewController,
  47. informationTitleLabel titleLabel: UILabel,
  48. authenticationButton button: UIButton
  49. ) {
  50. biometricViewController = viewController
  51. titleLabel.text = "bio_login_intro_text".localized()
  52. button.setTitle("login_text".localized(), for: .normal)
  53. }
  54. func didComplete(_ viewController: BiometricAuthenticationViewController) {
  55. self.doLogin()
  56. }
  57. func viewController(
  58. _ viewController: BiometricAuthenticationViewController,
  59. didFailWithError error: BiometricAuthenticationError,
  60. errorMessage: String?
  61. ) {
  62. print("BiometricAuthenticationWireframe Error: \(errorMessage ?? "")")
  63. // if don't use biometric authentication or user press password
  64. DispatchQueue.main.async {
  65. switch error {
  66. case .biometryNotAvailable, .userFallback:
  67. // viewController.openPasswordInput { (value) in
  68. // KeyChain.shared.save(data: value, key: .password)
  69. //
  70. // }
  71. self.doLogin()
  72. case .notBeConfigured, .biometryNotEnrolled:
  73. viewController.alert(type: .error, message: error.message)
  74. default:
  75. break
  76. }
  77. }
  78. }
  79. func doSelectLocalAuthenticationPolicy(
  80. _ viewController: BiometricAuthenticationViewController
  81. ) -> BiometricAuthenticationPolicy {
  82. return .deviceOwnerAuthenticationWithBiometrics
  83. }
  84. }
  85. // MARK: - Other Methods
  86. extension LauncherScreenWireframe {
  87. private func doLogin() {
  88. biometricViewController?.showProgressHud(backgroundColor: .clear, loadingColor: .white, textColor: .white)
  89. let loginService = LoginService()
  90. guard let userID = KeyChain.shared.get(key: .id),
  91. let password = KeyChain.shared.get(key: .password) else {
  92. (view as? LauncherScreenViewController)?.presenter?.goSplashScreen()
  93. return
  94. }
  95. GMEDB.shared.getFcmToken { (token, error) in
  96. loginService.accessCode(
  97. userId: userID,
  98. password: password,
  99. firebaseToken: token ?? "",
  100. success: { (user) in
  101. let accessCode = user?.accessCode ?? ""
  102. let accessCodeBase64 = accessCode
  103. Utility.save(user: user, accessCodeBase64: accessCodeBase64, password: password, login: true)
  104. let mainWireFrame = MainWireframe.shared
  105. let tabBarViewController = mainWireFrame?.getMainView()
  106. guard let sidemenuVc = UIStoryboard(name: "SideMenu", bundle: nil).instantiateViewController(
  107. withIdentifier: "SideMenuViewController"
  108. ) as? SideMenuViewController else {
  109. return
  110. }
  111. let navWithTab = UINavigationController(rootViewController: tabBarViewController!)
  112. let sideMenuController = LGSideMenuController(
  113. rootViewController: navWithTab,
  114. leftViewController: sidemenuVc,
  115. rightViewController: nil
  116. )
  117. sideMenuController.rootViewLayerShadowColor = UIColor(white: 0.9, alpha: 0.6)
  118. sideMenuController.rootViewLayerShadowRadius = 8.0
  119. sideMenuController.leftViewPresentationStyle = .slideAbove
  120. sideMenuController.leftViewWidth = UIScreen.main.bounds.width - 70.0
  121. sideMenuController.leftViewBackgroundBlurEffect = UIBlurEffect(style: .regular)
  122. self.biometricViewController?.hideProgressHud()
  123. self.biometricViewController?.dismiss(animated: true) {
  124. self.window?.backgroundColor = sidemenuVc.view.backgroundColor
  125. self.window?.rootViewController = sideMenuController
  126. }
  127. },
  128. failure: { (error) in
  129. self.biometricViewController?.hideProgressHud()
  130. self.biometricViewController?.alert(
  131. type: .error,
  132. message: error.localizedDescription,
  133. title: "Warning"
  134. ) {
  135. // if failed login, remove all in keychain, change logout status and then go splash screen
  136. KeyChain.shared.removeAll()
  137. self.biometricViewController?.dismiss(animated: true) {
  138. (self.view as? LauncherScreenViewController)?.presenter?.goSplashScreen()
  139. }
  140. }
  141. }
  142. )}
  143. }
  144. }