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

//
// ProfileChangePasswordViewController.swift
// GMERemittance
//
// Created by Fm-user on 1/9/18.
// Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved.
//
import UIKit
// there is not time to take this to viper archi. so applying mvc here.
class ProfileChangePasswordViewController: UIViewController {
@IBOutlet weak var textFieldCurrentPassword: UITextField!
@IBOutlet weak var textFieldNewPassword: UITextField!
@IBOutlet weak var textFieldConfirmPassword: UITextField!
@IBOutlet weak var topLabel: UILabel!
@IBOutlet weak var contentView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
self.setupNormalNavigation()
topLabel.font = UIFont.init(name: Fonts.Family.regular, size: 14)
topLabel.textColor = UIColor.init(hex: "4a4a4a")
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationItem.title = "Change Password"
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationItem.title = ""
}
@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
}
func hasUpperCase() -> Bool {
let characterSet = CharacterSet.uppercaseLetters
return password.rangeOfCharacter(from: characterSet) != nil
}
if password.isEmpty {
error = error + "\n Password cannot be empty"
isValid = false
}else {
if !isValidLength() {
error = error + "\n Password should have at least 9 characters"
isValid = false
return (isValid, error)
}
let validPassword = hasNumber() && hasLetter() && hasSpecialCharacter() && hasUpperCase()
if !validPassword {
isValid = false
error = error + "\n Password should contain at least one symbol, one capital letter and one number"
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
}
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)
}
}
}