Files
2022-05-27 20:21:01 +03:00

100 lines
3.3 KiB
Swift

//
// ChangeKeyCell.swift
// PayCash
//
// 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)?
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
}
private 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
let publicKey = publicKeyTextField.value
onValidateKeyPair?(privateKey, publicKey)
}
}