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.

195 lines
6.1 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
  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. // there is not time to take this to viper archi. so applying mvc here.
  10. class ProfileChangePasswordViewController: UIViewController {
  11. @IBOutlet weak var textFieldCurrentPassword: UITextField!
  12. @IBOutlet weak var textFieldNewPassword: UITextField!
  13. @IBOutlet weak var textFieldConfirmPassword: UITextField!
  14. @IBOutlet weak var topLabel: UILabel!
  15. @IBOutlet weak var contentView: UIView!
  16. override func viewDidLoad() {
  17. super.viewDidLoad()
  18. self.setupNormalNavigation()
  19. topLabel.font = UIFont.init(name: Fonts.Family.regular, size: 14)
  20. topLabel.textColor = UIColor.init(hex: "4a4a4a")
  21. }
  22. override func viewWillAppear(_ animated: Bool) {
  23. super.viewWillDisappear(animated)
  24. self.navigationItem.title = "Change Password"
  25. }
  26. override func viewWillDisappear(_ animated: Bool) {
  27. super.viewWillDisappear(animated)
  28. self.navigationItem.title = ""
  29. }
  30. @IBAction func savePasswordChanges(_ sender: Any) {
  31. let currentPassword = self.textFieldCurrentPassword.text!
  32. let newPassword = self.textFieldNewPassword.text!
  33. let confirmPassword = self.textFieldConfirmPassword.text!
  34. let result = self.isValid(currentPassword: currentPassword, password: newPassword, confirmPassword: confirmPassword)
  35. if result.isValid {
  36. let userId = Utility.getMyUserName()
  37. let param =
  38. [
  39. "UserId" : userId,
  40. "OldPassword": currentPassword,
  41. "NewPassword": newPassword
  42. ]
  43. self.updatePassword(param: param)
  44. }else {
  45. let message = result.error
  46. self.alert(message: message.localizedDescription)
  47. }
  48. }
  49. func updatePassword(param: [String: String]) {
  50. self.updatePassword(params: param, success: { (message) in
  51. self.alert(message: message, title: "Success", okAction: {
  52. self.navigationController?.popViewController(animated: true)
  53. })
  54. }) { (error) in
  55. self.alert(message: error.localizedDescription)
  56. }
  57. }
  58. }
  59. extension ProfileChangePasswordViewController {
  60. private func isValidPasword(password: String, confirmPassword: String) -> (isValid: Bool, error: String) {
  61. var error = ""
  62. var isValid = true
  63. // >= 9, 1 special character, 1 number
  64. func isValidLength() -> Bool {
  65. return password.count >= 9
  66. }
  67. func hasNumber() -> Bool {
  68. let characterSet = CharacterSet.init(charactersIn: "1234567890")
  69. return password.rangeOfCharacter(from: characterSet) != nil
  70. }
  71. func hasLetter() -> Bool {
  72. let characterSet = CharacterSet.letters
  73. return password.rangeOfCharacter(from: characterSet) != nil
  74. }
  75. func hasSpecialCharacter() -> Bool {
  76. let characterSet = CharacterSet.alphanumerics
  77. return password.rangeOfCharacter(from: characterSet.inverted) != nil
  78. }
  79. func hasUpperCase() -> Bool {
  80. let characterSet = CharacterSet.uppercaseLetters
  81. return password.rangeOfCharacter(from: characterSet) != nil
  82. }
  83. if password.isEmpty {
  84. error = error + "\n Password cannot be empty"
  85. isValid = false
  86. }else {
  87. if !isValidLength() {
  88. error = error + "\n Password should have at least 9 characters"
  89. isValid = false
  90. return (isValid, error)
  91. }
  92. let validPassword = hasNumber() && hasLetter() && hasSpecialCharacter() && hasUpperCase()
  93. if !validPassword {
  94. isValid = false
  95. error = error + "\n Password should contain at least one symbol, one capital letter and one number"
  96. return (isValid, error)
  97. }
  98. if password != confirmPassword {
  99. error = error + "\n Passwords does not match."
  100. isValid = false
  101. }
  102. }
  103. return (isValid, error)
  104. }
  105. // MARK: Converting entities
  106. private func isValid(currentPassword: String, password: String, confirmPassword: String) -> (isValid: Bool, error: Error) {
  107. var error = ""
  108. var isValid = true
  109. // user name
  110. if currentPassword.isEmpty {
  111. error = error + "current password field is required.";
  112. isValid = false
  113. }
  114. let result = self.isValidPasword(password: password, confirmPassword: confirmPassword)
  115. if !result.isValid {
  116. error = error + result.error
  117. isValid = false
  118. }
  119. let _error = NSError.init(domain: "LoginInteractor", code: 0, userInfo: [NSLocalizedDescriptionKey : error])
  120. return (isValid, _error)
  121. }
  122. }
  123. extension ProfileChangePasswordViewController: UpdatePasswordService {
  124. }
  125. protocol UpdatePasswordService: ApiServiceType {
  126. func updatePassword(params: [String: String], success: @escaping (String?) -> (), failure: @escaping (Error) -> ())
  127. }
  128. extension UpdatePasswordService {
  129. func updatePassword(params: [String: String], success: @escaping (String?) -> (), failure: @escaping (Error) -> ()) {
  130. let url = baseUrl + "mobile/ChangePassword"
  131. auth.request(method: .post, url: url, params: params, needsAuthorization: false, success: { (response: SuccessMessageContainer) in
  132. if (response.errorCode ?? "") == "1" {
  133. let error = NSError.init(domain: "Network", code: 0, userInfo: [NSLocalizedDescriptionKey : response.message ?? ""])
  134. failure(error)
  135. }else {
  136. success(response.data?.message)
  137. }
  138. }) { (error) in
  139. failure(error)
  140. }
  141. }
  142. }