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.

276 lines
10 KiB

6 years ago
6 years ago
4 years ago
4 years ago
4 years ago
6 years ago
4 years ago
6 years ago
6 years ago
4 years ago
4 years ago
4 years ago
6 years ago
6 years ago
4 years ago
4 years ago
4 years ago
6 years ago
6 years ago
4 years ago
  1. //
  2. // ProfileChangePasswordViewController.swift
  3. // GMERemittance
  4. //
  5. // Created by Fm-user on 1/9/18.
  6. // Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import UIKit
  9. import Localize_Swift
  10. class ProfileChangePasswordViewController: 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 saveText = "save_text".localized()
  19. let titleText = "change_password_text".localized()
  20. let successText = "success_text".localized()
  21. let emptyPasswordError = "password_empty_error".localized()
  22. let confirmPasswordError = "password_confirm_empty_error".localized()
  23. let passwordLengthError = "password_length_error".localized()
  24. let passwordPolicy = "password_error_text".localized()
  25. let passwordMatchError = "password_match_error".localized()
  26. }
  27. @IBOutlet weak var textFieldCurrentPassword: ValidationTextField!
  28. @IBOutlet weak var textFieldNewPassword: ValidationTextField!
  29. @IBOutlet weak var textFieldConfirmPassword: ValidationTextField!
  30. @IBOutlet weak var contentView: UIView!
  31. @IBOutlet weak var saveButton: UIButton!
  32. @IBOutlet weak var currentPassToggleButton: UIButton!
  33. @IBOutlet weak var newPassToggleButton: UIButton!
  34. @IBOutlet weak var confirmPassToggleButton: UIButton!
  35. @IBAction func currentPassToggle(_ sender: UIButton) {
  36. textFieldCurrentPassword.isSecureTextEntry = !textFieldCurrentPassword.isSecureTextEntry
  37. textFieldCurrentPassword.isSecureTextEntry ? currentPassToggleButton.setImage(UIImage(named: "Show"), for: .normal) : currentPassToggleButton.setImage(UIImage(named: "Hide"), for: .normal)
  38. }
  39. @IBAction func newPassToggle(_ sender: UIButton) {
  40. textFieldNewPassword.isSecureTextEntry = !textFieldNewPassword.isSecureTextEntry
  41. textFieldNewPassword.isSecureTextEntry ? newPassToggleButton.setImage(UIImage(named: "Show"), for: .normal) : newPassToggleButton.setImage(UIImage(named: "Hide"), for: .normal)
  42. }
  43. @IBAction func confirmPassToggle(_ sender: UIButton) {
  44. textFieldConfirmPassword.isSecureTextEntry = !textFieldConfirmPassword.isSecureTextEntry
  45. textFieldConfirmPassword.isSecureTextEntry ? confirmPassToggleButton.setImage(UIImage(named: "Show"), for: .normal) : confirmPassToggleButton.setImage(UIImage(named: "Hide"), for: .normal)
  46. }
  47. // MARK: - Properties
  48. var encryptedPassword: String = ""
  49. var encryptedOldPassword = ""
  50. var encryptedConfirmPassword = ""
  51. private var selectedType: PasswordType = .old
  52. private var isValid = false {
  53. didSet {
  54. // saveButton.isEnabled = isValid
  55. // saveButton.backgroundColor = isValid ? .themeRed : .themeText
  56. saveButton.isEnabled = true
  57. saveButton.backgroundColor = isValid ? .themeRed : .themeRed
  58. }
  59. }
  60. // MARK: Life Cycle
  61. override func viewDidLoad() {
  62. super.viewDidLoad()
  63. self.setupNormalNavigation()
  64. self.isValid = false
  65. self.textFieldNewPassword.isSecureTextEntry = true
  66. self.textFieldConfirmPassword.isSecureTextEntry = true
  67. self.textFieldCurrentPassword.isSecureTextEntry = true
  68. textFieldCurrentPassword.validCondition = { !$0.isEmpty && $0.validateRegex(regex: passwordRegex)}
  69. textFieldNewPassword.validCondition = { !$0.isEmpty && $0.validateRegex(regex: passwordRegex)}
  70. textFieldConfirmPassword.validCondition = { !$0.isEmpty && $0.validateRegex(regex: passwordRegex)}
  71. textFieldNewPassword.addTarget(self, action: #selector(editingChanged(_:)), for: .editingChanged)
  72. textFieldConfirmPassword.addTarget(self, action: #selector(editingChanged(_:)), for: .editingChanged)
  73. textFieldCurrentPassword.addTarget(self, action: #selector(editingChanged(_:)), for: .editingChanged)
  74. configureLanguage()
  75. saveButton.layer.cornerRadius = 6
  76. saveButton.setTitle("save_text".localized(), for: .normal)
  77. }
  78. override func viewWillAppear(_ animated: Bool) {
  79. super.viewWillDisappear(animated)
  80. self.navigationItem.title = StringConstants().titleText
  81. }
  82. override func viewWillDisappear(_ animated: Bool) {
  83. super.viewWillDisappear(animated)
  84. self.navigationItem.title = ""
  85. }
  86. func configureLanguage() {
  87. textFieldCurrentPassword.placeholder = "current_password_text".localized()
  88. textFieldCurrentPassword.titleText = "current_password_text".localized()
  89. textFieldCurrentPassword.errorMessage = "password_error_text".localized()
  90. textFieldNewPassword.placeholder = "new_password_text".localized()
  91. textFieldNewPassword.titleText = "new_password_text".localized()
  92. textFieldNewPassword.errorMessage = "password_error_text".localized()
  93. textFieldConfirmPassword.placeholder = "confirm_password_text".localized()
  94. textFieldConfirmPassword.titleText = "confirm_password_text".localized()
  95. textFieldConfirmPassword.errorMessage = "password_error_text".localized()
  96. }
  97. @IBAction func savePasswordChanges(_ sender: Any) {
  98. let currentPassword = self.encryptedOldPassword
  99. let newPassword = self.encryptedPassword
  100. let confirmPassword = self.encryptedConfirmPassword
  101. let result = self.isValid(
  102. currentPassword: currentPassword,
  103. password: newPassword,
  104. confirmPassword: confirmPassword
  105. )
  106. if result.isValid {
  107. let userId = Utility.getMyUserName()
  108. let param =
  109. [
  110. "UserId" : userId,
  111. "OldPassword": currentPassword,
  112. "NewPassword": newPassword,
  113. "ConfirmPassword": confirmPassword
  114. ]
  115. self.updatePassword(param: param)
  116. } else {
  117. let message = result.error
  118. self.alertWithOk(type: .error, message: message.localizedDescription, title: "Error", okTitle: "Ok", okAction: nil)
  119. }
  120. }
  121. func updatePassword(param: [String: String]) {
  122. self.updatePassword(
  123. params: param,
  124. success: { (message) in
  125. self.alertWithOk(
  126. type: .success,
  127. message: message,
  128. title: "success_text".localized(),
  129. okTitle: "Ok") {
  130. KeyChain.shared.save(data: self.encryptedPassword, key: .password)
  131. self.navigationController?.popViewController(animated: true)
  132. }
  133. },
  134. failure: { (error) in
  135. self.alertWithOk(type: .error, message: error.localizedDescription, title: "Error", okTitle: "Ok", okAction: nil)
  136. }
  137. )
  138. }
  139. }
  140. extension ProfileChangePasswordViewController {
  141. private func isValidPasword(password: String, confirmPassword: String) -> (isValid: Bool, error: String) {
  142. var error = ""
  143. var isValid = true
  144. if !password.validateRegex(regex: passwordRegex){
  145. error = "\(error)\n\(StringConstants().emptyPasswordError)"
  146. isValid = false
  147. }
  148. if !confirmPassword.validateRegex(regex: passwordRegex){
  149. error = "\(error)\n\(StringConstants().emptyPasswordError)"
  150. isValid = false
  151. }
  152. return (isValid, error)
  153. }
  154. // MARK: Converting entities
  155. private func isValid(
  156. currentPassword: String,
  157. password: String,
  158. confirmPassword: String
  159. ) -> (isValid: Bool, error: Error) {
  160. var error = ""
  161. var isValid = true
  162. // user name
  163. if !currentPassword.validateRegex(regex: passwordRegex){
  164. error = "\(error)\n\(StringConstants().emptyPasswordError)"
  165. isValid = false
  166. }
  167. let result = self.isValidPasword(password: password, confirmPassword: confirmPassword)
  168. if !result.isValid {
  169. error = "\(error) \(result.error)"
  170. isValid = false
  171. }
  172. let newError = NSError(
  173. domain: "LoginInteractor",
  174. code: 0,
  175. userInfo: [NSLocalizedDescriptionKey : error]
  176. )
  177. return (isValid, newError)
  178. }
  179. }
  180. extension ProfileChangePasswordViewController: UpdatePasswordService {
  181. }
  182. protocol UpdatePasswordService: ApiServiceType {
  183. func updatePassword(
  184. params: [String: String],
  185. success: @escaping (String?) -> Void,
  186. failure: @escaping (Error) -> Void
  187. )
  188. }
  189. extension UpdatePasswordService {
  190. func updatePassword(
  191. params: [String: String],
  192. success: @escaping (String?) -> Void,
  193. failure: @escaping (Error) -> Void
  194. ) {
  195. let url = baseUrl + "/mobile/ChangePassword"
  196. auth.request(
  197. method: .post,
  198. url: url,
  199. params: params,
  200. needsAuthorization: true,
  201. success: { (response: SuccessMessageContainer) in
  202. if (response.errorCode ?? "") == "1" {
  203. let error = NSError(
  204. domain: "Network",
  205. code: 0,
  206. userInfo: [NSLocalizedDescriptionKey : response.message ?? ""]
  207. )
  208. failure(error)
  209. } else {
  210. success(response.data?.message)
  211. }
  212. },
  213. failure: { (error) in
  214. failure(error)
  215. }
  216. )
  217. }
  218. }
  219. extension ProfileChangePasswordViewController {
  220. @objc private func editingChanged(_ textField: UITextField) {
  221. switch textField {
  222. case textFieldCurrentPassword:
  223. encryptedOldPassword = textField.text ?? ""
  224. case textFieldNewPassword:
  225. encryptedPassword = textField.text ?? ""
  226. case textFieldConfirmPassword:
  227. encryptedConfirmPassword = textField.text ?? ""
  228. default:
  229. break
  230. }
  231. self.isValid = (encryptedOldPassword.validateRegex(regex: passwordRegex) && encryptedPassword.validateRegex(regex: passwordRegex) && encryptedConfirmPassword.validateRegex(regex: passwordRegex) )
  232. }
  233. }