wireguard-apple/WireGuard/ViewControllers/TunnelsTableViewController....

289 lines
10 KiB
Swift
Raw Normal View History

2018-05-23 19:49:10 +00:00
//
// Copyright © 2018 WireGuard LLC. All rights reserved.
2018-05-23 19:49:10 +00:00
//
import UIKit
2018-09-15 19:24:52 +00:00
import os.log
2018-05-23 19:49:10 +00:00
import CoreData
2018-09-15 19:24:52 +00:00
2018-05-23 19:49:10 +00:00
import BNRCoreDataStack
2018-08-21 20:27:26 +00:00
import NetworkExtension
2018-05-23 19:49:10 +00:00
2018-05-26 07:17:36 +00:00
protocol TunnelsTableViewControllerDelegate: class {
func addProvider(tunnelsTableViewController: TunnelsTableViewController)
func connect(tunnel: Tunnel, tunnelsTableViewController: TunnelsTableViewController)
2018-08-21 20:27:26 +00:00
func disconnect(tunnel: Tunnel, tunnelsTableViewController: TunnelsTableViewController)
2018-09-15 19:24:52 +00:00
func info(tunnel: Tunnel, tunnelsTableViewController: TunnelsTableViewController)
2018-05-26 07:17:36 +00:00
func delete(tunnel: Tunnel, tunnelsTableViewController: TunnelsTableViewController)
2018-08-21 20:27:26 +00:00
func status(for tunnel: Tunnel, tunnelsTableViewController: TunnelsTableViewController) -> NEVPNStatus
func showSettings()
2018-05-23 19:49:10 +00:00
}
2018-05-26 07:17:36 +00:00
class TunnelsTableViewController: UITableViewController {
weak var delegate: TunnelsTableViewControllerDelegate?
2018-05-23 19:49:10 +00:00
var viewContext: NSManagedObjectContext!
@IBOutlet var settingsButton: UIBarButtonItem!
@IBOutlet var editButton: UIBarButtonItem!
@IBOutlet var doneButton: UIBarButtonItem!
2018-05-26 07:17:36 +00:00
private lazy var fetchedResultsController: FetchedResultsController<Tunnel> = {
let fetchRequest = NSFetchRequest<Tunnel>()
fetchRequest.entity = Tunnel.entity()
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "title", ascending: true)]
2018-05-26 07:17:36 +00:00
let frc = FetchedResultsController<Tunnel>(fetchRequest: fetchRequest,
managedObjectContext: viewContext)
frc.setDelegate(self.frcDelegate)
return frc
}()
2018-08-21 20:27:26 +00:00
public func updateStatus(for tunnelIdentifier: String) {
viewContext.perform {
2018-09-15 19:24:52 +00:00
do {
let tunnel = try Tunnel.findFirstInContext(self.viewContext, predicate: NSPredicate(format: "tunnelIdentifier == %@", tunnelIdentifier))
if let tunnel = tunnel {
if let indexPath = self.fetchedResultsController.indexPathForObject(tunnel) {
2018-09-29 20:29:34 +00:00
self.tableView.reloadRows(at: [indexPath], with: .none)
2018-09-15 19:24:52 +00:00
}
2018-08-21 20:27:26 +00:00
}
2018-09-15 19:24:52 +00:00
} catch {
os_log("Unable to load tunnel for tunnel identifier: %{public}@", log: Log.general, type: .error, error.localizedDescription)
2018-08-21 20:27:26 +00:00
}
}
}
2018-05-26 07:17:36 +00:00
private lazy var frcDelegate: TunnelFetchedResultsControllerDelegate = { // swiftlint:disable:this weak_delegate
return TunnelFetchedResultsControllerDelegate(tableView: self.tableView)
}()
2018-05-23 19:49:10 +00:00
override func viewDidLoad() {
super.viewDidLoad()
do {
try fetchedResultsController.performFetch()
} catch {
print("Failed to fetch objects: \(error)")
}
// Get rid of seperator lines in table.
tableView.tableFooterView = UIView(frame: CGRect.zero)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
updateBarButtons()
}
@IBAction func editTunnels(_ sender: Any) {
tableView.setEditing(!tableView.isEditing, animated: true)
updateBarButtons()
}
private func updateBarButtons() {
navigationController?.setToolbarHidden(tableView.isEditing, animated: true)
if tableView.isEditing {
self.navigationItem.setRightBarButtonItems([doneButton], animated: true)
} else {
self.navigationItem.setRightBarButtonItems([settingsButton, editButton], animated: true)
}
}
@IBAction func showSettings(_ sender: Any) {
delegate?.showSettings()
2018-09-02 20:31:39 +00:00
}
@IBAction func addProvider(_ sender: UIBarButtonItem) {
2018-05-26 07:17:36 +00:00
delegate?.addProvider(tunnelsTableViewController: self)
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return fetchedResultsController.sections?[0].objects.count ?? 0
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
2018-05-26 07:17:36 +00:00
let cell = tableView.dequeueReusableCell(type: TunnelTableViewCell.self, for: indexPath)
2018-08-21 20:27:26 +00:00
cell.delegate = self
guard let sections = fetchedResultsController.sections else {
fatalError("FetchedResultsController \(fetchedResultsController) should have sections, but found nil")
}
let section = sections[indexPath.section]
2018-05-26 07:17:36 +00:00
let tunnel = section.objects[indexPath.row]
2018-08-21 20:27:26 +00:00
cell.configure(tunnel: tunnel, status: delegate?.status(for: tunnel, tunnelsTableViewController: self) ?? .invalid)
return cell
2018-05-23 19:49:10 +00:00
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let sections = fetchedResultsController.sections else {
fatalError("FetchedResultsController \(fetchedResultsController) should have sections, but found nil")
}
let section = sections[indexPath.section]
2018-05-26 07:17:36 +00:00
let tunnel = section.objects[indexPath.row]
2018-09-15 19:24:52 +00:00
delegate?.info(tunnel: tunnel, tunnelsTableViewController: self)
tableView.deselectRow(at: indexPath, animated: true)
2018-05-23 19:49:10 +00:00
}
override func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {
guard let sections = fetchedResultsController.sections else {
fatalError("FetchedResultsController \(fetchedResultsController) should have sections, but found nil")
}
let section = sections[indexPath.section]
2018-05-26 07:17:36 +00:00
let tunnel = section.objects[indexPath.row]
2018-09-15 19:24:52 +00:00
delegate?.info(tunnel: tunnel, tunnelsTableViewController: self)
}
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
2018-10-02 16:12:30 +00:00
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
guard let sections = fetchedResultsController.sections else {
fatalError("FetchedResultsController \(fetchedResultsController) should have sections, but found nil")
}
let section = sections[indexPath.section]
2018-05-26 07:17:36 +00:00
let tunnel = section.objects[indexPath.row]
2018-05-26 07:17:36 +00:00
delegate?.delete(tunnel: tunnel, tunnelsTableViewController: self)
}
}
2018-05-23 19:49:10 +00:00
}
2018-08-21 20:27:26 +00:00
extension TunnelsTableViewController: TunnelTableViewCellDelegate {
func connect(tunnelIdentifier: String) {
let tunnel = try? Tunnel.findFirstInContext(self.viewContext, predicate: NSPredicate(format: "tunnelIdentifier == %@", tunnelIdentifier))
if let tunnel = tunnel {
self.delegate?.connect(tunnel: tunnel!, tunnelsTableViewController: self)
}
}
func disconnect(tunnelIdentifier: String) {
let tunnel = try? Tunnel.findFirstInContext(self.viewContext, predicate: NSPredicate(format: "tunnelIdentifier == %@", tunnelIdentifier))
if let tunnel = tunnel {
self.delegate?.disconnect(tunnel: tunnel!, tunnelsTableViewController: self)
}
}
}
2018-05-26 07:17:36 +00:00
extension TunnelsTableViewController: Identifyable {}
2018-05-26 07:17:36 +00:00
class TunnelFetchedResultsControllerDelegate: NSObject, FetchedResultsControllerDelegate {
private weak var tableView: UITableView?
// MARK: - Lifecycle
init(tableView: UITableView) {
self.tableView = tableView
}
2018-05-26 07:17:36 +00:00
func fetchedResultsControllerDidPerformFetch(_ controller: FetchedResultsController<Tunnel>) {
tableView?.reloadData()
}
2018-05-26 07:17:36 +00:00
func fetchedResultsControllerWillChangeContent(_ controller: FetchedResultsController<Tunnel>) {
tableView?.beginUpdates()
}
2018-05-26 07:17:36 +00:00
func fetchedResultsControllerDidChangeContent(_ controller: FetchedResultsController<Tunnel>) {
tableView?.endUpdates()
}
2018-05-26 07:17:36 +00:00
func fetchedResultsController(_ controller: FetchedResultsController<Tunnel>, didChangeObject change: FetchedResultsObjectChange<Tunnel>) {
guard let tableView = tableView else { return }
switch change {
case let .insert(_, indexPath):
tableView.insertRows(at: [indexPath], with: .automatic)
case let .delete(_, indexPath):
tableView.deleteRows(at: [indexPath], with: .automatic)
case let .move(_, fromIndexPath, toIndexPath):
tableView.moveRow(at: fromIndexPath, to: toIndexPath)
case let .update(_, indexPath):
tableView.reloadRows(at: [indexPath], with: .automatic)
}
}
2018-05-26 07:17:36 +00:00
func fetchedResultsController(_ controller: FetchedResultsController<Tunnel>, didChangeSection change: FetchedResultsSectionChange<Tunnel>) {
guard let tableView = tableView else { return }
switch change {
case let .insert(_, index):
tableView.insertSections(IndexSet(integer: index), with: .automatic)
case let .delete(_, index):
tableView.deleteSections(IndexSet(integer: index), with: .automatic)
}
}
}
2018-08-21 20:27:26 +00:00
protocol TunnelTableViewCellDelegate: class {
func connect(tunnelIdentifier: String)
func disconnect(tunnelIdentifier: String)
}
2018-05-26 07:17:36 +00:00
class TunnelTableViewCell: UITableViewCell {
2018-08-21 20:27:26 +00:00
@IBOutlet weak var tunnelTitleLabel: UILabel!
@IBOutlet weak var activityIndicator: UIActivityIndicatorView!
@IBOutlet weak var tunnelSwitch: UISwitch!
2018-08-21 20:27:26 +00:00
weak var delegate: TunnelTableViewCellDelegate?
private var tunnelIdentifier: String?
@IBAction func tunnelSwitchChanged(_ sender: Any) {
tunnelSwitch.isEnabled = false
2018-08-21 20:27:26 +00:00
guard let tunnelIdentifier = tunnelIdentifier else {
return
}
if tunnelSwitch.isOn {
delegate?.connect(tunnelIdentifier: tunnelIdentifier)
} else {
delegate?.disconnect(tunnelIdentifier: tunnelIdentifier)
}
}
func configure(tunnel: Tunnel, status: NEVPNStatus) {
self.tunnelTitleLabel?.text = tunnel.title
tunnelIdentifier = tunnel.tunnelIdentifier
if status == .connecting || status == .disconnecting || status == .reasserting {
2018-08-21 20:27:26 +00:00
activityIndicator.startAnimating()
tunnelSwitch.isHidden = true
} else {
2018-08-21 20:27:26 +00:00
activityIndicator.stopAnimating()
tunnelSwitch.isHidden = false
2018-08-21 20:27:26 +00:00
}
tunnelSwitch.isOn = status == .connected
tunnelSwitch.onTintColor = status == .invalid || status == .reasserting ? .gray : .green
tunnelSwitch.isEnabled = true
2018-08-21 20:27:26 +00:00
}
override func setEditing(_ editing: Bool, animated: Bool) {
super.setEditing(editing, animated: animated)
tunnelSwitch.isHidden = editing
}
}
2018-05-26 07:17:36 +00:00
extension TunnelTableViewCell: Identifyable {}