// // ProfileChangePhoneNumberViewController.swift // GME Remit // // Created by Jeongbae Kong on 21/01/2020. //Copyright © 2020 Gobal Money Express Co. Ltd. All rights reserved. // import UIKit class ChangePersonalInformationViewController: UIViewController { // MARK: Properties var presenter: ChangePersonalInformationModuleInterface? var type: TargetInfo = .mobile private var target: String? private var password: String? { didSet { guard let password = password else { return } let model = ChangePersonalInformationModel( passwordForChangePersonalInformation: password, newValue: changeInformationTextField.text ) target = type.rawValue presenter?.verifyPasswordForChangePersonalInformation(model: model, target: target ?? "") } } // MARK: Computed Properties // MARK: IBOutlets @IBOutlet weak var changeDescriptionLabel: UILabel! @IBOutlet weak var whatToChangeLabel: UILabel! @IBOutlet weak var changeInformationTextField: ValidationTextField! @IBOutlet weak var saveButton: UIButton! // MARK: VC's Life cycle override func viewDidLoad() { super.viewDidLoad() setup() } // MARK: IBActions @IBAction func saveNewPersonalInformationButton(_ sender: UIButton) { if let isUseBiometric = KeyChain.shared.get(key: .biometricAuth), isUseBiometric == "1" { let biometricAuthenticationWireframe = BiometricAuthenticationWireframe() biometricAuthenticationWireframe.openWithDelegate(on: self, delegate: self) } else { showSecureKeyPad() } } private func showSecureKeyPad() { self.openPasswordInput { (value) in self.password = value } } } // MARK: ChangePersonalInformationViewInterface extension ChangePersonalInformationViewController: ChangePersonalInformationViewInterface { func successChangeProfileInformation() { presenter?.goBackToPersonalInformation() } func successProfileValidationOTP() { self.alertWithOk( type: .success, message: "verification_completed_title_text".localized(), title: "will_change_your_personal_information_title_text".localized(), okTitle: "ok_text".localized() ) { if let newValue = self.changeInformationTextField.text { self.presenter?.changeProfileInformation(newValue: newValue, target: self.target ?? "") } else { self.alert(type: .error, message: "Cannot verify OTP.") } } } func readyForOpenOTP() { self.alertWithOk( type: .success, message: "will_proceed_to_check_otp_title_text".localized(), title: "successfully_verified_password_title_text".localized(), okTitle: "ok_text".localized() ) { if let newValue = self.changeInformationTextField.text { self.presenter?.openOTP(newValue: newValue) } else { self.alert(type: .error, message: "Cannot verify password") } } } func setError(with error: Error) { alert(message: error.localizedDescription) } } // MARK: Other Functions extension ChangePersonalInformationViewController { private func setup() { setupTargets() setupLabel() validationCheck() saveButton.layer.cornerRadius = 5 } private func setupTargets() { saveButton.isEnabled = false saveButton.backgroundColor = .lightGray changeInformationTextField.addTarget( self, action: #selector(newPersonalInformationType(sender:)), for: UIControl.Event.editingChanged ) } private func setupLabel() { switch type { case .mobile: navigationItem.title = "change_your_phonenumber_title_text".localized() changeDescriptionLabel.text = "change_your_phonenumber_instruction_text".localized() whatToChangeLabel.text = "new_phonenumber_text".localized() changeInformationTextField.placeholder = "enter_your_new_phonenumber_text".localized() case .email: changeInformationTextField.keyboardType = .emailAddress navigationItem.title = "change_your_email_title_text".localized() changeDescriptionLabel.text = "change_your_email_instruction_text".localized() whatToChangeLabel.text = "new_email_text".localized() changeInformationTextField.placeholder = "enter_your_new_email_text".localized() } } @objc private func newPersonalInformationType(sender: UITextField) { switch type { case .mobile: if changeInformationTextField.isValid { saveButton.isEnabled = true saveButton.backgroundColor = .themeRed } else { saveButton.isEnabled = false saveButton.backgroundColor = .lightGray } case .email: if changeInformationTextField.isValid { saveButton.isEnabled = true saveButton.backgroundColor = .themeRed } else { saveButton.isEnabled = false saveButton.backgroundColor = .lightGray } } } private func validationCheck() { switch type { case .mobile: changeInformationTextField.validCondition = { !$0.isEmpty && $0.count > 9 && $0.count < 12 } case .email: changeInformationTextField.validCondition = { $0.isEmail() } } } } // MARK: - BiometricAuthenticationViewControllerDelegate extension ChangePersonalInformationViewController: BiometricAuthenticationViewControllerDelegate { func viewController( _ viewController: BiometricAuthenticationViewController, didFailWithError error: BiometricAuthenticationError, errorMessage: String?) { print("BiometricAuthenticationWireframe Error: \(errorMessage ?? "")") viewController.dismiss(animated: true) { switch error { case .userFallback: self.showSecureKeyPad() case .authenticationFailed, .userCancel, .biometryNotAvailable, .biometryNotEnrolled, .biometryLockout, .notBeConfigured: self.alert(type: .error, message: error.message) } } } func doSelectLocalAuthenticationPolicy( _ viewController: BiometricAuthenticationViewController ) -> BiometricAuthenticationPolicy { return .deviceOwnerAuthenticationWithBiometrics } func didComplete(_ viewController: BiometricAuthenticationViewController) { viewController.dismiss(animated: true) { guard let encryptedPW = KeyChain.shared.get(key: .password) else { MainWireframe.logoutWarningAlert(message: "To use biometrics authentication you have to login again.") return } self.password = encryptedPW } } func viewController( _ viewController: BiometricAuthenticationViewController, informationTitleLabel titleLabel: UILabel, authenticationButton button: UIButton ) { titleLabel.text = "use_biometric_authentication_text".localized() button.setTitle("verify_account_button_text".localized(), for: .normal) } }