2018-10-24 01:37:28 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2018-10-30 02:57:35 +00:00
|
|
|
// Copyright © 2018 WireGuard LLC. All Rights Reserved.
|
2018-10-20 13:45:53 +00:00
|
|
|
|
|
|
|
import UIKit
|
|
|
|
|
2018-10-24 11:39:34 +00:00
|
|
|
protocol TunnelEditTableViewControllerDelegate: class {
|
2018-10-25 05:44:38 +00:00
|
|
|
func tunnelSaved(tunnel: TunnelContainer)
|
|
|
|
func tunnelEditingCancelled()
|
2018-10-24 11:39:34 +00:00
|
|
|
}
|
|
|
|
|
2018-10-20 13:45:53 +00:00
|
|
|
// MARK: TunnelEditTableViewController
|
|
|
|
|
|
|
|
class TunnelEditTableViewController: UITableViewController {
|
|
|
|
|
2018-11-03 18:35:25 +00:00
|
|
|
weak var delegate: TunnelEditTableViewControllerDelegate?
|
2018-10-24 11:39:34 +00:00
|
|
|
|
2018-10-23 12:32:10 +00:00
|
|
|
let interfaceFieldsBySection: [[TunnelViewModel.InterfaceField]] = [
|
2018-10-20 13:45:53 +00:00
|
|
|
[.name],
|
|
|
|
[.privateKey, .publicKey, .generateKeyPair],
|
|
|
|
[.addresses, .listenPort, .mtu, .dns]
|
|
|
|
]
|
|
|
|
|
2018-10-29 07:16:54 +00:00
|
|
|
let peerFields: [TunnelViewModel.PeerField] = [
|
|
|
|
.publicKey, .preSharedKey, .endpoint,
|
2018-10-29 11:08:32 +00:00
|
|
|
.allowedIPs, .excludePrivateIPs, .persistentKeepAlive,
|
2018-10-29 07:16:54 +00:00
|
|
|
.deletePeer
|
2018-10-20 13:45:53 +00:00
|
|
|
]
|
|
|
|
|
2018-11-12 10:23:18 +00:00
|
|
|
let activateOnDemandOptions: [ActivateOnDemandOption] = [
|
2018-11-28 07:11:35 +00:00
|
|
|
.useOnDemandOverWiFiOrCellular,
|
|
|
|
.useOnDemandOverWiFiOnly,
|
2018-11-10 13:20:09 +00:00
|
|
|
.useOnDemandOverCellularOnly
|
|
|
|
]
|
|
|
|
|
2018-10-23 11:53:46 +00:00
|
|
|
let tunnelsManager: TunnelsManager
|
2018-10-24 11:39:34 +00:00
|
|
|
let tunnel: TunnelContainer?
|
2018-10-23 09:53:18 +00:00
|
|
|
let tunnelViewModel: TunnelViewModel
|
2018-11-12 10:23:18 +00:00
|
|
|
var activateOnDemandSetting: ActivateOnDemandSetting
|
2018-10-20 13:45:53 +00:00
|
|
|
|
2018-12-12 17:40:57 +00:00
|
|
|
private var interfaceSectionCount: Int { return interfaceFieldsBySection.count }
|
|
|
|
private var peerSectionCount: Int { return tunnelViewModel.peersData.count }
|
|
|
|
|
|
|
|
init(tunnelsManager: TunnelsManager, tunnel: TunnelContainer) {
|
2018-10-25 02:30:12 +00:00
|
|
|
// Use this initializer to edit an existing tunnel.
|
2018-12-12 17:40:57 +00:00
|
|
|
self.tunnelsManager = tunnelsManager
|
|
|
|
self.tunnel = tunnel
|
|
|
|
tunnelViewModel = TunnelViewModel(tunnelConfiguration: tunnel.tunnelConfiguration())
|
|
|
|
activateOnDemandSetting = tunnel.activateOnDemandSetting()
|
2018-10-25 02:30:12 +00:00
|
|
|
super.init(style: .grouped)
|
|
|
|
}
|
|
|
|
|
2018-12-12 17:40:57 +00:00
|
|
|
init(tunnelsManager: TunnelsManager, tunnelConfiguration: TunnelConfiguration?) {
|
2018-10-25 02:30:12 +00:00
|
|
|
// Use this initializer to create a new tunnel.
|
|
|
|
// If tunnelConfiguration is passed, data will be prepopulated from that configuration.
|
2018-12-12 17:40:57 +00:00
|
|
|
self.tunnelsManager = tunnelsManager
|
2018-10-25 02:30:12 +00:00
|
|
|
tunnel = nil
|
|
|
|
tunnelViewModel = TunnelViewModel(tunnelConfiguration: tunnelConfiguration)
|
2018-11-12 10:23:18 +00:00
|
|
|
activateOnDemandSetting = ActivateOnDemandSetting.defaultSetting
|
2018-10-20 13:45:53 +00:00
|
|
|
super.init(style: .grouped)
|
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder aDecoder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
2018-12-12 17:40:57 +00:00
|
|
|
self.title = tunnel == nil ? "New configuration" : "Edit configuration"
|
2018-10-20 13:45:53 +00:00
|
|
|
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .save, target: self, action: #selector(saveTapped))
|
|
|
|
self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(cancelTapped))
|
|
|
|
|
2018-12-09 19:30:08 +00:00
|
|
|
self.tableView.estimatedRowHeight = 44
|
|
|
|
self.tableView.rowHeight = UITableView.automaticDimension
|
2018-10-20 13:45:53 +00:00
|
|
|
|
2018-12-12 17:40:57 +00:00
|
|
|
self.tableView.register(TunnelEditTableViewKeyValueCell.self, forCellReuseIdentifier: TunnelEditTableViewKeyValueCell.reuseIdentifier)
|
|
|
|
self.tableView.register(TunnelEditTableViewReadOnlyKeyValueCell.self, forCellReuseIdentifier: TunnelEditTableViewReadOnlyKeyValueCell.reuseIdentifier)
|
|
|
|
self.tableView.register(TunnelEditTableViewButtonCell.self, forCellReuseIdentifier: TunnelEditTableViewButtonCell.reuseIdentifier)
|
|
|
|
self.tableView.register(TunnelEditTableViewSwitchCell.self, forCellReuseIdentifier: TunnelEditTableViewSwitchCell.reuseIdentifier)
|
|
|
|
self.tableView.register(TunnelEditTableViewSelectionListCell.self, forCellReuseIdentifier: TunnelEditTableViewSelectionListCell.reuseIdentifier)
|
2018-10-20 13:45:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@objc func saveTapped() {
|
2018-10-23 11:53:46 +00:00
|
|
|
self.tableView.endEditing(false)
|
|
|
|
let tunnelSaveResult = tunnelViewModel.save()
|
2018-12-12 18:28:27 +00:00
|
|
|
switch tunnelSaveResult {
|
2018-10-23 11:53:46 +00:00
|
|
|
case .error(let errorMessage):
|
|
|
|
let erroringConfiguration = (tunnelViewModel.interfaceData.validatedConfiguration == nil) ? "Interface" : "Peer"
|
2018-10-31 19:15:09 +00:00
|
|
|
ErrorPresenter.showErrorAlert(title: "Invalid \(erroringConfiguration)", message: errorMessage, from: self)
|
2018-11-01 18:15:29 +00:00
|
|
|
self.tableView.reloadData() // Highlight erroring fields
|
2018-10-23 11:53:46 +00:00
|
|
|
case .saved(let tunnelConfiguration):
|
2018-10-24 11:49:14 +00:00
|
|
|
if let tunnel = tunnel {
|
|
|
|
// We're modifying an existing tunnel
|
2018-11-12 10:23:18 +00:00
|
|
|
tunnelsManager.modify(tunnel: tunnel,
|
|
|
|
tunnelConfiguration: tunnelConfiguration,
|
|
|
|
activateOnDemandSetting: activateOnDemandSetting) { [weak self] (error) in
|
2018-10-24 11:49:14 +00:00
|
|
|
if let error = error {
|
2018-10-31 19:15:09 +00:00
|
|
|
ErrorPresenter.showErrorAlert(error: error, from: self)
|
2018-10-24 11:49:14 +00:00
|
|
|
} else {
|
|
|
|
self?.dismiss(animated: true, completion: nil)
|
2018-10-25 05:44:38 +00:00
|
|
|
self?.delegate?.tunnelSaved(tunnel: tunnel)
|
2018-10-24 11:49:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// We're adding a new tunnel
|
2018-11-12 10:23:18 +00:00
|
|
|
tunnelsManager.add(tunnelConfiguration: tunnelConfiguration,
|
2018-12-06 10:28:27 +00:00
|
|
|
activateOnDemandSetting: activateOnDemandSetting) { [weak self] result in
|
|
|
|
if let error = result.error {
|
2018-10-31 19:15:09 +00:00
|
|
|
ErrorPresenter.showErrorAlert(error: error, from: self)
|
2018-10-24 11:49:14 +00:00
|
|
|
} else {
|
2018-12-06 10:28:27 +00:00
|
|
|
let tunnel: TunnelContainer = result.value!
|
2018-10-24 11:49:14 +00:00
|
|
|
self?.dismiss(animated: true, completion: nil)
|
2018-12-06 10:28:27 +00:00
|
|
|
self?.delegate?.tunnelSaved(tunnel: tunnel)
|
2018-10-24 11:49:14 +00:00
|
|
|
}
|
2018-10-23 11:53:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-20 13:45:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@objc func cancelTapped() {
|
|
|
|
dismiss(animated: true, completion: nil)
|
2018-10-25 05:44:38 +00:00
|
|
|
self.delegate?.tunnelEditingCancelled()
|
2018-10-20 13:45:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: UITableViewDataSource
|
|
|
|
|
|
|
|
extension TunnelEditTableViewController {
|
|
|
|
override func numberOfSections(in tableView: UITableView) -> Int {
|
2018-12-12 17:40:57 +00:00
|
|
|
return interfaceSectionCount + peerSectionCount + 1 /* Add Peer */ + 1 /* On-Demand */
|
2018-10-20 13:45:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
2018-12-12 18:28:27 +00:00
|
|
|
if section < interfaceSectionCount {
|
2018-10-20 13:45:53 +00:00
|
|
|
// Interface
|
2018-10-23 12:32:10 +00:00
|
|
|
return interfaceFieldsBySection[section].count
|
2018-12-12 18:28:27 +00:00
|
|
|
} else if (peerSectionCount > 0) && (section < (interfaceSectionCount + peerSectionCount)) {
|
2018-10-20 13:45:53 +00:00
|
|
|
// Peer
|
2018-12-12 17:40:57 +00:00
|
|
|
let peerIndex = (section - interfaceSectionCount)
|
2018-11-02 07:42:10 +00:00
|
|
|
let peerData = tunnelViewModel.peersData[peerIndex]
|
|
|
|
let peerFieldsToShow = peerData.shouldAllowExcludePrivateIPsControl ? peerFields : peerFields.filter { $0 != .excludePrivateIPs }
|
|
|
|
return peerFieldsToShow.count
|
2018-12-12 18:28:27 +00:00
|
|
|
} else if section < (interfaceSectionCount + peerSectionCount + 1) {
|
2018-10-20 13:45:53 +00:00
|
|
|
// Add peer
|
|
|
|
return 1
|
2018-11-10 13:20:09 +00:00
|
|
|
} else {
|
|
|
|
// On-Demand Rules
|
2018-12-12 18:28:27 +00:00
|
|
|
if activateOnDemandSetting.isActivateOnDemandEnabled {
|
2018-11-10 13:20:09 +00:00
|
|
|
return 4
|
2018-11-12 10:23:18 +00:00
|
|
|
} else {
|
|
|
|
return 1
|
2018-11-10 13:20:09 +00:00
|
|
|
}
|
2018-10-20 13:45:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
|
2018-12-12 18:28:27 +00:00
|
|
|
if section < interfaceSectionCount {
|
2018-10-20 13:45:53 +00:00
|
|
|
// Interface
|
|
|
|
return (section == 0) ? "Interface" : nil
|
2018-12-12 18:28:27 +00:00
|
|
|
} else if (peerSectionCount > 0) && (section < (interfaceSectionCount + peerSectionCount)) {
|
2018-10-20 13:45:53 +00:00
|
|
|
// Peer
|
2018-10-29 07:16:54 +00:00
|
|
|
return "Peer"
|
2018-12-12 18:28:27 +00:00
|
|
|
} else if section == (interfaceSectionCount + peerSectionCount) {
|
2018-10-20 13:45:53 +00:00
|
|
|
// Add peer
|
|
|
|
return nil
|
2018-11-10 13:20:09 +00:00
|
|
|
} else {
|
2018-12-12 17:40:57 +00:00
|
|
|
assert(section == (interfaceSectionCount + peerSectionCount + 1))
|
2018-11-10 13:20:09 +00:00
|
|
|
return "On-Demand Activation"
|
2018-10-20 13:45:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
2018-12-12 18:28:27 +00:00
|
|
|
if indexPath.section < interfaceSectionCount {
|
2018-12-12 17:40:57 +00:00
|
|
|
return interfaceFieldCell(for: tableView, at: indexPath)
|
2018-12-12 18:28:27 +00:00
|
|
|
} else if (peerSectionCount > 0) && (indexPath.section < (interfaceSectionCount + peerSectionCount)) {
|
2018-12-12 17:40:57 +00:00
|
|
|
return peerCell(for: tableView, at: indexPath)
|
2018-12-12 18:28:27 +00:00
|
|
|
} else if indexPath.section == (interfaceSectionCount + peerSectionCount) {
|
2018-12-12 17:40:57 +00:00
|
|
|
return addPeerCell(for: tableView, at: indexPath)
|
|
|
|
} else {
|
|
|
|
return onDemandCell(for: tableView, at: indexPath)
|
|
|
|
}
|
|
|
|
}
|
2018-10-20 13:45:53 +00:00
|
|
|
|
2018-12-12 17:40:57 +00:00
|
|
|
private func interfaceFieldCell(for tableView: UITableView, at indexPath: IndexPath) -> UITableViewCell {
|
|
|
|
let interfaceData = tunnelViewModel.interfaceData
|
|
|
|
let field = interfaceFieldsBySection[indexPath.section][indexPath.row]
|
2018-12-12 18:28:27 +00:00
|
|
|
if field == .generateKeyPair {
|
2018-12-12 17:40:57 +00:00
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: TunnelEditTableViewButtonCell.reuseIdentifier, for: indexPath) as! TunnelEditTableViewButtonCell
|
|
|
|
cell.buttonText = field.rawValue
|
|
|
|
cell.onTapped = { [weak self, weak interfaceData] in
|
|
|
|
if let interfaceData = interfaceData, let self = self {
|
|
|
|
interfaceData[.privateKey] = Curve25519.generatePrivateKey().base64EncodedString()
|
|
|
|
if let privateKeyRow = self.interfaceFieldsBySection[indexPath.section].firstIndex(of: .privateKey),
|
|
|
|
let publicKeyRow = self.interfaceFieldsBySection[indexPath.section].firstIndex(of: .publicKey) {
|
|
|
|
let privateKeyIndex = IndexPath(row: privateKeyRow, section: indexPath.section)
|
|
|
|
let publicKeyIndex = IndexPath(row: publicKeyRow, section: indexPath.section)
|
|
|
|
self.tableView.reloadRows(at: [privateKeyIndex, publicKeyIndex], with: .automatic)
|
2018-10-24 08:48:52 +00:00
|
|
|
}
|
2018-10-20 13:45:53 +00:00
|
|
|
}
|
2018-12-12 17:40:57 +00:00
|
|
|
}
|
|
|
|
return cell
|
|
|
|
} else if field == .publicKey {
|
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: TunnelEditTableViewReadOnlyKeyValueCell.reuseIdentifier, for: indexPath) as! TunnelEditTableViewReadOnlyKeyValueCell
|
|
|
|
cell.key = field.rawValue
|
|
|
|
cell.value = interfaceData[field]
|
|
|
|
return cell
|
|
|
|
} else {
|
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: TunnelEditTableViewKeyValueCell.reuseIdentifier, for: indexPath) as! TunnelEditTableViewKeyValueCell
|
|
|
|
// Set key
|
|
|
|
cell.key = field.rawValue
|
|
|
|
// Set placeholder text
|
|
|
|
switch field {
|
|
|
|
case .name:
|
|
|
|
cell.placeholderText = "Required"
|
|
|
|
case .privateKey:
|
|
|
|
cell.placeholderText = "Required"
|
|
|
|
case .addresses:
|
|
|
|
cell.placeholderText = "Optional"
|
|
|
|
case .listenPort:
|
|
|
|
cell.placeholderText = "Automatic"
|
|
|
|
case .mtu:
|
|
|
|
cell.placeholderText = "Automatic"
|
|
|
|
case .dns:
|
|
|
|
cell.placeholderText = "Optional"
|
|
|
|
case .publicKey: break
|
|
|
|
case .generateKeyPair: break
|
|
|
|
}
|
|
|
|
// Set keyboardType
|
|
|
|
if field == .mtu || field == .listenPort {
|
|
|
|
cell.keyboardType = .numberPad
|
|
|
|
} else if field == .addresses || field == .dns {
|
|
|
|
cell.keyboardType = .numbersAndPunctuation
|
|
|
|
}
|
|
|
|
// Show erroring fields
|
|
|
|
cell.isValueValid = (!interfaceData.fieldsWithError.contains(field))
|
|
|
|
// Bind values to view model
|
|
|
|
cell.value = interfaceData[field]
|
|
|
|
if field == .dns { // While editing DNS, you might directly set exclude private IPs
|
|
|
|
cell.onValueBeingEdited = { [weak interfaceData] value in
|
|
|
|
interfaceData?[field] = value
|
2018-10-29 00:47:48 +00:00
|
|
|
}
|
2018-12-12 17:40:57 +00:00
|
|
|
} else {
|
|
|
|
cell.onValueChanged = { [weak interfaceData] value in
|
|
|
|
interfaceData?[field] = value
|
2018-10-20 13:45:53 +00:00
|
|
|
}
|
2018-12-12 17:40:57 +00:00
|
|
|
}
|
|
|
|
// Compute public key live
|
|
|
|
if field == .privateKey {
|
|
|
|
cell.onValueBeingEdited = { [weak self, weak interfaceData] value in
|
|
|
|
if let interfaceData = interfaceData, let self = self {
|
|
|
|
interfaceData[.privateKey] = value
|
|
|
|
if let row = self.interfaceFieldsBySection[indexPath.section].firstIndex(of: .publicKey) {
|
|
|
|
self.tableView.reloadRows(at: [IndexPath(row: row, section: indexPath.section)], with: .none)
|
2018-10-24 08:41:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-20 13:45:53 +00:00
|
|
|
}
|
2018-12-12 17:40:57 +00:00
|
|
|
return cell
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func peerCell(for tableView: UITableView, at indexPath: IndexPath) -> UITableViewCell {
|
|
|
|
let peerIndex = indexPath.section - interfaceFieldsBySection.count
|
|
|
|
let peerData = tunnelViewModel.peersData[peerIndex]
|
|
|
|
let peerFieldsToShow = peerData.shouldAllowExcludePrivateIPsControl ? peerFields : peerFields.filter { $0 != .excludePrivateIPs }
|
|
|
|
let field = peerFieldsToShow[indexPath.row]
|
|
|
|
if field == .deletePeer {
|
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: TunnelEditTableViewButtonCell.reuseIdentifier, for: indexPath) as! TunnelEditTableViewButtonCell
|
|
|
|
cell.buttonText = field.rawValue
|
|
|
|
cell.hasDestructiveAction = true
|
|
|
|
cell.onTapped = { [weak self, weak peerData] in
|
|
|
|
guard let peerData = peerData else { return }
|
|
|
|
guard let self = self else { return }
|
|
|
|
self.showConfirmationAlert(message: "Delete this peer?", buttonTitle: "Delete", from: cell) { [weak self] in
|
|
|
|
guard let self = self else { return }
|
|
|
|
let removedSectionIndices = self.deletePeer(peer: peerData)
|
|
|
|
let shouldShowExcludePrivateIPs = (self.tunnelViewModel.peersData.count == 1 && self.tunnelViewModel.peersData[0].shouldAllowExcludePrivateIPsControl)
|
|
|
|
tableView.performBatchUpdates({
|
|
|
|
self.tableView.deleteSections(removedSectionIndices, with: .automatic)
|
|
|
|
if shouldShowExcludePrivateIPs {
|
|
|
|
if let row = self.peerFields.firstIndex(of: .excludePrivateIPs) {
|
|
|
|
let rowIndexPath = IndexPath(row: row, section: self.interfaceFieldsBySection.count /* First peer section */)
|
|
|
|
self.tableView.insertRows(at: [rowIndexPath], with: .automatic)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-10-20 13:45:53 +00:00
|
|
|
})
|
|
|
|
}
|
2018-12-12 17:40:57 +00:00
|
|
|
}
|
|
|
|
return cell
|
|
|
|
} else if field == .excludePrivateIPs {
|
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: TunnelEditTableViewSwitchCell.reuseIdentifier, for: indexPath) as! TunnelEditTableViewSwitchCell
|
|
|
|
cell.message = field.rawValue
|
|
|
|
cell.isEnabled = peerData.shouldAllowExcludePrivateIPsControl
|
|
|
|
cell.isOn = peerData.excludePrivateIPsValue
|
|
|
|
cell.onSwitchToggled = { [weak self] (isOn) in
|
|
|
|
guard let self = self else { return }
|
|
|
|
peerData.excludePrivateIPsValueChanged(isOn: isOn, dnsServers: self.tunnelViewModel.interfaceData[.dns])
|
|
|
|
if let row = self.peerFields.firstIndex(of: .allowedIPs) {
|
|
|
|
self.tableView.reloadRows(at: [IndexPath(row: row, section: indexPath.section)], with: .none)
|
2018-10-29 00:47:48 +00:00
|
|
|
}
|
2018-12-12 17:40:57 +00:00
|
|
|
}
|
|
|
|
return cell
|
|
|
|
} else {
|
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: TunnelEditTableViewKeyValueCell.reuseIdentifier, for: indexPath) as! TunnelEditTableViewKeyValueCell
|
|
|
|
// Set key
|
|
|
|
cell.key = field.rawValue
|
|
|
|
// Set placeholder text
|
|
|
|
switch field {
|
|
|
|
case .publicKey:
|
|
|
|
cell.placeholderText = "Required"
|
|
|
|
case .preSharedKey:
|
|
|
|
cell.placeholderText = "Optional"
|
|
|
|
case .endpoint:
|
|
|
|
cell.placeholderText = "Optional"
|
|
|
|
case .allowedIPs:
|
|
|
|
cell.placeholderText = "Optional"
|
|
|
|
case .persistentKeepAlive:
|
|
|
|
cell.placeholderText = "Off"
|
|
|
|
case .excludePrivateIPs: break
|
|
|
|
case .deletePeer: break
|
|
|
|
}
|
|
|
|
// Set keyboardType
|
|
|
|
if field == .persistentKeepAlive {
|
|
|
|
cell.keyboardType = .numberPad
|
|
|
|
} else if field == .allowedIPs {
|
|
|
|
cell.keyboardType = .numbersAndPunctuation
|
|
|
|
}
|
|
|
|
// Show erroring fields
|
|
|
|
cell.isValueValid = (!peerData.fieldsWithError.contains(field))
|
|
|
|
// Bind values to view model
|
|
|
|
cell.value = peerData[field]
|
|
|
|
if field != .allowedIPs {
|
|
|
|
cell.onValueChanged = { [weak peerData] value in
|
|
|
|
peerData?[field] = value
|
2018-10-29 11:08:32 +00:00
|
|
|
}
|
2018-12-12 17:40:57 +00:00
|
|
|
}
|
|
|
|
// Compute state of exclude private IPs live
|
|
|
|
if field == .allowedIPs {
|
|
|
|
cell.onValueBeingEdited = { [weak self, weak peerData] value in
|
|
|
|
if let peerData = peerData, let self = self {
|
|
|
|
let oldValue = peerData.shouldAllowExcludePrivateIPsControl
|
|
|
|
peerData[.allowedIPs] = value
|
|
|
|
if oldValue != peerData.shouldAllowExcludePrivateIPsControl {
|
|
|
|
if let row = self.peerFields.firstIndex(of: .excludePrivateIPs) {
|
|
|
|
if peerData.shouldAllowExcludePrivateIPsControl {
|
|
|
|
self.tableView.insertRows(at: [IndexPath(row: row, section: indexPath.section)], with: .automatic)
|
|
|
|
} else {
|
|
|
|
self.tableView.deleteRows(at: [IndexPath(row: row, section: indexPath.section)], with: .automatic)
|
2018-11-02 07:42:10 +00:00
|
|
|
}
|
2018-10-29 11:08:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-20 13:45:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return cell
|
2018-12-12 17:40:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func addPeerCell(for tableView: UITableView, at indexPath: IndexPath) -> UITableViewCell {
|
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: TunnelEditTableViewButtonCell.reuseIdentifier, for: indexPath) as! TunnelEditTableViewButtonCell
|
|
|
|
cell.buttonText = "Add peer"
|
|
|
|
cell.onTapped = { [weak self] in
|
|
|
|
guard let self = self else { return }
|
|
|
|
let shouldHideExcludePrivateIPs = (self.tunnelViewModel.peersData.count == 1 && self.tunnelViewModel.peersData[0].shouldAllowExcludePrivateIPsControl)
|
|
|
|
let addedSectionIndices = self.appendEmptyPeer()
|
|
|
|
tableView.performBatchUpdates({
|
|
|
|
tableView.insertSections(addedSectionIndices, with: .automatic)
|
|
|
|
if shouldHideExcludePrivateIPs {
|
|
|
|
if let row = self.peerFields.firstIndex(of: .excludePrivateIPs) {
|
|
|
|
let rowIndexPath = IndexPath(row: row, section: self.interfaceFieldsBySection.count /* First peer section */)
|
|
|
|
self.tableView.deleteRows(at: [rowIndexPath], with: .automatic)
|
2018-11-10 13:20:09 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-12 17:40:57 +00:00
|
|
|
}, completion: nil)
|
|
|
|
}
|
|
|
|
return cell
|
|
|
|
}
|
|
|
|
|
|
|
|
private func onDemandCell(for tableView: UITableView, at indexPath: IndexPath) -> UITableViewCell {
|
|
|
|
assert(indexPath.section == interfaceSectionCount + peerSectionCount + 1)
|
|
|
|
if indexPath.row == 0 {
|
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: TunnelEditTableViewSwitchCell.reuseIdentifier, for: indexPath) as! TunnelEditTableViewSwitchCell
|
|
|
|
cell.message = "Activate on demand"
|
|
|
|
cell.isOn = activateOnDemandSetting.isActivateOnDemandEnabled
|
|
|
|
cell.onSwitchToggled = { [weak self] (isOn) in
|
|
|
|
guard let self = self else { return }
|
|
|
|
let indexPaths: [IndexPath] = (1 ..< 4).map { IndexPath(row: $0, section: indexPath.section) }
|
|
|
|
if isOn {
|
|
|
|
self.activateOnDemandSetting.isActivateOnDemandEnabled = true
|
|
|
|
if self.activateOnDemandSetting.activateOnDemandOption == .none {
|
|
|
|
self.activateOnDemandSetting.activateOnDemandOption = TunnelViewModel.defaultActivateOnDemandOption()
|
|
|
|
}
|
|
|
|
self.tableView.insertRows(at: indexPaths, with: .automatic)
|
|
|
|
} else {
|
|
|
|
self.activateOnDemandSetting.isActivateOnDemandEnabled = false
|
|
|
|
self.tableView.deleteRows(at: indexPaths, with: .automatic)
|
|
|
|
}
|
2018-11-10 13:20:09 +00:00
|
|
|
}
|
2018-12-12 17:40:57 +00:00
|
|
|
return cell
|
|
|
|
} else {
|
|
|
|
assert(indexPath.row < 4)
|
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: TunnelEditTableViewSelectionListCell.reuseIdentifier, for: indexPath) as! TunnelEditTableViewSelectionListCell
|
|
|
|
let rowOption = activateOnDemandOptions[indexPath.row - 1]
|
|
|
|
let selectedOption = activateOnDemandSetting.activateOnDemandOption
|
|
|
|
assert(selectedOption != .none)
|
|
|
|
cell.message = TunnelViewModel.activateOnDemandOptionText(for: rowOption)
|
|
|
|
cell.isChecked = (selectedOption == rowOption)
|
|
|
|
return cell
|
2018-10-20 13:45:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func appendEmptyPeer() -> IndexSet {
|
2018-10-23 09:53:18 +00:00
|
|
|
tunnelViewModel.appendEmptyPeer()
|
|
|
|
let addedPeerIndex = tunnelViewModel.peersData.count - 1
|
2018-10-20 13:45:53 +00:00
|
|
|
|
2018-12-12 17:40:57 +00:00
|
|
|
let addedSectionIndices = IndexSet(integer: interfaceSectionCount + addedPeerIndex)
|
2018-10-20 13:45:53 +00:00
|
|
|
return addedSectionIndices
|
|
|
|
}
|
|
|
|
|
2018-10-23 09:53:18 +00:00
|
|
|
func deletePeer(peer: TunnelViewModel.PeerData) -> IndexSet {
|
|
|
|
assert(peer.index < tunnelViewModel.peersData.count)
|
|
|
|
tunnelViewModel.deletePeer(peer: peer)
|
2018-10-20 13:45:53 +00:00
|
|
|
|
2018-12-12 17:40:57 +00:00
|
|
|
let removedSectionIndices = IndexSet(integer: (interfaceSectionCount + peer.index))
|
2018-10-20 13:45:53 +00:00
|
|
|
return removedSectionIndices
|
|
|
|
}
|
|
|
|
|
2018-12-12 17:40:57 +00:00
|
|
|
func showConfirmationAlert(message: String, buttonTitle: String, from sourceView: UIView, onConfirmed: @escaping (() -> Void)) {
|
2018-11-03 18:35:25 +00:00
|
|
|
let destroyAction = UIAlertAction(title: buttonTitle, style: .destructive) { (_) in
|
2018-10-20 13:45:53 +00:00
|
|
|
onConfirmed()
|
|
|
|
}
|
|
|
|
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
|
|
|
|
let alert = UIAlertController(title: "", message: message, preferredStyle: .actionSheet)
|
|
|
|
alert.addAction(destroyAction)
|
|
|
|
alert.addAction(cancelAction)
|
|
|
|
|
|
|
|
// popoverPresentationController will be nil on iPhone and non-nil on iPad
|
|
|
|
alert.popoverPresentationController?.sourceView = sourceView
|
2018-11-07 14:13:36 +00:00
|
|
|
alert.popoverPresentationController?.sourceRect = sourceView.bounds
|
2018-10-20 13:45:53 +00:00
|
|
|
|
|
|
|
self.present(alert, animated: true, completion: nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-10 13:20:09 +00:00
|
|
|
// MARK: UITableViewDelegate
|
|
|
|
|
|
|
|
extension TunnelEditTableViewController {
|
|
|
|
override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
|
2018-12-12 17:40:57 +00:00
|
|
|
if indexPath.section == (interfaceSectionCount + peerSectionCount + 1) {
|
|
|
|
return (indexPath.row > 0) ? indexPath : nil
|
2018-11-10 13:20:09 +00:00
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
|
|
|
let section = indexPath.section
|
|
|
|
let row = indexPath.row
|
|
|
|
|
2018-12-12 17:40:57 +00:00
|
|
|
assert(section == (interfaceSectionCount + peerSectionCount + 1))
|
2018-11-10 13:20:09 +00:00
|
|
|
assert(row > 0)
|
|
|
|
|
|
|
|
let option = activateOnDemandOptions[row - 1]
|
2018-11-12 10:23:18 +00:00
|
|
|
assert(option != .none)
|
|
|
|
activateOnDemandSetting.activateOnDemandOption = option
|
2018-11-10 13:20:09 +00:00
|
|
|
|
|
|
|
let indexPaths: [IndexPath] = (1 ..< 4).map { IndexPath(row: $0, section: section) }
|
|
|
|
tableView.reloadRows(at: indexPaths, with: .automatic)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-06 13:31:13 +00:00
|
|
|
class TunnelEditTableViewKeyValueCell: UITableViewCell {
|
2018-12-12 17:40:57 +00:00
|
|
|
static let reuseIdentifier = "TunnelEditTableViewKeyValueCell"
|
2018-10-20 13:45:53 +00:00
|
|
|
var key: String {
|
|
|
|
get { return keyLabel.text ?? "" }
|
|
|
|
set(value) {keyLabel.text = value }
|
|
|
|
}
|
|
|
|
var value: String {
|
|
|
|
get { return valueTextField.text ?? "" }
|
|
|
|
set(value) { valueTextField.text = value }
|
|
|
|
}
|
|
|
|
var placeholderText: String {
|
|
|
|
get { return valueTextField.placeholder ?? "" }
|
|
|
|
set(value) { valueTextField.placeholder = value }
|
|
|
|
}
|
|
|
|
var isValueValid: Bool = true {
|
2018-11-01 18:15:29 +00:00
|
|
|
didSet {
|
2018-12-12 17:40:57 +00:00
|
|
|
if isValueValid {
|
2018-11-06 13:31:13 +00:00
|
|
|
keyLabel.textColor = UIColor.black
|
2018-10-20 13:45:53 +00:00
|
|
|
} else {
|
|
|
|
keyLabel.textColor = UIColor.red
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-29 00:47:48 +00:00
|
|
|
var keyboardType: UIKeyboardType {
|
|
|
|
get { return valueTextField.keyboardType }
|
|
|
|
set(value) { valueTextField.keyboardType = value }
|
|
|
|
}
|
2018-10-20 13:45:53 +00:00
|
|
|
|
2018-11-03 18:35:25 +00:00
|
|
|
var onValueChanged: ((String) -> Void)?
|
|
|
|
var onValueBeingEdited: ((String) -> Void)?
|
2018-10-22 09:54:58 +00:00
|
|
|
|
2018-10-20 13:45:53 +00:00
|
|
|
let keyLabel: UILabel
|
|
|
|
let valueTextField: UITextField
|
|
|
|
|
2018-12-09 19:30:08 +00:00
|
|
|
var isStackedHorizontally: Bool = false
|
|
|
|
var isStackedVertically: Bool = false
|
|
|
|
var contentSizeBasedConstraints: [NSLayoutConstraint] = []
|
|
|
|
|
2018-10-22 09:54:58 +00:00
|
|
|
private var textFieldValueOnBeginEditing: String = ""
|
|
|
|
|
2018-11-05 05:31:25 +00:00
|
|
|
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
|
2018-10-20 13:45:53 +00:00
|
|
|
keyLabel = UILabel()
|
2018-12-09 19:30:08 +00:00
|
|
|
keyLabel.font = UIFont.preferredFont(forTextStyle: .body)
|
|
|
|
keyLabel.adjustsFontForContentSizeCategory = true
|
2018-10-20 13:45:53 +00:00
|
|
|
valueTextField = UITextField()
|
2018-12-09 19:30:08 +00:00
|
|
|
valueTextField.font = UIFont.preferredFont(forTextStyle: .body)
|
|
|
|
valueTextField.adjustsFontForContentSizeCategory = true
|
2018-10-20 13:45:53 +00:00
|
|
|
super.init(style: style, reuseIdentifier: reuseIdentifier)
|
|
|
|
contentView.addSubview(keyLabel)
|
|
|
|
keyLabel.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
keyLabel.textAlignment = .right
|
|
|
|
let widthRatioConstraint = NSLayoutConstraint(item: keyLabel, attribute: .width,
|
2018-11-06 17:12:53 +00:00
|
|
|
relatedBy: .equal,
|
|
|
|
toItem: self, attribute: .width,
|
|
|
|
multiplier: 0.4, constant: 0)
|
2018-10-22 08:19:21 +00:00
|
|
|
// The "Persistent Keepalive" key doesn't fit into 0.4 * width on the iPhone SE,
|
|
|
|
// so set a CR priority > the 0.4-constraint's priority.
|
|
|
|
widthRatioConstraint.priority = .defaultHigh + 1
|
|
|
|
keyLabel.setContentCompressionResistancePriority(.defaultHigh + 2, for: .horizontal)
|
2018-10-20 13:45:53 +00:00
|
|
|
NSLayoutConstraint.activate([
|
2018-11-02 18:54:53 +00:00
|
|
|
keyLabel.leftAnchor.constraint(equalTo: contentView.layoutMarginsGuide.leftAnchor),
|
2018-12-09 19:30:08 +00:00
|
|
|
keyLabel.topAnchor.constraint(equalToSystemSpacingBelow: contentView.layoutMarginsGuide.topAnchor, multiplier: 0.5),
|
2018-10-20 13:45:53 +00:00
|
|
|
widthRatioConstraint
|
|
|
|
])
|
|
|
|
contentView.addSubview(valueTextField)
|
|
|
|
valueTextField.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
NSLayoutConstraint.activate([
|
2018-11-02 18:54:53 +00:00
|
|
|
valueTextField.rightAnchor.constraint(equalTo: contentView.layoutMarginsGuide.rightAnchor),
|
2018-12-09 19:30:08 +00:00
|
|
|
contentView.layoutMarginsGuide.bottomAnchor.constraint(equalToSystemSpacingBelow: valueTextField.bottomAnchor, multiplier: 0.5)
|
2018-10-20 13:45:53 +00:00
|
|
|
])
|
2018-10-22 09:54:58 +00:00
|
|
|
valueTextField.delegate = self
|
2018-10-29 00:47:48 +00:00
|
|
|
|
|
|
|
valueTextField.autocapitalizationType = .none
|
|
|
|
valueTextField.autocorrectionType = .no
|
|
|
|
valueTextField.spellCheckingType = .no
|
2018-12-09 19:30:08 +00:00
|
|
|
|
|
|
|
configureForContentSize()
|
|
|
|
}
|
|
|
|
|
|
|
|
func configureForContentSize() {
|
|
|
|
var constraints: [NSLayoutConstraint] = []
|
2018-12-12 18:28:27 +00:00
|
|
|
if self.traitCollection.preferredContentSizeCategory.isAccessibilityCategory {
|
2018-12-09 19:30:08 +00:00
|
|
|
// Stack vertically
|
2018-12-12 18:28:27 +00:00
|
|
|
if !isStackedVertically {
|
2018-12-09 19:30:08 +00:00
|
|
|
constraints = [
|
|
|
|
valueTextField.topAnchor.constraint(equalToSystemSpacingBelow: keyLabel.bottomAnchor, multiplier: 0.5),
|
|
|
|
valueTextField.leftAnchor.constraint(equalTo: contentView.layoutMarginsGuide.leftAnchor),
|
|
|
|
keyLabel.rightAnchor.constraint(equalTo: contentView.layoutMarginsGuide.rightAnchor)
|
|
|
|
]
|
|
|
|
isStackedVertically = true
|
|
|
|
isStackedHorizontally = false
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Stack horizontally
|
2018-12-12 18:28:27 +00:00
|
|
|
if !isStackedHorizontally {
|
2018-12-09 19:30:08 +00:00
|
|
|
constraints = [
|
|
|
|
contentView.layoutMarginsGuide.bottomAnchor.constraint(equalToSystemSpacingBelow: keyLabel.bottomAnchor, multiplier: 0.5),
|
|
|
|
valueTextField.leftAnchor.constraint(equalToSystemSpacingAfter: keyLabel.rightAnchor, multiplier: 1),
|
2018-12-12 17:40:57 +00:00
|
|
|
valueTextField.topAnchor.constraint(equalToSystemSpacingBelow: contentView.layoutMarginsGuide.topAnchor, multiplier: 0.5)
|
2018-12-09 19:30:08 +00:00
|
|
|
]
|
|
|
|
isStackedHorizontally = true
|
|
|
|
isStackedVertically = false
|
|
|
|
}
|
|
|
|
}
|
2018-12-12 18:28:27 +00:00
|
|
|
if !constraints.isEmpty {
|
2018-12-09 19:30:08 +00:00
|
|
|
NSLayoutConstraint.deactivate(self.contentSizeBasedConstraints)
|
|
|
|
NSLayoutConstraint.activate(constraints)
|
|
|
|
self.contentSizeBasedConstraints = constraints
|
|
|
|
}
|
2018-10-20 13:45:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder aDecoder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
override func prepareForReuse() {
|
|
|
|
super.prepareForReuse()
|
|
|
|
key = ""
|
|
|
|
value = ""
|
|
|
|
placeholderText = ""
|
|
|
|
isValueValid = true
|
2018-10-29 09:42:19 +00:00
|
|
|
keyboardType = .default
|
2018-10-22 09:54:58 +00:00
|
|
|
onValueChanged = nil
|
2018-11-08 06:55:14 +00:00
|
|
|
onValueBeingEdited = nil
|
2018-12-09 19:30:08 +00:00
|
|
|
configureForContentSize()
|
2018-10-22 09:54:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-28 20:45:43 +00:00
|
|
|
extension TunnelEditTableViewKeyValueCell: UITextFieldDelegate {
|
2018-10-22 09:54:58 +00:00
|
|
|
func textFieldDidBeginEditing(_ textField: UITextField) {
|
|
|
|
textFieldValueOnBeginEditing = textField.text ?? ""
|
|
|
|
isValueValid = true
|
|
|
|
}
|
|
|
|
func textFieldDidEndEditing(_ textField: UITextField) {
|
|
|
|
let isModified = (textField.text ?? "" != textFieldValueOnBeginEditing)
|
2018-12-12 18:28:27 +00:00
|
|
|
guard isModified else { return }
|
2018-10-22 09:54:58 +00:00
|
|
|
if let onValueChanged = onValueChanged {
|
|
|
|
onValueChanged(textField.text ?? "")
|
|
|
|
}
|
2018-10-20 13:45:53 +00:00
|
|
|
}
|
2018-10-24 08:41:34 +00:00
|
|
|
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)
|
|
|
|
onValueBeingEdited(modifiedText)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2018-10-20 13:45:53 +00:00
|
|
|
}
|
|
|
|
|
2018-11-06 11:35:03 +00:00
|
|
|
class TunnelEditTableViewReadOnlyKeyValueCell: CopyableLabelTableViewCell {
|
2018-12-12 17:40:57 +00:00
|
|
|
static let reuseIdentifier = "TunnelEditTableViewReadOnlyKeyValueCell"
|
2018-11-06 11:35:03 +00:00
|
|
|
var key: String {
|
|
|
|
get { return keyLabel.text ?? "" }
|
|
|
|
set(value) {keyLabel.text = value }
|
|
|
|
}
|
|
|
|
var value: String {
|
|
|
|
get { return valueLabel.text }
|
|
|
|
set(value) { valueLabel.text = value }
|
|
|
|
}
|
|
|
|
|
|
|
|
let keyLabel: UILabel
|
|
|
|
let valueLabel: ScrollableLabel
|
|
|
|
|
|
|
|
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
|
|
|
|
keyLabel = UILabel()
|
2018-12-09 19:30:08 +00:00
|
|
|
keyLabel.font = UIFont.preferredFont(forTextStyle: .body)
|
|
|
|
keyLabel.adjustsFontForContentSizeCategory = true
|
2018-11-06 11:35:03 +00:00
|
|
|
valueLabel = ScrollableLabel()
|
2018-12-09 19:30:08 +00:00
|
|
|
valueLabel.label.font = UIFont.preferredFont(forTextStyle: .body)
|
|
|
|
valueLabel.label.adjustsFontForContentSizeCategory = true
|
2018-11-06 11:35:03 +00:00
|
|
|
|
|
|
|
super.init(style: style, reuseIdentifier: reuseIdentifier)
|
|
|
|
|
|
|
|
keyLabel.textColor = UIColor.gray
|
|
|
|
valueLabel.textColor = UIColor.gray
|
|
|
|
|
|
|
|
contentView.addSubview(keyLabel)
|
|
|
|
keyLabel.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
keyLabel.textAlignment = .right
|
|
|
|
let widthRatioConstraint = NSLayoutConstraint(item: keyLabel, attribute: .width,
|
|
|
|
relatedBy: .equal,
|
|
|
|
toItem: self, attribute: .width,
|
|
|
|
multiplier: 0.4, constant: 0)
|
|
|
|
// In case the key doesn't fit into 0.4 * width,
|
|
|
|
// so set a CR priority > the 0.4-constraint's priority.
|
|
|
|
widthRatioConstraint.priority = .defaultHigh + 1
|
|
|
|
keyLabel.setContentCompressionResistancePriority(.defaultHigh + 2, for: .horizontal)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
keyLabel.centerYAnchor.constraint(equalTo: contentView.centerYAnchor),
|
|
|
|
keyLabel.leftAnchor.constraint(equalTo: contentView.layoutMarginsGuide.leftAnchor),
|
|
|
|
widthRatioConstraint
|
|
|
|
])
|
|
|
|
|
|
|
|
contentView.addSubview(valueLabel)
|
|
|
|
valueLabel.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
valueLabel.centerYAnchor.constraint(equalTo: contentView.centerYAnchor),
|
2018-12-09 19:30:08 +00:00
|
|
|
valueLabel.leftAnchor.constraint(equalToSystemSpacingAfter: keyLabel.rightAnchor, multiplier: 1),
|
2018-12-12 17:40:57 +00:00
|
|
|
valueLabel.rightAnchor.constraint(equalTo: contentView.layoutMarginsGuide.rightAnchor)
|
2018-11-06 11:35:03 +00:00
|
|
|
])
|
|
|
|
}
|
|
|
|
|
|
|
|
override var textToCopy: String? {
|
|
|
|
return self.valueLabel.text
|
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder aDecoder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
override func prepareForReuse() {
|
|
|
|
super.prepareForReuse()
|
|
|
|
key = ""
|
|
|
|
value = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-28 20:45:43 +00:00
|
|
|
class TunnelEditTableViewButtonCell: UITableViewCell {
|
2018-12-12 17:40:57 +00:00
|
|
|
static let reuseIdentifier = "TunnelEditTableViewButtonCell"
|
2018-10-20 13:45:53 +00:00
|
|
|
var buttonText: String {
|
|
|
|
get { return button.title(for: .normal) ?? "" }
|
|
|
|
set(value) { button.setTitle(value, for: .normal) }
|
|
|
|
}
|
2018-11-02 07:54:10 +00:00
|
|
|
var hasDestructiveAction: Bool {
|
|
|
|
get { return button.tintColor == UIColor.red }
|
|
|
|
set(value) { button.tintColor = value ? UIColor.red : buttonStandardTintColor }
|
|
|
|
}
|
2018-11-03 18:35:25 +00:00
|
|
|
var onTapped: (() -> Void)?
|
2018-10-20 13:45:53 +00:00
|
|
|
|
|
|
|
let button: UIButton
|
2018-11-02 07:54:10 +00:00
|
|
|
var buttonStandardTintColor: UIColor
|
2018-10-20 13:45:53 +00:00
|
|
|
|
2018-11-05 05:31:25 +00:00
|
|
|
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
|
2018-10-20 13:45:53 +00:00
|
|
|
button = UIButton(type: .system)
|
2018-12-09 19:30:08 +00:00
|
|
|
button.titleLabel?.font = UIFont.preferredFont(forTextStyle: .body)
|
|
|
|
button.titleLabel?.adjustsFontForContentSizeCategory = true
|
2018-11-02 07:54:10 +00:00
|
|
|
buttonStandardTintColor = button.tintColor
|
2018-10-20 13:45:53 +00:00
|
|
|
super.init(style: style, reuseIdentifier: reuseIdentifier)
|
|
|
|
contentView.addSubview(button)
|
|
|
|
button.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
NSLayoutConstraint.activate([
|
2018-12-09 19:30:08 +00:00
|
|
|
button.topAnchor.constraint(equalTo: contentView.layoutMarginsGuide.topAnchor),
|
|
|
|
contentView.layoutMarginsGuide.bottomAnchor.constraint(equalTo: button.bottomAnchor),
|
2018-10-20 13:45:53 +00:00
|
|
|
button.centerXAnchor.constraint(equalTo: contentView.centerXAnchor)
|
|
|
|
])
|
|
|
|
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func buttonTapped() {
|
|
|
|
onTapped?()
|
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder aDecoder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
override func prepareForReuse() {
|
2018-10-28 20:56:40 +00:00
|
|
|
super.prepareForReuse()
|
2018-10-20 13:45:53 +00:00
|
|
|
buttonText = ""
|
|
|
|
onTapped = nil
|
2018-11-02 07:54:10 +00:00
|
|
|
hasDestructiveAction = false
|
2018-10-20 13:45:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-28 20:45:43 +00:00
|
|
|
class TunnelEditTableViewSwitchCell: UITableViewCell {
|
2018-12-12 17:40:57 +00:00
|
|
|
static let reuseIdentifier = "TunnelEditTableViewSwitchCell"
|
2018-10-20 13:45:53 +00:00
|
|
|
var message: String {
|
|
|
|
get { return textLabel?.text ?? "" }
|
|
|
|
set(value) { textLabel!.text = value }
|
|
|
|
}
|
|
|
|
var isOn: Bool {
|
|
|
|
get { return switchView.isOn }
|
|
|
|
set(value) { switchView.isOn = value }
|
|
|
|
}
|
2018-10-29 11:08:32 +00:00
|
|
|
var isEnabled: Bool {
|
|
|
|
get { return switchView.isEnabled }
|
|
|
|
set(value) {
|
|
|
|
switchView.isEnabled = value
|
|
|
|
textLabel?.textColor = value ? UIColor.black : UIColor.gray
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-03 18:35:25 +00:00
|
|
|
var onSwitchToggled: ((Bool) -> Void)?
|
2018-10-20 13:45:53 +00:00
|
|
|
|
|
|
|
let switchView: UISwitch
|
|
|
|
|
2018-11-05 05:31:25 +00:00
|
|
|
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
|
2018-10-20 13:45:53 +00:00
|
|
|
switchView = UISwitch()
|
|
|
|
super.init(style: .default, reuseIdentifier: reuseIdentifier)
|
|
|
|
accessoryView = switchView
|
2018-10-29 11:08:32 +00:00
|
|
|
|
|
|
|
switchView.addTarget(self, action: #selector(switchToggled), for: .valueChanged)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func switchToggled() {
|
|
|
|
onSwitchToggled?(switchView.isOn)
|
2018-10-20 13:45:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder aDecoder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
override func prepareForReuse() {
|
2018-10-28 20:56:40 +00:00
|
|
|
super.prepareForReuse()
|
2018-10-20 13:45:53 +00:00
|
|
|
message = ""
|
|
|
|
isOn = false
|
|
|
|
}
|
|
|
|
}
|
2018-11-10 13:20:09 +00:00
|
|
|
|
|
|
|
class TunnelEditTableViewSelectionListCell: UITableViewCell {
|
2018-12-12 17:40:57 +00:00
|
|
|
static let reuseIdentifier = "TunnelEditTableViewSelectionListCell"
|
2018-11-10 13:20:09 +00:00
|
|
|
var message: String {
|
|
|
|
get { return textLabel?.text ?? "" }
|
|
|
|
set(value) { textLabel!.text = value }
|
|
|
|
}
|
|
|
|
var isChecked: Bool {
|
|
|
|
didSet {
|
|
|
|
accessoryType = isChecked ? .checkmark : .none
|
|
|
|
}
|
|
|
|
}
|
|
|
|
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
|
|
|
|
isChecked = false
|
|
|
|
super.init(style: .default, reuseIdentifier: reuseIdentifier)
|
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder aDecoder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
override func prepareForReuse() {
|
|
|
|
super.prepareForReuse()
|
|
|
|
message = ""
|
|
|
|
isChecked = false
|
|
|
|
}
|
|
|
|
}
|