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.

226 lines
6.8 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. //
  2. // ProfileChangePhoneNumberViewController.swift
  3. // GME Remit
  4. //
  5. // Created by Jeongbae Kong on 21/01/2020.
  6. //Copyright © 2020 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import UIKit
  9. class ChangePersonalInformationViewController: UIViewController {
  10. // MARK: Properties
  11. var presenter: ChangePersonalInformationModuleInterface?
  12. var type: TargetInfo = .mobile
  13. private var target: String?
  14. private var password: String? {
  15. didSet {
  16. guard
  17. let password = password
  18. else { return }
  19. let model = ChangePersonalInformationModel(
  20. passwordForChangePersonalInformation: password,
  21. newValue: changeInformationTextField.text
  22. )
  23. target = type.rawValue
  24. presenter?.verifyPasswordForChangePersonalInformation(model: model, target: target ?? "")
  25. }
  26. }
  27. // MARK: Computed Properties
  28. // MARK: IBOutlets
  29. @IBOutlet weak var changeDescriptionLabel: UILabel!
  30. @IBOutlet weak var whatToChangeLabel: UILabel!
  31. @IBOutlet weak var changeInformationTextField: ValidationTextField!
  32. @IBOutlet weak var saveButton: UIButton!
  33. // MARK: VC's Life cycle
  34. override func viewDidLoad() {
  35. super.viewDidLoad()
  36. setup()
  37. }
  38. // MARK: IBActions
  39. @IBAction func saveNewPersonalInformationButton(_ sender: UIButton) {
  40. if let isUseBiometric = KeyChain.shared.get(key: .biometricAuth), isUseBiometric == "1" {
  41. let biometricAuthenticationWireframe = BiometricAuthenticationWireframe()
  42. biometricAuthenticationWireframe.openWithDelegate(on: self, delegate: self)
  43. } else {
  44. showSecureKeyPad()
  45. }
  46. }
  47. private func showSecureKeyPad() {
  48. self.openPasswordInput { (value) in
  49. self.password = value
  50. }
  51. }
  52. }
  53. // MARK: ChangePersonalInformationViewInterface
  54. extension ChangePersonalInformationViewController: ChangePersonalInformationViewInterface {
  55. func successChangeProfileInformation() {
  56. presenter?.goBackToPersonalInformation()
  57. }
  58. func successProfileValidationOTP() {
  59. self.alertWithOk(
  60. type: .success,
  61. message: "verification_completed_title_text".localized(),
  62. title: "will_change_your_personal_information_title_text".localized(),
  63. okTitle: "ok_text".localized()
  64. ) {
  65. if let newValue = self.changeInformationTextField.text {
  66. self.presenter?.changeProfileInformation(newValue: newValue, target: self.target ?? "")
  67. } else {
  68. self.alert(type: .error, message: "Cannot verify OTP.")
  69. }
  70. }
  71. }
  72. func readyForOpenOTP() {
  73. self.alertWithOk(
  74. type: .success,
  75. message: "will_proceed_to_check_otp_title_text".localized(),
  76. title: "successfully_verified_password_title_text".localized(),
  77. okTitle: "ok_text".localized()
  78. ) {
  79. if let newValue = self.changeInformationTextField.text {
  80. self.presenter?.openOTP(newValue: newValue)
  81. } else {
  82. self.alert(type: .error, message: "Cannot verify password")
  83. }
  84. }
  85. }
  86. func setError(with error: Error) {
  87. alert(message: error.localizedDescription)
  88. }
  89. }
  90. // MARK: Other Functions
  91. extension ChangePersonalInformationViewController {
  92. private func setup() {
  93. setupTargets()
  94. setupLabel()
  95. validationCheck()
  96. saveButton.layer.cornerRadius = 5
  97. }
  98. private func setupTargets() {
  99. saveButton.isEnabled = false
  100. saveButton.backgroundColor = .lightGray
  101. changeInformationTextField.addTarget(
  102. self,
  103. action: #selector(newPersonalInformationType(sender:)),
  104. for: UIControl.Event.editingChanged
  105. )
  106. }
  107. private func setupLabel() {
  108. switch type {
  109. case .mobile:
  110. navigationItem.title = "change_your_phonenumber_title_text".localized()
  111. changeDescriptionLabel.text = "change_your_phonenumber_instruction_text".localized()
  112. whatToChangeLabel.text = "new_phonenumber_text".localized()
  113. changeInformationTextField.placeholder = "enter_your_new_phonenumber_text".localized()
  114. case .email:
  115. changeInformationTextField.keyboardType = .emailAddress
  116. navigationItem.title = "change_your_email_title_text".localized()
  117. changeDescriptionLabel.text = "change_your_email_instruction_text".localized()
  118. whatToChangeLabel.text = "new_email_text".localized()
  119. changeInformationTextField.placeholder = "enter_your_new_email_text".localized()
  120. }
  121. }
  122. @objc private func newPersonalInformationType(sender: UITextField) {
  123. switch type {
  124. case .mobile:
  125. if changeInformationTextField.isValid {
  126. saveButton.isEnabled = true
  127. saveButton.backgroundColor = .themeRed
  128. } else {
  129. saveButton.isEnabled = false
  130. saveButton.backgroundColor = .lightGray
  131. }
  132. case .email:
  133. if changeInformationTextField.isValid {
  134. saveButton.isEnabled = true
  135. saveButton.backgroundColor = .themeRed
  136. } else {
  137. saveButton.isEnabled = false
  138. saveButton.backgroundColor = .lightGray
  139. }
  140. }
  141. }
  142. private func validationCheck() {
  143. switch type {
  144. case .mobile:
  145. changeInformationTextField.validCondition = { !$0.isEmpty && $0.count > 9 && $0.count < 12 }
  146. case .email:
  147. changeInformationTextField.validCondition = { $0.isEmail() }
  148. }
  149. }
  150. }
  151. // MARK: - BiometricAuthenticationViewControllerDelegate
  152. extension ChangePersonalInformationViewController: BiometricAuthenticationViewControllerDelegate {
  153. func viewController(
  154. _ viewController: BiometricAuthenticationViewController,
  155. didFailWithError error: BiometricAuthenticationError, errorMessage: String?) {
  156. print("BiometricAuthenticationWireframe Error: \(errorMessage ?? "")")
  157. viewController.dismiss(animated: true) {
  158. switch error {
  159. case .userFallback:
  160. self.showSecureKeyPad()
  161. case .authenticationFailed, .userCancel, .biometryNotAvailable,
  162. .biometryNotEnrolled, .biometryLockout, .notBeConfigured:
  163. self.alert(type: .error, message: error.message)
  164. }
  165. }
  166. }
  167. func doSelectLocalAuthenticationPolicy(
  168. _ viewController: BiometricAuthenticationViewController
  169. ) -> BiometricAuthenticationPolicy {
  170. return .deviceOwnerAuthenticationWithBiometrics
  171. }
  172. func didComplete(_ viewController: BiometricAuthenticationViewController) {
  173. viewController.dismiss(animated: true) {
  174. guard let encryptedPW = KeyChain.shared.get(key: .password) else {
  175. MainWireframe.logoutWarningAlert(message: "To use biometrics authentication you have to login again.")
  176. return
  177. }
  178. self.password = encryptedPW
  179. }
  180. }
  181. func viewController(
  182. _ viewController: BiometricAuthenticationViewController,
  183. informationTitleLabel titleLabel: UILabel,
  184. authenticationButton button: UIButton
  185. ) {
  186. titleLabel.text = "use_biometric_authentication_text".localized()
  187. button.setTitle("verify_account_button_text".localized(), for: .normal)
  188. }
  189. }