Files
2022-12-14 23:28:32 +03:00

124 lines
4.3 KiB
Swift

//
// ChangeKeyCell.swift
// Wallet
//
// Created by Alexandr Serpokrylow on 17.02.2022.
// Copyright © 2022 AM. All rights reserved.
//
import Foundation
import UIKit
final class ChangeKeyCell: UITableViewCell {
typealias OnHanler = () -> Void
@IBOutlet weak var checkBoxButton: UIButton!
@IBOutlet weak var usernameLabel: UILabel!
@IBOutlet weak var permissionLabel: UILabel!
@IBOutlet weak var privateKeyTextField: CommonTextField!
@IBOutlet weak var publicKeyTextField: CommonTextField!
var onCheckboxClicked: OnHanler?
var onGenerateNewKeysClicked: OnHanler?
var onValidateKeyPair: ((String, String) -> Void)?
var onValidatePrivateKey: ((String) -> Void)?
override func awakeFromNib() {
super.awakeFromNib()
self.selectionStyle = .none
}
func configureView(account: ChangeKeyModel) {
self.usernameLabel.lzText = account.wallet.name
self.permissionLabel.lzText = "@\(account.keyType.rawValue)"
self.privateKeyTextField.value = account.privateKey
self.publicKeyTextField.value = account.publicKey
self.checkBoxButton.isUserInteractionEnabled = true
self.configurePrivateTextField()
self.configurePublicTextField()
self.selectAccount(isChecked: account.isChecked)
}
func configureError(for textField: CommonTextField, errorMessage: String?) {
textField.error = errorMessage
}
private func configurePrivateTextField() {
let copyButton = UIButton(type: .custom)
copyButton.setImage(Asset.commonCopy.image, for: .normal)
copyButton.frame.size = Asset.commonCopy.image.size
copyButton.addTarget(self, action: #selector(self.onPrivateCopyClicked(sender:)), for: .touchUpInside)
self.privateKeyTextField.accessoryView = copyButton
self.privateKeyTextField.delegate = self
}
private func configurePublicTextField() {
let copyButton = UIButton(type: .custom)
copyButton.setImage(Asset.commonCopy.image, for: .normal)
copyButton.frame.size = Asset.commonCopy.image.size
copyButton.addTarget(self, action: #selector(self.onPublicCopyClicked(sender:)), for: .touchUpInside)
self.publicKeyTextField.accessoryView = copyButton
self.publicKeyTextField.delegate = self
}
func selectAccount(isChecked: Bool) {
if isChecked {
self.checkBoxButton.setImage(Asset.checkboxOn.image, for: .normal)
} else {
self.checkBoxButton.setImage(Asset.checkboxOff.image, for: .normal)
}
}
@objc func onPrivateCopyClicked(sender: CommonTextField) {
Alert.copy(self.privateKeyTextField.value)
}
@objc func onPublicCopyClicked(sender: CommonTextField) {
Alert.copy(self.publicKeyTextField.value)
}
@IBAction func onGenerateNewKeysClicked(_ sender: Any) {
self.onGenerateNewKeysClicked?()
}
@IBAction func onCheckboxClicked(_ sender: Any) {
self.onCheckboxClicked?()
}
}
extension ChangeKeyCell: CommonTextFieldDelegate {
func textFieldDidStartEditing(_ textField: CommonTextField) {
self.configureError(for: textField, errorMessage: nil)
}
func textFieldDidEndEditing(_ textField: CommonTextField) {
let privateKey = self.privateKeyTextField.value
if textField == self.privateKeyTextField {
self.onValidatePrivateKey?(privateKey)
} else {
let publicKey = self.publicKeyTextField.value
self.onValidateKeyPair?(privateKey, publicKey)
}
}
func textField(_ textField: CommonTextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let maxkeyCount = textField == self.privateKeyTextField
? AccountControllerChangeKey.Constants.maxPrivateKeyTextCount
: AccountControllerChangeKey.Constants.maxPublicKeyTextCount
let currentText = textField.value
guard let stringRange = Range(range, in: currentText) else { return false }
let updatedText = currentText.replacingCharacters(in: stringRange, with: string)
let shouldChange = updatedText.count <= maxkeyCount
return shouldChange
}
}