aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/WireGuard/UI/iOS/View/KeyValueCell.swift
diff options
context:
space:
mode:
Diffstat (limited to 'WireGuard/WireGuard/UI/iOS/View/KeyValueCell.swift')
-rw-r--r--WireGuard/WireGuard/UI/iOS/View/KeyValueCell.swift56
1 files changed, 28 insertions, 28 deletions
diff --git a/WireGuard/WireGuard/UI/iOS/View/KeyValueCell.swift b/WireGuard/WireGuard/UI/iOS/View/KeyValueCell.swift
index a456de1..81a1712 100644
--- a/WireGuard/WireGuard/UI/iOS/View/KeyValueCell.swift
+++ b/WireGuard/WireGuard/UI/iOS/View/KeyValueCell.swift
@@ -4,7 +4,7 @@
import UIKit
class KeyValueCell: UITableViewCell {
-
+
let keyLabel: UILabel = {
let keyLabel = UILabel()
keyLabel.font = UIFont.preferredFont(forTextStyle: .body)
@@ -13,7 +13,7 @@ class KeyValueCell: UITableViewCell {
keyLabel.textAlignment = .left
return keyLabel
}()
-
+
let valueLabelScrollView: UIScrollView = {
let scrollView = UIScrollView(frame: .zero)
scrollView.isDirectionalLockEnabled = true
@@ -21,7 +21,7 @@ class KeyValueCell: UITableViewCell {
scrollView.showsVerticalScrollIndicator = false
return scrollView
}()
-
+
let valueTextField: UITextField = {
let valueTextField = UITextField()
valueTextField.textAlignment = .right
@@ -34,7 +34,7 @@ class KeyValueCell: UITableViewCell {
valueTextField.textColor = .gray
return valueTextField
}()
-
+
var copyableGesture = true
var key: String {
@@ -53,7 +53,7 @@ class KeyValueCell: UITableViewCell {
get { return valueTextField.keyboardType }
set(value) { valueTextField.keyboardType = value }
}
-
+
var isValueValid = true {
didSet {
if isValueValid {
@@ -63,26 +63,26 @@ class KeyValueCell: UITableViewCell {
}
}
}
-
+
var isStackedHorizontally = false
var isStackedVertically = false
var contentSizeBasedConstraints = [NSLayoutConstraint]()
-
+
var onValueChanged: ((String) -> Void)?
var onValueBeingEdited: ((String) -> Void)?
-
+
private var textFieldValueOnBeginEditing: String = ""
-
+
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
-
+
contentView.addSubview(keyLabel)
keyLabel.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
keyLabel.leftAnchor.constraint(equalTo: contentView.layoutMarginsGuide.leftAnchor),
keyLabel.topAnchor.constraint(equalToSystemSpacingBelow: contentView.layoutMarginsGuide.topAnchor, multiplier: 0.5)
])
-
+
valueTextField.delegate = self
valueLabelScrollView.addSubview(valueTextField)
valueTextField.translatesAutoresizingMaskIntoConstraints = false
@@ -96,31 +96,31 @@ class KeyValueCell: UITableViewCell {
let expandToFitValueLabelConstraint = NSLayoutConstraint(item: valueTextField, attribute: .width, relatedBy: .equal, toItem: valueLabelScrollView, attribute: .width, multiplier: 1, constant: 0)
expandToFitValueLabelConstraint.priority = .defaultLow + 1
expandToFitValueLabelConstraint.isActive = true
-
+
contentView.addSubview(valueLabelScrollView)
-
+
contentView.addSubview(valueLabelScrollView)
valueLabelScrollView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
valueLabelScrollView.rightAnchor.constraint(equalTo: contentView.layoutMarginsGuide.rightAnchor),
contentView.layoutMarginsGuide.bottomAnchor.constraint(equalToSystemSpacingBelow: valueLabelScrollView.bottomAnchor, multiplier: 0.5)
])
-
+
keyLabel.setContentCompressionResistancePriority(.defaultHigh + 1, for: .horizontal)
keyLabel.setContentHuggingPriority(.defaultHigh, for: .horizontal)
valueLabelScrollView.setContentHuggingPriority(.defaultLow, for: .horizontal)
-
+
let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTapGesture(_:)))
addGestureRecognizer(gestureRecognizer)
isUserInteractionEnabled = true
-
+
configureForContentSize()
}
-
+
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
-
+
func configureForContentSize() {
var constraints = [NSLayoutConstraint]()
if traitCollection.preferredContentSizeCategory.isAccessibilityCategory {
@@ -152,13 +152,13 @@ class KeyValueCell: UITableViewCell {
contentSizeBasedConstraints = constraints
}
}
-
+
@objc func handleTapGesture(_ recognizer: UIGestureRecognizer) {
if !copyableGesture {
return
}
guard recognizer.state == .recognized else { return }
-
+
if let recognizerView = recognizer.view,
let recognizerSuperView = recognizerView.superview, recognizerView.becomeFirstResponder() {
let menuController = UIMenuController.shared
@@ -166,19 +166,19 @@ class KeyValueCell: UITableViewCell {
menuController.setMenuVisible(true, animated: true)
}
}
-
+
override var canBecomeFirstResponder: Bool {
return true
}
-
+
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
return (action == #selector(UIResponderStandardEditActions.copy(_:)))
}
-
+
override func copy(_ sender: Any?) {
UIPasteboard.general.string = valueTextField.text
}
-
+
override func prepareForReuse() {
super.prepareForReuse()
copyableGesture = true
@@ -194,18 +194,18 @@ class KeyValueCell: UITableViewCell {
}
extension KeyValueCell: UITextFieldDelegate {
-
+
func textFieldDidBeginEditing(_ textField: UITextField) {
textFieldValueOnBeginEditing = textField.text ?? ""
isValueValid = true
}
-
+
func textFieldDidEndEditing(_ textField: UITextField) {
let isModified = textField.text ?? "" != textFieldValueOnBeginEditing
guard isModified else { return }
onValueChanged?(textField.text ?? "")
}
-
+
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if let onValueBeingEdited = onValueBeingEdited {
let modifiedText = ((textField.text ?? "") as NSString).replacingCharacters(in: range, with: string)
@@ -213,5 +213,5 @@ extension KeyValueCell: UITextFieldDelegate {
}
return true
}
-
+
}