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.

173 lines
5.7 KiB

3 years ago
2 years ago
4 years ago
4 years ago
1 year 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. return
  74. //viewController.alert(type: .error, message: error.message)
  75. default:
  76. break
  77. }
  78. }
  79. }
  80. func doSelectLocalAuthenticationPolicy(
  81. _ viewController: BiometricAuthenticationViewController
  82. ) -> BiometricAuthenticationPolicy {
  83. return .deviceOwnerAuthenticationWithBiometrics
  84. }
  85. }
  86. // MARK: - Other Methods
  87. extension LauncherScreenWireframe {
  88. private func doLogin() {
  89. biometricViewController?.showProgressHud(backgroundColor: .clear, loadingColor: .white, textColor: .white)
  90. let loginService = LoginService()
  91. guard let userID = KeyChain.shared.get(key: .id),
  92. let password = KeyChain.shared.get(key: .password) else {
  93. (view as? LauncherScreenViewController)?.presenter?.goSplashScreen()
  94. return
  95. }
  96. GMEDB.shared.getFcmToken { (token, error) in
  97. loginService.accessCode(
  98. userId: userID,
  99. password: password,
  100. firebaseToken: token ?? "", customerType: "",
  101. success: { (user) in
  102. let accessCode = user?.accessCode ?? ""
  103. let accessCodeBase64 = accessCode
  104. Utility.save(user: user, accessCodeBase64: accessCodeBase64, password: password, login: true)
  105. let mainWireFrame = MainWireframe.shared
  106. let tabBarViewController = mainWireFrame?.getMainView()
  107. guard let sidemenuVc = UIStoryboard(name: "SideMenu", bundle: nil).instantiateViewController(
  108. withIdentifier: "SideMenuViewController"
  109. ) as? SideMenuViewController else {
  110. return
  111. }
  112. let navWithTab = UINavigationController(rootViewController: tabBarViewController!)
  113. let sideMenuController = LGSideMenuController(
  114. rootViewController: navWithTab,
  115. leftViewController: nil,
  116. rightViewController: nil
  117. )
  118. sideMenuController.rootViewLayerShadowColor = UIColor(white: 0.9, alpha: 0.6)
  119. sideMenuController.rootViewLayerShadowRadius = 8.0
  120. sideMenuController.leftViewPresentationStyle = .slideAbove
  121. sideMenuController.leftViewWidth = UIScreen.main.bounds.width - 70.0
  122. sideMenuController.leftViewBackgroundBlurEffect = UIBlurEffect(style: .regular)
  123. self.biometricViewController?.hideProgressHud()
  124. self.biometricViewController?.dismiss(animated: true) {
  125. self.window?.backgroundColor = sidemenuVc.view.backgroundColor
  126. self.window?.rootViewController = sideMenuController
  127. }
  128. },
  129. failure: { (error) in
  130. self.biometricViewController?.hideProgressHud()
  131. self.biometricViewController?.alert(
  132. type: .error,
  133. message: error.localizedDescription,
  134. title: "Warning"
  135. ) {
  136. // if failed login, remove all in keychain, change logout status and then go splash screen
  137. KeyChain.shared.removeAll()
  138. self.biometricViewController?.dismiss(animated: true) {
  139. (self.view as? LauncherScreenViewController)?.presenter?.goSplashScreen()
  140. }
  141. }
  142. }
  143. )}
  144. }
  145. }