Files
raspberry/iOS/Wallet/Sources/Account/Controller/Change/Cell/ChangeKeyCell.swift
T

117 lines
4.1 KiB
Swift

//
// ChangeKeyCell.swift
// Wallet
//
// Created by Alexandr Serpokrylow on 17.02.2022.
// Copyright © 2022 AM. All rights reserved.
//
import Foundation
import UIKit
class ChangeKeyCell: UITableViewCell {
@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: (() -> Void)?
var onGenerateNewKeysClicked: (() -> Void)?
var onValidateKeyPair: ((String, String) -> Void)?
var onValidatePrivateKey: ((String) -> Void)?
override func awakeFromNib() {
super.awakeFromNib()
selectionStyle = .none
}
func configureView(account: Account.Model.ChangeKey) {
usernameLabel.lzText = account.username
permissionLabel.lzText = "@\(account.permission.permName)"
privateKeyTextField.value = account.privateKey
publicKeyTextField.value = account.publicKey
checkBoxButton.isUserInteractionEnabled = true
configurePrivateTextField()
configurePublicTextField()
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(onPrivateCopyClicked(sender:)), for: .touchUpInside)
privateKeyTextField.accessoryView = copyButton
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(onPublicCopyClicked(sender:)), for: .touchUpInside)
publicKeyTextField.accessoryView = copyButton
publicKeyTextField.delegate = self
}
func selectAccount(isChecked: Bool) {
if isChecked {
checkBoxButton.setImage(Asset.checkboxOn.image, for: .normal)
} else {
checkBoxButton.setImage(Asset.checkboxOff.image, for: .normal)
}
}
@objc func onPrivateCopyClicked(sender: CommonTextField) {
Alert.copy(privateKeyTextField.value)
}
@objc func onPublicCopyClicked(sender: CommonTextField) {
Alert.copy(publicKeyTextField.value)
}
@IBAction func onGenerateNewKeysClicked(_ sender: Any) {
onGenerateNewKeysClicked?()
}
@IBAction func onCheckboxClicked(_ sender: Any) {
onCheckboxClicked?()
}
}
extension ChangeKeyCell: CommonTextFieldDelegate {
func textFieldDidStartEditing(_ textField: CommonTextField) {
configureError(for: textField, errorMessage: nil)
}
func textFieldDidEndEditing(_ textField: CommonTextField) {
let privateKey = privateKeyTextField.value
if (textField == privateKeyTextField) {
onValidatePrivateKey?(privateKey)
} else {
let publicKey = publicKeyTextField.value
onValidateKeyPair?(privateKey, publicKey)
}
}
func textField(_ textField: CommonTextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let maxkeyCount = textField == 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
}
}