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.

274 lines
9.5 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. //
  2. // ProfileChangePinViewController.swift
  3. // GME Remit
  4. //
  5. // Created by Shiran on 8/17/20.
  6. // Copyright © 2020 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import UIKit
  9. import Localize_Swift
  10. class ProfileChangePinViewController: UIViewController {
  11. enum PasswordType {
  12. case old
  13. case new
  14. case newConfirm
  15. }
  16. struct StringConstants {
  17. let passwordErrorText = "password_error_text".localized()
  18. let newPinTitleText = "New PIN"
  19. let currentPinTitleText = "Current PIN"
  20. let confirmPinTitleText = "Confirm PIN"
  21. let saveText = "save_text".localized()
  22. let titleText = "Change PIN"
  23. let successText = "success_text".localized()
  24. let emptyPasswordError = "PIN cannot be empty"
  25. let confirmPasswordError = "Confirm PIN cannot be empty"
  26. let passwordPolicy = "PIN must be of 6 characters"
  27. }
  28. @IBOutlet weak var changePinBttn: UIButton!
  29. @IBOutlet weak var currentPinTxtField: ValidationTextField!
  30. @IBOutlet weak var newPinTxtField: ValidationTextField!
  31. @IBOutlet weak var confirmPinTxtField: ValidationTextField!
  32. @IBOutlet weak var currentPinToggleButton: UIButton!
  33. @IBOutlet weak var newPinToggleButton: UIButton!
  34. @IBOutlet weak var confirmPinToggleButton: UIButton!
  35. @IBAction func changePinBttnPressed(_ sender: UIButton) {
  36. let currentPassword = self.encryptedOldPassword
  37. let newPassword = self.encryptedPassword
  38. let confirmPassword = self.encryptedConfirmPassword
  39. let result = self.isValid(
  40. currentPassword: currentPassword,
  41. password: newPassword,
  42. confirmPassword: confirmPassword
  43. )
  44. if result.isValid {
  45. let userId = Utility.getMyUserName()
  46. let param =
  47. [
  48. "UserId" : userId,
  49. "CurrentPin": currentPassword,
  50. "NewPin": newPassword,
  51. "ConfirmNewPin": confirmPassword
  52. ]
  53. self.updatePassword(param: param)
  54. }else {
  55. let message = result.error
  56. self.alert(type: .error, message: message.localizedDescription)
  57. }
  58. }
  59. @IBAction func currentPinButton(_ sender: UIButton) {
  60. currentPinTxtField.isSecureTextEntry = !currentPinTxtField.isSecureTextEntry
  61. currentPinTxtField.isSecureTextEntry ? currentPinToggleButton.setImage(UIImage(named: "Show"), for: .normal) : currentPinToggleButton.setImage(UIImage(named: "Hide"), for: .normal)
  62. }
  63. @IBAction func newPinButton(_ sender: UIButton) {
  64. newPinTxtField.isSecureTextEntry = !newPinTxtField.isSecureTextEntry
  65. newPinTxtField.isSecureTextEntry ? newPinToggleButton.setImage(UIImage(named: "Show"), for: .normal) : newPinToggleButton.setImage(UIImage(named: "Hide"), for: .normal)
  66. }
  67. @IBAction func confirmPinButton(_ sender: UIButton) {
  68. confirmPinTxtField.isSecureTextEntry = !confirmPinTxtField.isSecureTextEntry
  69. confirmPinTxtField.isSecureTextEntry ? confirmPinToggleButton.setImage(UIImage(named: "Show"), for: .normal) : confirmPinToggleButton.setImage(UIImage(named: "Hide"), for: .normal)
  70. }
  71. func updatePassword(param: [String: String]) {
  72. self.updatePin(
  73. params: param,
  74. success: { (message) in
  75. self.alertWithOk(
  76. type: .success,
  77. message: message,
  78. title: "success_text".localized(),
  79. okTitle: "Ok") {
  80. self.navigationController?.popViewController(animated: true)
  81. }
  82. },
  83. failure: { (error) in
  84. self.alert(type: .error, message: error.localizedDescription)
  85. }
  86. )
  87. }
  88. // MARK: - Properties
  89. var encryptedPassword: String = ""
  90. var encryptedOldPassword = ""
  91. var encryptedConfirmPassword = ""
  92. private var selectedType: PasswordType = .old
  93. private var isValid = false {
  94. didSet {
  95. // changePinBttn.isEnabled = isValid
  96. // changePinBttn.backgroundColor = isValid ? .themeRed : .themeText
  97. changePinBttn.isEnabled = isValid
  98. changePinBttn.backgroundColor = isValid ? .themeRed : .themeRed
  99. }
  100. }
  101. // MARK: Life Cycle
  102. override func viewDidLoad() {
  103. super.viewDidLoad()
  104. self.isValid = false
  105. self.setupNormalNavigation()
  106. self.newPinTxtField.isSecureTextEntry = true
  107. self.confirmPinTxtField.isSecureTextEntry = true
  108. self.currentPinTxtField.isSecureTextEntry = true
  109. newPinTxtField.addTarget(self, action: #selector(editingChanged(_:)), for: .editingChanged)
  110. confirmPinTxtField.addTarget(self, action: #selector(editingChanged(_:)), for: .editingChanged)
  111. currentPinTxtField.addTarget(self, action: #selector(editingChanged(_:)), for: .editingChanged)
  112. currentPinTxtField.validCondition = {$0.count > 5}
  113. newPinTxtField.validCondition = {$0.count > 5}
  114. confirmPinTxtField.validCondition = {$0.count > 5}
  115. configureLanguage()
  116. changePinBttn.layer.cornerRadius = 6
  117. }
  118. override func viewWillAppear(_ animated: Bool) {
  119. super.viewWillDisappear(animated)
  120. self.navigationItem.title = StringConstants().titleText
  121. }
  122. override func viewWillDisappear(_ animated: Bool) {
  123. super.viewWillDisappear(animated)
  124. self.navigationItem.title = ""
  125. }
  126. func configureLanguage() {
  127. currentPinTxtField.placeholder = "currentPin_text".localized()
  128. currentPinTxtField.titleText = "currentPin_text".localized()
  129. currentPinTxtField.errorMessage = "new_pin_error_text".localized()
  130. newPinTxtField.placeholder = "newPin_text".localized()
  131. newPinTxtField.titleText = "newPin_text".localized()
  132. newPinTxtField.errorMessage = "new_pin_error_text".localized()
  133. confirmPinTxtField.placeholder = "confirmNewPin_text".localized()
  134. confirmPinTxtField.titleText = "confirmNewPin_text".localized()
  135. confirmPinTxtField.errorMessage = "new_pin_error_text".localized()
  136. self.changePinBttn.setTitle(StringConstants().saveText, for: UIControl.State.normal)
  137. }
  138. @objc private func editingChanged(_ textField: UITextField) {
  139. switch textField {
  140. case currentPinTxtField:
  141. encryptedOldPassword = textField.text ?? ""
  142. case newPinTxtField:
  143. encryptedPassword = textField.text ?? ""
  144. case confirmPinTxtField:
  145. encryptedConfirmPassword = textField.text ?? ""
  146. default:
  147. break
  148. }
  149. self.isValid = (encryptedOldPassword != "" && encryptedPassword != "" && encryptedConfirmPassword != "")
  150. }
  151. }
  152. extension ProfileChangePinViewController {
  153. private func isValidPasword(password: String, confirmPassword: String) -> (isValid: Bool, error: String) {
  154. var error = ""
  155. var isValid = true
  156. if password.isEmpty && password.validateRegex(regex: passwordRegex){
  157. error = "\(error)\n\(StringConstants().emptyPasswordError)"
  158. isValid = false
  159. }
  160. if confirmPassword.isEmpty && confirmPassword.validateRegex(regex: passwordRegex){
  161. error = "\(error)\n\(StringConstants().emptyPasswordError)"
  162. isValid = false
  163. }
  164. return (isValid, error)
  165. }
  166. // MARK: Converting entities
  167. private func isValid(
  168. currentPassword: String,
  169. password: String,
  170. confirmPassword: String
  171. ) -> (isValid: Bool, error: Error) {
  172. var error = ""
  173. var isValid = true
  174. // user name
  175. if currentPassword.isEmpty {
  176. error = "\(error)\n\(StringConstants().emptyPasswordError)"
  177. isValid = false
  178. }
  179. let result = self.isValidPasword(password: password, confirmPassword: confirmPassword)
  180. if !result.isValid {
  181. error = "\(error) \(result.error)"
  182. isValid = false
  183. }
  184. let newError = NSError(
  185. domain: "LoginInteractor",
  186. code: 0,
  187. userInfo: [NSLocalizedDescriptionKey : error]
  188. )
  189. return (isValid, newError)
  190. }
  191. }
  192. extension ProfileChangePinViewController: UpdatePinService {
  193. }
  194. protocol UpdatePinService: ApiServiceType {
  195. func updatePin(
  196. params: [String: String],
  197. success: @escaping (String?) -> Void,
  198. failure: @escaping (Error) -> Void
  199. )
  200. }
  201. extension UpdatePinService {
  202. func updatePin(
  203. params: [String: String],
  204. success: @escaping (String?) -> Void,
  205. failure: @escaping (Error) -> Void
  206. ) {
  207. let url = baseUrl + "/mobile/changeTxnPin"
  208. auth.request(
  209. method: .post,
  210. url: url,
  211. params: params,
  212. needsAuthorization: true,
  213. success: { (response: SuccessMessageContainer) in
  214. if (response.errorCode ?? "") == "1" {
  215. let error = NSError(
  216. domain: "Network",
  217. code: 0,
  218. userInfo: [NSLocalizedDescriptionKey : response.message ?? ""]
  219. )
  220. failure(error)
  221. } else {
  222. success(response.message)
  223. }
  224. },
  225. failure: { (error) in
  226. failure(error)
  227. }
  228. )
  229. }
  230. }