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.

194 lines
5.9 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. }
  20. override func viewWillAppear(_ animated: Bool) {
  21. super.viewWillDisappear(animated)
  22. self.navigationItem.title = "Change Password"
  23. }
  24. override func viewWillDisappear(_ animated: Bool) {
  25. super.viewWillDisappear(animated)
  26. self.navigationItem.title = ""
  27. }
  28. // "{
  29. // ""UserId"":""pathaksumitra4@gmail.com"",
  30. // ""OldPassword"":""P23FAB"",
  31. // ""NewPassword"":""swift""
  32. //
  33. //}"
  34. @IBAction func savePasswordChanges(_ sender: Any) {
  35. let currentPassword = self.textFieldCurrentPassword.text!
  36. let newPassword = self.textFieldNewPassword.text!
  37. let confirmPassword = self.textFieldConfirmPassword.text!
  38. let result = self.isValid(currentPassword: currentPassword, password: newPassword, confirmPassword: confirmPassword)
  39. if result.isValid {
  40. let userId = Utility.getMyUserName()
  41. let param =
  42. [
  43. "UserId" : userId,
  44. "OldPassword": currentPassword,
  45. "NewPassword": newPassword
  46. ]
  47. self.updatePassword(param: param)
  48. }else {
  49. let message = result.error
  50. self.alert(message: message.localizedDescription)
  51. }
  52. }
  53. func updatePassword(param: [String: String]) {
  54. self.updatePassword(params: param, success: { (message) in
  55. self.alert(message: message, title: "Success", okAction: {
  56. self.navigationController?.popViewController(animated: true)
  57. })
  58. }) { (error) in
  59. self.alert(message: error.localizedDescription)
  60. }
  61. }
  62. }
  63. extension ProfileChangePasswordViewController {
  64. private func isValidPasword(password: String, confirmPassword: String) -> (isValid: Bool, error: String) {
  65. var error = ""
  66. var isValid = true
  67. // >= 9, 1 special character, 1 number
  68. func isValidLength() -> Bool {
  69. return password.count >= 9
  70. }
  71. func hasNumber() -> Bool {
  72. let characterSet = CharacterSet.init(charactersIn: "1234567890")
  73. return password.rangeOfCharacter(from: characterSet) != nil
  74. }
  75. func hasLetter() -> Bool {
  76. let characterSet = CharacterSet.letters
  77. return password.rangeOfCharacter(from: characterSet) != nil
  78. }
  79. func hasSpecialCharacter() -> Bool {
  80. let characterSet = CharacterSet.alphanumerics
  81. return password.rangeOfCharacter(from: characterSet.inverted) != 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 be greater than 8 character."
  89. isValid = false
  90. return (isValid, error)
  91. }
  92. let validPassword = hasNumber() && hasLetter() && hasSpecialCharacter()
  93. if !validPassword {
  94. isValid = false
  95. error = error + "\n Password should contain at least 1 number, 1 letter and 1 special character"
  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. }