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
|
//
// DeviceManagementContentView.swift
// MullvadVPN
//
// Created by pronebird on 19/07/2022.
// Copyright © 2025 Mullvad VPN AB. All rights reserved.
//
import UIKit
class DeviceManagementContentView: UIView {
private let scrollView: UIScrollView = {
let scrollView = UIScrollView()
scrollView.translatesAutoresizingMaskIntoConstraints = false
return scrollView
}()
let scrollContentView: UIView = {
let view = UIView()
view.directionalLayoutMargins = UIMetrics.contentLayoutMargins
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
let statusImageView: StatusImageView = {
let imageView = StatusImageView(style: .failure)
imageView.translatesAutoresizingMaskIntoConstraints = false
return imageView
}()
let titleLabel: UILabel = {
let textLabel = UILabel()
textLabel.font = .mullvadLarge
textLabel.adjustsFontForContentSizeCategory = true
textLabel.textColor = .white
textLabel.translatesAutoresizingMaskIntoConstraints = false
return textLabel
}()
let messageLabel: UILabel = {
let textLabel = UILabel()
textLabel.font = .mullvadSmall
textLabel.adjustsFontForContentSizeCategory = true
textLabel.textColor = .white
textLabel.translatesAutoresizingMaskIntoConstraints = false
textLabel.numberOfLines = 0
textLabel.lineBreakStrategy = []
return textLabel
}()
let deviceStackView: UIStackView = {
let stackView = UIStackView(arrangedSubviews: [])
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.axis = .vertical
stackView.spacing = 1
stackView.clipsToBounds = true
stackView.distribution = .fillEqually
return stackView
}()
let continueButton: AppButton = {
let button = AppButton(style: .success)
button.translatesAutoresizingMaskIntoConstraints = false
button.setTitle(
NSLocalizedString("Continue with login", comment: ""),
for: .normal
)
button.isEnabled = false
button.setAccessibilityIdentifier(.continueWithLoginButton)
return button
}()
let cancelButton: AppButton = {
let button = AppButton(style: .default)
button.translatesAutoresizingMaskIntoConstraints = false
button.setTitle(
NSLocalizedString("Cancel", comment: ""),
for: .normal
)
return button
}()
private lazy var buttonStackView: UIStackView = {
let stackView = UIStackView(arrangedSubviews: [continueButton, cancelButton])
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.axis = .vertical
stackView.distribution = .fillEqually
stackView.spacing = UIMetrics.interButtonSpacing
return stackView
}()
var handleDeviceDeletion: (@Sendable (DeviceViewModel, @escaping @Sendable () -> Void) -> Void)?
private var currentDeviceModels = [DeviceViewModel]()
var canContinue = false {
didSet {
updateView()
}
}
override init(frame: CGRect) {
super.init(frame: frame)
addViews()
constraintViews()
updateView()
setAccessibilityIdentifier(.deviceManagementView)
}
private func addViews() {
try? [scrollView, buttonStackView].forEach(addSubview)
scrollView.addSubview(scrollContentView)
try? [statusImageView, titleLabel, messageLabel, deviceStackView]
.forEach(scrollContentView.addSubview)
}
private func constraintViews() {
NSLayoutConstraint.activate([
scrollView.topAnchor.constraint(equalTo: topAnchor, constant: 16),
scrollView.leadingAnchor.constraint(equalTo: leadingAnchor),
scrollView.trailingAnchor.constraint(equalTo: trailingAnchor),
buttonStackView.topAnchor.constraint(
equalTo: scrollView.bottomAnchor,
constant: UIMetrics.contentLayoutMargins.top
),
buttonStackView.leadingAnchor.constraint(
equalTo: leadingAnchor,
constant: UIMetrics.contentLayoutMargins.leading
),
buttonStackView.trailingAnchor.constraint(
equalTo: trailingAnchor,
constant: -UIMetrics.contentLayoutMargins.trailing
),
buttonStackView.bottomAnchor.constraint(
equalTo: safeAreaLayoutGuide.bottomAnchor,
constant: -UIMetrics.contentLayoutMargins.bottom
),
scrollContentView.topAnchor.constraint(equalTo: scrollView.topAnchor),
scrollContentView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor),
scrollContentView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor),
scrollContentView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),
scrollContentView.widthAnchor.constraint(equalTo: scrollView.widthAnchor),
statusImageView.topAnchor
.constraint(equalTo: scrollContentView.topAnchor),
statusImageView.centerXAnchor.constraint(equalTo: scrollContentView.centerXAnchor),
titleLabel.topAnchor.constraint(equalTo: statusImageView.bottomAnchor, constant: 22),
titleLabel.leadingAnchor
.constraint(equalTo: scrollContentView.layoutMarginsGuide.leadingAnchor),
titleLabel.trailingAnchor
.constraint(equalTo: scrollContentView.layoutMarginsGuide.trailingAnchor),
messageLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 8),
messageLabel.leadingAnchor
.constraint(equalTo: scrollContentView.layoutMarginsGuide.leadingAnchor),
messageLabel.trailingAnchor
.constraint(equalTo: scrollContentView.layoutMarginsGuide.trailingAnchor),
deviceStackView.topAnchor.constraint(
equalTo: messageLabel.bottomAnchor,
constant: UIMetrics.TableView.sectionSpacing
),
deviceStackView.leadingAnchor.constraint(equalTo: scrollContentView.leadingAnchor),
deviceStackView.trailingAnchor.constraint(equalTo: scrollContentView.trailingAnchor),
deviceStackView.bottomAnchor.constraint(equalTo: scrollContentView.bottomAnchor),
])
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setDeviceViewModels(_ newModels: [DeviceViewModel], animated: Bool) {
let difference = newModels.difference(from: currentDeviceModels) { newModel, model in
newModel.id == model.id
}
currentDeviceModels = newModels
var viewsToAdd: [(view: UIView, offset: Int)] = []
var viewsToRemove: [UIView] = []
difference.forEach { change in
switch change {
case let .insert(offset, model, _):
viewsToAdd.append((createDeviceRowView(from: model), offset))
case let .remove(offset, _, _):
viewsToRemove.append(deviceStackView.arrangedSubviews[offset])
}
}
viewsToAdd.forEach { item in
deviceStackView.insertArrangedSubview(item.view, at: item.offset)
}
// Layout inserted subviews before running animations to achieve a folding effect.
if animated {
UIView.performWithoutAnimation {
deviceStackView.layoutIfNeeded()
}
}
if animated {
UIView.animate(
withDuration: 0.25,
delay: 0,
options: [.curveEaseInOut],
animations: { [weak self] in
self?.showHideViews(viewsToAdd: viewsToAdd, viewsToRemove: viewsToRemove)
self?.deviceStackView.layoutIfNeeded()
},
completion: { [weak self] _ in
self?.removeViews(viewsToRemove: viewsToRemove)
}
)
} else {
showHideViews(viewsToAdd: viewsToAdd, viewsToRemove: viewsToRemove)
removeViews(viewsToRemove: viewsToRemove)
}
}
private func showHideViews(viewsToAdd: [(view: UIView, offset: Int)], viewsToRemove: [UIView]) {
viewsToRemove.forEach { view in
view.alpha = 0
view.isHidden = true
}
viewsToAdd.forEach { item in
item.view.alpha = 1
item.view.isHidden = false
}
}
private func removeViews(viewsToRemove: [UIView]) {
viewsToRemove.forEach { view in
view.removeFromSuperview()
}
}
private func createDeviceRowView(from model: DeviceViewModel) -> DeviceRowView {
let view = DeviceRowView(viewModel: model)
view.isHidden = true
view.alpha = 0
view.deleteHandler = { [weak self] _ in
view.showsActivityIndicator = true
self?.handleDeviceDeletion?(view.viewModel) {
Task { @MainActor in
view.showsActivityIndicator = false
}
}
}
return view
}
private func updateView() {
titleLabel.text = titleText
messageLabel.text = messageText
continueButton.isEnabled = canContinue
statusImageView.style = canContinue ? .success : .failure
}
private var titleText: String {
if canContinue {
return NSLocalizedString("Super!", comment: "")
} else {
return NSLocalizedString("Too many devices", comment: "")
}
}
private var messageText: String {
if canContinue {
return NSLocalizedString("You can now continue logging in on this device.", comment: "")
} else {
return NSLocalizedString(
"""
Please log out of at least one by removing it from the list below. You can find \
the corresponding device name under the device’s Account settings.
""", comment: "")
}
}
}
|