USB-Meter / USB Meter / Views / ChargedDevices / ChargedDeviceDetailView.swift
Newer Older
1123 lines | 47.353kb
Bogdan Timofte authored a month ago
1
//
2
//  ChargedDeviceDetailView.swift
3
//  USB Meter
4
//
5
//  Created by Codex on 10/04/2026.
6
//
7

            
8
import SwiftUI
9

            
10
struct ChargedDeviceDetailView: View {
11
    @EnvironmentObject private var appData: AppData
12
    @Environment(\.dismiss) private var dismiss
13
    @State private var editorVisibility = false
14
    @State private var targetNotificationEditorVisibility = false
15
    @State private var pendingSessionDeletion: ChargeSessionSummary?
Bogdan Timofte authored a month ago
16
    @State private var pendingCheckpointDeletion: ChargeCheckpointSummary?
Bogdan Timofte authored a month ago
17
    @State private var pendingSessionStopRequest: DeviceSessionStopRequest?
Bogdan Timofte authored a month ago
18
    @State private var deleteConfirmationVisibility = false
19

            
20
    let chargedDeviceID: UUID
21

            
22
    var body: some View {
23
        Group {
24
            if let chargedDevice = appData.chargedDeviceSummary(id: chargedDeviceID) {
25
                ScrollView {
26
                    VStack(spacing: 18) {
27
                        headerCard(chargedDevice)
28
                        insightsCard(chargedDevice)
29

            
Bogdan Timofte authored a month ago
30
                        if chargedDevice.isCharger {
31
                            standbyPowerCard(chargedDevice)
32
                        }
33

            
Bogdan Timofte authored a month ago
34
                        if let activeSession = chargedDevice.activeSession {
35
                            activeSessionCard(activeSession, chargedDevice: chargedDevice)
36
                        }
37

            
38
                        if let curveSession = preferredStoredCurveSession(for: chargedDevice) {
39
                            storedCurveCard(curveSession)
40
                        }
41

            
42
                        if !chargedDevice.capacityHistory.isEmpty {
43
                            capacityEvolutionCard(chargedDevice)
44
                        }
45

            
46
                        if !chargedDevice.typicalCurve.isEmpty {
47
                            typicalCurveCard(chargedDevice)
48
                        }
49

            
50
                        if !chargedDevice.sessions.isEmpty {
51
                            sessionsCard(chargedDevice)
52
                        }
53
                    }
54
                    .padding()
55
                }
56
                .background(
57
                    LinearGradient(
58
                        colors: [tint(for: chargedDevice).opacity(0.18), Color.clear],
59
                        startPoint: .topLeading,
60
                        endPoint: .bottomTrailing
61
                    )
62
                    .ignoresSafeArea()
63
                )
64
                .navigationTitle(chargedDevice.name)
65
                .toolbar {
66
                    ToolbarItemGroup(placement: .primaryAction) {
67
                        Button("Edit") {
68
                            editorVisibility = true
69
                        }
70
                        Button(role: .destructive) {
71
                            deleteConfirmationVisibility = true
72
                        } label: {
73
                            Image(systemName: "trash")
74
                        }
75
                    }
76
                }
77
            } else {
78
                Text("This device is no longer available.")
79
                    .foregroundColor(.secondary)
80
                    .navigationTitle("Device")
81
            }
82
        }
83
        .sheet(isPresented: $editorVisibility) {
84
            if let chargedDevice = appData.chargedDeviceSummary(id: chargedDeviceID) {
Bogdan Timofte authored a month ago
85
                if chargedDevice.isCharger {
86
                    ChargerEditorSheetView(
87
                        appData: appData,
88
                        chargedDevice: chargedDevice
89
                    )
90
                } else {
91
                    ChargedDeviceEditorSheetView(
92
                        meterMACAddress: nil,
93
                        chargedDevice: chargedDevice
94
                    )
95
                    .environmentObject(appData)
96
                }
Bogdan Timofte authored a month ago
97
            }
98
        }
99
        .sheet(isPresented: $targetNotificationEditorVisibility) {
100
            if let activeSession = appData.chargedDeviceSummary(id: chargedDeviceID)?.activeSession {
101
                ChargedDeviceTargetNotificationEditorSheetView(
102
                    sessionID: activeSession.id,
103
                    initialTargetPercent: activeSession.targetBatteryPercent
104
                )
105
                .environmentObject(appData)
106
            }
107
        }
Bogdan Timofte authored a month ago
108
        .sheet(item: $pendingSessionStopRequest) { request in
109
            ChargeSessionCompletionSheetView(
110
                sessionID: request.sessionID,
111
                title: request.title,
112
                confirmTitle: request.confirmTitle,
113
                explanation: request.explanation
114
            )
115
            .environmentObject(appData)
116
        }
Bogdan Timofte authored a month ago
117
        .alert(item: $pendingSessionDeletion) { session in
118
            Alert(
119
                title: Text("Delete Session?"),
120
                message: Text("Deleting this charging session also recalculates capacity and every derived metric that used it."),
121
                primaryButton: .destructive(Text("Delete")) {
122
                    _ = appData.deleteChargeSession(sessionID: session.id)
123
                },
124
                secondaryButton: .cancel()
125
            )
126
        }
Bogdan Timofte authored a month ago
127
        .alert(item: $pendingCheckpointDeletion) { checkpoint in
128
            Alert(
129
                title: Text("Delete Battery Checkpoint"),
130
                message: Text("Remove the checkpoint at \(checkpoint.timestamp.format()) with \(checkpoint.batteryPercent.format(decimalDigits: 0))% from this session?"),
131
                primaryButton: .destructive(Text("Delete")) {
132
                    _ = appData.deleteBatteryCheckpoint(
133
                        checkpointID: checkpoint.id,
134
                        for: checkpoint.sessionID
135
                    )
136
                },
137
                secondaryButton: .cancel()
138
            )
139
        }
Bogdan Timofte authored a month ago
140
        .confirmationDialog("Delete \(deletionTitle)?", isPresented: $deleteConfirmationVisibility, titleVisibility: .visible) {
141
            Button("Delete", role: .destructive) {
142
                if appData.deleteChargedDevice(id: chargedDeviceID) {
143
                    dismiss()
144
                }
145
            }
146
            Button("Cancel", role: .cancel) {}
147
        } message: {
148
            Text(deletionMessage)
149
        }
150
    }
151

            
152
    private func headerCard(_ chargedDevice: ChargedDeviceSummary) -> some View {
153
        HStack(alignment: .top, spacing: 18) {
154
            ChargedDeviceQRCodeView(qrIdentifier: chargedDevice.qrIdentifier, side: 118)
155

            
156
            VStack(alignment: .leading, spacing: 10) {
Bogdan Timofte authored a month ago
157
                ChargedDeviceIdentityLabelView(
158
                    chargedDevice: chargedDevice,
159
                    iconPointSize: 22
160
                )
161
                .font(.title3.weight(.bold))
Bogdan Timofte authored a month ago
162

            
Bogdan Timofte authored a month ago
163
                Text(chargedDevice.identityTitle)
Bogdan Timofte authored a month ago
164
                    .font(.subheadline.weight(.semibold))
165
                    .foregroundColor(.secondary)
166

            
167
                if let meterMAC = chargedDevice.lastAssociatedMeterMAC {
168
                    Text("Default meter: \(meterMAC)")
169
                        .font(.caption)
170
                        .foregroundColor(.secondary)
171
                }
172

            
173
                Text(chargedDevice.qrIdentifier)
174
                    .font(.caption2.monospaced())
175
                    .foregroundColor(.secondary)
176
                    .textSelection(.enabled)
177
            }
178

            
179
            Spacer(minLength: 0)
180
        }
181
        .frame(maxWidth: .infinity, alignment: .leading)
182
        .padding(18)
183
        .meterCard(tint: tint(for: chargedDevice), fillOpacity: 0.20, strokeOpacity: 0.26, cornerRadius: 20)
184
    }
185

            
186
    private func insightsCard(_ chargedDevice: ChargedDeviceSummary) -> some View {
187
        MeterInfoCardView(title: "Insights", tint: tint(for: chargedDevice)) {
Bogdan Timofte authored a month ago
188
            if chargedDevice.isCharger {
189
                chargerInsights(chargedDevice)
190
            } else {
191
                deviceInsights(chargedDevice)
192
            }
193

            
194
            if let notes = chargedDevice.notes, !notes.isEmpty {
195
                Divider()
196
                Text(notes)
197
                    .font(.footnote)
198
                    .foregroundColor(.secondary)
199
                    .frame(maxWidth: .infinity, alignment: .leading)
200
            }
201
        }
202
    }
203

            
204
    @ViewBuilder
205
    private func deviceInsights(_ chargedDevice: ChargedDeviceSummary) -> some View {
Bogdan Timofte authored a month ago
206
        if chargedDevice.hasMultipleChargingStateModes {
207
            MeterInfoRowView(
208
                label: "Charge Modes",
209
                value: chargedDevice.chargingStateAvailability.title
210
            )
211
        }
212
        if chargedDevice.hasMultipleChargingTransports {
213
            MeterInfoRowView(
214
                label: "Charging Support",
215
                value: chargedDevice.supportedChargingModes.map(\.title).joined(separator: " + ")
216
            )
217
        }
218
        if chargedDevice.showsWirelessProfileDetails {
Bogdan Timofte authored a month ago
219
            MeterInfoRowView(
Bogdan Timofte authored a month ago
220
                label: "Wireless Profile",
221
                value: chargedDevice.wirelessChargingProfile.title
Bogdan Timofte authored a month ago
222
            )
Bogdan Timofte authored a month ago
223
        }
224

            
225
        ForEach(completionSessionKinds(for: chargedDevice), id: \.rawValue) { sessionKind in
Bogdan Timofte authored a month ago
226
            MeterInfoRowView(
Bogdan Timofte authored a month ago
227
                label: completionCurrentLabel(for: chargedDevice, sessionKind: sessionKind),
Bogdan Timofte authored a month ago
228
                value: completionCurrentDescription(for: chargedDevice, sessionKind: sessionKind)
Bogdan Timofte authored a month ago
229
            )
Bogdan Timofte authored a month ago
230
        }
231
        MeterInfoRowView(
232
            label: "Estimated Capacity",
233
            value: chargedDevice.estimatedBatteryCapacityWh.map { "\($0.format(decimalDigits: 2)) Wh" } ?? "Not enough data"
234
        )
235
        if let wiredCapacity = chargedDevice.wiredEstimatedBatteryCapacityWh {
Bogdan Timofte authored a month ago
236
            if chargedDevice.hasMultipleChargingTransports {
237
                MeterInfoRowView(
238
                    label: "Wired Capacity",
239
                    value: "\(wiredCapacity.format(decimalDigits: 2)) Wh"
240
                )
241
            }
Bogdan Timofte authored a month ago
242
        }
243
        if let wirelessCapacity = chargedDevice.wirelessEstimatedBatteryCapacityWh {
Bogdan Timofte authored a month ago
244
            if chargedDevice.hasMultipleChargingTransports {
245
                MeterInfoRowView(
246
                    label: "Wireless Capacity",
247
                    value: "\(wirelessCapacity.format(decimalDigits: 2)) Wh"
248
                )
249
            }
Bogdan Timofte authored a month ago
250
        }
Bogdan Timofte authored a month ago
251
        if let wirelessEfficiencyFactor = chargedDevice.wirelessChargerEfficiencyFactor,
252
           chargedDevice.showsWirelessProfileDetails {
Bogdan Timofte authored a month ago
253
            MeterInfoRowView(
254
                label: "Wireless Efficiency",
255
                value: "\(Int((wirelessEfficiencyFactor * 100).rounded()))%"
256
            )
257
        }
258
        MeterInfoRowView(
259
            label: "Charge Sessions",
260
            value: "\(chargedDevice.sessionCount)"
261
        )
262
    }
Bogdan Timofte authored a month ago
263

            
Bogdan Timofte authored a month ago
264
    @ViewBuilder
265
    private func chargerInsights(_ chargedDevice: ChargedDeviceSummary) -> some View {
Bogdan Timofte authored a month ago
266
        if let chargerType = chargedDevice.chargerType {
267
            MeterInfoRowView(
268
                label: "Type",
269
                value: chargerType.title
270
            )
271
        }
Bogdan Timofte authored a month ago
272
        if !chargedDevice.chargerObservedVoltageSelections.isEmpty {
Bogdan Timofte authored a month ago
273
            MeterInfoRowView(
Bogdan Timofte authored a month ago
274
                label: "Observed Voltages",
275
                value: chargedDevice.chargerObservedVoltageSelections
276
                    .map { "\($0.format(decimalDigits: 1)) V" }
277
                    .joined(separator: ", ")
Bogdan Timofte authored a month ago
278
            )
Bogdan Timofte authored a month ago
279
        }
280
        if let chargerIdleCurrentAmps = chargedDevice.chargerIdleCurrentAmps {
Bogdan Timofte authored a month ago
281
            MeterInfoRowView(
Bogdan Timofte authored a month ago
282
                label: "Idle Current",
283
                value: "\(chargerIdleCurrentAmps.format(decimalDigits: 2)) A"
Bogdan Timofte authored a month ago
284
            )
Bogdan Timofte authored a month ago
285
        }
286
        if let chargerEfficiencyFactor = chargedDevice.chargerEfficiencyFactor {
Bogdan Timofte authored a month ago
287
            MeterInfoRowView(
Bogdan Timofte authored a month ago
288
                label: "Efficiency",
289
                value: "\(Int((chargerEfficiencyFactor * 100).rounded()))%"
Bogdan Timofte authored a month ago
290
            )
Bogdan Timofte authored a month ago
291
        }
292
        if let chargerMaximumPowerWatts = chargedDevice.chargerMaximumPowerWatts {
293
            MeterInfoRowView(
294
                label: "Max Power",
295
                value: "\(chargerMaximumPowerWatts.format(decimalDigits: 2)) W"
296
            )
297
        }
Bogdan Timofte authored a month ago
298
        if let latestStandbyPowerMeasurement = chargedDevice.latestStandbyPowerMeasurement {
299
            MeterInfoRowView(
300
                label: "Standby Power",
301
                value: "\(latestStandbyPowerMeasurement.averagePowerWatts.format(decimalDigits: 3)) W"
302
            )
303
            MeterInfoRowView(
304
                label: "Standby Projection",
305
                value: standbyEnergyLabel(latestStandbyPowerMeasurement.projectedYearlyEnergyWh) + " / year"
306
            )
307
        }
Bogdan Timofte authored a month ago
308
        MeterInfoRowView(
309
            label: "Wireless Sessions",
310
            value: "\(chargedDevice.sessionCount)"
311
        )
Bogdan Timofte authored a month ago
312

            
Bogdan Timofte authored a month ago
313
        if chargedDevice.chargerIdleCurrentAmps == nil {
314
            Text("Idle current is missing. Wireless sessions that use this charger can still be recorded, but they cannot learn or auto-apply the wireless stop threshold yet.")
315
                .font(.caption2)
316
                .foregroundColor(.orange)
Bogdan Timofte authored a month ago
317
        }
318
    }
319

            
Bogdan Timofte authored a month ago
320
    private func standbyPowerCard(_ chargedDevice: ChargedDeviceSummary) -> some View {
321
        let latestMeasurement = chargedDevice.latestStandbyPowerMeasurement
322

            
323
        return MeterInfoCardView(
324
            title: "Standby Power",
325
            tint: .orange
326
        ) {
327
            if standbyMeasurementMeters.isEmpty {
328
                Text("Connect a meter first. Standby measurement is launched from a live meter feed, so only currently available meters can be selected here.")
329
                    .font(.footnote)
330
                    .foregroundColor(.secondary)
331
                    .frame(maxWidth: .infinity, alignment: .leading)
332
            } else {
333
                NavigationLink(
334
                    destination: ChargerStandbyPowerWizardView(
335
                        preferredChargerID: chargedDevice.id,
336
                        locksChargerSelection: true
337
                    )
338
                ) {
339
                    Label("New Measurement", systemImage: "plus.circle.fill")
340
                        .font(.subheadline.weight(.semibold))
341
                        .foregroundColor(.orange)
342
                }
343
                .buttonStyle(.plain)
344
            }
345

            
346
            if let latestMeasurement {
347
                Divider()
348

            
349
                NavigationLink(
350
                    destination: ChargerStandbyPowerMeasurementDetailView(
351
                        chargerID: chargedDevice.id,
352
                        measurementID: latestMeasurement.id
353
                    )
354
                ) {
355
                    VStack(alignment: .leading, spacing: 8) {
356
                        HStack {
357
                            Text("Latest Measurement")
358
                                .font(.subheadline.weight(.semibold))
359
                                .foregroundColor(.primary)
360
                            Spacer()
361
                            Text("\(latestMeasurement.averagePowerWatts.format(decimalDigits: 3)) W")
362
                                .font(.subheadline.weight(.bold))
363
                                .foregroundColor(.primary)
364
                                .monospacedDigit()
365
                        }
366

            
367
                        Text(
368
                            "\(latestMeasurement.endedAt.format()) • \(latestMeasurement.sampleCount) samples • \(standbyEnergyLabel(latestMeasurement.projectedYearlyEnergyWh)) / year"
369
                        )
370
                        .font(.caption)
371
                        .foregroundColor(.secondary)
372
                    }
373
                }
374
                .buttonStyle(.plain)
375
            }
376

            
377
            if chargedDevice.standbyPowerMeasurements.isEmpty == false {
378
                Divider()
379

            
380
                NavigationLink(
381
                    destination: ChargerStandbyPowerMeasurementsView(chargerID: chargedDevice.id)
382
                ) {
383
                    Label("View Saved Measurements", systemImage: "clock.arrow.trianglehead.counterclockwise.rotate.90")
384
                        .font(.subheadline.weight(.semibold))
385
                        .foregroundColor(.blue)
386
                }
387
                .buttonStyle(.plain)
388
            }
389
        }
390
    }
391

            
Bogdan Timofte authored a month ago
392
    private func activeSessionCard(
393
        _ activeSession: ChargeSessionSummary,
394
        chargedDevice: ChargedDeviceSummary
395
    ) -> some View {
Bogdan Timofte authored a month ago
396
        MeterInfoCardView(title: "Open Session", tint: .green) {
Bogdan Timofte authored a month ago
397
            MeterInfoRowView(label: "Started", value: activeSession.startedAt.format())
Bogdan Timofte authored a month ago
398
            MeterInfoRowView(label: "Status", value: activeSession.status.title)
Bogdan Timofte authored a month ago
399
            MeterInfoRowView(label: "Charging Type", value: activeSession.chargingTransportMode.title)
Bogdan Timofte authored a month ago
400
            MeterInfoRowView(label: "Charging Mode", value: activeSession.chargingStateMode.title)
Bogdan Timofte authored a month ago
401
            MeterInfoRowView(label: "Battery Energy", value: "\(activeSession.effectiveOrMeasuredEnergyWh.format(decimalDigits: 3)) Wh")
402
            if activeSession.chargingTransportMode == .wireless,
403
               let effectiveBatteryEnergyWh = activeSession.effectiveBatteryEnergyWh,
404
               abs(effectiveBatteryEnergyWh - activeSession.measuredEnergyWh) > 0.01 {
405
                MeterInfoRowView(label: "Charger Energy", value: "\(activeSession.measuredEnergyWh.format(decimalDigits: 3)) Wh")
406
            }
Bogdan Timofte authored a month ago
407
            MeterInfoRowView(label: "Auto Stop", value: autoStopDescription(for: activeSession))
Bogdan Timofte authored a month ago
408
            MeterInfoRowView(label: "Source", value: activeSession.sourceMode.title)
409
            if chargedDevice.isCharger == false,
410
               let chargerID = activeSession.chargerID,
411
               let charger = appData.chargedDeviceSummary(id: chargerID) {
412
                MeterInfoRowView(label: "Wireless Charger", value: charger.name)
413
            }
414
            if let maximumObservedCurrentAmps = activeSession.maximumObservedCurrentAmps {
415
                MeterInfoRowView(label: "Max Current", value: "\(maximumObservedCurrentAmps.format(decimalDigits: 2)) A")
416
            }
417
            if let maximumObservedPowerWatts = activeSession.maximumObservedPowerWatts {
418
                MeterInfoRowView(label: "Max Power", value: "\(maximumObservedPowerWatts.format(decimalDigits: 2)) W")
419
            }
420
            if activeSession.chargingTransportMode == .wired,
421
               let maximumObservedVoltageVolts = activeSession.maximumObservedVoltageVolts {
422
                MeterInfoRowView(label: "Max Voltage", value: "\(maximumObservedVoltageVolts.format(decimalDigits: 2)) V")
423
            }
424
            if chargedDevice.isCharger, let selectedSourceVoltageVolts = activeSession.selectedSourceVoltageVolts {
425
                MeterInfoRowView(label: "Selected Voltage", value: "\(selectedSourceVoltageVolts.format(decimalDigits: 2)) V")
426
            }
427
            if let targetBatteryPercent = activeSession.targetBatteryPercent {
428
                MeterInfoRowView(
429
                    label: "Target Notification",
430
                    value: "\(targetBatteryPercent.format(decimalDigits: 0))%"
431
                )
432
            }
Bogdan Timofte authored a month ago
433
            if let sessionWarning = sessionWarning(for: activeSession) {
434
                Text(sessionWarning)
435
                    .font(.caption2)
436
                    .foregroundColor(.orange)
437
            }
Bogdan Timofte authored a month ago
438
            if let wirelessSessionHint = wirelessSessionHint(for: activeSession) {
439
                Text(wirelessSessionHint)
440
                    .font(.caption2)
441
                    .foregroundColor(activeSession.shouldWarnAboutLowWirelessEfficiency ? .orange : .secondary)
442
            }
443
            if let batteryPrediction = chargedDevice.batteryLevelPrediction(for: activeSession) {
444
                MeterInfoRowView(
445
                    label: "Predicted Battery",
446
                    value: "\(batteryPrediction.predictedPercent.format(decimalDigits: 0))%"
447
                )
448
                Text(
449
                    "Anchored to \(batteryPrediction.anchorDescription) at \(batteryPrediction.anchorPercent.format(decimalDigits: 0))% using \(batteryPrediction.estimatedCapacityWh.format(decimalDigits: 2)) Wh estimated capacity."
450
                )
451
                .font(.caption2)
452
                .foregroundColor(.secondary)
453
            }
454

            
Bogdan Timofte authored a month ago
455
            BatteryCheckpointSectionView(
456
                sessionID: activeSession.id,
457
                checkpoints: activeSession.checkpoints,
458
                message: "The checkpoint is stored on the active charge session and is used for capacity estimation and charge-level prediction.",
459
                canAddCheckpoint: appData.canAddBatteryCheckpoint(to: activeSession.id),
460
                requirementMessage: appData.batteryCheckpointCaptureRequirementMessage(for: activeSession.id),
461
                effectiveEnergyWhOverride: nil,
462
                measuredChargeAhOverride: nil,
463
                onDelete: { checkpoint in
464
                    pendingCheckpointDeletion = checkpoint
Bogdan Timofte authored a month ago
465
                }
Bogdan Timofte authored a month ago
466
            )
Bogdan Timofte authored a month ago
467

            
Bogdan Timofte authored a month ago
468
            Button(activeSession.targetBatteryPercent == nil ? "Set Target Notification" : "Change Target Notification") {
469
                targetNotificationEditorVisibility = true
470
            }
471
            .frame(maxWidth: .infinity)
472
            .padding(.vertical, 10)
473
            .meterCard(tint: .indigo, fillOpacity: 0.16, strokeOpacity: 0.22, cornerRadius: 14)
474
            .buttonStyle(.plain)
475

            
476
            if activeSession.targetBatteryPercent != nil {
477
                Button("Clear Target Notification") {
478
                    _ = appData.setTargetBatteryPercent(nil, for: activeSession.id)
479
                }
480
                .frame(maxWidth: .infinity)
481
                .padding(.vertical, 10)
482
                .meterCard(tint: .secondary, fillOpacity: 0.12, strokeOpacity: 0.18, cornerRadius: 14)
483
                .buttonStyle(.plain)
484
            }
485

            
Bogdan Timofte authored a month ago
486
            if activeSession.status == .active {
487
                Button("Pause Session") {
488
                    _ = appData.pauseChargeSession(sessionID: activeSession.id)
489
                }
490
                .frame(maxWidth: .infinity)
491
                .padding(.vertical, 10)
492
                .meterCard(tint: .orange, fillOpacity: 0.16, strokeOpacity: 0.22, cornerRadius: 14)
493
                .buttonStyle(.plain)
494
            } else if activeSession.status == .paused {
495
                Button("Resume Session") {
496
                    _ = appData.resumeChargeSession(sessionID: activeSession.id)
497
                }
498
                .frame(maxWidth: .infinity)
499
                .padding(.vertical, 10)
500
                .meterCard(tint: .blue, fillOpacity: 0.16, strokeOpacity: 0.22, cornerRadius: 14)
501
                .buttonStyle(.plain)
502

            
503
                Text("Paused sessions close automatically after 10 minutes.")
504
                    .font(.caption2)
505
                    .foregroundColor(.secondary)
506
            }
507

            
508
            Button(activeSession.requiresCompletionConfirmation ? "Finish Session With Checkpoint" : "Stop Session") {
509
                pendingSessionStopRequest = DeviceSessionStopRequest(
510
                    sessionID: activeSession.id,
511
                    title: activeSession.requiresCompletionConfirmation ? "Finish Session" : "Stop Session",
512
                    confirmTitle: activeSession.requiresCompletionConfirmation ? "Finish" : "Stop",
513
                    explanation: "Add the final battery checkpoint before closing this session."
514
                )
Bogdan Timofte authored a month ago
515
            }
516
            .frame(maxWidth: .infinity)
517
            .padding(.vertical, 10)
Bogdan Timofte authored a month ago
518
            .meterCard(tint: .red, fillOpacity: 0.16, strokeOpacity: 0.22, cornerRadius: 14)
Bogdan Timofte authored a month ago
519
            .buttonStyle(.plain)
520

            
521
            if activeSession.requiresCompletionConfirmation {
522
                Divider()
523
                if let contradictionPercent = activeSession.completionContradictionPercent {
524
                    Text("Current says charging may have stopped, but the estimated battery level is only \(contradictionPercent.format(decimalDigits: 0))%.")
525
                        .font(.caption2)
526
                        .foregroundColor(.secondary)
527
                }
528

            
529
                Button("Keep Monitoring") {
530
                    _ = appData.continueChargeSessionMonitoring(sessionID: activeSession.id)
531
                }
532
                .frame(maxWidth: .infinity)
533
                .padding(.vertical, 10)
534
                .meterCard(tint: .blue, fillOpacity: 0.16, strokeOpacity: 0.22, cornerRadius: 14)
535
                .buttonStyle(.plain)
536
            }
537
        }
538
    }
539

            
540
    private func capacityEvolutionCard(_ chargedDevice: ChargedDeviceSummary) -> some View {
541
        VStack(alignment: .leading, spacing: 12) {
542
            Text("Capacity Evolution")
543
                .font(.headline)
544

            
545
            ForEach(chargedDevice.capacityHistory.suffix(6)) { point in
546
                HStack {
547
                    Text(point.timestamp.format())
548
                        .font(.caption)
549
                        .foregroundColor(.secondary)
550
                    Spacer()
Bogdan Timofte authored a month ago
551
                    if chargedDevice.shouldShowChargingTransport(point.chargingTransportMode) {
552
                        Text(point.chargingTransportMode.title)
553
                            .font(.caption2)
554
                            .foregroundColor(.secondary)
555
                        Text("•")
556
                            .foregroundColor(.secondary)
557
                    }
Bogdan Timofte authored a month ago
558
                    Text("\(point.capacityWh.format(decimalDigits: 2)) Wh")
559
                        .font(.footnote.weight(.semibold))
560
                }
561
            }
562
        }
563
        .frame(maxWidth: .infinity, alignment: .leading)
564
        .padding(18)
565
        .meterCard(tint: .orange, fillOpacity: 0.14, strokeOpacity: 0.20, cornerRadius: 18)
566
    }
567

            
568
    private func preferredStoredCurveSession(for chargedDevice: ChargedDeviceSummary) -> ChargeSessionSummary? {
569
        if let activeSession = chargedDevice.activeSession, !activeSession.aggregatedSamples.isEmpty {
570
            return activeSession
571
        }
572

            
573
        return chargedDevice.sessions.first(where: { !$0.aggregatedSamples.isEmpty })
574
    }
575

            
576
    private func storedCurveCard(_ session: ChargeSessionSummary) -> some View {
577
        let currentSeries = storedSeriesSnapshot(
578
            from: session.aggregatedSamples,
579
            minimumYSpan: 0.15
580
        ) { $0.averageCurrentAmps }
581
        let energySeries = storedSeriesSnapshot(
582
            from: session.aggregatedSamples,
583
            minimumYSpan: 0.2
584
        ) { $0.measuredEnergyWh }
585

            
586
        return VStack(alignment: .leading, spacing: 14) {
587
            HStack(alignment: .firstTextBaseline) {
588
                VStack(alignment: .leading, spacing: 4) {
Bogdan Timofte authored a month ago
589
                    HStack(spacing: 8) {
590
                        Text("Stored Session Curve")
591
                            .font(.headline)
592
                        ContextInfoButton(
593
                            title: "Stored Session Curve",
594
                            message: "Database storage and iCloud sync use 300 aggregated points per hour. Active sessions persist their partial aggregated curve continuously, so a reconnect or restart can restore the stored progress."
595
                        )
596
                    }
Bogdan Timofte authored a month ago
597
                    Text(session.status.isOpen ? "Open session, persisted as aggregated samples." : "Most recent persisted session at aggregated resolution.")
Bogdan Timofte authored a month ago
598
                        .font(.caption)
599
                        .foregroundColor(.secondary)
600
                }
601

            
602
                Spacer()
603

            
604
                Text("\(session.aggregatedSamples.count) points")
605
                    .font(.caption.weight(.semibold))
606
                    .foregroundColor(.secondary)
607
            }
608

            
609
            if let currentSeries {
610
                storedSeriesChart(
611
                    title: "Current",
612
                    unit: "A",
613
                    strokeColor: .blue,
614
                    snapshot: currentSeries
615
                )
616
            }
617

            
618
            if let energySeries {
619
                storedSeriesChart(
620
                    title: "Energy",
621
                    unit: "Wh",
622
                    strokeColor: .teal,
623
                    areaChart: true,
624
                    snapshot: energySeries
625
                )
626
            }
627

            
628
        }
629
        .frame(maxWidth: .infinity, alignment: .leading)
630
        .padding(18)
631
        .meterCard(tint: .indigo, fillOpacity: 0.12, strokeOpacity: 0.18, cornerRadius: 18)
632
    }
633

            
634
    private func typicalCurveCard(_ chargedDevice: ChargedDeviceSummary) -> some View {
635
        VStack(alignment: .leading, spacing: 12) {
636
            Text("Typical Charge Curve")
637
                .font(.headline)
638

            
639
            ForEach(chargedDevice.typicalCurve) { point in
640
                HStack {
641
                    Text("\(point.percentBin)%")
642
                        .font(.footnote.weight(.semibold))
643
                    Spacer()
644
                    Text("\(point.averageEnergyWh.format(decimalDigits: 2)) Wh")
645
                        .font(.caption.weight(.semibold))
646
                    Text("•")
647
                        .foregroundColor(.secondary)
648
                    Text("\(point.sampleCount) sample\(point.sampleCount == 1 ? "" : "s")")
649
                        .font(.caption2)
650
                        .foregroundColor(.secondary)
651
                }
652
            }
653
        }
654
        .frame(maxWidth: .infinity, alignment: .leading)
655
        .padding(18)
656
        .meterCard(tint: .blue, fillOpacity: 0.14, strokeOpacity: 0.20, cornerRadius: 18)
657
    }
658

            
659
    private func sessionsCard(_ chargedDevice: ChargedDeviceSummary) -> some View {
660
        VStack(alignment: .leading, spacing: 12) {
Bogdan Timofte authored a month ago
661
            HStack(spacing: 8) {
662
                Text("Charge Sessions")
663
                    .font(.headline)
664
                ContextInfoButton(
665
                    title: "Charge Sessions",
666
                    message: "Use these summaries to spot odd sessions quickly before they influence device estimates."
667
                )
668
            }
Bogdan Timofte authored a month ago
669

            
670
            ForEach(chargedDevice.sessions, id: \.id) { session in
671
                VStack(alignment: .leading, spacing: 6) {
672
                    HStack(alignment: .firstTextBaseline, spacing: 10) {
673
                        Text(session.startedAt.format())
674
                            .font(.caption.weight(.semibold))
675
                        Text(session.status.title)
676
                            .font(.caption2.weight(.semibold))
677
                            .padding(.horizontal, 8)
678
                            .padding(.vertical, 4)
679
                            .background(
680
                                Capsule()
681
                                    .fill(statusTint(for: session).opacity(0.16))
682
                            )
683
                        Spacer()
684
                        Button {
685
                            pendingSessionDeletion = session
686
                        } label: {
687
                            Image(systemName: "trash")
688
                                .font(.caption.weight(.semibold))
689
                                .foregroundColor(.red)
690
                                .padding(8)
691
                                .background(
692
                                    Circle()
693
                                        .fill(Color.red.opacity(0.10))
694
                                )
695
                        }
696
                        .buttonStyle(.plain)
697
                    }
698

            
699
                    Text(sessionSummaryLine(session))
700
                        .font(.caption2)
701
                        .foregroundColor(.secondary)
702

            
703
                    MeterInfoRowView(
704
                        label: "Duration",
705
                        value: sessionDurationText(session)
706
                    )
707
                    MeterInfoRowView(
708
                        label: "Energy",
709
                        value: "\(session.effectiveOrMeasuredEnergyWh.format(decimalDigits: 2)) Wh"
710
                    )
711
                    if session.chargingTransportMode == .wireless,
712
                       let effectiveBatteryEnergyWh = session.effectiveBatteryEnergyWh,
713
                       abs(effectiveBatteryEnergyWh - session.measuredEnergyWh) > 0.01 {
714
                        MeterInfoRowView(
715
                            label: "Charger Energy",
716
                            value: "\(session.measuredEnergyWh.format(decimalDigits: 2)) Wh"
717
                        )
718
                    }
719
                    if let maximumObservedCurrentAmps = session.maximumObservedCurrentAmps {
720
                        MeterInfoRowView(
721
                            label: "Max Current",
722
                            value: "\(maximumObservedCurrentAmps.format(decimalDigits: 2)) A"
723
                        )
724
                    }
725
                    if let maximumObservedPowerWatts = session.maximumObservedPowerWatts {
726
                        MeterInfoRowView(
727
                            label: "Max Power",
728
                            value: "\(maximumObservedPowerWatts.format(decimalDigits: 2)) W"
729
                        )
730
                    }
731
                    if session.chargingTransportMode == .wired,
732
                       let maximumObservedVoltageVolts = session.maximumObservedVoltageVolts {
733
                        MeterInfoRowView(
734
                            label: "Max Voltage",
735
                            value: "\(maximumObservedVoltageVolts.format(decimalDigits: 2)) V"
736
                        )
737
                    }
738
                    if chargedDevice.isCharger, let selectedSourceVoltageVolts = session.selectedSourceVoltageVolts {
739
                        MeterInfoRowView(
740
                            label: "Selected Voltage",
741
                            value: "\(selectedSourceVoltageVolts.format(decimalDigits: 2)) V"
742
                        )
743
                    }
744
                    if chargedDevice.isCharger == false,
745
                       let chargerID = session.chargerID,
746
                       let charger = appData.chargedDeviceSummary(id: chargerID) {
747
                        MeterInfoRowView(
Bogdan Timofte authored a month ago
748
                            label: chargedDevice.hasMultipleChargingTransports ? "Wireless Charger" : "Charger",
Bogdan Timofte authored a month ago
749
                            value: charger.name
750
                        )
751
                    }
752
                    if let wirelessSessionHint = wirelessSessionHint(for: session) {
753
                        Text(wirelessSessionHint)
754
                            .font(.caption2)
755
                            .foregroundColor(session.shouldWarnAboutLowWirelessEfficiency ? .orange : .secondary)
756
                    }
757
                }
758
                .padding(14)
759
                .meterCard(
760
                    tint: statusTint(for: session),
761
                    fillOpacity: 0.10,
762
                    strokeOpacity: 0.16,
763
                    cornerRadius: 16
764
                )
765
            }
766
        }
767
        .frame(maxWidth: .infinity, alignment: .leading)
768
        .padding(18)
769
        .meterCard(tint: .teal, fillOpacity: 0.14, strokeOpacity: 0.20, cornerRadius: 18)
770
    }
771

            
772
    private func sessionSummaryLine(_ session: ChargeSessionSummary) -> String {
773
        var components: [String] = []
Bogdan Timofte authored a month ago
774
        let chargedDevice = appData.chargedDeviceSummary(id: chargedDeviceID)
Bogdan Timofte authored a month ago
775

            
776
        if let batteryDeltaPercent = session.batteryDeltaPercent {
777
            components.append("\(batteryDeltaPercent.format(decimalDigits: 0))%")
778
        }
779

            
780
        if let capacityEstimateWh = session.capacityEstimateWh {
781
            components.append("\(capacityEstimateWh.format(decimalDigits: 2)) Wh capacity")
782
        }
783

            
Bogdan Timofte authored a month ago
784
        if chargedDevice?.shouldShowChargingTransport(session.chargingTransportMode) != false {
785
            components.append(session.chargingTransportMode.title)
786
        }
787
        if chargedDevice?.shouldShowChargingStateMode(session.chargingStateMode) != false {
788
            components.append(session.chargingStateMode.title)
789
        }
Bogdan Timofte authored a month ago
790
        components.append(session.sourceMode.title)
791
        return components.joined(separator: " • ")
792
    }
793

            
794
    private func wirelessSessionHint(for session: ChargeSessionSummary) -> String? {
795
        guard session.chargingTransportMode == .wireless else {
796
            return nil
797
        }
798

            
799
        var components: [String] = []
800
        if let wirelessEfficiencyFactor = session.wirelessEfficiencyFactor {
801
            components.append("Efficiency \(Int((wirelessEfficiencyFactor * 100).rounded()))%")
802
        }
803
        if session.usesEstimatedWirelessEfficiency {
804
            components.append("Estimated from wired baseline and checkpoints")
805
        }
806
        if session.shouldWarnAboutLowWirelessEfficiency {
807
            components.append("Low wireless efficiency, so capacity confidence is reduced")
808
        }
809

            
810
        return components.isEmpty ? nil : components.joined(separator: " • ")
811
    }
812

            
813
    private func sessionDurationText(_ session: ChargeSessionSummary) -> String {
814
        let formatter = DateComponentsFormatter()
Bogdan Timofte authored a month ago
815
        let effectiveDuration = max(session.effectiveDuration, 0)
816
        formatter.allowedUnits = effectiveDuration >= 3600 ? [.hour, .minute] : [.minute, .second]
Bogdan Timofte authored a month ago
817
        formatter.unitsStyle = .abbreviated
818
        formatter.zeroFormattingBehavior = .dropAll
Bogdan Timofte authored a month ago
819
        return formatter.string(from: effectiveDuration) ?? "0m"
Bogdan Timofte authored a month ago
820
    }
821

            
822
    private func statusTint(for session: ChargeSessionSummary) -> Color {
823
        switch session.status {
824
        case .active:
825
            return .green
Bogdan Timofte authored a month ago
826
        case .paused:
827
            return .orange
Bogdan Timofte authored a month ago
828
        case .completed:
829
            return .teal
830
        case .abandoned:
Bogdan Timofte authored a month ago
831
            return .secondary
Bogdan Timofte authored a month ago
832
        }
833
    }
834

            
835
    private func tint(for chargedDevice: ChargedDeviceSummary) -> Color {
836
        switch chargedDevice.deviceClass {
837
        case .iphone:
838
            return .blue
839
        case .watch:
840
            return .green
841
        case .powerbank:
842
            return .orange
843
        case .charger:
844
            return .pink
845
        case .other:
846
            return .secondary
847
        }
848
    }
849

            
Bogdan Timofte authored a month ago
850
    private func standbyEnergyLabel(_ wattHours: Double) -> String {
851
        if wattHours >= 1000 {
852
            return "\((wattHours / 1000).format(decimalDigits: 3)) kWh"
853
        }
854
        return "\(wattHours.format(decimalDigits: 2)) Wh"
855
    }
856

            
857
    private var standbyMeasurementMeters: [AppData.MeterSummary] {
858
        appData.meterSummaries.filter { $0.meter != nil }
859
    }
860

            
Bogdan Timofte authored a month ago
861
    private func completionCurrentDescription(
862
        for chargedDevice: ChargedDeviceSummary,
Bogdan Timofte authored a month ago
863
        sessionKind: ChargeSessionKind
Bogdan Timofte authored a month ago
864
    ) -> String {
Bogdan Timofte authored a month ago
865
        if let configuredCurrent = chargedDevice.configuredCompletionCurrentAmps(for: sessionKind) {
866
            if let learnedCurrent = chargedDevice.learnedCompletionCurrentAmps(for: sessionKind),
Bogdan Timofte authored a month ago
867
               abs(configuredCurrent - learnedCurrent) >= 0.01 {
868
                return "\(configuredCurrent.format(decimalDigits: 2)) A configured • \(learnedCurrent.format(decimalDigits: 2)) A learned"
869
            }
870
            return "\(configuredCurrent.format(decimalDigits: 2)) A configured"
871
        }
872

            
Bogdan Timofte authored a month ago
873
        if let learnedCurrent = chargedDevice.learnedCompletionCurrentAmps(for: sessionKind) {
Bogdan Timofte authored a month ago
874
            return "\(learnedCurrent.format(decimalDigits: 2)) A learned"
875
        }
876

            
877
        return "Learning"
878
    }
879

            
Bogdan Timofte authored a month ago
880
    private func completionCurrentLabel(
881
        for chargedDevice: ChargedDeviceSummary,
882
        sessionKind: ChargeSessionKind
883
    ) -> String {
884
        let showsTransport = chargedDevice.shouldShowChargingTransport(sessionKind.chargingTransportMode)
885
        let showsState = chargedDevice.shouldShowChargingStateMode(sessionKind.chargingStateMode)
886

            
887
        switch (showsTransport, showsState) {
888
        case (true, true):
889
            return "\(sessionKind.shortTitle) Stop Current"
890
        case (true, false):
891
            return "\(sessionKind.chargingTransportMode.title) Stop Current"
892
        case (false, true):
893
            return "\(sessionKind.chargingStateMode.title) Stop Current"
894
        case (false, false):
895
            return "Stop Current"
896
        }
897
    }
898

            
Bogdan Timofte authored a month ago
899
    private func completionSessionKinds(for chargedDevice: ChargedDeviceSummary) -> [ChargeSessionKind] {
900
        chargedDevice.supportedChargingModes.flatMap { chargingTransportMode in
901
            chargedDevice.supportedChargingStateModes.map { chargingStateMode in
902
                ChargeSessionKind(
903
                    chargingTransportMode: chargingTransportMode,
904
                    chargingStateMode: chargingStateMode
905
                )
906
            }
907
        }
908
    }
909

            
910
    private func autoStopDescription(for session: ChargeSessionSummary) -> String {
911
        if session.autoStopEnabled == false {
912
            return "Manual"
913
        }
914

            
915
        if let sessionWarning = sessionWarning(for: session),
916
           sessionWarning.contains("idle-current") {
917
            return "Blocked by charger setup"
918
        }
919

            
920
        if session.stopThresholdAmps > 0 {
921
            return "\(session.stopThresholdAmps.format(decimalDigits: 2)) A"
922
        }
923

            
924
        return "Learning"
925
    }
926

            
927
    private func sessionWarning(for session: ChargeSessionSummary) -> String? {
928
        guard session.chargingTransportMode == .wireless,
929
              let chargerID = session.chargerID,
930
              let charger = appData.chargedDeviceSummary(id: chargerID),
931
              charger.chargerIdleCurrentAmps == nil else {
932
            return nil
933
        }
934

            
935
        return "This charger has no idle-current measurement, so the wireless stop threshold cannot be learned or auto-applied for this session."
936
    }
937

            
Bogdan Timofte authored a month ago
938
    private var deletionTitle: String {
939
        appData.chargedDeviceSummary(id: chargedDeviceID)?.isCharger == true ? "charger" : "device"
940
    }
941

            
942
    private var deletionMessage: String {
943
        if appData.chargedDeviceSummary(id: chargedDeviceID)?.isCharger == true {
944
            return "This removes the charger from the library and unlinks it from wireless sessions that used it."
945
        }
946
        return "This removes the device and its stored charging history from the library."
947
    }
948

            
949
    private func storedSeriesSnapshot(
950
        from samples: [ChargeSessionSampleSummary],
951
        minimumYSpan: Double,
952
        value: (ChargeSessionSampleSummary) -> Double
953
    ) -> StoredSeriesSnapshot? {
954
        let sortedSamples = samples.sorted { lhs, rhs in
955
            if lhs.bucketIndex != rhs.bucketIndex {
956
                return lhs.bucketIndex < rhs.bucketIndex
957
            }
958
            return lhs.timestamp < rhs.timestamp
959
        }
960

            
961
        guard
962
            let firstSample = sortedSamples.first,
963
            let lastSample = sortedSamples.last
964
        else {
965
            return nil
966
        }
967

            
968
        let points = sortedSamples.enumerated().map { index, sample in
969
            Measurements.Measurement.Point(
970
                id: index,
971
                timestamp: sample.timestamp,
972
                value: value(sample),
973
                kind: .sample
974
            )
975
        }
976

            
977
        let minimumValue = points.map(\.value).min() ?? 0
978
        let maximumValue = points.map(\.value).max() ?? minimumValue
979
        let context = ChartContext()
980
        context.setBounds(
981
            xMin: CGFloat(firstSample.timestamp.timeIntervalSince1970),
982
            xMax: CGFloat(max(lastSample.timestamp.timeIntervalSince1970, firstSample.timestamp.timeIntervalSince1970 + 1)),
983
            yMin: CGFloat(minimumValue),
984
            yMax: CGFloat(maximumValue)
985
        )
986
        context.ensureMinimumSize(width: 1, height: CGFloat(max(maximumValue - minimumValue, minimumYSpan)))
987

            
988
        return StoredSeriesSnapshot(
989
            points: points,
990
            context: context,
991
            minimumValue: minimumValue,
992
            maximumValue: maximumValue
993
        )
994
    }
995

            
996
    private func storedSeriesChart(
997
        title: String,
998
        unit: String,
999
        strokeColor: Color,
1000
        areaChart: Bool = false,
1001
        snapshot: StoredSeriesSnapshot
1002
    ) -> some View {
1003
        VStack(alignment: .leading, spacing: 8) {
1004
            HStack(alignment: .firstTextBaseline) {
1005
                Text(title)
1006
                    .font(.subheadline.weight(.semibold))
1007
                Spacer()
1008
                Text(
1009
                    "Latest \(snapshot.lastValue.format(decimalDigits: 2)) \(unit) • \(snapshot.minimumValue.format(decimalDigits: 2))-\(snapshot.maximumValue.format(decimalDigits: 2)) \(unit)"
1010
                )
1011
                .font(.caption2)
1012
                .foregroundColor(.secondary)
1013
            }
1014

            
1015
            Chart(
1016
                points: snapshot.points,
1017
                context: snapshot.context,
1018
                areaChart: areaChart,
1019
                strokeColor: strokeColor
1020
            )
1021
            .frame(height: 118)
1022
            .padding(.horizontal, 6)
1023
            .padding(.vertical, 8)
1024
            .background(
1025
                RoundedRectangle(cornerRadius: 16)
1026
                    .fill(strokeColor.opacity(areaChart ? 0.08 : 0.06))
1027
            )
1028

            
1029
            HStack {
1030
                Text(snapshot.startLabel)
1031
                Spacer()
1032
                Text(snapshot.endLabel)
1033
            }
1034
            .font(.caption2)
1035
            .foregroundColor(.secondary)
1036
        }
1037
    }
1038
}
1039

            
1040
private struct StoredSeriesSnapshot {
1041
    let points: [Measurements.Measurement.Point]
1042
    let context: ChartContext
1043
    let minimumValue: Double
1044
    let maximumValue: Double
1045

            
1046
    var lastValue: Double {
1047
        points.last?.value ?? 0
1048
    }
1049

            
1050
    var startLabel: String {
1051
        guard let firstTimestamp = points.first?.timestamp else { return "" }
1052
        return firstTimestamp.formatted(date: .omitted, time: .shortened)
1053
    }
1054

            
1055
    var endLabel: String {
1056
        guard let lastTimestamp = points.last?.timestamp else { return "" }
1057
        return lastTimestamp.formatted(date: .omitted, time: .shortened)
1058
    }
1059
}
1060

            
1061
private struct ChargedDeviceTargetNotificationEditorSheetView: View {
1062
    @Environment(\.dismiss) private var dismiss
1063
    @EnvironmentObject private var appData: AppData
1064

            
1065
    let sessionID: UUID
1066
    let initialTargetPercent: Double?
1067

            
1068
    @State private var targetPercent: Double
1069

            
1070
    init(sessionID: UUID, initialTargetPercent: Double?) {
1071
        self.sessionID = sessionID
1072
        self.initialTargetPercent = initialTargetPercent
1073
        _targetPercent = State(initialValue: initialTargetPercent ?? 80)
1074
    }
1075

            
1076
    var body: some View {
1077
        NavigationView {
1078
            Form {
Bogdan Timofte authored a month ago
1079
                Section(
1080
                    header: ContextInfoHeader(
1081
                        title: "Target Level",
1082
                        message: "A local notification will be generated on synced devices when the estimated battery level reaches this target."
1083
                    )
1084
                ) {
Bogdan Timofte authored a month ago
1085
                    VStack(alignment: .leading, spacing: 12) {
1086
                        Text("\(targetPercent.format(decimalDigits: 0))%")
1087
                            .font(.title3.weight(.bold))
1088
                        Slider(value: $targetPercent, in: 20...100, step: 1)
1089
                    }
1090
                }
1091
            }
1092
            .navigationTitle("Battery Target")
1093
            .navigationBarTitleDisplayMode(.inline)
1094
            .toolbar {
1095
                ToolbarItem(placement: .cancellationAction) {
1096
                    Button("Cancel") {
1097
                        dismiss()
1098
                    }
1099
                }
1100

            
1101
                ToolbarItem(placement: .confirmationAction) {
1102
                    Button("Save") {
1103
                        if appData.setTargetBatteryPercent(targetPercent, for: sessionID) {
1104
                            dismiss()
1105
                        }
1106
                    }
1107
                }
1108
            }
1109
        }
1110
        .navigationViewStyle(StackNavigationViewStyle())
1111
    }
1112
}
Bogdan Timofte authored a month ago
1113

            
1114
private struct DeviceSessionStopRequest: Identifiable {
1115
    let sessionID: UUID
1116
    let title: String
1117
    let confirmTitle: String
1118
    let explanation: String
1119

            
1120
    var id: UUID {
1121
        sessionID
1122
    }
1123
}