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.
 
 
 
 

276 lines
10 KiB

//
// ProfileChangePasswordViewController.swift
// GMERemittance
//
// Created by Fm-user on 1/9/18.
// Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved.
//
import UIKit
import Localize_Swift
class ProfileChangePasswordViewController: UIViewController {
enum PasswordType {
case old
case new
case newConfirm
}
struct StringConstants {
let passwordErrorText = "password_error_text".localized()
let saveText = "save_text".localized()
let titleText = "change_password_text".localized()
let successText = "success_text".localized()
let emptyPasswordError = "password_empty_error".localized()
let confirmPasswordError = "password_confirm_empty_error".localized()
let passwordLengthError = "password_length_error".localized()
let passwordPolicy = "password_error_text".localized()
let passwordMatchError = "password_match_error".localized()
}
@IBOutlet weak var textFieldCurrentPassword: ValidationTextField!
@IBOutlet weak var textFieldNewPassword: ValidationTextField!
@IBOutlet weak var textFieldConfirmPassword: ValidationTextField!
@IBOutlet weak var contentView: UIView!
@IBOutlet weak var saveButton: UIButton!
@IBOutlet weak var currentPassToggleButton: UIButton!
@IBOutlet weak var newPassToggleButton: UIButton!
@IBOutlet weak var confirmPassToggleButton: UIButton!
@IBAction func currentPassToggle(_ sender: UIButton) {
textFieldCurrentPassword.isSecureTextEntry = !textFieldCurrentPassword.isSecureTextEntry
textFieldCurrentPassword.isSecureTextEntry ? currentPassToggleButton.setImage(UIImage(named: "Show"), for: .normal) : currentPassToggleButton.setImage(UIImage(named: "Hide"), for: .normal)
}
@IBAction func newPassToggle(_ sender: UIButton) {
textFieldNewPassword.isSecureTextEntry = !textFieldNewPassword.isSecureTextEntry
textFieldNewPassword.isSecureTextEntry ? newPassToggleButton.setImage(UIImage(named: "Show"), for: .normal) : newPassToggleButton.setImage(UIImage(named: "Hide"), for: .normal)
}
@IBAction func confirmPassToggle(_ sender: UIButton) {
textFieldConfirmPassword.isSecureTextEntry = !textFieldConfirmPassword.isSecureTextEntry
textFieldConfirmPassword.isSecureTextEntry ? confirmPassToggleButton.setImage(UIImage(named: "Show"), for: .normal) : confirmPassToggleButton.setImage(UIImage(named: "Hide"), for: .normal)
}
// MARK: - Properties
var encryptedPassword: String = ""
var encryptedOldPassword = ""
var encryptedConfirmPassword = ""
private var selectedType: PasswordType = .old
private var isValid = false {
didSet {
// saveButton.isEnabled = isValid
// saveButton.backgroundColor = isValid ? .themeRed : .themeText
saveButton.isEnabled = true
saveButton.backgroundColor = isValid ? .themeRed : .themeRed
}
}
// MARK: Life Cycle
override func viewDidLoad() {
super.viewDidLoad()
self.setupNormalNavigation()
self.isValid = false
self.textFieldNewPassword.isSecureTextEntry = true
self.textFieldConfirmPassword.isSecureTextEntry = true
self.textFieldCurrentPassword.isSecureTextEntry = true
textFieldCurrentPassword.validCondition = { !$0.isEmpty && $0.validateRegex(regex: passwordRegex)}
textFieldNewPassword.validCondition = { !$0.isEmpty && $0.validateRegex(regex: passwordRegex)}
textFieldConfirmPassword.validCondition = { !$0.isEmpty && $0.validateRegex(regex: passwordRegex)}
textFieldNewPassword.addTarget(self, action: #selector(editingChanged(_:)), for: .editingChanged)
textFieldConfirmPassword.addTarget(self, action: #selector(editingChanged(_:)), for: .editingChanged)
textFieldCurrentPassword.addTarget(self, action: #selector(editingChanged(_:)), for: .editingChanged)
configureLanguage()
saveButton.layer.cornerRadius = 6
saveButton.setTitle("save_text".localized(), for: .normal)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationItem.title = StringConstants().titleText
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationItem.title = ""
}
func configureLanguage() {
textFieldCurrentPassword.placeholder = "current_password_text".localized()
textFieldCurrentPassword.titleText = "current_password_text".localized()
textFieldCurrentPassword.errorMessage = "password_error_text".localized()
textFieldNewPassword.placeholder = "new_password_text".localized()
textFieldNewPassword.titleText = "new_password_text".localized()
textFieldNewPassword.errorMessage = "password_error_text".localized()
textFieldConfirmPassword.placeholder = "confirm_password_text".localized()
textFieldConfirmPassword.titleText = "confirm_password_text".localized()
textFieldConfirmPassword.errorMessage = "password_error_text".localized()
}
@IBAction func savePasswordChanges(_ sender: Any) {
let currentPassword = self.encryptedOldPassword
let newPassword = self.encryptedPassword
let confirmPassword = self.encryptedConfirmPassword
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,
"ConfirmPassword": confirmPassword
]
self.updatePassword(param: param)
} else {
let message = result.error
self.alertWithOk(type: .error, message: message.localizedDescription, title: "Error", okTitle: "Ok", okAction: nil)
}
}
func updatePassword(param: [String: String]) {
self.updatePassword(
params: param,
success: { (message) in
self.alertWithOk(
type: .success,
message: message,
title: "success_text".localized(),
okTitle: "Ok") {
KeyChain.shared.save(data: self.encryptedPassword, key: .password)
self.navigationController?.popViewController(animated: true)
}
},
failure: { (error) in
self.alertWithOk(type: .error, message: error.localizedDescription, title: "Error", okTitle: "Ok", okAction: nil)
}
)
}
}
extension ProfileChangePasswordViewController {
private func isValidPasword(password: String, confirmPassword: String) -> (isValid: Bool, error: String) {
var error = ""
var isValid = true
if !password.validateRegex(regex: passwordRegex){
error = "\(error)\n\(StringConstants().emptyPasswordError)"
isValid = false
}
if !confirmPassword.validateRegex(regex: passwordRegex){
error = "\(error)\n\(StringConstants().emptyPasswordError)"
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.validateRegex(regex: passwordRegex){
error = "\(error)\n\(StringConstants().emptyPasswordError)"
isValid = false
}
let result = self.isValidPasword(password: password, confirmPassword: confirmPassword)
if !result.isValid {
error = "\(error) \(result.error)"
isValid = false
}
let newError = NSError(
domain: "LoginInteractor",
code: 0,
userInfo: [NSLocalizedDescriptionKey : error]
)
return (isValid, newError)
}
}
extension ProfileChangePasswordViewController: UpdatePasswordService {
}
protocol UpdatePasswordService: ApiServiceType {
func updatePassword(
params: [String: String],
success: @escaping (String?) -> Void,
failure: @escaping (Error) -> Void
)
}
extension UpdatePasswordService {
func updatePassword(
params: [String: String],
success: @escaping (String?) -> Void,
failure: @escaping (Error) -> Void
) {
let url = baseUrl + "/mobile/ChangePassword"
auth.request(
method: .post,
url: url,
params: params,
needsAuthorization: true,
success: { (response: SuccessMessageContainer) in
if (response.errorCode ?? "") == "1" {
let error = NSError(
domain: "Network",
code: 0,
userInfo: [NSLocalizedDescriptionKey : response.message ?? ""]
)
failure(error)
} else {
success(response.data?.message)
}
},
failure: { (error) in
failure(error)
}
)
}
}
extension ProfileChangePasswordViewController {
@objc private func editingChanged(_ textField: UITextField) {
switch textField {
case textFieldCurrentPassword:
encryptedOldPassword = textField.text ?? ""
case textFieldNewPassword:
encryptedPassword = textField.text ?? ""
case textFieldConfirmPassword:
encryptedConfirmPassword = textField.text ?? ""
default:
break
}
self.isValid = (encryptedOldPassword.validateRegex(regex: passwordRegex) && encryptedPassword.validateRegex(regex: passwordRegex) && encryptedConfirmPassword.validateRegex(regex: passwordRegex) )
}
}