-
Notifications
You must be signed in to change notification settings - Fork 384
/
Copy pathDeviceManagementViewController.swift
286 lines (243 loc) · 9 KB
/
DeviceManagementViewController.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
//
// DeviceManagementViewController.swift
// MullvadVPN
//
// Created by pronebird on 15/07/2022.
// Copyright © 2025 Mullvad VPN AB. All rights reserved.
//
@preconcurrency import MullvadLogging
import MullvadREST
import MullvadTypes
import Operations
import UIKit
protocol DeviceManagementViewControllerDelegate: AnyObject, Sendable {
func deviceManagementViewControllerDidFinish(_ controller: DeviceManagementViewController)
func deviceManagementViewControllerDidCancel(_ controller: DeviceManagementViewController)
}
class DeviceManagementViewController: UIViewController, RootContainment {
weak var delegate: DeviceManagementViewControllerDelegate?
var preferredHeaderBarPresentation: HeaderBarPresentation {
.default
}
var prefersHeaderBarHidden: Bool {
false
}
override var preferredStatusBarStyle: UIStatusBarStyle {
.lightContent
}
private let contentView: DeviceManagementContentView = {
let contentView = DeviceManagementContentView()
contentView.translatesAutoresizingMaskIntoConstraints = false
return contentView
}()
nonisolated(unsafe) private let logger = Logger(label: "DeviceManagementViewController")
let interactor: DeviceManagementInteractor
private let alertPresenter: AlertPresenter
init(interactor: DeviceManagementInteractor, alertPresenter: AlertPresenter) {
self.interactor = interactor
self.alertPresenter = alertPresenter
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .secondaryColor
view.addSubview(contentView)
contentView.backButton.addTarget(
self,
action: #selector(didTapBackButton(_:)),
for: .touchUpInside
)
contentView.continueButton.addTarget(
self,
action: #selector(didTapContinueButton(_:)),
for: .touchUpInside
)
contentView.handleDeviceDeletion = { [weak self] viewModel, finish in
Task { @MainActor in
self?.handleDeviceDeletion(viewModel, completionHandler: finish)
}
}
NSLayoutConstraint.activate([
contentView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
contentView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
contentView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
contentView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
])
}
func fetchDevices(
animateUpdates: Bool,
completionHandler: (@Sendable (Result<Void, Error>) -> Void)? = nil
) {
interactor.getDevices { [weak self] result in
guard let self = self else { return }
if let devices = result.value {
setDevices(devices, animated: animateUpdates)
}
completionHandler?(result.map { _ in () })
}
}
// MARK: - Private
nonisolated private func setDevices(_ devices: [Device], animated: Bool) {
let viewModels = devices.map { restDevice -> DeviceViewModel in
DeviceViewModel(
id: restDevice.id,
name: restDevice.name.capitalized,
creationDate: DateFormatter.localizedString(
from: restDevice.created,
dateStyle: .short,
timeStyle: .none
)
)
}
Task { @MainActor in
contentView.canContinue = viewModels.count < ApplicationConfiguration.maxAllowedDevices
contentView.setDeviceViewModels(viewModels, animated: animated)
}
}
private func handleDeviceDeletion(
_ device: DeviceViewModel,
completionHandler: @escaping @Sendable () -> Void
) {
showLogoutConfirmation(deviceName: device.name) { [weak self] shouldDelete in
guard let self else { return }
guard shouldDelete else {
completionHandler()
return
}
deleteDevice(identifier: device.id) { [weak self] error in
guard let self = self else { return }
if let error {
Task { @MainActor in
self.showErrorAlert(
title: NSLocalizedString(
"LOGOUT_DEVICE_ERROR_ALERT_TITLE",
tableName: "DeviceManagement",
value: "Failed to log out device",
comment: ""
),
error: error
)
}
}
completionHandler()
}
}
}
private func getErrorDescription(_ error: Error) -> String {
if case let .network(urlError) = error as? REST.Error {
return urlError.localizedDescription
} else {
return error.localizedDescription
}
}
private func showErrorAlert(title: String, error: Error) {
let presentation = AlertPresentation(
id: "delete-device-error-alert",
title: title,
message: getErrorDescription(error),
buttons: [
AlertAction(
title: NSLocalizedString(
"ERROR_ALERT_OK_ACTION",
tableName: "DeviceManagement",
value: "Got it!",
comment: ""
),
style: .default
),
]
)
alertPresenter.showAlert(presentation: presentation, animated: true)
}
private func showLogoutConfirmation(
deviceName: String,
completion: @escaping (_ shouldDelete: Bool) -> Void
) {
let text = String(
format: NSLocalizedString(
"DELETE_ALERT_TITLE",
tableName: "DeviceManagement",
value: "Are you sure you want to log **\(deviceName)** out?",
comment: ""
)
)
let attributedText = NSAttributedString(
markdownString: text,
options: MarkdownStylingOptions(
font: .preferredFont(forTextStyle: .body)
)
)
let presentation = AlertPresentation(
id: "logout-confirmation-alert",
icon: .alert,
attributedMessage: attributedText,
buttons: [
AlertAction(
title: NSLocalizedString(
"DELETE_ALERT_CONFIRM_ACTION",
tableName: "DeviceManagement",
value: "Yes, log out device",
comment: ""
),
style: .destructive,
accessibilityId: .logOutDeviceConfirmButton,
handler: {
completion(true)
}
),
AlertAction(
title: NSLocalizedString(
"DELETE_ALERT_CANCEL_ACTION",
tableName: "DeviceManagement",
value: "Back",
comment: ""
),
style: .default,
accessibilityId: .logOutDeviceCancelButton,
handler: {
completion(false)
}
),
]
)
alertPresenter.showAlert(presentation: presentation, animated: true)
}
private func deleteDevice(identifier: String, completionHandler: @escaping @Sendable (Error?) -> Void) {
interactor.deleteDevice(identifier) { [weak self] completion in
guard let self = self else { return }
switch completion {
case .success:
Task { @MainActor in
fetchDevices(animateUpdates: true) { completion in
completionHandler(completion.error)
}
}
case let .failure(error):
if error.isOperationCancellationError {
completionHandler(nil)
} else {
logger.error(
error: error,
message: "Failed to delete device."
)
completionHandler(error)
}
}
}
}
// MARK: - Actions
@objc private func didTapBackButton(_ sender: Any?) {
delegate?.deviceManagementViewControllerDidCancel(self)
}
@objc private func didTapContinueButton(_ sender: Any?) {
delegate?.deviceManagementViewControllerDidFinish(self)
}
}
struct DeviceViewModel: Sendable {
let id: String
let name: String
let creationDate: String
}