2018-10-24 01:37:28 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-01-02 00:56:33 +00:00
|
|
|
// Copyright © 2018-2019 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
|
2020-11-09 09:52:06 +00:00
|
|
|
import WireGuardKit
|
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?
|
2018-10-17 08:17:57 +00:00
|
|
|
|
2019-03-18 12:47:34 +00:00
|
|
|
enum TableState: Equatable {
|
|
|
|
case normal
|
|
|
|
case rowSwiped
|
|
|
|
case multiSelect(selectionCount: Int)
|
|
|
|
}
|
|
|
|
|
2018-12-13 18:58:50 +00:00
|
|
|
let tableView: UITableView = {
|
|
|
|
let tableView = UITableView(frame: CGRect.zero, style: .plain)
|
|
|
|
tableView.estimatedRowHeight = 60
|
|
|
|
tableView.rowHeight = UITableView.automaticDimension
|
|
|
|
tableView.separatorStyle = .none
|
|
|
|
tableView.register(TunnelListCell.self)
|
|
|
|
return tableView
|
|
|
|
}()
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-13 18:58:50 +00:00
|
|
|
let centeredAddButton: BorderedTextButton = {
|
|
|
|
let button = BorderedTextButton()
|
2018-12-18 11:00:16 +00:00
|
|
|
button.title = tr("tunnelsListCenteredAddTunnelButtonTitle")
|
2018-12-13 18:58:50 +00:00
|
|
|
button.isHidden = true
|
|
|
|
return button
|
|
|
|
}()
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-13 18:58:50 +00:00
|
|
|
let busyIndicator: UIActivityIndicatorView = {
|
2019-10-14 21:43:56 +00:00
|
|
|
if #available(iOS 13.0, *) {
|
|
|
|
let busyIndicator = UIActivityIndicatorView(style: .medium)
|
|
|
|
busyIndicator.hidesWhenStopped = true
|
|
|
|
return busyIndicator
|
|
|
|
} else {
|
|
|
|
let busyIndicator = UIActivityIndicatorView(style: .gray)
|
|
|
|
busyIndicator.hidesWhenStopped = true
|
|
|
|
return busyIndicator
|
|
|
|
}
|
2018-12-13 18:58:50 +00:00
|
|
|
}()
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2019-01-23 10:15:24 +00:00
|
|
|
var detailDisplayedTunnel: TunnelContainer?
|
2019-03-18 12:47:34 +00:00
|
|
|
var tableState: TableState = .normal {
|
|
|
|
didSet {
|
|
|
|
handleTableStateChange()
|
|
|
|
}
|
|
|
|
}
|
2019-01-23 10:15:24 +00:00
|
|
|
|
2018-12-13 18:58:50 +00:00
|
|
|
override func loadView() {
|
|
|
|
view = UIView()
|
2019-10-25 08:59:16 +00:00
|
|
|
if #available(iOS 13.0, *) {
|
|
|
|
view.backgroundColor = .systemBackground
|
|
|
|
} else {
|
|
|
|
view.backgroundColor = .white
|
|
|
|
}
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-13 18:58:50 +00:00
|
|
|
tableView.dataSource = self
|
|
|
|
tableView.delegate = self
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-13 18:58:50 +00:00
|
|
|
view.addSubview(tableView)
|
|
|
|
tableView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
NSLayoutConstraint.activate([
|
2018-12-22 02:37:22 +00:00
|
|
|
tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
|
|
|
|
tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
|
2018-12-13 18:58:50 +00:00
|
|
|
tableView.topAnchor.constraint(equalTo: view.topAnchor),
|
|
|
|
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
|
|
|
|
])
|
2018-11-03 03:20:27 +00:00
|
|
|
|
|
|
|
view.addSubview(busyIndicator)
|
|
|
|
busyIndicator.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
busyIndicator.centerXAnchor.constraint(equalTo: view.centerXAnchor),
|
|
|
|
busyIndicator.centerYAnchor.constraint(equalTo: view.centerYAnchor)
|
2018-12-12 21:33:14 +00:00
|
|
|
])
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-13 18:58:50 +00:00
|
|
|
view.addSubview(centeredAddButton)
|
2018-12-03 13:21:51 +00:00
|
|
|
centeredAddButton.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
NSLayoutConstraint.activate([
|
2018-12-13 18:58:50 +00:00
|
|
|
centeredAddButton.centerXAnchor.constraint(equalTo: view.centerXAnchor),
|
|
|
|
centeredAddButton.centerYAnchor.constraint(equalTo: view.centerYAnchor)
|
2018-12-12 21:33:14 +00:00
|
|
|
])
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-03 13:21:51 +00:00
|
|
|
centeredAddButton.onTapped = { [weak self] in
|
2018-12-13 18:58:50 +00:00
|
|
|
guard let self = self else { return }
|
|
|
|
self.addButtonTapped(sender: self.centeredAddButton)
|
2018-10-17 08:17:57 +00:00
|
|
|
}
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-13 18:58:50 +00:00
|
|
|
busyIndicator.startAnimating()
|
|
|
|
}
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-13 18:58:50 +00:00
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
2018-12-03 13:21:51 +00:00
|
|
|
|
2019-03-18 12:47:34 +00:00
|
|
|
tableState = .normal
|
2018-12-13 18:58:50 +00:00
|
|
|
restorationIdentifier = "TunnelsListVC"
|
|
|
|
}
|
2018-12-03 13:21:51 +00:00
|
|
|
|
2019-03-18 12:47:34 +00:00
|
|
|
func handleTableStateChange() {
|
|
|
|
switch tableState {
|
|
|
|
case .normal:
|
|
|
|
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addButtonTapped(sender:)))
|
|
|
|
navigationItem.leftBarButtonItem = UIBarButtonItem(title: tr("tunnelsListSettingsButtonTitle"), style: .plain, target: self, action: #selector(settingsButtonTapped(sender:)))
|
|
|
|
case .rowSwiped:
|
|
|
|
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(doneButtonTapped))
|
|
|
|
navigationItem.leftBarButtonItem = UIBarButtonItem(title: tr("tunnelsListSelectButtonTitle"), style: .plain, target: self, action: #selector(selectButtonTapped))
|
|
|
|
case .multiSelect(let selectionCount):
|
|
|
|
if selectionCount > 0 {
|
|
|
|
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(cancelButtonTapped))
|
|
|
|
navigationItem.leftBarButtonItem = UIBarButtonItem(title: tr("tunnelsListDeleteButtonTitle"), style: .plain, target: self, action: #selector(deleteButtonTapped(sender:)))
|
|
|
|
} else {
|
|
|
|
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(cancelButtonTapped))
|
|
|
|
navigationItem.leftBarButtonItem = UIBarButtonItem(title: tr("tunnelsListSelectAllButtonTitle"), style: .plain, target: self, action: #selector(selectAllButtonTapped))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if case .multiSelect(let selectionCount) = tableState, selectionCount > 0 {
|
|
|
|
navigationItem.title = tr(format: "tunnelsListSelectedTitle (%d)", selectionCount)
|
|
|
|
} else {
|
|
|
|
navigationItem.title = tr("tunnelsListTitle")
|
|
|
|
}
|
|
|
|
if case .multiSelect = tableState {
|
|
|
|
tableView.allowsMultipleSelectionDuringEditing = true
|
|
|
|
} else {
|
|
|
|
tableView.allowsMultipleSelectionDuringEditing = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-13 18:58:50 +00:00
|
|
|
func setTunnelsManager(tunnelsManager: TunnelsManager) {
|
2018-12-03 13:21:51 +00:00
|
|
|
self.tunnelsManager = tunnelsManager
|
|
|
|
tunnelsManager.tunnelsListDelegate = self
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-13 18:58:50 +00:00
|
|
|
busyIndicator.stopAnimating()
|
|
|
|
tableView.reloadData()
|
|
|
|
centeredAddButton.isHidden = tunnelsManager.numberOfTunnels() > 0
|
2018-10-13 01:38:30 +00:00
|
|
|
}
|
|
|
|
|
2018-12-07 06:19:10 +00:00
|
|
|
override func viewWillAppear(_: Bool) {
|
2018-12-13 18:58:50 +00:00
|
|
|
if let selectedRowIndexPath = tableView.indexPathForSelectedRow {
|
2018-12-07 06:19:10 +00:00
|
|
|
tableView.deselectRow(at: selectedRowIndexPath, animated: false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-03 04:52:05 +00:00
|
|
|
@objc func addButtonTapped(sender: AnyObject) {
|
2018-12-14 23:12:59 +00:00
|
|
|
guard tunnelsManager != nil else { return }
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-18 11:00:16 +00:00
|
|
|
let alert = UIAlertController(title: "", message: tr("addTunnelMenuHeader"), preferredStyle: .actionSheet)
|
|
|
|
let importFileAction = UIAlertAction(title: tr("addTunnelMenuImportFile"), style: .default) { [weak self] _ in
|
2018-10-25 09:05:23 +00:00
|
|
|
self?.presentViewControllerForFileImport()
|
|
|
|
}
|
|
|
|
alert.addAction(importFileAction)
|
|
|
|
|
2018-12-18 11:00:16 +00:00
|
|
|
let scanQRCodeAction = UIAlertAction(title: tr("addTunnelMenuQRCode"), style: .default) { [weak self] _ in
|
2018-10-28 16:52:27 +00:00
|
|
|
self?.presentViewControllerForScanningQRCode()
|
|
|
|
}
|
|
|
|
alert.addAction(scanQRCodeAction)
|
|
|
|
|
2018-12-18 11:00:16 +00:00
|
|
|
let createFromScratchAction = UIAlertAction(title: tr("addTunnelMenuFromScratch"), style: .default) { [weak self] _ in
|
2018-12-12 17:40:57 +00:00
|
|
|
if let self = self, let tunnelsManager = self.tunnelsManager {
|
2018-12-19 11:00:34 +00:00
|
|
|
self.presentViewControllerForTunnelCreation(tunnelsManager: tunnelsManager)
|
2018-10-17 10:03:06 +00:00
|
|
|
}
|
2018-10-25 09:05:23 +00:00
|
|
|
}
|
|
|
|
alert.addAction(createFromScratchAction)
|
|
|
|
|
2018-12-18 11:00:16 +00:00
|
|
|
let cancelAction = UIAlertAction(title: tr("actionCancel"), style: .cancel)
|
2018-10-25 09:05:23 +00:00
|
|
|
alert.addAction(cancelAction)
|
|
|
|
|
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-12-14 23:12:59 +00:00
|
|
|
present(alert, animated: true, completion: nil)
|
2018-10-13 01:38:30 +00:00
|
|
|
}
|
2018-10-25 05:40:18 +00:00
|
|
|
|
2018-12-20 17:22:37 +00:00
|
|
|
@objc func settingsButtonTapped(sender: UIBarButtonItem) {
|
2018-12-14 23:12:59 +00:00
|
|
|
guard tunnelsManager != nil else { return }
|
2018-12-21 22:34:56 +00:00
|
|
|
|
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
|
2018-12-14 23:12:59 +00:00
|
|
|
present(settingsNC, animated: true)
|
2018-10-29 12:04:02 +00:00
|
|
|
}
|
|
|
|
|
2018-12-19 11:00:34 +00:00
|
|
|
func presentViewControllerForTunnelCreation(tunnelsManager: TunnelsManager) {
|
|
|
|
let editVC = TunnelEditTableViewController(tunnelsManager: tunnelsManager)
|
2018-10-25 05:40:18 +00:00
|
|
|
let editNC = UINavigationController(rootViewController: editVC)
|
2019-10-15 12:51:04 +00:00
|
|
|
editNC.modalPresentationStyle = .fullScreen
|
2018-12-14 23:12:59 +00:00
|
|
|
present(editNC, animated: true)
|
2018-10-25 05:40:18 +00:00
|
|
|
}
|
|
|
|
|
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
|
2018-12-14 23:12:59 +00:00
|
|
|
present(filePicker, animated: true)
|
2018-10-25 09:05:23 +00:00
|
|
|
}
|
|
|
|
|
2018-10-28 16:52:27 +00:00
|
|
|
func presentViewControllerForScanningQRCode() {
|
|
|
|
let scanQRCodeVC = QRScanViewController()
|
|
|
|
scanQRCodeVC.delegate = self
|
|
|
|
let scanQRCodeNC = UINavigationController(rootViewController: scanQRCodeVC)
|
|
|
|
scanQRCodeNC.modalPresentationStyle = .fullScreen
|
2018-12-14 23:12:59 +00:00
|
|
|
present(scanQRCodeNC, animated: true)
|
2018-10-28 16:52:27 +00:00
|
|
|
}
|
2019-03-18 12:47:34 +00:00
|
|
|
|
|
|
|
@objc func selectButtonTapped() {
|
|
|
|
let shouldCancelSwipe = tableState == .rowSwiped
|
|
|
|
tableState = .multiSelect(selectionCount: 0)
|
|
|
|
if shouldCancelSwipe {
|
|
|
|
tableView.setEditing(false, animated: false)
|
|
|
|
}
|
|
|
|
tableView.setEditing(true, animated: true)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func doneButtonTapped() {
|
|
|
|
tableState = .normal
|
|
|
|
tableView.setEditing(false, animated: true)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func selectAllButtonTapped() {
|
|
|
|
guard tableView.isEditing else { return }
|
|
|
|
guard let tunnelsManager = tunnelsManager else { return }
|
|
|
|
for index in 0 ..< tunnelsManager.numberOfTunnels() {
|
|
|
|
tableView.selectRow(at: IndexPath(row: index, section: 0), animated: false, scrollPosition: .none)
|
|
|
|
}
|
|
|
|
tableState = .multiSelect(selectionCount: tableView.indexPathsForSelectedRows?.count ?? 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func cancelButtonTapped() {
|
|
|
|
tableState = .normal
|
|
|
|
tableView.setEditing(false, animated: true)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func deleteButtonTapped(sender: AnyObject?) {
|
|
|
|
guard let sender = sender as? UIBarButtonItem else { return }
|
|
|
|
guard let tunnelsManager = tunnelsManager else { return }
|
|
|
|
|
|
|
|
let selectedTunnelIndices = tableView.indexPathsForSelectedRows?.map { $0.row } ?? []
|
|
|
|
let selectedTunnels = selectedTunnelIndices.compactMap { tunnelIndex in
|
|
|
|
tunnelIndex >= 0 && tunnelIndex < tunnelsManager.numberOfTunnels() ? tunnelsManager.tunnel(at: tunnelIndex) : nil
|
|
|
|
}
|
|
|
|
guard !selectedTunnels.isEmpty else { return }
|
|
|
|
let message = selectedTunnels.count == 1 ?
|
|
|
|
tr(format: "deleteTunnelConfirmationAlertButtonMessage (%d)", selectedTunnels.count) :
|
|
|
|
tr(format: "deleteTunnelsConfirmationAlertButtonMessage (%d)", selectedTunnels.count)
|
|
|
|
let title = tr("deleteTunnelsConfirmationAlertButtonTitle")
|
2019-03-18 19:54:06 +00:00
|
|
|
ConfirmationAlertPresenter.showConfirmationAlert(message: message, buttonTitle: title,
|
|
|
|
from: sender, presentingVC: self) { [weak self] in
|
2019-03-18 12:47:34 +00:00
|
|
|
self?.tunnelsManager?.removeMultiple(tunnels: selectedTunnels) { [weak self] error in
|
|
|
|
guard let self = self else { return }
|
|
|
|
if let error = error {
|
|
|
|
ErrorPresenter.showErrorAlert(error: error, from: self)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
self.tableState = .normal
|
|
|
|
self.tableView.setEditing(false, animated: true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-18 21:34:23 +00:00
|
|
|
|
|
|
|
func showTunnelDetail(for tunnel: TunnelContainer, animated: Bool) {
|
|
|
|
guard let tunnelsManager = tunnelsManager else { return }
|
|
|
|
guard let splitViewController = splitViewController else { return }
|
2019-05-18 22:35:44 +00:00
|
|
|
guard let navController = navigationController else { return }
|
2019-05-18 21:34:23 +00:00
|
|
|
|
2019-05-27 10:06:34 +00:00
|
|
|
let tunnelDetailVC = TunnelDetailTableViewController(tunnelsManager: tunnelsManager,
|
|
|
|
tunnel: tunnel)
|
|
|
|
let tunnelDetailNC = UINavigationController(rootViewController: tunnelDetailVC)
|
|
|
|
tunnelDetailNC.restorationIdentifier = "DetailNC"
|
|
|
|
if splitViewController.isCollapsed && navController.viewControllers.count > 1 {
|
|
|
|
navController.setViewControllers([self, tunnelDetailNC], animated: animated)
|
|
|
|
} else {
|
|
|
|
splitViewController.showDetailViewController(tunnelDetailNC, sender: self, animated: animated)
|
2019-05-18 21:34:23 +00:00
|
|
|
}
|
2019-05-27 10:06:34 +00:00
|
|
|
detailDisplayedTunnel = tunnel
|
2019-05-18 22:35:44 +00:00
|
|
|
self.presentedViewController?.dismiss(animated: false, completion: nil)
|
2019-05-18 21:34:23 +00:00
|
|
|
}
|
2018-10-13 01:38:30 +00:00
|
|
|
}
|
|
|
|
|
2018-10-25 09:05:23 +00:00
|
|
|
extension TunnelsListTableViewController: UIDocumentPickerDelegate {
|
|
|
|
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
|
2018-12-28 20:06:46 +00:00
|
|
|
guard let tunnelsManager = tunnelsManager else { return }
|
2019-02-25 10:52:52 +00:00
|
|
|
TunnelImporter.importFromFile(urls: urls, into: tunnelsManager, sourceVC: self, errorPresenterType: ErrorPresenter.self)
|
2018-10-25 09:05:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-28 16:52:27 +00:00
|
|
|
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-12-06 10:28:27 +00:00
|
|
|
tunnelsManager?.add(tunnelConfiguration: tunnelConfiguration) { result in
|
2019-04-08 07:52:06 +00:00
|
|
|
switch result {
|
|
|
|
case .failure(let error):
|
2018-10-31 20:06:28 +00:00
|
|
|
ErrorPresenter.showErrorAlert(error: error, from: qrScanViewController, onDismissal: completionHandler)
|
2019-04-08 07:52:06 +00:00
|
|
|
case .success:
|
2018-10-31 20:06:28 +00:00
|
|
|
completionHandler?()
|
2018-10-28 18:02:15 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-28 16:52:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-12-13 18:58:50 +00:00
|
|
|
let cell: TunnelListCell = tableView.dequeueReusableCell(for: indexPath)
|
2018-10-17 08:17:57 +00:00
|
|
|
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
|
2018-12-12 17:40:57 +00:00
|
|
|
guard let self = self, let tunnelsManager = self.tunnelsManager else { return }
|
2018-12-12 18:28:27 +00:00
|
|
|
if isOn {
|
2018-12-13 13:25:20 +00:00
|
|
|
tunnelsManager.startActivation(of: tunnel)
|
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-11-03 03:20:27 +00:00
|
|
|
extension TunnelsListTableViewController: UITableViewDelegate {
|
|
|
|
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
2019-03-18 12:47:34 +00:00
|
|
|
guard !tableView.isEditing else {
|
|
|
|
tableState = .multiSelect(selectionCount: tableView.indexPathsForSelectedRows?.count ?? 0)
|
|
|
|
return
|
|
|
|
}
|
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)
|
2019-05-18 21:34:23 +00:00
|
|
|
showTunnelDetail(for: tunnel, animated: true)
|
2018-10-23 16:48:45 +00:00
|
|
|
}
|
2018-11-01 17:34:56 +00:00
|
|
|
|
2019-03-18 12:47:34 +00:00
|
|
|
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
|
|
|
|
guard !tableView.isEditing else {
|
|
|
|
tableState = .multiSelect(selectionCount: tableView.indexPathsForSelectedRows?.count ?? 0)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-12-18 11:00:16 +00:00
|
|
|
let deleteAction = UIContextualAction(style: .destructive, title: tr("tunnelsListSwipeDeleteButtonTitle")) { [weak self] _, _, completionHandler in
|
2018-11-01 17:34:56 +00:00
|
|
|
guard let tunnelsManager = self?.tunnelsManager else { return }
|
|
|
|
let tunnel = tunnelsManager.tunnel(at: indexPath.row)
|
2018-12-12 21:33:14 +00:00
|
|
|
tunnelsManager.remove(tunnel: tunnel) { error in
|
2018-12-12 18:28:27 +00:00
|
|
|
if error != nil {
|
2018-11-01 17:34:56 +00:00
|
|
|
ErrorPresenter.showErrorAlert(error: error!, from: self)
|
|
|
|
completionHandler(false)
|
|
|
|
} else {
|
|
|
|
completionHandler(true)
|
|
|
|
}
|
2018-12-12 21:33:14 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-01 17:34:56 +00:00
|
|
|
return UISwipeActionsConfiguration(actions: [deleteAction])
|
|
|
|
}
|
2019-03-18 12:47:34 +00:00
|
|
|
|
|
|
|
func tableView(_ tableView: UITableView, willBeginEditingRowAt indexPath: IndexPath) {
|
|
|
|
if tableState == .normal {
|
|
|
|
tableState = .rowSwiped
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func tableView(_ tableView: UITableView, didEndEditingRowAt indexPath: IndexPath?) {
|
|
|
|
if tableState == .rowSwiped {
|
|
|
|
tableState = .normal
|
|
|
|
}
|
|
|
|
}
|
2018-10-23 16:48:45 +00:00
|
|
|
}
|
|
|
|
|
2018-12-03 13:21:51 +00:00
|
|
|
extension TunnelsListTableViewController: TunnelsManagerListDelegate {
|
2018-10-25 10:20:27 +00:00
|
|
|
func tunnelAdded(at index: Int) {
|
2018-12-13 18:58:50 +00:00
|
|
|
tableView.insertRows(at: [IndexPath(row: index, section: 0)], with: .automatic)
|
|
|
|
centeredAddButton.isHidden = (tunnelsManager?.numberOfTunnels() ?? 0 > 0)
|
2018-10-25 10:20:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func tunnelModified(at index: Int) {
|
2018-12-13 18:58:50 +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-12-12 18:28:27 +00:00
|
|
|
func tunnelMoved(from oldIndex: Int, to newIndex: Int) {
|
2018-12-13 18:58:50 +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
|
|
|
|
2019-01-23 10:15:24 +00:00
|
|
|
func tunnelRemoved(at index: Int, tunnel: TunnelContainer) {
|
2018-12-13 18:58:50 +00:00
|
|
|
tableView.deleteRows(at: [IndexPath(row: index, section: 0)], with: .automatic)
|
|
|
|
centeredAddButton.isHidden = tunnelsManager?.numberOfTunnels() ?? 0 > 0
|
2019-01-23 10:15:24 +00:00
|
|
|
if detailDisplayedTunnel == tunnel, let splitViewController = splitViewController {
|
2019-01-23 07:21:05 +00:00
|
|
|
if splitViewController.isCollapsed != false {
|
2019-01-23 10:15:24 +00:00
|
|
|
(splitViewController.viewControllers[0] as? UINavigationController)?.popToRootViewController(animated: false)
|
2019-01-23 07:21:05 +00:00
|
|
|
} else {
|
|
|
|
let detailVC = UIViewController()
|
2019-10-25 08:59:16 +00:00
|
|
|
if #available(iOS 13.0, *) {
|
|
|
|
detailVC.view.backgroundColor = .systemBackground
|
|
|
|
} else {
|
|
|
|
detailVC.view.backgroundColor = .white
|
|
|
|
}
|
2019-01-23 07:21:05 +00:00
|
|
|
let detailNC = UINavigationController(rootViewController: detailVC)
|
|
|
|
splitViewController.showDetailViewController(detailNC, sender: self)
|
|
|
|
}
|
2019-01-23 10:15:24 +00:00
|
|
|
detailDisplayedTunnel = nil
|
2019-01-23 07:21:05 +00:00
|
|
|
if let presentedNavController = self.presentedViewController as? UINavigationController, presentedNavController.viewControllers.first is TunnelEditTableViewController {
|
|
|
|
self.presentedViewController?.dismiss(animated: false, completion: nil)
|
|
|
|
}
|
|
|
|
}
|
2018-11-03 04:52:05 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-18 22:35:44 +00:00
|
|
|
|
|
|
|
extension UISplitViewController {
|
2020-12-02 14:08:45 +00:00
|
|
|
func showDetailViewController(_ viewController: UIViewController, sender: Any?, animated: Bool) {
|
2019-05-18 22:35:44 +00:00
|
|
|
if animated {
|
2020-12-02 14:08:45 +00:00
|
|
|
showDetailViewController(viewController, sender: sender)
|
2019-05-18 22:35:44 +00:00
|
|
|
} else {
|
|
|
|
UIView.performWithoutAnimation {
|
2020-12-02 14:08:45 +00:00
|
|
|
showDetailViewController(viewController, sender: sender)
|
2019-05-18 22:35:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|