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.

310 lines
8.8 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 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_policy_text".localized()
  18. let newPasswordTitleText = "new_password_text".localized()
  19. let currentPasswordTitleText = "current_password_text".localized()
  20. let confirmPasswordTitleText = "confirm_password_text".localized()
  21. let saveText = "save_text".localized()
  22. let titleText = "change_password_title_text".localized()
  23. let successText = "success_text".localized()
  24. let emptyPasswordError = "password_empty_error".localized()
  25. let confirmPasswordError = "password_confirm_empty_error".localized()
  26. let passwordLengthError = "password_length_error".localized()
  27. let passwordPolicy = "password_policy_error".localized()
  28. let passwordMatchError = "password_match_error".localized()
  29. }
  30. @IBOutlet weak var textFieldCurrentPassword: UITextField!
  31. @IBOutlet weak var textFieldNewPassword: UITextField!
  32. @IBOutlet weak var textFieldConfirmPassword: UITextField!
  33. @IBOutlet weak var topLabel: UILabel!
  34. @IBOutlet weak var contentView: UIView!
  35. @IBOutlet weak var currentPasswowrdTitleLabel: UILabel!
  36. @IBOutlet weak var newPasswordTitleLabel: UILabel!
  37. @IBOutlet weak var confirmPasswordTitlelabel: UILabel!
  38. @IBOutlet weak var saveButton: UIButton!
  39. // MARK: - Properties
  40. var password = "" {
  41. didSet {
  42. self.textFieldNewPassword.text = password
  43. }
  44. }
  45. var confirmPassword = "" {
  46. didSet {
  47. self.textFieldConfirmPassword.text = confirmPassword
  48. }
  49. }
  50. var oldPassword = "" {
  51. didSet {
  52. self.textFieldCurrentPassword.text = oldPassword
  53. }
  54. }
  55. var encryptedPassword: String = ""
  56. var encryptedOldPassword = ""
  57. var encryptedConfirmPassword = ""
  58. private var selectedType: PasswordType = .old
  59. // MARK: Life Cycle
  60. override func viewDidLoad() {
  61. super.viewDidLoad()
  62. self.setupNormalNavigation()
  63. topLabel.font = .sanfrancisco(.regular, size: 14)
  64. topLabel.textColor = .themeText
  65. self.textFieldNewPassword.delegate = self
  66. self.textFieldConfirmPassword.delegate = self
  67. self.textFieldCurrentPassword.delegate = self
  68. configureLanguage()
  69. saveButton.backgroundColor = .themeRed
  70. saveButton.layer.cornerRadius = 5
  71. }
  72. override func viewWillAppear(_ animated: Bool) {
  73. super.viewWillDisappear(animated)
  74. self.navigationItem.title = StringConstants().titleText
  75. }
  76. override func viewWillDisappear(_ animated: Bool) {
  77. super.viewWillDisappear(animated)
  78. self.navigationItem.title = ""
  79. }
  80. func configureLanguage() {
  81. self.topLabel.text = "password_policy_text".localized()
  82. self.textFieldCurrentPassword.placeholder = "current_password_placeholder_text".localized()
  83. self.textFieldNewPassword.placeholder = "new_password_placeholder_text".localized()
  84. self.textFieldConfirmPassword.placeholder = "confirm_password_text".localized()
  85. self.newPasswordTitleLabel.text = "new_password_text".localized()
  86. self.currentPasswowrdTitleLabel.text = "current_password_text".localized()
  87. self.confirmPasswordTitlelabel.text = "confirm_password_text".localized()
  88. self.saveButton.setTitle("save_text".localized(), for: UIControl.State.normal)
  89. }
  90. @IBAction func savePasswordChanges(_ sender: Any) {
  91. let currentPassword = self.encryptedOldPassword
  92. let newPassword = self.encryptedPassword
  93. let confirmPassword = self.encryptedConfirmPassword
  94. let result = self.isValid(
  95. currentPassword: currentPassword,
  96. password: newPassword,
  97. confirmPassword: confirmPassword
  98. )
  99. if result.isValid {
  100. let userId = Utility.getMyUserName()
  101. let param =
  102. [
  103. "UserId" : userId,
  104. "OldPassword": currentPassword,
  105. "NewPassword": newPassword,
  106. "ConfirmPassword": confirmPassword
  107. ]
  108. self.updatePassword(param: param)
  109. } else {
  110. let message = result.error
  111. self.alert(type: .error, message: message.localizedDescription)
  112. }
  113. }
  114. func updatePassword(param: [String: String]) {
  115. self.updatePassword(
  116. params: param,
  117. success: { (message) in
  118. self.alert(message: message, title: StringConstants().successText, okAction: {
  119. KeyChain.shared.save(data: self.encryptedPassword, key: .password)
  120. self.navigationController?.popViewController(animated: true)
  121. })
  122. },
  123. failure: { (error) in
  124. self.alert(type: .error, message: error.localizedDescription)
  125. }
  126. )
  127. }
  128. private func showSecureKeypad(type: PasswordType) {
  129. let secureKeypad = SecureKeypad(target: self)
  130. secureKeypad.delegate = self
  131. switch type {
  132. case .old:
  133. secureKeypad.title = "current_password_text".localized()
  134. secureKeypad.placeholder = "current_password_placeholder_text".localized()
  135. case .new:
  136. secureKeypad.title = "new_password_text".localized()
  137. secureKeypad.placeholder = "new_password_placeholder_text".localized()
  138. case .newConfirm:
  139. secureKeypad.title = "confirm_password_text".localized()
  140. secureKeypad.placeholder = "confirm_password_text".localized()
  141. }
  142. secureKeypad.present(animated: true)
  143. }
  144. }
  145. extension ProfileChangePasswordViewController {
  146. private func isValidPasword(password: String, confirmPassword: String) -> (isValid: Bool, error: String) {
  147. var error = ""
  148. var isValid = true
  149. if password.isEmpty {
  150. error = "\(error)\n\(StringConstants().emptyPasswordError)"
  151. isValid = false
  152. }
  153. if confirmPassword.isEmpty {
  154. error = "\(error)\n\(StringConstants().emptyPasswordError)"
  155. isValid = false
  156. }
  157. return (isValid, error)
  158. }
  159. // MARK: Converting entities
  160. private func isValid(
  161. currentPassword: String,
  162. password: String,
  163. confirmPassword: String
  164. ) -> (isValid: Bool, error: Error) {
  165. var error = ""
  166. var isValid = true
  167. // user name
  168. if currentPassword.isEmpty {
  169. error = "\(error)\n\(StringConstants().emptyPasswordError)"
  170. isValid = false
  171. }
  172. let result = self.isValidPasword(password: password, confirmPassword: confirmPassword)
  173. if !result.isValid {
  174. error = "\(error) \(result.error)"
  175. isValid = false
  176. }
  177. let newError = NSError(
  178. domain: "LoginInteractor",
  179. code: 0,
  180. userInfo: [NSLocalizedDescriptionKey : error]
  181. )
  182. return (isValid, newError)
  183. }
  184. }
  185. extension ProfileChangePasswordViewController: UpdatePasswordService {
  186. }
  187. protocol UpdatePasswordService: ApiServiceType {
  188. func updatePassword(
  189. params: [String: String],
  190. success: @escaping (String?) -> Void,
  191. failure: @escaping (Error) -> Void
  192. )
  193. }
  194. extension UpdatePasswordService {
  195. func updatePassword(
  196. params: [String: String],
  197. success: @escaping (String?) -> Void,
  198. failure: @escaping (Error) -> Void
  199. ) {
  200. let url = baseUrl + "/mobile/ChangePassword"
  201. auth.request(
  202. method: .post,
  203. url: url,
  204. params: params,
  205. needsAuthorization: true,
  206. success: { (response: SuccessMessageContainer) in
  207. if (response.errorCode ?? "") == "1" {
  208. let error = NSError(
  209. domain: "Network",
  210. code: 0,
  211. userInfo: [NSLocalizedDescriptionKey : response.message ?? ""]
  212. )
  213. failure(error)
  214. } else {
  215. success(response.data?.message)
  216. }
  217. },
  218. failure: { (error) in
  219. failure(error)
  220. }
  221. )
  222. }
  223. }
  224. extension ProfileChangePasswordViewController: UITextFieldDelegate {
  225. func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
  226. if textField == textFieldCurrentPassword {
  227. selectedType = .old
  228. showSecureKeypad(type: selectedType)
  229. return false
  230. }
  231. if textField == textFieldNewPassword {
  232. selectedType = .new
  233. showSecureKeypad(type: selectedType)
  234. return false
  235. }
  236. if textField == textFieldConfirmPassword {
  237. selectedType = .newConfirm
  238. showSecureKeypad(type: selectedType)
  239. return false
  240. }
  241. return true
  242. }
  243. }
  244. extension ProfileChangePasswordViewController: SecureKeypadDelegate {
  245. func didComplete(_ encryptedString: String, garbagePassword: String, length: Int) {
  246. if length < 5 {
  247. alert(message: StringConstants().passwordLengthError)
  248. } else {
  249. switch selectedType {
  250. case .old:
  251. encryptedOldPassword = encryptedString
  252. oldPassword = garbagePassword
  253. case .new:
  254. encryptedPassword = encryptedString
  255. password = garbagePassword
  256. case .newConfirm:
  257. encryptedConfirmPassword = encryptedString
  258. confirmPassword = garbagePassword
  259. }
  260. }
  261. }
  262. }