Tunnel creation: Save typed values in a scratchpad
This commit is contained in:
parent
d831bd44c4
commit
42c3d63d13
|
@ -50,25 +50,41 @@ class TunnelEditTableViewController: UITableViewController {
|
||||||
|
|
||||||
// Scratchpad for entered data
|
// Scratchpad for entered data
|
||||||
|
|
||||||
class InterfaceDataSource {
|
class InterfaceData {
|
||||||
var scratchpad: [InterfaceEditField: (value: String, isValid: Bool)] = [:]
|
var scratchpad: [InterfaceEditField: String] = [:]
|
||||||
}
|
subscript(field: InterfaceEditField) -> String {
|
||||||
|
get {
|
||||||
class PeerDataSource {
|
return scratchpad[field] ?? ""
|
||||||
var index: Int
|
}
|
||||||
var scratchpad: [PeerEditField: (value: String, isValid: Bool)] = [:]
|
set(stringValue) {
|
||||||
init(index: Int) {
|
scratchpad[field] = stringValue
|
||||||
self.index = index
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var interfaceData: InterfaceDataSource
|
class PeerData {
|
||||||
var peersData: [PeerDataSource]
|
var index: Int
|
||||||
|
var scratchpad: [PeerEditField: String] = [:]
|
||||||
|
init(index: Int) {
|
||||||
|
self.index = index
|
||||||
|
}
|
||||||
|
subscript(field: PeerEditField) -> String {
|
||||||
|
get {
|
||||||
|
return scratchpad[field] ?? ""
|
||||||
|
}
|
||||||
|
set(stringValue) {
|
||||||
|
scratchpad[field] = stringValue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var interfaceData: InterfaceData
|
||||||
|
var peersData: [PeerData]
|
||||||
|
|
||||||
// MARK: TunnelEditTableViewController methods
|
// MARK: TunnelEditTableViewController methods
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
interfaceData = InterfaceDataSource()
|
interfaceData = InterfaceData()
|
||||||
peersData = []
|
peersData = []
|
||||||
super.init(style: .grouped)
|
super.init(style: .grouped)
|
||||||
self.modalPresentationStyle = .formSheet
|
self.modalPresentationStyle = .formSheet
|
||||||
|
@ -172,19 +188,44 @@ extension TunnelEditTableViewController {
|
||||||
switch (field) {
|
switch (field) {
|
||||||
case .name:
|
case .name:
|
||||||
cell.placeholderText = "Required"
|
cell.placeholderText = "Required"
|
||||||
|
cell.value = interfaceData[.name]
|
||||||
|
cell.onValueChanged = { [weak interfaceData] value in
|
||||||
|
interfaceData?[.name] = value
|
||||||
|
}
|
||||||
case .privateKey:
|
case .privateKey:
|
||||||
cell.placeholderText = "Required"
|
cell.placeholderText = "Required"
|
||||||
|
cell.value = interfaceData[.privateKey]
|
||||||
|
cell.onValueChanged = { [weak interfaceData] value in
|
||||||
|
interfaceData?[.privateKey] = value
|
||||||
|
}
|
||||||
case .publicKey:
|
case .publicKey:
|
||||||
cell.isValueEditable = false
|
cell.isValueEditable = false
|
||||||
|
cell.value = "Unimplemented"
|
||||||
case .generateKeyPair:
|
case .generateKeyPair:
|
||||||
break
|
break
|
||||||
case .addresses:
|
case .addresses:
|
||||||
|
cell.value = interfaceData[.addresses]
|
||||||
|
cell.onValueChanged = { [weak interfaceData] value in
|
||||||
|
interfaceData?[.addresses] = value
|
||||||
|
}
|
||||||
break
|
break
|
||||||
case .listenPort:
|
case .listenPort:
|
||||||
|
cell.value = interfaceData[.listenPort]
|
||||||
|
cell.onValueChanged = { [weak interfaceData] value in
|
||||||
|
interfaceData?[.listenPort] = value
|
||||||
|
}
|
||||||
break
|
break
|
||||||
case .mtu:
|
case .mtu:
|
||||||
cell.placeholderText = "Automatic"
|
cell.placeholderText = "Automatic"
|
||||||
|
cell.value = interfaceData[.mtu]
|
||||||
|
cell.onValueChanged = { [weak interfaceData] value in
|
||||||
|
interfaceData?[.mtu] = value
|
||||||
|
}
|
||||||
case .dns:
|
case .dns:
|
||||||
|
cell.value = interfaceData[.dns]
|
||||||
|
cell.onValueChanged = { [weak interfaceData] value in
|
||||||
|
interfaceData?[.dns] = value
|
||||||
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
return cell
|
return cell
|
||||||
|
@ -220,13 +261,33 @@ extension TunnelEditTableViewController {
|
||||||
switch (field) {
|
switch (field) {
|
||||||
case .publicKey:
|
case .publicKey:
|
||||||
cell.placeholderText = "Required"
|
cell.placeholderText = "Required"
|
||||||
|
cell.value = peerData[.publicKey]
|
||||||
|
cell.onValueChanged = { [weak peerData] value in
|
||||||
|
peerData?[.publicKey] = value
|
||||||
|
}
|
||||||
case .preSharedKey:
|
case .preSharedKey:
|
||||||
|
cell.value = peerData[.preSharedKey]
|
||||||
|
cell.onValueChanged = { [weak peerData] value in
|
||||||
|
peerData?[.preSharedKey] = value
|
||||||
|
}
|
||||||
break
|
break
|
||||||
case .endpoint:
|
case .endpoint:
|
||||||
|
cell.value = peerData[.endpoint]
|
||||||
|
cell.onValueChanged = { [weak peerData] value in
|
||||||
|
peerData?[.endpoint] = value
|
||||||
|
}
|
||||||
break
|
break
|
||||||
case .persistentKeepAlive:
|
case .persistentKeepAlive:
|
||||||
|
cell.value = peerData[.persistentKeepAlive]
|
||||||
|
cell.onValueChanged = { [weak peerData] value in
|
||||||
|
peerData?[.persistentKeepAlive] = value
|
||||||
|
}
|
||||||
break
|
break
|
||||||
case .allowedIPs:
|
case .allowedIPs:
|
||||||
|
cell.value = peerData[.allowedIPs]
|
||||||
|
cell.onValueChanged = { [weak peerData] value in
|
||||||
|
peerData?[.allowedIPs] = value
|
||||||
|
}
|
||||||
break
|
break
|
||||||
case .excludePrivateIPs:
|
case .excludePrivateIPs:
|
||||||
break
|
break
|
||||||
|
@ -254,7 +315,7 @@ extension TunnelEditTableViewController {
|
||||||
let numberOfPeerSections = peerEditFieldsBySection.count
|
let numberOfPeerSections = peerEditFieldsBySection.count
|
||||||
let numberOfPeers = peersData.count
|
let numberOfPeers = peersData.count
|
||||||
|
|
||||||
let peer = PeerDataSource(index: peersData.count)
|
let peer = PeerData(index: peersData.count)
|
||||||
peersData.append(peer)
|
peersData.append(peer)
|
||||||
|
|
||||||
let firstAddedSectionIndex = (numberOfInterfaceSections + numberOfPeers * numberOfPeerSections)
|
let firstAddedSectionIndex = (numberOfInterfaceSections + numberOfPeers * numberOfPeerSections)
|
||||||
|
@ -262,7 +323,7 @@ extension TunnelEditTableViewController {
|
||||||
return addedSectionIndices
|
return addedSectionIndices
|
||||||
}
|
}
|
||||||
|
|
||||||
func deletePeer(peer: PeerDataSource) -> IndexSet {
|
func deletePeer(peer: PeerData) -> IndexSet {
|
||||||
let numberOfInterfaceSections = interfaceEditFieldsBySection.count
|
let numberOfInterfaceSections = interfaceEditFieldsBySection.count
|
||||||
let numberOfPeerSections = peerEditFieldsBySection.count
|
let numberOfPeerSections = peerEditFieldsBySection.count
|
||||||
let numberOfPeers = peersData.count
|
let numberOfPeers = peersData.count
|
||||||
|
@ -329,9 +390,13 @@ class TunnelsEditTableViewKeyValueCell: UITableViewCell {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var onValueChanged: ((String) -> Void)? = nil
|
||||||
|
|
||||||
let keyLabel: UILabel
|
let keyLabel: UILabel
|
||||||
let valueTextField: UITextField
|
let valueTextField: UITextField
|
||||||
|
|
||||||
|
private var textFieldValueOnBeginEditing: String = ""
|
||||||
|
|
||||||
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
|
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
|
||||||
keyLabel = UILabel()
|
keyLabel = UILabel()
|
||||||
valueTextField = UITextField()
|
valueTextField = UITextField()
|
||||||
|
@ -359,6 +424,7 @@ class TunnelsEditTableViewKeyValueCell: UITableViewCell {
|
||||||
valueTextField.leftAnchor.constraint(equalTo: keyLabel.rightAnchor, constant: 16),
|
valueTextField.leftAnchor.constraint(equalTo: keyLabel.rightAnchor, constant: 16),
|
||||||
valueTextField.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: -8),
|
valueTextField.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: -8),
|
||||||
])
|
])
|
||||||
|
valueTextField.delegate = self
|
||||||
}
|
}
|
||||||
|
|
||||||
required init?(coder aDecoder: NSCoder) {
|
required init?(coder aDecoder: NSCoder) {
|
||||||
|
@ -372,6 +438,21 @@ class TunnelsEditTableViewKeyValueCell: UITableViewCell {
|
||||||
placeholderText = ""
|
placeholderText = ""
|
||||||
isValueEditable = true
|
isValueEditable = true
|
||||||
isValueValid = true
|
isValueValid = true
|
||||||
|
onValueChanged = nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension TunnelsEditTableViewKeyValueCell: UITextFieldDelegate {
|
||||||
|
func textFieldDidBeginEditing(_ textField: UITextField) {
|
||||||
|
textFieldValueOnBeginEditing = textField.text ?? ""
|
||||||
|
isValueValid = true
|
||||||
|
}
|
||||||
|
func textFieldDidEndEditing(_ textField: UITextField) {
|
||||||
|
let isModified = (textField.text ?? "" != textFieldValueOnBeginEditing)
|
||||||
|
guard (isModified) else { return }
|
||||||
|
if let onValueChanged = onValueChanged {
|
||||||
|
onValueChanged(textField.text ?? "")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue