-
Notifications
You must be signed in to change notification settings - Fork 384
/
Copy pathTunnelControlView.swift
442 lines (374 loc) · 15.5 KB
/
TunnelControlView.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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
//
// TunnelControlView.swift
// MullvadVPN
//
// Created by pronebird on 09/03/2021.
// Copyright © 2021 Mullvad VPN AB. All rights reserved.
//
import MapKit
import MullvadREST
import MullvadTypes
import PacketTunnelCore
import UIKit
enum TunnelControlAction {
case connect
case disconnect
case cancel
case reconnect
case selectLocation
}
private enum TunnelControlActionButton {
case connect
case disconnect
case cancel
case selectLocation
}
final class TunnelControlView: UIView {
private let secureLabel = makeBoldTextLabel(ofSize: 20, numberOfLines: 0)
private let cityLabel = makeBoldTextLabel(ofSize: 34)
private let countryLabel = makeBoldTextLabel(ofSize: 34)
private let activityIndicator: SpinnerActivityIndicatorView = {
let activityIndicator = SpinnerActivityIndicatorView(style: .large)
activityIndicator.translatesAutoresizingMaskIntoConstraints = false
activityIndicator.tintColor = .white
activityIndicator.setContentHuggingPriority(.defaultHigh, for: .horizontal)
activityIndicator.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
return activityIndicator
}()
private let locationContainerView: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.isAccessibilityElement = true
view.accessibilityTraits = .summaryElement
return view
}()
private let connectionPanel: ConnectionPanelView = {
let view = ConnectionPanelView()
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
private let buttonsStackView: UIStackView = {
let stackView = UIStackView()
stackView.spacing = UIMetrics.interButtonSpacing
stackView.axis = .vertical
stackView.translatesAutoresizingMaskIntoConstraints = false
return stackView
}()
private let connectButton: AppButton = {
let button = AppButton(style: .success)
button.accessibilityIdentifier = .secureConnectionButton
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
private let cancelButton: AppButton = {
let button = AppButton(style: .translucentDanger)
button.accessibilityIdentifier = .cancelButton
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
private let selectLocationButton: AppButton = {
let button = AppButton(style: .translucentNeutral)
button.accessibilityIdentifier = .selectLocationButton
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
private let selectLocationButtonBlurView: TranslucentButtonBlurView
private let connectButtonBlurView: TranslucentButtonBlurView
private let cancelButtonBlurView: TranslucentButtonBlurView
private let splitDisconnectButton: DisconnectSplitButton = {
let button = DisconnectSplitButton()
button.primaryButton.accessibilityIdentifier = .disconnectButton
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
private let containerView: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
private var traitConstraints = [NSLayoutConstraint]()
private var viewModel: TunnelControlViewModel?
var actionHandler: ((TunnelControlAction) -> Void)?
var mapCenterAlignmentView: UIView {
activityIndicator
}
override init(frame: CGRect) {
selectLocationButtonBlurView = TranslucentButtonBlurView(button: selectLocationButton)
connectButtonBlurView = TranslucentButtonBlurView(button: connectButton)
cancelButtonBlurView = TranslucentButtonBlurView(button: cancelButton)
super.init(frame: frame)
backgroundColor = .clear
directionalLayoutMargins = UIMetrics.contentLayoutMargins
accessibilityContainerType = .semanticGroup
accessibilityIdentifier = .tunnelControlView
addSubviews()
addButtonHandlers()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func update(with model: TunnelControlViewModel) {
viewModel = model
let tunnelState = model.tunnelStatus.state
secureLabel.text = model.secureLabelText
secureLabel.textColor = tunnelState.textColorForSecureLabel
selectLocationButtonBlurView.isEnabled = model.enableButtons
connectButtonBlurView.isEnabled = model.enableButtons
cityLabel.attributedText = attributedStringForLocation(string: model.city)
countryLabel.attributedText = attributedStringForLocation(string: model.country)
connectionPanel.connectedRelayName = model.connectedRelaysName
connectionPanel.dataSource = model.connectionPanel
updateSecureLabel(tunnelState: tunnelState)
updateActionButtons(tunnelState: tunnelState)
if tunnelState.isSecured {
updateTunnelRelays(tunnelRelays: tunnelState.relays)
} else {
updateTunnelRelays(tunnelRelays: nil)
}
}
func setAnimatingActivity(_ isAnimating: Bool) {
if isAnimating {
activityIndicator.startAnimating()
} else {
activityIndicator.stopAnimating()
}
}
private func updateActionButtons(tunnelState: TunnelState) {
let actionButtons = tunnelState.actionButtons(traitCollection: traitCollection)
let views = actionButtons.map { self.view(forActionButton: $0) }
updateButtonTitles(tunnelState: tunnelState)
updateButtonEnabledStates(shouldEnableButtons: tunnelState.shouldEnableButtons)
setArrangedButtons(views)
}
private func updateSecureLabel(tunnelState: TunnelState) {
secureLabel.text = tunnelState.localizedTitleForSecureLabel.uppercased()
secureLabel.textColor = tunnelState.textColorForSecureLabel
switch tunnelState {
case .connected:
secureLabel.accessibilityIdentifier = .connectionStatusConnectedLabel
case .connecting:
secureLabel.accessibilityIdentifier = .connectionStatusConnectingLabel
default:
secureLabel.accessibilityIdentifier = .connectionStatusNotConnectedLabel
}
}
private func updateButtonTitles(tunnelState: TunnelState) {
connectButton.setTitle(
NSLocalizedString(
"CONNECT_BUTTON_TITLE",
tableName: "Main",
value: "Secure connection",
comment: ""
), for: .normal
)
selectLocationButton.setTitle(
tunnelState.localizedTitleForSelectLocationButton,
for: .normal
)
cancelButton.setTitle(
NSLocalizedString(
"CANCEL_BUTTON_TITLE",
tableName: "Main",
value: tunnelState == .waitingForConnectivity(.noConnection) ? "Disconnect" : "Cancel",
comment: ""
), for: .normal
)
splitDisconnectButton.primaryButton.setTitle(
NSLocalizedString(
"DISCONNECT_BUTTON_TITLE",
tableName: "Main",
value: "Disconnect",
comment: ""
), for: .normal
)
splitDisconnectButton.secondaryButton.accessibilityLabel = NSLocalizedString(
"RECONNECT_BUTTON_ACCESSIBILITY_LABEL",
tableName: "Main",
value: "Reconnect",
comment: ""
)
}
private func updateButtonEnabledStates(shouldEnableButtons: Bool) {
selectLocationButtonBlurView.isEnabled = shouldEnableButtons
connectButtonBlurView.isEnabled = shouldEnableButtons
}
private func updateTunnelRelays(tunnelRelays: SelectedRelays?) {
if let tunnelRelays {
cityLabel.attributedText = attributedStringForLocation(
string: tunnelRelays.exit.location.city
)
countryLabel.attributedText = attributedStringForLocation(
string: tunnelRelays.exit.location.country
)
connectionPanel.isHidden = false
connectionPanel.connectedRelayName = tunnelRelays.exit
.hostname + "\(tunnelRelays.entry.flatMap { " via \($0.hostname)" } ?? "")"
} else {
countryLabel.attributedText = attributedStringForLocation(string: " ")
cityLabel.attributedText = attributedStringForLocation(string: " ")
connectionPanel.dataSource = nil
connectionPanel.isHidden = true
}
locationContainerView.accessibilityLabel = viewModel?.tunnelStatus.state.localizedAccessibilityLabel
}
// MARK: - Private
private func addSubviews() {
for subview in [secureLabel, countryLabel, cityLabel] {
locationContainerView.addSubview(subview)
}
for subview in [
activityIndicator,
locationContainerView,
connectionPanel,
buttonsStackView,
] {
containerView.addSubview(subview)
}
addSubview(containerView)
NSLayoutConstraint.activate([
containerView.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor),
containerView.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor),
containerView.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor),
locationContainerView.topAnchor.constraint(greaterThanOrEqualTo: containerView.topAnchor),
locationContainerView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
locationContainerView.trailingAnchor.constraint(equalTo: containerView.trailingAnchor),
activityIndicator.centerXAnchor.constraint(equalTo: containerView.centerXAnchor),
locationContainerView.topAnchor.constraint(
equalTo: activityIndicator.bottomAnchor,
constant: 22
),
secureLabel.topAnchor.constraint(equalTo: locationContainerView.topAnchor),
secureLabel.leadingAnchor.constraint(equalTo: locationContainerView.leadingAnchor),
secureLabel.trailingAnchor.constraint(equalTo: locationContainerView.trailingAnchor),
countryLabel.topAnchor.constraint(equalTo: secureLabel.bottomAnchor, constant: 8),
countryLabel.leadingAnchor.constraint(equalTo: locationContainerView.leadingAnchor),
countryLabel.trailingAnchor.constraint(equalTo: locationContainerView.trailingAnchor),
cityLabel.topAnchor.constraint(equalTo: countryLabel.bottomAnchor, constant: 8),
cityLabel.leadingAnchor.constraint(equalTo: locationContainerView.leadingAnchor),
cityLabel.trailingAnchor.constraint(equalTo: locationContainerView.trailingAnchor),
cityLabel.bottomAnchor.constraint(equalTo: locationContainerView.bottomAnchor),
connectionPanel.topAnchor.constraint(
equalTo: locationContainerView.bottomAnchor,
constant: 8
),
connectionPanel.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
connectionPanel.trailingAnchor.constraint(equalTo: containerView.trailingAnchor),
buttonsStackView.topAnchor.constraint(
equalTo: connectionPanel.bottomAnchor,
constant: 24
),
buttonsStackView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
buttonsStackView.trailingAnchor.constraint(equalTo: containerView.trailingAnchor),
buttonsStackView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor),
])
updateTraitConstraints()
}
private func addButtonHandlers() {
connectButton.addTarget(
self,
action: #selector(handleConnect),
for: .touchUpInside
)
cancelButton.addTarget(
self,
action: #selector(handleCancel),
for: .touchUpInside
)
splitDisconnectButton.primaryButton.addTarget(
self,
action: #selector(handleDisconnect),
for: .touchUpInside
)
splitDisconnectButton.secondaryButton.addTarget(
self,
action: #selector(handleReconnect),
for: .touchUpInside
)
selectLocationButton.addTarget(
self,
action: #selector(handleSelectLocation),
for: .touchUpInside
)
}
private func updateTraitConstraints() {
var layoutConstraints = [NSLayoutConstraint]()
layoutConstraints.append(
containerView.trailingAnchor.constraint(equalTo: layoutMarginsGuide.trailingAnchor)
)
removeConstraints(traitConstraints)
traitConstraints = layoutConstraints
NSLayoutConstraint.activate(layoutConstraints)
}
private func setArrangedButtons(_ newButtons: [UIView]) {
buttonsStackView.arrangedSubviews.forEach { button in
if !newButtons.contains(button) {
buttonsStackView.removeArrangedSubview(button)
button.removeFromSuperview()
}
}
newButtons.forEach { button in
buttonsStackView.addArrangedSubview(button)
}
}
private func view(forActionButton actionButton: TunnelControlActionButton) -> UIView {
switch actionButton {
case .connect:
return connectButton
case .disconnect:
return splitDisconnectButton
case .cancel:
return cancelButtonBlurView
case .selectLocation:
return selectLocationButtonBlurView
}
}
private func attributedStringForLocation(string: String) -> NSAttributedString {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 0
paragraphStyle.lineHeightMultiple = 0.80
return NSAttributedString(
string: string,
attributes: [.paragraphStyle: paragraphStyle]
)
}
private static func makeBoldTextLabel(ofSize fontSize: CGFloat, numberOfLines: Int = 1) -> UILabel {
let textLabel = UILabel()
textLabel.translatesAutoresizingMaskIntoConstraints = false
textLabel.font = UIFont.boldSystemFont(ofSize: fontSize)
textLabel.textColor = .white
textLabel.numberOfLines = numberOfLines
return textLabel
}
// MARK: - Actions
@objc private func handleConnect() {
actionHandler?(.connect)
}
@objc private func handleCancel() {
actionHandler?(.cancel)
}
@objc private func handleDisconnect() {
actionHandler?(.disconnect)
}
@objc private func handleReconnect() {
actionHandler?(.reconnect)
}
@objc private func handleSelectLocation() {
actionHandler?(.selectLocation)
}
}
private extension TunnelState {
func actionButtons(traitCollection: UITraitCollection) -> [TunnelControlActionButton] {
switch self {
case .disconnected, .disconnecting(.nothing), .waitingForConnectivity(.noNetwork):
[.selectLocation, .connect]
case .connecting, .pendingReconnect, .disconnecting(.reconnect),
.waitingForConnectivity(.noConnection):
[.selectLocation, .cancel]
case .negotiatingPostQuantumKey:
[.selectLocation, .cancel]
case .connected, .reconnecting, .error:
[.selectLocation, .disconnect]
}
}
// swiftlint:disable:next file_length
}