-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathVPNTodayViewController.swift
121 lines (99 loc) · 4.07 KB
/
VPNTodayViewController.swift
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
//
// VPNViewController.swift
// Today
//
// Copyright © 2019 Confirmed, Inc. All rights reserved.
//
import UIKit
import NotificationCenter
import NetworkExtension
import CloudKit
import CocoaLumberjackSwift
import PromiseKit
class VPNTodayViewController: UIViewController, NCWidgetProviding {
var lastVPNStatus: NEVPNStatus?
@IBOutlet weak var regionLabel: UILabel!
@IBOutlet weak var toggleVPN: UIButton!
@IBOutlet weak var vpnStatusLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
self.toggleVPN.layer.borderWidth = 2.5
self.toggleVPN.layer.borderColor = UIColor.tunnelsBlue.cgColor
self.toggleVPN.layer.cornerRadius = self.toggleVPN.frame.size.width / 2.0
NotificationCenter.default.addObserver(self, selector: #selector(vpnStatusDidChange(_:)), name: .NEVPNStatusDidChange, object: nil)
setupVPNButtons()
self.extensionContext?.widgetLargestAvailableDisplayMode = .expanded
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
setupVPNButtons()
}
func widgetActiveDisplayModeDidChange(_ activeDisplayMode: NCWidgetDisplayMode, withMaximumSize maxSize: CGSize) {
if activeDisplayMode == .expanded {
self.preferredContentSize = CGSize(width: self.view.frame.size.width, height: 170)
} else if activeDisplayMode == .compact{
self.preferredContentSize = CGSize(width: maxSize.width, height: 110)
}
}
func widgetPerformUpdate(completionHandler: (@escaping (NCUpdateResult) -> Void)) {
completionHandler(NCUpdateResult.newData)
}
// MARK: - VPN Status/Button
@objc func vpnStatusDidChange(_ notification: Notification) {
if let neVPNConnection = notification.object as? NEVPNConnection {
DDLogInfo("Widget VPNStatusDidChange as NEVPNConnection with status: \(neVPNConnection.status.rawValue)");
if (neVPNConnection.status != self.lastVPNStatus) {
self.setupVPNButtons()
}
}
}
@IBAction func toggleVPNButton(sender: UIButton) {
switch VPNController.shared.status() {
case .connected, .connecting, .reasserting:
setVPNButtonDisconnecting()
DDLogInfo("Widget Toggle VPN: on currently, turning it off")
VPNController.shared.setEnabled(false)
case .disconnected, .disconnecting, .invalid:
DDLogInfo("Widget Toggle VPN: off currently, turning it on")
setVPNButtonConnecting()
// TODO: Subscription checking or alerts
VPNController.shared.setEnabled(true)
}
}
func setupVPNButtons() {
switch VPNController.shared.status() {
case .disconnected, .invalid:
self.setVPNButtonDisconnected()
case .connected:
self.setVPNButtonConnected()
case .disconnecting:
self.setVPNButtonDisconnecting()
case .connecting, .reasserting:
self.setVPNButtonConnecting()
}
regionLabel.text = getSavedVPNRegion().regionDisplayName
}
func setVPNButtonConnected() {
vpnStatusLabel.text = NSLocalizedString("VPN Active", comment: "")
toggleVPN?.tintColor = .tunnelsBlue
toggleVPN.layer.borderColor = UIColor.tunnelsBlue.cgColor
}
func setVPNButtonDisconnected() {
vpnStatusLabel.text = NSLocalizedString("VPN Not Active", comment: "")
toggleVPN?.tintColor = UIColor.darkGray
toggleVPN?.layer.borderColor = UIColor.darkGray.cgColor
}
func setVPNButtonConnecting() {
vpnStatusLabel.text = NSLocalizedString("Activating", comment: "")
}
func setVPNButtonDisconnecting() {
vpnStatusLabel.text = NSLocalizedString("Deactivating", comment: "")
}
// MARK: - Helpers
func openApp() {
self.extensionContext?.open(URL(string: "lockdown://")!, completionHandler: nil)
}
@IBAction func openLockdown(sender: UIButton) {
openApp()
}
}