Open History to see the last commit
1import SwiftUI
2
3public struct DevicesView: View {
4 private let manager: any FeatureDeviceManaging
5
6 @State private var sessions: [FeatureDeviceSession] = []
7 @State private var isLoading = true
8 @State private var isRevoking = false
9 @State private var errorMessage: String?
10 @State private var revokeTarget: FeatureDeviceSession?
11 @State private var showingRevokeOthers = false
12
13 public init(manager: any FeatureDeviceManaging) {
14 self.manager = manager
15 }
16
17 public var body: some View {
18 Group {
19 if isLoading, sessions.isEmpty {
20 Text("Loading devices")
21 .font(T3Typography.supporting)
22 .foregroundStyle(T3Colors.textTertiary)
23 } else if let errorMessage, sessions.isEmpty {
24 ContentUnavailableView {
25 Label("Couldn’t load devices", systemImage: "exclamationmark.circle")
26 } description: {
27 Text(errorMessage)
28 } actions: {
29 Button("Try again") {
30 Task { await reload() }
31 }
32 .buttonStyle(.borderedProminent)
33 }
34 } else if sessions.isEmpty {
35 ContentUnavailableView {
36 Label("No devices found", systemImage: "laptopcomputer.and.iphone")
37 } description: {
38 Text("Device sessions will appear here when this server supports access management.")
39 }
40 } else {
41 deviceList
42 }
43 }
44 .frame(maxWidth: .infinity, maxHeight: .infinity)
45 .background(T3Colors.background)
46 .navigationTitle("Devices")
47 .navigationBarTitleDisplayMode(.inline)
48 .toolbar {
49 if !otherSessions.isEmpty {
50 ToolbarItem(placement: .primaryAction) {
51 Menu {
52 Button(role: .destructive) {
53 showingRevokeOthers = true
54 } label: {
55 Label("Remove all other devices", systemImage: "rectangle.stack.badge.minus")
56 }
57 } label: {
58 Image(systemName: "ellipsis.circle")
59 }
60 .disabled(isRevoking)
61 .accessibilityLabel("Device actions")
62 }
63 }
64 }
65 .task {
66 await reload()
67 }
68 .alert(
69 "Remove this device?",
70 isPresented: Binding(
71 get: { revokeTarget != nil },
72 set: { if !$0 { revokeTarget = nil } }
73 ),
74 presenting: revokeTarget
75 ) { device in
76 Button(manager.managesServerSessions ? "Remove access" : "Remove device", role: .destructive) {
77 Task { await revoke(device) }
78 }
79 Button("Cancel", role: .cancel) {}
80 } message: { device in
81 Text(
82 manager.managesServerSessions
83 ? "\(device.displayName) will need a new pairing code to reconnect."
84 : "\(device.displayName) will stop receiving T3 Connect notifications."
85 )
86 }
87 .confirmationDialog(
88 "Remove all other devices?",
89 isPresented: $showingRevokeOthers,
90 titleVisibility: .visible
91 ) {
92 Button("Remove \(otherSessions.count) devices", role: .destructive) {
93 Task { await revokeOthers() }
94 }
95 Button("Cancel", role: .cancel) {}
96 } message: {
97 Text(
98 manager.managesServerSessions
99 ? "Every other phone, tablet, browser, and desktop will be signed out."
100 : "Other registered devices will stop receiving T3 Connect notifications."
101 )
102 }
103 }
104
105 private var deviceList: some View {
106 List {
107 if let currentSession {
108 Section {
109 DeviceSessionRow(session: currentSession)
110 } header: {
111 sectionHeader("This device")
112 }
113 }
114
115 if !otherSessions.isEmpty {
116 Section {
117 ForEach(otherSessions) { session in
118 DeviceSessionRow(session: session)
119 .contentShape(Rectangle())
120 .swipeActions {
121 Button("Remove", role: .destructive) {
122 revokeTarget = session
123 }
124 }
125 .contextMenu {
126 Button(role: .destructive) {
127 revokeTarget = session
128 } label: {
129 Label("Remove access", systemImage: "trash")
130 }
131 }
132 }
133 } header: {
134 sectionHeader("Other devices")
135 }
136 }
137
138 if let errorMessage {
139 Section {
140 VStack(alignment: .leading, spacing: 10) {
141 Label(errorMessage, systemImage: "exclamationmark.circle")
142 .font(T3Typography.control)
143 .foregroundStyle(T3Colors.warning)
144 Button("Try again") {
145 Task { await reload() }
146 }
147 .font(T3Typography.control.weight(.semibold))
148 }
149 .padding(.vertical, 4)
150 }
151 }
152 }
153 .listStyle(.plain)
154 .scrollContentBackground(.hidden)
155 .background(T3Colors.background)
156 .refreshable {
157 await reload()
158 }
159 .overlay(alignment: .top) {
160 if isRevoking {
161 Text("Updating device access")
162 .font(T3Typography.supporting)
163 .foregroundStyle(T3Colors.textTertiary)
164 .padding(.top, 12)
165 }
166 }
167 }
168
169 private func sectionHeader(_ title: String) -> some View {
170 Text(title)
171 .font(T3Typography.navigationTitle)
172 .foregroundStyle(T3Colors.textPrimary)
173 .textCase(nil)
174 .accessibilityAddTraits(.isHeader)
175 }
176
177 private var currentSession: FeatureDeviceSession? {
178 sessions.first(where: \.isCurrent)
179 }
180
181 private var otherSessions: [FeatureDeviceSession] {
182 sessions.filter { !$0.isCurrent }
183 }
184
185 @MainActor
186 private func reload() async {
187 isLoading = true
188 defer { isLoading = false }
189 do {
190 sessions = FeatureDeviceSession.sortedForDisplay(
191 try await manager.loadDeviceSessions()
192 )
193 errorMessage = nil
194 } catch {
195 errorMessage = DeviceManagementErrorCopy.message(for: error)
196 }
197 }
198
199 @MainActor
200 private func revoke(_ session: FeatureDeviceSession) async {
201 isRevoking = true
202 defer {
203 isRevoking = false
204 revokeTarget = nil
205 }
206 do {
207 try await manager.revokeDeviceSession(id: session.id)
208 sessions.removeAll { $0.id == session.id }
209 errorMessage = nil
210 } catch {
211 errorMessage = DeviceManagementErrorCopy.message(for: error)
212 }
213 }
214
215 @MainActor
216 private func revokeOthers() async {
217 isRevoking = true
218 defer { isRevoking = false }
219 do {
220 try await manager.revokeOtherDeviceSessions()
221 sessions.removeAll { !$0.isCurrent }
222 errorMessage = nil
223 } catch {
224 errorMessage = DeviceManagementErrorCopy.message(for: error)
225 }
226 }
227}
228
229private struct DeviceSessionRow: View {
230 let session: FeatureDeviceSession
231
232 var body: some View {
233 HStack(alignment: .top, spacing: 13) {
234 Image(systemName: session.deviceType.systemImage)
235 .font(.system(size: 19, weight: .medium))
236 .foregroundStyle(session.isCurrent ? T3Colors.success : T3Colors.textSecondary)
237 .frame(width: 26, height: 26)
238
239 VStack(alignment: .leading, spacing: 4) {
240 HStack(spacing: 8) {
241 Text(session.displayName)
242 .font(T3Typography.homeTitle)
243 .foregroundStyle(T3Colors.textPrimary)
244 if session.isCurrent {
245 Text("Current")
246 .font(T3Typography.supportingStrong)
247 .foregroundStyle(T3Colors.success)
248 } else if session.isConnected {
249 Text("Online")
250 .font(T3Typography.supportingStrong)
251 .foregroundStyle(T3Colors.success)
252 }
253 }
254
255 if !session.platformDescription.isEmpty {
256 Text(session.platformDescription)
257 .font(T3Typography.supporting)
258 .foregroundStyle(T3Colors.textSecondary)
259 }
260
261 Text(lastSeenDescription)
262 .font(T3Typography.supporting)
263 .foregroundStyle(T3Colors.textSecondary)
264
265 if let ipAddress = session.ipAddress, !ipAddress.isEmpty {
266 Text(ipAddress)
267 .font(T3Typography.tool)
268 .foregroundStyle(T3Colors.textSecondary)
269 }
270 }
271 Spacer(minLength: 8)
272 }
273 .padding(.vertical, 6)
274 .accessibilityElement(children: .combine)
275 }
276
277 private var lastSeenDescription: String {
278 if session.isConnected {
279 return "Active now"
280 }
281 return "Last seen \(session.lastSeenAt.formatted(.relative(presentation: .named)))"
282 }
283}