-
Notifications
You must be signed in to change notification settings - Fork 381
/
Copy pathTunnelViewController.swift
199 lines (158 loc) · 6.4 KB
/
TunnelViewController.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
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
//
// TunnelViewController.swift
// MullvadVPN
//
// Created by pronebird on 20/03/2019.
// Copyright © 2019 Mullvad VPN AB. All rights reserved.
//
import MapKit
import MullvadLogging
import MullvadTypes
import UIKit
class TunnelViewController: UIViewController, RootContainment {
private let logger = Logger(label: "TunnelViewController")
private let interactor: TunnelViewControllerInteractor
private let contentView = TunnelControlView(frame: CGRect(x: 0, y: 0, width: 320, height: 480))
private var tunnelState: TunnelState = .disconnected
var shouldShowSelectLocationPicker: (() -> Void)?
var shouldShowCancelTunnelAlert: (() -> Void)?
private let mapViewController = MapViewController()
override var preferredStatusBarStyle: UIStatusBarStyle {
.lightContent
}
var preferredHeaderBarPresentation: HeaderBarPresentation {
switch interactor.deviceState {
case .loggedIn, .revoked:
return HeaderBarPresentation(
style: tunnelState.isSecured ? .secured : .unsecured,
showsDivider: false
)
case .loggedOut:
return HeaderBarPresentation(style: .default, showsDivider: true)
}
}
var prefersHeaderBarHidden: Bool {
false
}
init(interactor: TunnelViewControllerInteractor) {
self.interactor = interactor
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
interactor.didUpdateDeviceState = { [weak self] _, _ in
self?.setNeedsHeaderBarStyleAppearanceUpdate()
}
interactor.didUpdateTunnelStatus = { [weak self] tunnelStatus in
self?.setTunnelState(tunnelStatus.state, animated: true)
}
contentView.actionHandler = { [weak self] action in
switch action {
case .connect:
self?.interactor.startTunnel()
case .cancel:
if case .waitingForConnectivity(.noConnection) = self?.interactor.tunnelStatus.state {
self?.shouldShowCancelTunnelAlert?()
} else {
self?.interactor.stopTunnel()
}
case .disconnect:
self?.interactor.stopTunnel()
case .reconnect:
self?.interactor.reconnectTunnel(selectNewRelay: true)
case .selectLocation:
self?.shouldShowSelectLocationPicker?()
}
}
addMapController()
addContentView()
tunnelState = interactor.tunnelStatus.state
updateContentView(animated: false)
updateMap(animated: false)
}
override func viewWillTransition(
to size: CGSize,
with coordinator: UIViewControllerTransitionCoordinator
) {
super.viewWillTransition(to: size, with: coordinator)
coordinator.animate(alongsideTransition: nil, completion: { context in
self.updateContentView(animated: context.isAnimated)
})
}
func setMainContentHidden(_ isHidden: Bool, animated: Bool) {
let actions = {
self.contentView.alpha = isHidden ? 0 : 1
}
if animated {
UIView.animate(withDuration: 0.25, animations: actions)
} else {
actions()
}
}
// MARK: - Private
private func setTunnelState(_ tunnelState: TunnelState, animated: Bool) {
self.tunnelState = tunnelState
setNeedsHeaderBarStyleAppearanceUpdate()
guard isViewLoaded else { return }
updateContentView(animated: animated)
updateMap(animated: animated)
}
private func updateMap(animated: Bool) {
switch tunnelState {
case let .connecting(tunnelRelay):
mapViewController.removeLocationMarker()
contentView.setAnimatingActivity(true)
mapViewController.setCenter(tunnelRelay?.location.geoCoordinate, animated: animated)
case let .reconnecting(tunnelRelay):
mapViewController.removeLocationMarker()
contentView.setAnimatingActivity(true)
mapViewController.setCenter(tunnelRelay.location.geoCoordinate, animated: animated)
case let .connected(tunnelRelay):
let center = tunnelRelay.location.geoCoordinate
mapViewController.setCenter(center, animated: animated) {
self.contentView.setAnimatingActivity(false)
self.mapViewController.addLocationMarker(coordinate: center)
}
case .pendingReconnect:
mapViewController.removeLocationMarker()
contentView.setAnimatingActivity(true)
case .waitingForConnectivity:
mapViewController.removeLocationMarker()
contentView.setAnimatingActivity(false)
case .disconnected, .disconnecting:
mapViewController.removeLocationMarker()
contentView.setAnimatingActivity(false)
mapViewController.setCenter(nil, animated: animated)
}
}
private func updateContentView(animated: Bool) {
contentView.update(from: tunnelState, animated: animated)
}
private func addMapController() {
let mapView = mapViewController.view!
mapView.translatesAutoresizingMaskIntoConstraints = false
mapViewController.alignmentView = contentView.mapCenterAlignmentView
addChild(mapViewController)
view.addSubview(mapView)
mapViewController.didMove(toParent: self)
NSLayoutConstraint.activate([
mapView.topAnchor.constraint(equalTo: view.topAnchor),
mapView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
mapView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
mapView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
])
}
private func addContentView() {
contentView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(contentView)
NSLayoutConstraint.activate([
contentView.topAnchor.constraint(equalTo: view.topAnchor),
contentView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
contentView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
contentView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
])
}
}