|
|
@ -29,25 +29,155 @@ class ProfileChangePasswordViewController: UIViewController { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// "{ |
|
|
|
// ""UserId"":""pathaksumitra4@gmail.com"", |
|
|
|
// ""OldPassword"":""P23FAB"", |
|
|
|
// ""NewPassword"":""swift"" |
|
|
|
// |
|
|
|
//}" |
|
|
|
|
|
|
|
@IBAction func savePasswordChanges(_ sender: Any) { |
|
|
|
let currentPassword = self.textFieldCurrentPassword.text! |
|
|
|
let newPassword = self.textFieldNewPassword.text! |
|
|
|
let confirmPassword = self.textFieldConfirmPassword.text! |
|
|
|
|
|
|
|
let result = self.isValid(currentPassword: currentPassword, password: newPassword, confirmPassword: confirmPassword) |
|
|
|
if result.isValid { |
|
|
|
let userId = Utility.getMyUserName() |
|
|
|
let param = |
|
|
|
[ |
|
|
|
"UserId" : userId, |
|
|
|
"OldPassword": currentPassword, |
|
|
|
"NewPassword": newPassword |
|
|
|
] |
|
|
|
self.updatePassword(param: param) |
|
|
|
}else { |
|
|
|
let message = result.error |
|
|
|
self.alert(message: message.localizedDescription) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func updatePassword(param: [String: String]) { |
|
|
|
self.updatePassword(params: param, success: { (message) in |
|
|
|
self.alert(message: message, title: "Success", okAction: { |
|
|
|
self.navigationController?.popViewController(animated: true) |
|
|
|
}) |
|
|
|
}) { (error) in |
|
|
|
self.alert(message: error.localizedDescription) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
extension ProfileChangePasswordViewController { |
|
|
|
private func isValidPasword(password: String, confirmPassword: String) -> (isValid: Bool, error: String) { |
|
|
|
var error = "" |
|
|
|
var isValid = true |
|
|
|
|
|
|
|
// >= 9, 1 special character, 1 number |
|
|
|
|
|
|
|
func isValidLength() -> Bool { |
|
|
|
return password.count >= 9 |
|
|
|
} |
|
|
|
|
|
|
|
func hasNumber() -> Bool { |
|
|
|
let characterSet = CharacterSet.init(charactersIn: "1234567890") |
|
|
|
return password.rangeOfCharacter(from: characterSet) != nil |
|
|
|
} |
|
|
|
|
|
|
|
func hasLetter() -> Bool { |
|
|
|
let characterSet = CharacterSet.letters |
|
|
|
return password.rangeOfCharacter(from: characterSet) != nil |
|
|
|
} |
|
|
|
|
|
|
|
func hasSpecialCharacter() -> Bool { |
|
|
|
let characterSet = CharacterSet.alphanumerics |
|
|
|
return password.rangeOfCharacter(from: characterSet.inverted) != nil |
|
|
|
} |
|
|
|
|
|
|
|
override func didReceiveMemoryWarning() { |
|
|
|
super.didReceiveMemoryWarning() |
|
|
|
|
|
|
|
|
|
|
|
if password.isEmpty { |
|
|
|
error = error + "\n Password cannot be empty" |
|
|
|
isValid = false |
|
|
|
}else { |
|
|
|
|
|
|
|
if !isValidLength() { |
|
|
|
error = error + "\n Password should be greater than 8 character." |
|
|
|
isValid = false |
|
|
|
return (isValid, error) |
|
|
|
} |
|
|
|
|
|
|
|
let validPassword = hasNumber() && hasLetter() && hasSpecialCharacter() |
|
|
|
|
|
|
|
if !validPassword { |
|
|
|
isValid = false |
|
|
|
error = error + "\n Password should contain at least 1 number, 1 letter and 1 special character" |
|
|
|
return (isValid, error) |
|
|
|
} |
|
|
|
|
|
|
|
if password != confirmPassword { |
|
|
|
error = error + "\n Passwords does not match." |
|
|
|
isValid = false |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return (isValid, error) |
|
|
|
} |
|
|
|
// MARK: Converting entities |
|
|
|
|
|
|
|
private func isValid(currentPassword: String, password: String, confirmPassword: String) -> (isValid: Bool, error: Error) { |
|
|
|
var error = "" |
|
|
|
var isValid = true |
|
|
|
// user name |
|
|
|
|
|
|
|
if currentPassword.isEmpty { |
|
|
|
error = error + "current password field is required."; |
|
|
|
isValid = false |
|
|
|
} |
|
|
|
|
|
|
|
let result = self.isValidPasword(password: password, confirmPassword: confirmPassword) |
|
|
|
|
|
|
|
if !result.isValid { |
|
|
|
error = error + result.error |
|
|
|
isValid = false |
|
|
|
} |
|
|
|
|
|
|
|
func popOutCurrentViewController() { |
|
|
|
dismiss(animated: true, completion: nil) |
|
|
|
navigationController?.popViewController(animated: true) |
|
|
|
|
|
|
|
let _error = NSError.init(domain: "LoginInteractor", code: 0, userInfo: [NSLocalizedDescriptionKey : error]) |
|
|
|
return (isValid, _error) |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
extension ProfileChangePasswordViewController: UpdatePasswordService { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protocol UpdatePasswordService: ApiServiceType { |
|
|
|
func updatePassword(params: [String: String], success: @escaping (String?) -> (), failure: @escaping (Error) -> ()) |
|
|
|
} |
|
|
|
|
|
|
|
extension UpdatePasswordService { |
|
|
|
func updatePassword(params: [String: String], success: @escaping (String?) -> (), failure: @escaping (Error) -> ()) { |
|
|
|
let url = baseUrl + "mobile/ChangePassword" |
|
|
|
auth.request(method: .post, url: url, params: params, needsAuthorization: false, success: { (response: SuccessMessageContainer) in |
|
|
|
if (response.errorCode ?? "") == "1" { |
|
|
|
let error = NSError.init(domain: "Network", code: 0, userInfo: [NSLocalizedDescriptionKey : response.message ?? ""]) |
|
|
|
failure(error) |
|
|
|
}else { |
|
|
|
success(response.data?.message) |
|
|
|
} |
|
|
|
}) { (error) in |
|
|
|
failure(error) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|