107 lines
3.9 KiB
Swift
107 lines
3.9 KiB
Swift
//
|
|
// AccountViewPassword.swift
|
|
// Wallet
|
|
//
|
|
// Created by Igor Danich on 31.08.2020.
|
|
// Copyright © 2020 List. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
final class AccountViewPassword: CommonViewCustom {
|
|
|
|
// @IBOutlet private var passwordView: UIView!
|
|
@IBOutlet private weak var passwordTxtFld: CommonTextField!
|
|
@IBOutlet private weak var newPasswordTxtFld: CommonTextField!
|
|
// @IBOutlet private weak var lengthImgView: UIImageView!
|
|
@IBOutlet private weak var submitBtn: UIButton!
|
|
|
|
private var didSubmit: (() -> Void)?
|
|
|
|
static func show(in viewController: UIViewController, oldPassword: String? = nil) {
|
|
let view = AccountViewPassword()
|
|
view.passwordTxtFld.isSecure = true
|
|
view.passwordTxtFld.delegate = view
|
|
// view.passwordTxtFld.rightViewMode = .always
|
|
view.newPasswordTxtFld.isSecure = true
|
|
view.newPasswordTxtFld.delegate = view
|
|
// view.newPasswordTxtFld.rightViewMode = .always
|
|
view.updateSecure()
|
|
view.update()
|
|
|
|
if let oldPassword {
|
|
view.passwordTxtFld.isHidden = true
|
|
view.passwordTxtFld.value = oldPassword
|
|
view.didSubmit = { [weak viewController] in
|
|
UserDefaults.standard.setValue(!UserDefaults.standard.bool(forKey: "isPinPwdMode"),
|
|
forKey: "isPinPwdMode")
|
|
viewController?.dismiss(animated: true)
|
|
}
|
|
} else {
|
|
view.didSubmit = { [weak viewController] in
|
|
viewController?.dismiss(animated: true)
|
|
}
|
|
}
|
|
|
|
let vc =
|
|
CommonControllerPopup(image: Asset.accountPasswordPopup.image,
|
|
title: L10n.Account.Password.title,
|
|
views: [view], spacing: 0)
|
|
|
|
viewController.present(vc, animated: true)
|
|
}
|
|
|
|
private func updateSecure() {
|
|
// passwordTxtFld.rightView = UIButton.button(
|
|
// image: passwordTxtFld.isSecureTextEntry ? Asset.commonPasswordShow.image : Asset.commonPasswordHide.image,
|
|
// action: { [weak self] in
|
|
// guard let self = self else { return }
|
|
// self.passwordTxtFld.isSecureTextEntry = !self.passwordTxtFld.isSecureTextEntry
|
|
// self.updateSecure()
|
|
// }
|
|
// )
|
|
// newPasswordTxtFld.rightView = UIButton.button(
|
|
// image: newPasswordTxtFld.isSecureTextEntry ? Asset.commonPasswordShow.image : Asset.commonPasswordHide.image,
|
|
// action: { [weak self] in
|
|
// guard let self = self else { return }
|
|
// self.newPasswordTxtFld.isSecureTextEntry = !self.newPasswordTxtFld.isSecureTextEntry
|
|
// self.updateSecure()
|
|
// }
|
|
// )
|
|
}
|
|
|
|
private func update() {
|
|
let text = newPasswordTxtFld.value
|
|
// lengthImgView.image = Accounts().validate(password: text) ? Asset.commonAccessorySuccess.image : Asset.commonAccessoryFailed.image
|
|
submitBtn.isEnabled = Accounts().validate(password: passwordTxtFld.value) && Accounts().validate(password: text)
|
|
}
|
|
|
|
@IBAction private func onSubmit(_ : AnyObject?) {
|
|
do {
|
|
try Accounts().update(password: newPasswordTxtFld.value, old: passwordTxtFld.value)
|
|
didSubmit?()
|
|
} catch {
|
|
Alert.error(text: L10n.Account.Password.currentWrong)
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
extension AccountViewPassword: CommonTextFieldDelegate {
|
|
func textFieldShouldReturn(_ textField: CommonTextField) -> Bool {
|
|
textField.resignFirstResponder()
|
|
return false
|
|
}
|
|
|
|
func textField(
|
|
_ textField: CommonTextField,
|
|
shouldChangeCharactersIn range: NSRange,
|
|
replacementString string: String
|
|
) -> Bool {
|
|
let value = ((textField.value) as NSString).replacingCharacters(in: range, with: string)
|
|
textField.value = value
|
|
update()
|
|
return false
|
|
}
|
|
}
|