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
|
import MullvadTypes
import SwiftUI
struct DeviceManagementView: View {
enum Style {
case tooManyDevices((Bool) -> Void)
case deviceManagement
var actionButtonTitle: LocalizedStringKey {
switch self {
case .deviceManagement:
return "Remove"
case .tooManyDevices:
return "Yes, log out device"
}
}
var actionButtonStyle: MainButtonStyle.Style {
switch self {
case .tooManyDevices:
.danger
case .deviceManagement:
.default
}
}
func warningText(deviceName: String) -> [LocalizedStringKey] {
var attributedDeviceName: AttributedString {
var fullText = AttributedString(deviceName.capitalized)
fullText.foregroundColor = Color.mullvadTextPrimary
return fullText
}
return switch self {
case .tooManyDevices:
[
LocalizedStringKey(
"Are you sure you want to log \(attributedDeviceName) out?"
)
]
case .deviceManagement:
[
LocalizedStringKey("Remove \(attributedDeviceName)?"),
LocalizedStringKey("The device will be removed from the list and logged out."),
]
}
}
}
let deviceManaging: any DeviceManaging
let style: Style
let onError: (String, Error) -> Void
@State private var loggedInDevices: [DeviceListView.Device]?
@State private var loading = true
var canLoginNewDevice: Bool {
guard let loggedInDevices else {
return false
}
return loggedInDevices.count < ApplicationConfiguration.maxAllowedDevices
}
var bodyText: LocalizedStringKey {
// swiftlint:disable line_length
switch style {
case .deviceManagement:
"View and manage all your logged in devices. You can have up to 5 devices on one account at a time. Each device gets a name when logged in to help you tell them apart easily."
case .tooManyDevices:
if canLoginNewDevice {
"You can now continue logging in on this device."
} else {
"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."
}
}
// swiftlint:enable line_length
}
private func fetchDevices() {
loading = true
_ = deviceManaging.getDevices { result in
Task { @MainActor in
loading = false
switch result {
case let .success(devices):
self.loggedInDevices = devices.map {
DeviceListView.Device(
id: $0.id,
name: $0.name.capitalized,
created: $0.created,
isCurrentDevice: $0.id == self.deviceManaging.currentDeviceId,
isBeingRemoved: false
)
}
case let .failure(error):
onError("Failed to fetch devices", error)
}
}
}
}
@State var deviceManagementAlert: MullvadAlert?
var body: some View {
VStack {
DeviceListView(
devices: $loggedInDevices,
loading: $loading,
onRemoveDevice: { device in
deviceManagementAlert = MullvadAlert(
type: .warning,
messages: style.warningText(deviceName: device.name),
action: .init(
type: style.actionButtonStyle,
title: style.actionButtonTitle,
identifier: AccessibilityIdentifier.logOutDeviceConfirmButton,
handler: {
await withCheckedContinuation { continuation in
guard let loggedInDevices else {
return
}
self.loggedInDevices = loggedInDevices.map {
$0.id == device.id ? $0.setIsBeingRemoved(true) : $0
}
deviceManagementAlert = nil
_ = deviceManaging.deleteDevice(
device.id,
completionHandler: { result in
Task { @MainActor in
switch result {
case .success:
self.loggedInDevices?.removeAll(where: { $0.id == device.id })
case let .failure(error):
self.loggedInDevices = loggedInDevices.map {
$0.id
== device
.id ? $0.setIsBeingRemoved(false) : $0
}
onError("Failed to log out device", error)
}
continuation.resume()
}
}
)
}
}
),
dismissButtonTitle: "Cancel"
)
},
header: {
AnyView(
VStack(alignment: .leading, spacing: 8) {
if case .tooManyDevices = style {
if canLoginNewDevice {
HStack {
Spacer()
Image.mullvadIconSuccess
Spacer()
}
Text("Super!")
.font(.mullvadBig)
.foregroundStyle(Color.mullvadTextPrimary)
} else {
HStack {
Spacer()
Image.mullvadIconFail
Spacer()
}
Text("Too many devices")
.font(.mullvadBig)
.foregroundStyle(Color.mullvadTextPrimary)
}
}
Text(bodyText)
.foregroundColor(.mullvadTextPrimary)
.opacity(0.6)
.font(.mullvadTinySemiBold)
})
}
)
Spacer()
if case let .tooManyDevices(backToLogin) = style {
MainButton(
text: "Continue with login",
style: .success
) {
backToLogin(true)
}
.accessibilityIdentifier(AccessibilityIdentifier.continueWithLoginButton)
.disabled(!canLoginNewDevice)
.padding(EdgeInsets(UIMetrics.contentLayoutMargins))
}
}
.mullvadAlert(item: $deviceManagementAlert)
.background(Color.mullvadBackground)
.task {
fetchDevices()
}
.accessibilityElement(children: .contain)
.accessibilityIdentifier(
.deviceManagementView
)
}
}
#Preview {
Text("Too many devices")
.sheet(isPresented: .constant(true)) {
DeviceManagementView(
deviceManaging: MockDeviceManaging(),
style: .tooManyDevices { _ in },
onError: { _, _ in }
)
}
}
#Preview("Too many devices: Success") {
Text("")
.sheet(isPresented: .constant(true)) {
DeviceManagementView(
deviceManaging: MockDeviceManaging(
devicesToReturn: ApplicationConfiguration.maxAllowedDevices - 1
),
style: .tooManyDevices { _ in },
onError: { _, _ in }
)
}
}
#Preview("Device Management") {
Text("")
.sheet(isPresented: .constant(true)) {
NavigationView {
DeviceManagementView(
deviceManaging: MockDeviceManaging(),
style: .deviceManagement,
onError: { _, _ in }
)
.navigationTitle("Manage Devices")
}
}
}
|