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.

286 lines
10 KiB

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