aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/ViewControllers/TunnelsTableViewController.swift
blob: 5f7f15f51573f844eeeb6644ca7e586088f3e9a5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
//
//  TunnelsTableViewController.swift
//  WireGuard
//
//  Created by Jeroen Leenarts on 23-05-18.
//  Copyright © 2018 WireGuard LLC. All rights reserved.
//

import UIKit

import CoreData
import BNRCoreDataStack
import NetworkExtension

protocol TunnelsTableViewControllerDelegate: class {
    func exportTunnels(tunnelsTableViewController: TunnelsTableViewController, barButtonItem: UIBarButtonItem)
    func addProvider(tunnelsTableViewController: TunnelsTableViewController)
    func connect(tunnel: Tunnel, tunnelsTableViewController: TunnelsTableViewController)
    func disconnect(tunnel: Tunnel, tunnelsTableViewController: TunnelsTableViewController)
    func configure(tunnel: Tunnel, tunnelsTableViewController: TunnelsTableViewController)
    func delete(tunnel: Tunnel, tunnelsTableViewController: TunnelsTableViewController)
    func status(for tunnel: Tunnel, tunnelsTableViewController: TunnelsTableViewController) -> NEVPNStatus
}

class TunnelsTableViewController: UITableViewController {
    weak var delegate: TunnelsTableViewControllerDelegate?

    var viewContext: NSManagedObjectContext!

    private lazy var fetchedResultsController: FetchedResultsController<Tunnel> = {
        let fetchRequest = NSFetchRequest<Tunnel>()
        fetchRequest.entity = Tunnel.entity()
        fetchRequest.sortDescriptors = [NSSortDescriptor(key: "title", ascending: true)]
        let frc = FetchedResultsController<Tunnel>(fetchRequest: fetchRequest,
                                                    managedObjectContext: viewContext)
        frc.setDelegate(self.frcDelegate)
        return frc
    }()

    public func updateStatus(for tunnelIdentifier: String) {
        viewContext.perform {
            let tunnel = try? Tunnel.findFirstInContext(self.viewContext, predicate: NSPredicate(format: "tunnelIdentifier == %@", tunnelIdentifier))
            if let tunnel = tunnel {
                if let indexPath = self.fetchedResultsController.indexPathForObject(tunnel!) {
                    self.tableView.reloadRows(at: [indexPath], with: UITableViewRowAnimation.none)
                }
            }
        }
    }

    private lazy var frcDelegate: TunnelFetchedResultsControllerDelegate = { // swiftlint:disable:this weak_delegate
        return TunnelFetchedResultsControllerDelegate(tableView: self.tableView)
    }()

    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)
    }

    @IBAction func exportTunnels(_ sender: UIBarButtonItem) {
        delegate?.exportTunnels(tunnelsTableViewController: self, barButtonItem: sender)
    }

    @IBAction func addProvider(_ sender: UIBarButtonItem) {
        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 {
        let cell = tableView.dequeueReusableCell(type: TunnelTableViewCell.self, for: indexPath)
        cell.delegate = self

        guard let sections = fetchedResultsController.sections else {
            fatalError("FetchedResultsController \(fetchedResultsController) should have sections, but found nil")
        }

        let section = sections[indexPath.section]
        let tunnel = section.objects[indexPath.row]

        cell.configure(tunnel: tunnel, status: delegate?.status(for: tunnel, tunnelsTableViewController: self) ?? .invalid)

        return cell
    }

    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]
        let tunnel = section.objects[indexPath.row]

        delegate?.configure(tunnel: tunnel, tunnelsTableViewController: self)

        tableView.deselectRow(at: indexPath, animated: true)
    }

    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]
        let tunnel = section.objects[indexPath.row]

        delegate?.configure(tunnel: tunnel, tunnelsTableViewController: self)

    }

    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }

    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, 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]
            let tunnel = section.objects[indexPath.row]

            delegate?.delete(tunnel: tunnel, tunnelsTableViewController: self)
        }
    }
}

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)
        }
    }

}

extension TunnelsTableViewController: Identifyable {}

class TunnelFetchedResultsControllerDelegate: NSObject, FetchedResultsControllerDelegate {

    private weak var tableView: UITableView?

    // MARK: - Lifecycle
    init(tableView: UITableView) {
        self.tableView = tableView
    }

    func fetchedResultsControllerDidPerformFetch(_ controller: FetchedResultsController<Tunnel>) {
        tableView?.reloadData()
    }

    func fetchedResultsControllerWillChangeContent(_ controller: FetchedResultsController<Tunnel>) {
        tableView?.beginUpdates()
    }

    func fetchedResultsControllerDidChangeContent(_ controller: FetchedResultsController<Tunnel>) {
        tableView?.endUpdates()
    }

    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)
        }
    }

    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)
        }
    }
}

protocol TunnelTableViewCellDelegate: class {
    func connect(tunnelIdentifier: String)
    func disconnect(tunnelIdentifier: String)
}

class TunnelTableViewCell: UITableViewCell {

    @IBOutlet weak var tunnelTitleLabel: UILabel!
    @IBOutlet weak var activityIndicator: UIActivityIndicatorView!
    @IBOutlet weak var tunnelSwitch: UISwitch!

    weak var delegate: TunnelTableViewCellDelegate?
    private var tunnelIdentifier: String?

    @IBAction func tunnelSwitchChanged(_ sender: Any) {
        tunnelSwitch.isEnabled = false
        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 {
            activityIndicator.startAnimating()
            tunnelSwitch.isHidden = true
        } else {
            activityIndicator.stopAnimating()
            tunnelSwitch.isHidden = false
        }

        tunnelSwitch.isOn = status == .connected
        tunnelSwitch.onTintColor = status == .invalid || status == .reasserting ? .gray : .green
        tunnelSwitch.isEnabled = true
    }
}

extension TunnelTableViewCell: Identifyable {}