// // ProfileChangePinViewController.swift // GME Remit // // Created by Shiran on 8/17/20. // Copyright © 2020 Gobal Money Express Co. Ltd. All rights reserved. // import UIKit import Localize_Swift class ProfileChangePinViewController: UIViewController { enum PasswordType { case old case new case newConfirm } struct StringConstants { let passwordErrorText = "password_error_text".localized() let newPinTitleText = "New PIN" let currentPinTitleText = "Current PIN" let confirmPinTitleText = "Confirm PIN" let saveText = "save_text".localized() let titleText = "Change PIN" let successText = "success_text".localized() let emptyPasswordError = "PIN cannot be empty" let confirmPasswordError = "Confirm PIN cannot be empty" let passwordPolicy = "PIN must be of 6 characters" } @IBOutlet weak var changePinBttn: UIButton! @IBOutlet weak var currentPinTxtField: ValidationTextField! @IBOutlet weak var newPinTxtField: ValidationTextField! @IBOutlet weak var confirmPinTxtField: ValidationTextField! @IBOutlet weak var currentPinToggleButton: UIButton! @IBOutlet weak var newPinToggleButton: UIButton! @IBOutlet weak var confirmPinToggleButton: UIButton! @IBAction func changePinBttnPressed(_ sender: UIButton) { let currentPassword = self.encryptedOldPassword let newPassword = self.encryptedPassword let confirmPassword = self.encryptedConfirmPassword let result = self.isValid( currentPassword: currentPassword, password: newPassword, confirmPassword: confirmPassword ) if result.isValid { let userId = Utility.getMyUserName() let param = [ "UserId" : userId, "CurrentPin": currentPassword, "NewPin": newPassword, "ConfirmNewPin": confirmPassword ] self.updatePassword(param: param) }else { let message = result.error self.alert(type: .error, message: message.localizedDescription) } } @IBAction func currentPinButton(_ sender: UIButton) { currentPinTxtField.isSecureTextEntry = !currentPinTxtField.isSecureTextEntry currentPinTxtField.isSecureTextEntry ? currentPinToggleButton.setImage(UIImage(named: "Show"), for: .normal) : currentPinToggleButton.setImage(UIImage(named: "Hide"), for: .normal) } @IBAction func newPinButton(_ sender: UIButton) { newPinTxtField.isSecureTextEntry = !newPinTxtField.isSecureTextEntry newPinTxtField.isSecureTextEntry ? newPinToggleButton.setImage(UIImage(named: "Show"), for: .normal) : newPinToggleButton.setImage(UIImage(named: "Hide"), for: .normal) } @IBAction func confirmPinButton(_ sender: UIButton) { confirmPinTxtField.isSecureTextEntry = !confirmPinTxtField.isSecureTextEntry confirmPinTxtField.isSecureTextEntry ? confirmPinToggleButton.setImage(UIImage(named: "Show"), for: .normal) : confirmPinToggleButton.setImage(UIImage(named: "Hide"), for: .normal) } func updatePassword(param: [String: String]) { self.updatePin( params: param, success: { (message) in self.alertWithOk( type: .success, message: message, title: "success_text".localized(), okTitle: "Ok") { self.navigationController?.popViewController(animated: true) } }, failure: { (error) in self.alert(type: .error, message: error.localizedDescription) } ) } // MARK: - Properties var encryptedPassword: String = "" var encryptedOldPassword = "" var encryptedConfirmPassword = "" private var selectedType: PasswordType = .old private var isValid = false { didSet { // changePinBttn.isEnabled = isValid // changePinBttn.backgroundColor = isValid ? .themeRed : .themeText changePinBttn.isEnabled = isValid changePinBttn.backgroundColor = isValid ? .themeRed : .themeRed } } // MARK: Life Cycle override func viewDidLoad() { super.viewDidLoad() self.isValid = false self.setupNormalNavigation() self.newPinTxtField.isSecureTextEntry = true self.confirmPinTxtField.isSecureTextEntry = true self.currentPinTxtField.isSecureTextEntry = true newPinTxtField.addTarget(self, action: #selector(editingChanged(_:)), for: .editingChanged) confirmPinTxtField.addTarget(self, action: #selector(editingChanged(_:)), for: .editingChanged) currentPinTxtField.addTarget(self, action: #selector(editingChanged(_:)), for: .editingChanged) currentPinTxtField.validCondition = {$0.count > 5} newPinTxtField.validCondition = {$0.count > 5} confirmPinTxtField.validCondition = {$0.count > 5} configureLanguage() changePinBttn.layer.cornerRadius = 6 } override func viewWillAppear(_ animated: Bool) { super.viewWillDisappear(animated) self.navigationItem.title = StringConstants().titleText } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) self.navigationItem.title = "" } func configureLanguage() { currentPinTxtField.placeholder = "currentPin_text".localized() currentPinTxtField.titleText = "currentPin_text".localized() currentPinTxtField.errorMessage = "new_pin_error_text".localized() newPinTxtField.placeholder = "newPin_text".localized() newPinTxtField.titleText = "newPin_text".localized() newPinTxtField.errorMessage = "new_pin_error_text".localized() confirmPinTxtField.placeholder = "confirmNewPin_text".localized() confirmPinTxtField.titleText = "confirmNewPin_text".localized() confirmPinTxtField.errorMessage = "new_pin_error_text".localized() self.changePinBttn.setTitle(StringConstants().saveText, for: UIControl.State.normal) } @objc private func editingChanged(_ textField: UITextField) { switch textField { case currentPinTxtField: encryptedOldPassword = textField.text ?? "" case newPinTxtField: encryptedPassword = textField.text ?? "" case confirmPinTxtField: encryptedConfirmPassword = textField.text ?? "" default: break } self.isValid = (encryptedOldPassword != "" && encryptedPassword != "" && encryptedConfirmPassword != "") } } extension ProfileChangePinViewController { private func isValidPasword(password: String, confirmPassword: String) -> (isValid: Bool, error: String) { var error = "" var isValid = true if password.isEmpty && password.validateRegex(regex: passwordRegex){ error = "\(error)\n\(StringConstants().emptyPasswordError)" isValid = false } if confirmPassword.isEmpty && confirmPassword.validateRegex(regex: passwordRegex){ error = "\(error)\n\(StringConstants().emptyPasswordError)" isValid = false } return (isValid, error) } // MARK: Converting entities private func isValid( currentPassword: String, password: String, confirmPassword: String ) -> (isValid: Bool, error: Error) { var error = "" var isValid = true // user name if currentPassword.isEmpty { error = "\(error)\n\(StringConstants().emptyPasswordError)" isValid = false } let result = self.isValidPasword(password: password, confirmPassword: confirmPassword) if !result.isValid { error = "\(error) \(result.error)" isValid = false } let newError = NSError( domain: "LoginInteractor", code: 0, userInfo: [NSLocalizedDescriptionKey : error] ) return (isValid, newError) } } extension ProfileChangePinViewController: UpdatePinService { } protocol UpdatePinService: ApiServiceType { func updatePin( params: [String: String], success: @escaping (String?) -> Void, failure: @escaping (Error) -> Void ) } extension UpdatePinService { func updatePin( params: [String: String], success: @escaping (String?) -> Void, failure: @escaping (Error) -> Void ) { let url = baseUrl + "/mobile/changeTxnPin" auth.request( method: .post, url: url, params: params, needsAuthorization: true, success: { (response: SuccessMessageContainer) in if (response.errorCode ?? "") == "1" { let error = NSError( domain: "Network", code: 0, userInfo: [NSLocalizedDescriptionKey : response.message ?? ""] ) failure(error) } else { success(response.message) } }, failure: { (error) in failure(error) } ) } }