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
|
//
// DeviceManagementViewController.swift
// MullvadVPN
//
// Created by pronebird on 15/07/2022.
// Copyright © 2022 Mullvad VPN AB. All rights reserved.
//
import MullvadLogging
import MullvadREST
import Operations
import UIKit
protocol DeviceManagementViewControllerDelegate: AnyObject {
func deviceManagementViewControllerDidFinish(_ controller: DeviceManagementViewController)
func deviceManagementViewControllerDidCancel(_ controller: DeviceManagementViewController)
}
class DeviceManagementViewController: UIViewController, RootContainment {
weak var delegate: DeviceManagementViewControllerDelegate?
var preferredHeaderBarPresentation: HeaderBarPresentation {
return .default
}
var prefersHeaderBarHidden: Bool {
return false
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
private let alertPresenter = AlertPresenter()
private let contentView: DeviceManagementContentView = {
let contentView = DeviceManagementContentView()
contentView.translatesAutoresizingMaskIntoConstraints = false
return contentView
}()
private let logger = Logger(label: "DeviceManagementViewController")
private let interactor: DeviceManagementInteractor
init(interactor: DeviceManagementInteractor) {
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()
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
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: ((OperationCompletion<Void, Error>) -> Void)? = nil
) {
interactor.getDevices { [weak self] completion in
guard let self = self else { return }
if let devices = completion.value {
self.setDevices(devices, animated: animateUpdates)
}
completionHandler?(completion.ignoreOutput())
}
}
// MARK: - Private
private func setDevices(_ devices: [REST.Device], animated: Bool) {
let viewModels = devices.map { restDevice -> DeviceViewModel in
return DeviceViewModel(
id: restDevice.id,
name: restDevice.name
)
}
contentView.canContinue = viewModels.count < ApplicationConfiguration.maxAllowedDevices
contentView.setDeviceViewModels(viewModels, animated: animated)
}
private func handleDeviceDeletion(
_ device: DeviceViewModel,
completionHandler: @escaping () -> Void
) {
showDeleteConfirmation(deviceName: device.displayName) { [weak self] shouldDelete in
guard let self = self else { return }
guard shouldDelete else {
completionHandler()
return
}
self.deleteDevice(identifier: device.id) { error in
if let error = error {
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 alertController = UIAlertController(
title: title,
message: getErrorDescription(error),
preferredStyle: .alert
)
alertController.addAction(
UIAlertAction(
title: NSLocalizedString(
"ERROR_ALERT_OK_ACTION",
tableName: "DeviceManagement",
value: "OK",
comment: ""
),
style: .cancel
)
)
alertPresenter.enqueue(alertController, presentingController: self)
}
private func showDeleteConfirmation(
deviceName: String,
completion: @escaping (_ shouldDelete: Bool) -> Void
) {
let localizedTitle = String(
format: NSLocalizedString(
"DELETE_ALERT_TITLE",
tableName: "DeviceManagement",
value: "Are you sure you want to log out of %@?",
comment: ""
), deviceName
)
let alertController = UIAlertController(
title: localizedTitle,
message: nil,
preferredStyle: .alert
)
let actions = [
UIAlertAction(
title: NSLocalizedString(
"DELETE_ALERT_CANCEL_ACTION",
tableName: "DeviceManagement",
value: "Back",
comment: ""
),
style: .cancel,
handler: { _ in
completion(false)
}
),
UIAlertAction(
title: NSLocalizedString(
"DELETE_ALERT_CONFIRM_ACTION",
tableName: "DeviceManagement",
value: "Yes, log out device",
comment: ""
),
style: .destructive,
handler: { _ in
completion(true)
}
),
]
for action in actions {
alertController.addAction(action)
}
alertPresenter.enqueue(alertController, presentingController: self)
}
private func deleteDevice(identifier: String, completionHandler: @escaping (Error?) -> Void) {
interactor.deleteDevice(identifier) { [weak self] completion in
guard let self = self else { return }
switch completion {
case .success:
self.fetchDevices(animateUpdates: true) { completion in
completionHandler(completion.error)
}
case let .failure(error):
self.logger.error(
error: error,
message: "Failed to delete device."
)
completionHandler(error)
case .cancelled:
completionHandler(nil)
}
}
}
// MARK: - Actions
@objc private func didTapBackButton(_ sender: Any?) {
delegate?.deviceManagementViewControllerDidCancel(self)
}
@objc private func didTapContinueButton(_ sender: Any?) {
delegate?.deviceManagementViewControllerDidFinish(self)
}
}
struct DeviceViewModel {
var id: String
var name: String
var displayName: String {
return name.capitalized
}
}
|