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-13 01:38:30 +00:00
|
|
|
|
|
|
|
import UIKit
|
2018-10-28 20:22:43 +00:00
|
|
|
import MobileCoreServices
|
2018-11-09 11:23:52 +00:00
|
|
|
import UserNotifications
|
2018-10-13 01:38:30 +00:00
|
|
|
|
2018-11-03 03:20:27 +00:00
|
|
|
class TunnelsListTableViewController: UIViewController {
|
2018-10-13 01:38:30 +00:00
|
|
|
|
2018-11-03 18:35:25 +00:00
|
|
|
var tunnelsManager: TunnelsManager?
|
|
|
|
var onTunnelsManagerReady: ((TunnelsManager) -> Void)?
|
2018-10-17 08:17:57 +00:00
|
|
|
|
2018-11-03 18:35:25 +00:00
|
|
|
var busyIndicator: UIActivityIndicatorView?
|
|
|
|
var centeredAddButton: BorderedTextButton?
|
|
|
|
var tableView: UITableView?
|
2018-10-17 07:50:20 +00:00
|
|
|
|
2018-10-13 01:38:30 +00:00
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
2018-11-03 03:20:27 +00:00
|
|
|
view.backgroundColor = UIColor.white
|
|
|
|
|
|
|
|
// Set up the navigation bar
|
2018-10-13 01:38:30 +00:00
|
|
|
self.title = "WireGuard"
|
|
|
|
let addButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addButtonTapped(sender:)))
|
|
|
|
self.navigationItem.rightBarButtonItem = addButtonItem
|
2018-10-29 12:04:02 +00:00
|
|
|
let settingsButtonItem = UIBarButtonItem(title: "Settings", style: .plain, target: self, action: #selector(settingsButtonTapped(sender:)))
|
|
|
|
self.navigationItem.leftBarButtonItem = settingsButtonItem
|
2018-10-17 08:17:57 +00:00
|
|
|
|
2018-11-03 03:20:27 +00:00
|
|
|
// Set up the busy indicator
|
2018-11-05 05:31:25 +00:00
|
|
|
let busyIndicator = UIActivityIndicatorView(style: .gray)
|
2018-11-03 03:20:27 +00:00
|
|
|
busyIndicator.hidesWhenStopped = true
|
|
|
|
|
|
|
|
// Add the busyIndicator, centered
|
|
|
|
view.addSubview(busyIndicator)
|
|
|
|
busyIndicator.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
busyIndicator.centerXAnchor.constraint(equalTo: view.centerXAnchor),
|
|
|
|
busyIndicator.centerYAnchor.constraint(equalTo: view.centerYAnchor)
|
|
|
|
])
|
|
|
|
busyIndicator.startAnimating()
|
|
|
|
self.busyIndicator = busyIndicator
|
|
|
|
|
|
|
|
// Create the tunnels manager, and when it's ready, create the tableView
|
2018-10-17 08:17:57 +00:00
|
|
|
TunnelsManager.create { [weak self] tunnelsManager in
|
|
|
|
guard let tunnelsManager = tunnelsManager else { return }
|
2018-11-03 03:20:27 +00:00
|
|
|
guard let s = self else { return }
|
|
|
|
|
|
|
|
let tableView = UITableView(frame: CGRect.zero, style: .plain)
|
|
|
|
tableView.rowHeight = 60
|
2018-11-03 04:55:32 +00:00
|
|
|
tableView.separatorStyle = .none
|
2018-11-03 03:20:27 +00:00
|
|
|
tableView.register(TunnelsListTableViewCell.self, forCellReuseIdentifier: TunnelsListTableViewCell.id)
|
|
|
|
|
|
|
|
s.view.addSubview(tableView)
|
|
|
|
tableView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
tableView.leftAnchor.constraint(equalTo: s.view.leftAnchor),
|
|
|
|
tableView.rightAnchor.constraint(equalTo: s.view.rightAnchor),
|
|
|
|
tableView.topAnchor.constraint(equalTo: s.view.topAnchor),
|
|
|
|
tableView.bottomAnchor.constraint(equalTo: s.view.bottomAnchor)
|
|
|
|
])
|
|
|
|
tableView.dataSource = s
|
|
|
|
tableView.delegate = s
|
|
|
|
s.tableView = tableView
|
|
|
|
|
2018-11-03 04:52:05 +00:00
|
|
|
// Add an add button, centered
|
|
|
|
let centeredAddButton = BorderedTextButton()
|
|
|
|
centeredAddButton.title = "Add a tunnel"
|
|
|
|
centeredAddButton.isHidden = true
|
|
|
|
s.view.addSubview(centeredAddButton)
|
|
|
|
centeredAddButton.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
centeredAddButton.centerXAnchor.constraint(equalTo: s.view.centerXAnchor),
|
|
|
|
centeredAddButton.centerYAnchor.constraint(equalTo: s.view.centerYAnchor)
|
|
|
|
])
|
|
|
|
centeredAddButton.onTapped = { [weak self] in
|
|
|
|
self?.addButtonTapped(sender: centeredAddButton)
|
|
|
|
}
|
|
|
|
s.centeredAddButton = centeredAddButton
|
|
|
|
|
|
|
|
centeredAddButton.isHidden = (tunnelsManager.numberOfTunnels() > 0)
|
2018-11-03 03:20:27 +00:00
|
|
|
busyIndicator.stopAnimating()
|
|
|
|
|
|
|
|
tunnelsManager.delegate = s
|
|
|
|
s.tunnelsManager = tunnelsManager
|
|
|
|
s.onTunnelsManagerReady?(tunnelsManager)
|
|
|
|
s.onTunnelsManagerReady = nil
|
2018-10-17 08:17:57 +00:00
|
|
|
}
|
2018-10-13 01:38:30 +00:00
|
|
|
}
|
|
|
|
|
2018-11-03 04:52:05 +00:00
|
|
|
@objc func addButtonTapped(sender: AnyObject) {
|
2018-11-02 18:55:35 +00:00
|
|
|
if (self.tunnelsManager == nil) { return } // Do nothing until we've loaded the tunnels
|
2018-11-01 17:59:58 +00:00
|
|
|
let alert = UIAlertController(title: "", message: "Add a new WireGuard tunnel", preferredStyle: .actionSheet)
|
2018-11-03 18:35:25 +00:00
|
|
|
let importFileAction = UIAlertAction(title: "Create from file or archive", style: .default) { [weak self] (_) in
|
2018-10-25 09:05:23 +00:00
|
|
|
self?.presentViewControllerForFileImport()
|
|
|
|
}
|
|
|
|
alert.addAction(importFileAction)
|
|
|
|
|
2018-11-03 18:35:25 +00:00
|
|
|
let scanQRCodeAction = UIAlertAction(title: "Create from QR code", style: .default) { [weak self] (_) in
|
2018-10-28 16:52:27 +00:00
|
|
|
self?.presentViewControllerForScanningQRCode()
|
|
|
|
}
|
|
|
|
alert.addAction(scanQRCodeAction)
|
|
|
|
|
2018-11-03 18:35:25 +00:00
|
|
|
let createFromScratchAction = UIAlertAction(title: "Create from scratch", style: .default) { [weak self] (_) in
|
2018-10-25 09:05:23 +00:00
|
|
|
if let s = self, let tunnelsManager = s.tunnelsManager {
|
|
|
|
s.presentViewControllerForTunnelCreation(tunnelsManager: tunnelsManager, tunnelConfiguration: nil)
|
2018-10-17 10:03:06 +00:00
|
|
|
}
|
2018-10-25 09:05:23 +00:00
|
|
|
}
|
|
|
|
alert.addAction(createFromScratchAction)
|
|
|
|
|
|
|
|
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
|
|
|
|
alert.addAction(cancelAction)
|
|
|
|
|
2018-10-17 10:03:06 +00:00
|
|
|
// popoverPresentationController will be nil on iPhone and non-nil on iPad
|
2018-11-03 04:52:05 +00:00
|
|
|
if let sender = sender as? UIBarButtonItem {
|
|
|
|
alert.popoverPresentationController?.barButtonItem = sender
|
|
|
|
} else if let sender = sender as? UIView {
|
|
|
|
alert.popoverPresentationController?.sourceView = sender
|
2018-11-07 12:53:12 +00:00
|
|
|
alert.popoverPresentationController?.sourceRect = sender.bounds
|
2018-11-03 04:52:05 +00:00
|
|
|
}
|
2018-10-17 10:03:06 +00:00
|
|
|
self.present(alert, animated: true, completion: nil)
|
2018-10-13 01:38:30 +00:00
|
|
|
}
|
2018-10-25 05:40:18 +00:00
|
|
|
|
2018-10-29 12:04:02 +00:00
|
|
|
@objc func settingsButtonTapped(sender: UIBarButtonItem!) {
|
2018-11-02 18:55:35 +00:00
|
|
|
if (self.tunnelsManager == nil) { return } // Do nothing until we've loaded the tunnels
|
2018-10-29 17:26:25 +00:00
|
|
|
let settingsVC = SettingsTableViewController(tunnelsManager: tunnelsManager)
|
2018-10-29 12:04:02 +00:00
|
|
|
let settingsNC = UINavigationController(rootViewController: settingsVC)
|
|
|
|
settingsNC.modalPresentationStyle = .formSheet
|
|
|
|
self.present(settingsNC, animated: true)
|
|
|
|
}
|
|
|
|
|
2018-10-25 05:40:18 +00:00
|
|
|
func presentViewControllerForTunnelCreation(tunnelsManager: TunnelsManager, tunnelConfiguration: TunnelConfiguration?) {
|
|
|
|
let editVC = TunnelEditTableViewController(tunnelsManager: tunnelsManager, tunnelConfiguration: tunnelConfiguration)
|
|
|
|
let editNC = UINavigationController(rootViewController: editVC)
|
2018-10-25 07:53:09 +00:00
|
|
|
editNC.modalPresentationStyle = .formSheet
|
2018-10-25 05:40:18 +00:00
|
|
|
self.present(editNC, animated: true)
|
|
|
|
}
|
|
|
|
|
2018-10-25 09:05:23 +00:00
|
|
|
func presentViewControllerForFileImport() {
|
2018-11-07 13:42:36 +00:00
|
|
|
let documentTypes = ["com.wireguard.config.quick", String(kUTTypeText), String(kUTTypeZipArchive)]
|
2018-10-28 20:22:43 +00:00
|
|
|
let filePicker = UIDocumentPickerViewController(documentTypes: documentTypes, in: .import)
|
2018-10-25 09:05:23 +00:00
|
|
|
filePicker.delegate = self
|
|
|
|
self.present(filePicker, animated: true)
|
|
|
|
}
|
|
|
|
|
2018-10-28 16:52:27 +00:00
|
|
|
func presentViewControllerForScanningQRCode() {
|
|
|
|
let scanQRCodeVC = QRScanViewController()
|
|
|
|
scanQRCodeVC.delegate = self
|
|
|
|
let scanQRCodeNC = UINavigationController(rootViewController: scanQRCodeVC)
|
|
|
|
scanQRCodeNC.modalPresentationStyle = .fullScreen
|
|
|
|
self.present(scanQRCodeNC, animated: true)
|
|
|
|
}
|
|
|
|
|
2018-10-25 05:40:18 +00:00
|
|
|
func showErrorAlert(title: String, message: String) {
|
2018-11-01 20:22:12 +00:00
|
|
|
let okAction = UIAlertAction(title: "OK", style: .default)
|
2018-10-25 05:40:18 +00:00
|
|
|
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
|
|
|
|
alert.addAction(okAction)
|
|
|
|
|
|
|
|
self.present(alert, animated: true, completion: nil)
|
|
|
|
}
|
2018-10-28 20:38:40 +00:00
|
|
|
|
2018-11-06 19:16:40 +00:00
|
|
|
func importFromFile(url: URL) {
|
|
|
|
if (url.pathExtension == "zip") {
|
2018-10-28 20:38:40 +00:00
|
|
|
var unarchivedFiles: [(fileName: String, contents: Data)] = []
|
|
|
|
do {
|
|
|
|
unarchivedFiles = try ZipArchive.unarchive(url: url, requiredFileExtensions: ["conf"])
|
|
|
|
} catch ZipArchiveError.cantOpenInputZipFile {
|
2018-11-03 01:51:32 +00:00
|
|
|
showErrorAlert(title: "Unable to read zip archive", message: "The zip archive could not be read.")
|
|
|
|
return
|
2018-10-28 20:38:40 +00:00
|
|
|
} catch ZipArchiveError.badArchive {
|
2018-11-03 01:51:32 +00:00
|
|
|
showErrorAlert(title: "Unable to read zip archive", message: "Bad or corrupt zip archive.")
|
|
|
|
return
|
2018-10-28 20:38:40 +00:00
|
|
|
} catch (let error) {
|
2018-11-01 17:59:58 +00:00
|
|
|
showErrorAlert(title: "Unable to read zip archive", message: "Unexpected error: \(String(describing: error))")
|
2018-11-03 01:51:32 +00:00
|
|
|
return
|
2018-10-28 20:38:40 +00:00
|
|
|
}
|
2018-11-03 18:35:25 +00:00
|
|
|
|
2018-11-03 01:51:32 +00:00
|
|
|
for (i, unarchivedFile) in unarchivedFiles.enumerated().reversed() {
|
2018-11-03 10:15:29 +00:00
|
|
|
let fileBaseName = URL(string: unarchivedFile.fileName)?.deletingPathExtension().lastPathComponent
|
|
|
|
if let trimmedName = fileBaseName?.trimmingCharacters(in: .whitespacesAndNewlines), !trimmedName.isEmpty {
|
2018-11-03 01:51:32 +00:00
|
|
|
unarchivedFiles[i].fileName = trimmedName
|
2018-10-28 20:38:40 +00:00
|
|
|
} else {
|
2018-11-03 01:51:32 +00:00
|
|
|
unarchivedFiles.remove(at: i)
|
2018-10-28 20:38:40 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-03 01:51:32 +00:00
|
|
|
if (unarchivedFiles.isEmpty) {
|
|
|
|
showErrorAlert(title: "No tunnels in zip archive", message: "No .conf tunnel files were found inside the zip archive.")
|
2018-11-01 10:29:17 +00:00
|
|
|
return
|
|
|
|
}
|
2018-11-03 01:51:32 +00:00
|
|
|
guard let tunnelsManager = tunnelsManager else { return }
|
|
|
|
unarchivedFiles.sort { $0.fileName < $1.fileName }
|
2018-11-03 18:35:25 +00:00
|
|
|
var lastFileName: String?
|
2018-11-03 01:51:32 +00:00
|
|
|
var configs: [TunnelConfiguration] = []
|
|
|
|
for file in unarchivedFiles {
|
|
|
|
if file.fileName == lastFileName {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
lastFileName = file.fileName
|
|
|
|
guard let fileContents = String(data: file.contents, encoding: .utf8) else {
|
|
|
|
continue
|
2018-10-31 09:00:25 +00:00
|
|
|
}
|
2018-11-03 01:51:32 +00:00
|
|
|
guard let tunnelConfig = try? WgQuickConfigFileParser.parse(fileContents, name: file.fileName) else {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
configs.append(tunnelConfig)
|
2018-10-31 09:00:25 +00:00
|
|
|
}
|
2018-11-03 01:51:32 +00:00
|
|
|
tunnelsManager.addMultiple(tunnelConfigurations: configs) { [weak self] (numberSuccessful) in
|
|
|
|
if numberSuccessful == unarchivedFiles.count {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
self?.showErrorAlert(title: "Created \(numberSuccessful) tunnels",
|
|
|
|
message: "Created \(numberSuccessful) of \(unarchivedFiles.count) tunnels from zip archive")
|
2018-10-28 20:38:40 +00:00
|
|
|
}
|
2018-11-06 19:16:40 +00:00
|
|
|
} else /* if (url.pathExtension == "conf") -- we assume everything else is a conf */ {
|
|
|
|
let fileBaseName = url.deletingPathExtension().lastPathComponent.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
|
|
if let fileContents = try? String(contentsOf: url),
|
|
|
|
let tunnelConfiguration = try? WgQuickConfigFileParser.parse(fileContents, name: fileBaseName) {
|
|
|
|
tunnelsManager?.add(tunnelConfiguration: tunnelConfiguration) { (_, error) in
|
|
|
|
if let error = error {
|
|
|
|
ErrorPresenter.showErrorAlert(error: error, from: self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
showErrorAlert(title: "Unable to import tunnel", message: "An error occured when importing the tunnel configuration.")
|
|
|
|
}
|
2018-10-28 20:38:40 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-09 13:59:34 +00:00
|
|
|
|
|
|
|
func refreshTunnelConnectionStatuses() {
|
|
|
|
if let tunnelsManager = tunnelsManager {
|
2018-11-12 10:34:03 +00:00
|
|
|
tunnelsManager.refreshStatuses()
|
2018-11-09 13:59:34 +00:00
|
|
|
} else {
|
|
|
|
onTunnelsManagerReady = { tunnelsManager in
|
2018-11-12 10:34:03 +00:00
|
|
|
tunnelsManager.refreshStatuses()
|
2018-11-09 13:59:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-13 01:38:30 +00:00
|
|
|
}
|
|
|
|
|
2018-10-25 09:05:23 +00:00
|
|
|
// MARK: UIDocumentPickerDelegate
|
|
|
|
|
|
|
|
extension TunnelsListTableViewController: UIDocumentPickerDelegate {
|
|
|
|
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
|
2018-11-06 19:16:40 +00:00
|
|
|
urls.forEach(importFromFile)
|
2018-10-25 09:05:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-28 16:52:27 +00:00
|
|
|
// MARK: QRScanViewControllerDelegate
|
|
|
|
|
|
|
|
extension TunnelsListTableViewController: QRScanViewControllerDelegate {
|
2018-10-31 20:06:28 +00:00
|
|
|
func addScannedQRCode(tunnelConfiguration: TunnelConfiguration, qrScanViewController: QRScanViewController,
|
2018-11-06 17:12:53 +00:00
|
|
|
completionHandler: (() -> Void)?) {
|
2018-11-03 18:35:25 +00:00
|
|
|
tunnelsManager?.add(tunnelConfiguration: tunnelConfiguration) { (_, error) in
|
2018-10-28 18:02:15 +00:00
|
|
|
if let error = error {
|
2018-10-31 20:06:28 +00:00
|
|
|
ErrorPresenter.showErrorAlert(error: error, from: qrScanViewController, onDismissal: completionHandler)
|
|
|
|
} else {
|
|
|
|
completionHandler?()
|
2018-10-28 18:02:15 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-28 16:52:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-13 01:38:30 +00:00
|
|
|
// MARK: UITableViewDataSource
|
|
|
|
|
2018-11-03 03:20:27 +00:00
|
|
|
extension TunnelsListTableViewController: UITableViewDataSource {
|
|
|
|
func numberOfSections(in tableView: UITableView) -> Int {
|
2018-10-17 08:17:57 +00:00
|
|
|
return 1
|
2018-10-13 01:38:30 +00:00
|
|
|
}
|
|
|
|
|
2018-11-03 03:20:27 +00:00
|
|
|
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
2018-10-17 08:17:57 +00:00
|
|
|
return (tunnelsManager?.numberOfTunnels() ?? 0)
|
|
|
|
}
|
|
|
|
|
2018-11-03 03:20:27 +00:00
|
|
|
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
2018-10-17 08:17:57 +00:00
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: TunnelsListTableViewCell.id, for: indexPath) as! TunnelsListTableViewCell
|
|
|
|
if let tunnelsManager = tunnelsManager {
|
|
|
|
let tunnel = tunnelsManager.tunnel(at: indexPath.row)
|
2018-10-28 09:26:10 +00:00
|
|
|
cell.tunnel = tunnel
|
|
|
|
cell.onSwitchToggled = { [weak self] isOn in
|
|
|
|
guard let s = self, let tunnelsManager = s.tunnelsManager else { return }
|
|
|
|
if (isOn) {
|
2018-11-01 11:28:33 +00:00
|
|
|
tunnelsManager.startActivation(of: tunnel) { [weak s] error in
|
2018-10-29 18:54:50 +00:00
|
|
|
if let error = error {
|
2018-11-03 11:58:16 +00:00
|
|
|
ErrorPresenter.showErrorAlert(error: error, from: s, onPresented: {
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
cell.statusSwitch.isOn = false
|
|
|
|
}
|
|
|
|
})
|
2018-10-29 18:54:50 +00:00
|
|
|
}
|
2018-10-28 09:26:10 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-11-10 06:55:17 +00:00
|
|
|
tunnelsManager.startDeactivation(of: tunnel)
|
2018-10-28 09:26:10 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-17 08:17:57 +00:00
|
|
|
}
|
|
|
|
return cell
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-23 16:48:45 +00:00
|
|
|
// MARK: UITableViewDelegate
|
|
|
|
|
2018-11-03 03:20:27 +00:00
|
|
|
extension TunnelsListTableViewController: UITableViewDelegate {
|
|
|
|
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
2018-11-03 10:24:02 +00:00
|
|
|
tableView.deselectRow(at: indexPath, animated: true)
|
2018-10-23 16:48:45 +00:00
|
|
|
guard let tunnelsManager = tunnelsManager else { return }
|
2018-10-24 11:39:34 +00:00
|
|
|
let tunnel = tunnelsManager.tunnel(at: indexPath.row)
|
2018-10-23 16:48:45 +00:00
|
|
|
let tunnelDetailVC = TunnelDetailTableViewController(tunnelsManager: tunnelsManager,
|
2018-10-24 11:39:34 +00:00
|
|
|
tunnel: tunnel)
|
2018-10-25 06:41:05 +00:00
|
|
|
let tunnelDetailNC = UINavigationController(rootViewController: tunnelDetailVC)
|
|
|
|
showDetailViewController(tunnelDetailNC, sender: self) // Shall get propagated up to the split-vc
|
2018-10-23 16:48:45 +00:00
|
|
|
}
|
2018-11-01 17:34:56 +00:00
|
|
|
|
2018-11-03 03:20:27 +00:00
|
|
|
func tableView(_ tableView: UITableView,
|
2018-11-06 17:12:53 +00:00
|
|
|
trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
|
2018-11-01 17:34:56 +00:00
|
|
|
let deleteAction = UIContextualAction(style: .destructive, title: "Delete", handler: { [weak self] (_, _, completionHandler) in
|
|
|
|
guard let tunnelsManager = self?.tunnelsManager else { return }
|
|
|
|
let tunnel = tunnelsManager.tunnel(at: indexPath.row)
|
|
|
|
tunnelsManager.remove(tunnel: tunnel, completionHandler: { (error) in
|
|
|
|
if (error != nil) {
|
|
|
|
ErrorPresenter.showErrorAlert(error: error!, from: self)
|
|
|
|
completionHandler(false)
|
|
|
|
} else {
|
|
|
|
completionHandler(true)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
return UISwipeActionsConfiguration(actions: [deleteAction])
|
|
|
|
}
|
2018-10-23 16:48:45 +00:00
|
|
|
}
|
|
|
|
|
2018-10-23 12:11:37 +00:00
|
|
|
// MARK: TunnelsManagerDelegate
|
|
|
|
|
|
|
|
extension TunnelsListTableViewController: TunnelsManagerDelegate {
|
2018-10-25 10:20:27 +00:00
|
|
|
func tunnelAdded(at index: Int) {
|
2018-11-03 03:20:27 +00:00
|
|
|
tableView?.insertRows(at: [IndexPath(row: index, section: 0)], with: .automatic)
|
2018-11-03 04:52:05 +00:00
|
|
|
centeredAddButton?.isHidden = (tunnelsManager?.numberOfTunnels() ?? 0 > 0)
|
2018-10-25 10:20:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func tunnelModified(at index: Int) {
|
2018-11-03 03:20:27 +00:00
|
|
|
tableView?.reloadRows(at: [IndexPath(row: index, section: 0)], with: .automatic)
|
2018-10-25 10:20:27 +00:00
|
|
|
}
|
2018-11-03 03:20:27 +00:00
|
|
|
|
2018-11-03 02:40:23 +00:00
|
|
|
func tunnelMoved(at oldIndex: Int, to newIndex: Int) {
|
2018-11-03 03:20:27 +00:00
|
|
|
tableView?.moveRow(at: IndexPath(row: oldIndex, section: 0), to: IndexPath(row: newIndex, section: 0))
|
2018-10-23 12:11:37 +00:00
|
|
|
}
|
2018-11-03 18:35:25 +00:00
|
|
|
|
2018-10-28 23:25:50 +00:00
|
|
|
func tunnelRemoved(at index: Int) {
|
2018-11-03 03:20:27 +00:00
|
|
|
tableView?.deleteRows(at: [IndexPath(row: index, section: 0)], with: .automatic)
|
2018-11-03 04:52:05 +00:00
|
|
|
centeredAddButton?.isHidden = (tunnelsManager?.numberOfTunnels() ?? 0 > 0)
|
2018-10-28 23:25:50 +00:00
|
|
|
}
|
2018-10-23 12:11:37 +00:00
|
|
|
}
|
|
|
|
|
2018-10-17 08:17:57 +00:00
|
|
|
class TunnelsListTableViewCell: UITableViewCell {
|
|
|
|
static let id: String = "TunnelsListTableViewCell"
|
2018-10-28 09:26:10 +00:00
|
|
|
var tunnel: TunnelContainer? {
|
|
|
|
didSet(value) {
|
|
|
|
// Bind to the tunnel's name
|
|
|
|
nameLabel.text = tunnel?.name ?? ""
|
|
|
|
nameObservervationToken = tunnel?.observe(\.name) { [weak self] (tunnel, _) in
|
|
|
|
self?.nameLabel.text = tunnel.name
|
|
|
|
}
|
|
|
|
// Bind to the tunnel's status
|
|
|
|
update(from: tunnel?.status)
|
|
|
|
statusObservervationToken = tunnel?.observe(\.status) { [weak self] (tunnel, _) in
|
|
|
|
self?.update(from: tunnel.status)
|
|
|
|
}
|
|
|
|
}
|
2018-10-17 08:17:57 +00:00
|
|
|
}
|
2018-11-03 18:35:25 +00:00
|
|
|
var onSwitchToggled: ((Bool) -> Void)?
|
2018-10-28 09:26:10 +00:00
|
|
|
|
|
|
|
let nameLabel: UILabel
|
|
|
|
let busyIndicator: UIActivityIndicatorView
|
|
|
|
let statusSwitch: UISwitch
|
|
|
|
|
2018-11-03 18:35:25 +00:00
|
|
|
private var statusObservervationToken: AnyObject?
|
|
|
|
private var nameObservervationToken: AnyObject?
|
2018-10-17 08:17:57 +00:00
|
|
|
|
2018-11-05 05:31:25 +00:00
|
|
|
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
|
2018-10-28 09:26:10 +00:00
|
|
|
nameLabel = UILabel()
|
2018-11-05 05:31:25 +00:00
|
|
|
busyIndicator = UIActivityIndicatorView(style: .gray)
|
2018-10-28 09:26:10 +00:00
|
|
|
busyIndicator.hidesWhenStopped = true
|
|
|
|
statusSwitch = UISwitch()
|
2018-10-17 08:17:57 +00:00
|
|
|
super.init(style: style, reuseIdentifier: reuseIdentifier)
|
2018-10-28 09:26:10 +00:00
|
|
|
contentView.addSubview(statusSwitch)
|
|
|
|
statusSwitch.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
statusSwitch.centerYAnchor.constraint(equalTo: contentView.centerYAnchor),
|
|
|
|
statusSwitch.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: -8)
|
|
|
|
])
|
|
|
|
contentView.addSubview(busyIndicator)
|
|
|
|
busyIndicator.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
busyIndicator.centerYAnchor.constraint(equalTo: contentView.centerYAnchor),
|
|
|
|
busyIndicator.rightAnchor.constraint(equalTo: statusSwitch.leftAnchor, constant: -8)
|
|
|
|
])
|
|
|
|
contentView.addSubview(nameLabel)
|
|
|
|
nameLabel.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
nameLabel.centerYAnchor.constraint(equalTo: contentView.centerYAnchor),
|
|
|
|
nameLabel.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 16),
|
|
|
|
nameLabel.rightAnchor.constraint(equalTo: busyIndicator.leftAnchor)
|
|
|
|
])
|
2018-10-17 08:17:57 +00:00
|
|
|
self.accessoryType = .disclosureIndicator
|
2018-10-28 09:26:10 +00:00
|
|
|
|
|
|
|
statusSwitch.addTarget(self, action: #selector(switchToggled), for: .valueChanged)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func switchToggled() {
|
|
|
|
onSwitchToggled?(statusSwitch.isOn)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func update(from status: TunnelStatus?) {
|
|
|
|
guard let status = status else {
|
|
|
|
reset()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
DispatchQueue.main.async { [weak statusSwitch, weak busyIndicator] in
|
|
|
|
guard let statusSwitch = statusSwitch, let busyIndicator = busyIndicator else { return }
|
|
|
|
statusSwitch.isOn = !(status == .deactivating || status == .inactive)
|
|
|
|
statusSwitch.isUserInteractionEnabled = (status == .inactive || status == .active)
|
|
|
|
if (status == .inactive || status == .active) {
|
|
|
|
busyIndicator.stopAnimating()
|
|
|
|
} else {
|
|
|
|
busyIndicator.startAnimating()
|
|
|
|
}
|
|
|
|
}
|
2018-10-17 08:17:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder aDecoder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
|
2018-10-28 09:26:10 +00:00
|
|
|
private func reset() {
|
|
|
|
statusSwitch.isOn = false
|
|
|
|
statusSwitch.isUserInteractionEnabled = false
|
|
|
|
busyIndicator.stopAnimating()
|
|
|
|
}
|
|
|
|
|
2018-10-17 08:17:57 +00:00
|
|
|
override func prepareForReuse() {
|
|
|
|
super.prepareForReuse()
|
2018-10-28 09:26:10 +00:00
|
|
|
reset()
|
2018-10-13 01:38:30 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-03 04:52:05 +00:00
|
|
|
|
|
|
|
class BorderedTextButton: UIView {
|
|
|
|
let button: UIButton
|
|
|
|
|
|
|
|
override var intrinsicContentSize: CGSize {
|
|
|
|
let buttonSize = button.intrinsicContentSize
|
|
|
|
return CGSize(width: buttonSize.width + 32, height: buttonSize.height + 16)
|
|
|
|
}
|
|
|
|
|
|
|
|
var title: String {
|
|
|
|
get { return button.title(for: .normal) ?? "" }
|
|
|
|
set(value) { button.setTitle(value, for: .normal) }
|
|
|
|
}
|
|
|
|
|
2018-11-03 18:35:25 +00:00
|
|
|
var onTapped: (() -> Void)?
|
2018-11-03 04:52:05 +00:00
|
|
|
|
|
|
|
init() {
|
|
|
|
button = UIButton(type: .system)
|
|
|
|
super.init(frame: CGRect.zero)
|
|
|
|
addSubview(button)
|
|
|
|
button.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
button.centerXAnchor.constraint(equalTo: self.centerXAnchor),
|
|
|
|
button.centerYAnchor.constraint(equalTo: self.centerYAnchor)
|
|
|
|
])
|
|
|
|
layer.borderWidth = 1
|
|
|
|
layer.cornerRadius = 5
|
|
|
|
layer.borderColor = button.tintColor.cgColor
|
|
|
|
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func buttonTapped() {
|
|
|
|
onTapped?()
|
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder aDecoder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
}
|