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.
310 lines
8.8 KiB
310 lines
8.8 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_policy_text".localized()
|
|
|
|
let newPasswordTitleText = "new_password_text".localized()
|
|
let currentPasswordTitleText = "current_password_text".localized()
|
|
let confirmPasswordTitleText = "confirm_password_text".localized()
|
|
let saveText = "save_text".localized()
|
|
let titleText = "change_password_title_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_policy_error".localized()
|
|
let passwordMatchError = "password_match_error".localized()
|
|
|
|
}
|
|
|
|
@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!
|
|
|
|
@IBOutlet weak var currentPasswowrdTitleLabel: UILabel!
|
|
@IBOutlet weak var newPasswordTitleLabel: UILabel!
|
|
@IBOutlet weak var confirmPasswordTitlelabel: UILabel!
|
|
@IBOutlet weak var saveButton: UIButton!
|
|
|
|
// MARK: - Properties
|
|
|
|
var password = "" {
|
|
didSet {
|
|
self.textFieldNewPassword.text = password
|
|
}
|
|
}
|
|
|
|
var confirmPassword = "" {
|
|
didSet {
|
|
self.textFieldConfirmPassword.text = confirmPassword
|
|
}
|
|
}
|
|
|
|
var oldPassword = "" {
|
|
didSet {
|
|
self.textFieldCurrentPassword.text = oldPassword
|
|
}
|
|
}
|
|
|
|
var encryptedPassword: String = ""
|
|
var encryptedOldPassword = ""
|
|
var encryptedConfirmPassword = ""
|
|
|
|
private var selectedType: PasswordType = .old
|
|
|
|
// MARK: Life Cycle
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
self.setupNormalNavigation()
|
|
topLabel.font = .sanfrancisco(.regular, size: 14)
|
|
topLabel.textColor = .themeText
|
|
|
|
self.textFieldNewPassword.delegate = self
|
|
self.textFieldConfirmPassword.delegate = self
|
|
self.textFieldCurrentPassword.delegate = self
|
|
configureLanguage()
|
|
|
|
saveButton.backgroundColor = .themeRed
|
|
saveButton.layer.cornerRadius = 5
|
|
}
|
|
|
|
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() {
|
|
self.topLabel.text = "password_policy_text".localized()
|
|
self.textFieldCurrentPassword.placeholder = "current_password_placeholder_text".localized()
|
|
self.textFieldNewPassword.placeholder = "new_password_placeholder_text".localized()
|
|
self.textFieldConfirmPassword.placeholder = "confirm_password_text".localized()
|
|
self.newPasswordTitleLabel.text = "new_password_text".localized()
|
|
self.currentPasswowrdTitleLabel.text = "current_password_text".localized()
|
|
self.confirmPasswordTitlelabel.text = "confirm_password_text".localized()
|
|
self.saveButton.setTitle("save_text".localized(), for: UIControl.State.normal)
|
|
}
|
|
|
|
@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.alert(type: .error, message: message.localizedDescription)
|
|
}
|
|
}
|
|
|
|
func updatePassword(param: [String: String]) {
|
|
self.updatePassword(
|
|
params: param,
|
|
success: { (message) in
|
|
self.alert(message: message, title: StringConstants().successText, okAction: {
|
|
KeyChain.shared.save(data: self.encryptedPassword, key: .password)
|
|
self.navigationController?.popViewController(animated: true)
|
|
})
|
|
},
|
|
failure: { (error) in
|
|
self.alert(type: .error, message: error.localizedDescription)
|
|
}
|
|
)
|
|
}
|
|
|
|
private func showSecureKeypad(type: PasswordType) {
|
|
let secureKeypad = SecureKeypad(target: self)
|
|
secureKeypad.delegate = self
|
|
|
|
switch type {
|
|
case .old:
|
|
secureKeypad.title = "current_password_text".localized()
|
|
secureKeypad.placeholder = "current_password_placeholder_text".localized()
|
|
case .new:
|
|
secureKeypad.title = "new_password_text".localized()
|
|
secureKeypad.placeholder = "new_password_placeholder_text".localized()
|
|
case .newConfirm:
|
|
secureKeypad.title = "confirm_password_text".localized()
|
|
secureKeypad.placeholder = "confirm_password_text".localized()
|
|
}
|
|
|
|
secureKeypad.present(animated: true)
|
|
}
|
|
|
|
}
|
|
|
|
extension ProfileChangePasswordViewController {
|
|
private func isValidPasword(password: String, confirmPassword: String) -> (isValid: Bool, error: String) {
|
|
var error = ""
|
|
var isValid = true
|
|
|
|
if password.isEmpty {
|
|
error = "\(error)\n\(StringConstants().emptyPasswordError)"
|
|
isValid = false
|
|
}
|
|
if confirmPassword.isEmpty {
|
|
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.isEmpty {
|
|
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: UITextFieldDelegate {
|
|
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
|
|
if textField == textFieldCurrentPassword {
|
|
selectedType = .old
|
|
showSecureKeypad(type: selectedType)
|
|
return false
|
|
}
|
|
|
|
if textField == textFieldNewPassword {
|
|
selectedType = .new
|
|
showSecureKeypad(type: selectedType)
|
|
return false
|
|
}
|
|
|
|
if textField == textFieldConfirmPassword {
|
|
selectedType = .newConfirm
|
|
showSecureKeypad(type: selectedType)
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
}
|
|
|
|
extension ProfileChangePasswordViewController: SecureKeypadDelegate {
|
|
func didComplete(_ encryptedString: String, garbagePassword: String, length: Int) {
|
|
if length < 5 {
|
|
alert(message: StringConstants().passwordLengthError)
|
|
} else {
|
|
switch selectedType {
|
|
case .old:
|
|
encryptedOldPassword = encryptedString
|
|
oldPassword = garbagePassword
|
|
case .new:
|
|
encryptedPassword = encryptedString
|
|
password = garbagePassword
|
|
case .newConfirm:
|
|
encryptedConfirmPassword = encryptedString
|
|
confirmPassword = garbagePassword
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|