Files
raspberry/iOS/Wallet/Sources/Account/View/Authorize/AccountViewAuthorize.swift
T

177 lines
7.2 KiB
Swift

//
// AccountViewAuthorize.swift
// Wallet
//
// Created by Igor Danich on 20.08.2020.
// Copyright © 2020 List. All rights reserved.
//
import UIKit
class AccountViewControllerAuthorize: UIViewController {
let authView = AccountViewAuthorize()
override func viewDidLoad() {
super.viewDidLoad()
title = L10n.Account.Authorize.title
image = Asset.accountPasswordPopup.image
view.addSubview(authView)
authView.translatesAutoresizingMaskIntoConstraints = false
authView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
authView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
authView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
authView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
}
}
class AccountViewAuthorize: CommonViewCustom {
@IBOutlet private var descriptionLabel: UILabel!
@IBOutlet private weak var textField: CommonTextField!
@IBOutlet private weak var biometricksBtn: UIButton!
private var didSubmit: ((String) -> Void)?
private var isGetPassword = false
static func showGetPrivateKey(in viewController: UIViewController,
description: String = "",
showPinBackButton: Bool = true,
_ completion: @escaping (String) -> Void) {
showGet(in: viewController, isPasswordRequired: false, completion,
description: description, showPinBackButton: showPinBackButton)
}
static func getPrivateKey() -> String {
if UserDefaults.standard.bool(forKey: "isPinPwdMode") {
if let pin = Account.Service.Authorize.shared.password,
let password = Accounts().getPassword(password: pin),
!password.isEmpty, password == pin, password.count == 4 {
return Accounts().current?.privateKey(password: password) ?? ""
}
return ""
} else {
return ""
}
}
static func showGetPassword(in viewController: UIViewController,
description: String = "",
showPinBackButton: Bool = true,
_ completion: @escaping (String) -> Void) {
showGet(in: viewController, isPasswordRequired: true, completion,
description: description, showPinBackButton: showPinBackButton)
}
private static func showGet(in viewController: UIViewController, isPasswordRequired: Bool,
_ completion: @escaping (String) -> Void, description: String, showPinBackButton: Bool) {
if UserDefaults.standard.bool(forKey: "isPinPwdMode") {
if let pin = Account.Service.Authorize.shared.password,
let password = Accounts().getPassword(password: pin),
!password.isEmpty, password == pin, password.count == 4 {
completion(isPasswordRequired
? password
: Accounts().current?.privateKey(password: password) ?? "")
return ()
}
let accountControollerPin = StoryboardScene.Account.accountControllerPin.instantiate()
let ctrl = UINavigationController(rootViewController: accountControollerPin)
accountControollerPin.showBackButton = showPinBackButton
accountControollerPin.pinValidate = { [weak ctrl] (pin: String) -> Bool in
if let password = Accounts().getPassword(password: pin),
!password.isEmpty, password == pin, password.count == 4 {
Account.Service.Authorize.shared.password = password
ctrl?.dismiss(animated: true)
completion(isPasswordRequired
? password
: Accounts().current?.privateKey(password: password) ?? "")
Account.Service.Authorize.shared.clearPinTries()
Notification.post(name: .didUpdateAuthorization)
return true
} else {
Account.Service.Authorize.shared.savePinTry()
return false
}
}
accountControollerPin.faceSubmit = { [weak ctrl] in
if let password = Accounts().getPassword(password: nil),
!password.isEmpty, password.count == 4 {
Account.Service.Authorize.shared.password = password
ctrl?.dismiss(animated: true) {
completion(isPasswordRequired
? password
: Accounts().current?.privateKey(password: password) ?? "")
Notification.post(name: .didUpdateAuthorization)
}
}
}
ctrl.modalPresentationStyle = .fullScreen
viewController.present(ctrl, animated: true)
} else {
let ctrl = AccountViewControllerAuthorize()
ctrl.authView.isGetPassword = isPasswordRequired
ctrl.authView.descriptionLabel.text = description
ctrl.authView.descriptionLabel.isHidden = description.isEmpty
ctrl.authView.didSubmit = { [unowned ctrl] in
ctrl.dismiss(animated: true)
completion($0)
Notification.post(name: .didUpdateAuthorization)
}
Popup.show(content: ctrl, in: viewController)
}
}
override func setup() {
super.setup()
textField.lzText = kAuthorizationPassword
textField.delegate = self
biometricksBtn.isHidden = !Accounts().isBiometricksAvailable
}
@IBAction private func onSubmit(_ : AnyObject?) {
if isGetPassword {
if let password = Accounts().getPassword(password: textField.lzText ?? ""), !password.isEmpty {
self.didSubmit?(password)
} else {
self.textField.error = L10n.Account.Authorize.error
}
} else {
if let privateKey = Accounts().current?.privateKey(password: textField.lzText ?? ""), !privateKey.isEmpty {
self.didSubmit?(privateKey)
} else {
self.textField.error = L10n.Account.Authorize.error
}
}
}
@IBAction private func onFaceId(_ : AnyObject?) {
if isGetPassword {
if let password = Accounts().getPassword(password: nil), !password.isEmpty {
self.didSubmit?(password)
} else {
self.textField.error = L10n.Account.Authorize.error
}
} else {
if let privateKey = Accounts().current?.privateKey(password: nil), !privateKey.isEmpty {
self.didSubmit?(privateKey)
} else {
self.textField.error = L10n.Account.Authorize.error
}
}
}
}
extension AccountViewAuthorize: CommonTextFieldDelegate {
func textFieldDidSubmit(_ textField: CommonTextField) {
onSubmit(nil)
}
func textFieldDidChange(_ textField: CommonTextField) {
textField.error = nil
}
}