USB-Meter / USB Meter / Views / ChargedDevices / ChargedDeviceDetailView.swift
Newer Older
1153 lines | 47.701kb
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 checkpointEditorVisibility = false
15
    @State private var targetNotificationEditorVisibility = false
16
    @State private var pendingSessionDeletion: ChargeSessionSummary?
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) {
85
                ChargedDeviceEditorSheetView(
86
                    meterMACAddress: nil,
Bogdan Timofte authored a month ago
87
                    kind: chargedDevice.kind,
Bogdan Timofte authored a month ago
88
                    chargedDevice: chargedDevice
89
                )
90
                .environmentObject(appData)
91
            }
92
        }
93
        .sheet(isPresented: $checkpointEditorVisibility) {
94
            if let sessionID = appData.chargedDeviceSummary(id: chargedDeviceID)?.activeSession?.id {
95
                ChargedDeviceCheckpointEditorSheetView(sessionID: sessionID)
96
                    .environmentObject(appData)
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
        }
127
        .confirmationDialog("Delete \(deletionTitle)?", isPresented: $deleteConfirmationVisibility, titleVisibility: .visible) {
128
            Button("Delete", role: .destructive) {
129
                if appData.deleteChargedDevice(id: chargedDeviceID) {
130
                    dismiss()
131
                }
132
            }
133
            Button("Cancel", role: .cancel) {}
134
        } message: {
135
            Text(deletionMessage)
136
        }
137
    }
138

            
139
    private func headerCard(_ chargedDevice: ChargedDeviceSummary) -> some View {
140
        HStack(alignment: .top, spacing: 18) {
141
            ChargedDeviceQRCodeView(qrIdentifier: chargedDevice.qrIdentifier, side: 118)
142

            
143
            VStack(alignment: .leading, spacing: 10) {
Bogdan Timofte authored a month ago
144
                Label(chargedDevice.name, systemImage: chargedDevice.identitySymbolName)
Bogdan Timofte authored a month ago
145
                    .font(.title3.weight(.bold))
146

            
Bogdan Timofte authored a month ago
147
                Text(chargedDevice.identityTitle)
Bogdan Timofte authored a month ago
148
                    .font(.subheadline.weight(.semibold))
149
                    .foregroundColor(.secondary)
150

            
151
                if let meterMAC = chargedDevice.lastAssociatedMeterMAC {
152
                    Text("Default meter: \(meterMAC)")
153
                        .font(.caption)
154
                        .foregroundColor(.secondary)
155
                }
156

            
157
                Text(chargedDevice.qrIdentifier)
158
                    .font(.caption2.monospaced())
159
                    .foregroundColor(.secondary)
160
                    .textSelection(.enabled)
161
            }
162

            
163
            Spacer(minLength: 0)
164
        }
165
        .frame(maxWidth: .infinity, alignment: .leading)
166
        .padding(18)
167
        .meterCard(tint: tint(for: chargedDevice), fillOpacity: 0.20, strokeOpacity: 0.26, cornerRadius: 20)
168
    }
169

            
170
    private func insightsCard(_ chargedDevice: ChargedDeviceSummary) -> some View {
171
        MeterInfoCardView(title: "Insights", tint: tint(for: chargedDevice)) {
Bogdan Timofte authored a month ago
172
            if chargedDevice.isCharger {
173
                chargerInsights(chargedDevice)
174
            } else {
175
                deviceInsights(chargedDevice)
176
            }
177

            
178
            if let notes = chargedDevice.notes, !notes.isEmpty {
179
                Divider()
180
                Text(notes)
181
                    .font(.footnote)
182
                    .foregroundColor(.secondary)
183
                    .frame(maxWidth: .infinity, alignment: .leading)
184
            }
185
        }
186
    }
187

            
188
    @ViewBuilder
189
    private func deviceInsights(_ chargedDevice: ChargedDeviceSummary) -> some View {
190
        MeterInfoRowView(
191
            label: "Charge Modes",
192
            value: chargedDevice.chargingStateAvailability.title
193
        )
194
        MeterInfoRowView(
195
            label: "Charging Support",
196
            value: chargedDevice.supportedChargingModes.map(\.title).joined(separator: " + ")
197
        )
198
        if chargedDevice.supportsWirelessCharging {
Bogdan Timofte authored a month ago
199
            MeterInfoRowView(
Bogdan Timofte authored a month ago
200
                label: "Wireless Profile",
201
                value: chargedDevice.wirelessChargingProfile.title
Bogdan Timofte authored a month ago
202
            )
Bogdan Timofte authored a month ago
203
        }
204

            
205
        ForEach(completionSessionKinds(for: chargedDevice), id: \.rawValue) { sessionKind in
Bogdan Timofte authored a month ago
206
            MeterInfoRowView(
Bogdan Timofte authored a month ago
207
                label: "\(sessionKind.shortTitle) Stop Current",
208
                value: completionCurrentDescription(for: chargedDevice, sessionKind: sessionKind)
Bogdan Timofte authored a month ago
209
            )
Bogdan Timofte authored a month ago
210
        }
211
        MeterInfoRowView(
212
            label: "Estimated Capacity",
213
            value: chargedDevice.estimatedBatteryCapacityWh.map { "\($0.format(decimalDigits: 2)) Wh" } ?? "Not enough data"
214
        )
215
        if let wiredCapacity = chargedDevice.wiredEstimatedBatteryCapacityWh {
Bogdan Timofte authored a month ago
216
            MeterInfoRowView(
Bogdan Timofte authored a month ago
217
                label: "Wired Capacity",
218
                value: "\(wiredCapacity.format(decimalDigits: 2)) Wh"
Bogdan Timofte authored a month ago
219
            )
Bogdan Timofte authored a month ago
220
        }
221
        if let wirelessCapacity = chargedDevice.wirelessEstimatedBatteryCapacityWh {
222
            MeterInfoRowView(
223
                label: "Wireless Capacity",
224
                value: "\(wirelessCapacity.format(decimalDigits: 2)) Wh"
225
            )
226
        }
227
        if let wirelessEfficiencyFactor = chargedDevice.wirelessChargerEfficiencyFactor {
228
            MeterInfoRowView(
229
                label: "Wireless Efficiency",
230
                value: "\(Int((wirelessEfficiencyFactor * 100).rounded()))%"
231
            )
232
        }
233
        MeterInfoRowView(
234
            label: "End-of-Charge Current",
235
            value: chargedDevice.minimumCurrentAmps.map { "\($0.format(decimalDigits: 2)) A" } ?? "Learning"
236
        )
237
        MeterInfoRowView(
238
            label: "Charge Sessions",
239
            value: "\(chargedDevice.sessionCount)"
240
        )
241
    }
Bogdan Timofte authored a month ago
242

            
Bogdan Timofte authored a month ago
243
    @ViewBuilder
244
    private func chargerInsights(_ chargedDevice: ChargedDeviceSummary) -> some View {
245
        if !chargedDevice.chargerObservedVoltageSelections.isEmpty {
Bogdan Timofte authored a month ago
246
            MeterInfoRowView(
Bogdan Timofte authored a month ago
247
                label: "Observed Voltages",
248
                value: chargedDevice.chargerObservedVoltageSelections
249
                    .map { "\($0.format(decimalDigits: 1)) V" }
250
                    .joined(separator: ", ")
Bogdan Timofte authored a month ago
251
            )
Bogdan Timofte authored a month ago
252
        }
253
        if let chargerIdleCurrentAmps = chargedDevice.chargerIdleCurrentAmps {
Bogdan Timofte authored a month ago
254
            MeterInfoRowView(
Bogdan Timofte authored a month ago
255
                label: "Idle Current",
256
                value: "\(chargerIdleCurrentAmps.format(decimalDigits: 2)) A"
Bogdan Timofte authored a month ago
257
            )
Bogdan Timofte authored a month ago
258
        }
259
        if let chargerEfficiencyFactor = chargedDevice.chargerEfficiencyFactor {
Bogdan Timofte authored a month ago
260
            MeterInfoRowView(
Bogdan Timofte authored a month ago
261
                label: "Efficiency",
262
                value: "\(Int((chargerEfficiencyFactor * 100).rounded()))%"
Bogdan Timofte authored a month ago
263
            )
Bogdan Timofte authored a month ago
264
        }
265
        if let chargerMaximumPowerWatts = chargedDevice.chargerMaximumPowerWatts {
266
            MeterInfoRowView(
267
                label: "Max Power",
268
                value: "\(chargerMaximumPowerWatts.format(decimalDigits: 2)) W"
269
            )
270
        }
Bogdan Timofte authored a month ago
271
        if let latestStandbyPowerMeasurement = chargedDevice.latestStandbyPowerMeasurement {
272
            MeterInfoRowView(
273
                label: "Standby Power",
274
                value: "\(latestStandbyPowerMeasurement.averagePowerWatts.format(decimalDigits: 3)) W"
275
            )
276
            MeterInfoRowView(
277
                label: "Standby Projection",
278
                value: standbyEnergyLabel(latestStandbyPowerMeasurement.projectedYearlyEnergyWh) + " / year"
279
            )
280
        }
Bogdan Timofte authored a month ago
281
        MeterInfoRowView(
282
            label: "Wireless Sessions",
283
            value: "\(chargedDevice.sessionCount)"
284
        )
Bogdan Timofte authored a month ago
285

            
Bogdan Timofte authored a month ago
286
        if chargedDevice.chargerIdleCurrentAmps == nil {
287
            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.")
288
                .font(.caption2)
289
                .foregroundColor(.orange)
Bogdan Timofte authored a month ago
290
        }
291
    }
292

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

            
296
        return MeterInfoCardView(
297
            title: "Standby Power",
298
            tint: .orange
299
        ) {
300
            if standbyMeasurementMeters.isEmpty {
301
                Text("Connect a meter first. Standby measurement is launched from a live meter feed, so only currently available meters can be selected here.")
302
                    .font(.footnote)
303
                    .foregroundColor(.secondary)
304
                    .frame(maxWidth: .infinity, alignment: .leading)
305
            } else {
306
                NavigationLink(
307
                    destination: ChargerStandbyPowerWizardView(
308
                        preferredChargerID: chargedDevice.id,
309
                        locksChargerSelection: true
310
                    )
311
                ) {
312
                    Label("New Measurement", systemImage: "plus.circle.fill")
313
                        .font(.subheadline.weight(.semibold))
314
                        .foregroundColor(.orange)
315
                }
316
                .buttonStyle(.plain)
317
            }
318

            
319
            if let latestMeasurement {
320
                Divider()
321

            
322
                NavigationLink(
323
                    destination: ChargerStandbyPowerMeasurementDetailView(
324
                        chargerID: chargedDevice.id,
325
                        measurementID: latestMeasurement.id
326
                    )
327
                ) {
328
                    VStack(alignment: .leading, spacing: 8) {
329
                        HStack {
330
                            Text("Latest Measurement")
331
                                .font(.subheadline.weight(.semibold))
332
                                .foregroundColor(.primary)
333
                            Spacer()
334
                            Text("\(latestMeasurement.averagePowerWatts.format(decimalDigits: 3)) W")
335
                                .font(.subheadline.weight(.bold))
336
                                .foregroundColor(.primary)
337
                                .monospacedDigit()
338
                        }
339

            
340
                        Text(
341
                            "\(latestMeasurement.endedAt.format()) • \(latestMeasurement.sampleCount) samples • \(standbyEnergyLabel(latestMeasurement.projectedYearlyEnergyWh)) / year"
342
                        )
343
                        .font(.caption)
344
                        .foregroundColor(.secondary)
345
                    }
346
                }
347
                .buttonStyle(.plain)
348
            }
349

            
350
            if chargedDevice.standbyPowerMeasurements.isEmpty == false {
351
                Divider()
352

            
353
                NavigationLink(
354
                    destination: ChargerStandbyPowerMeasurementsView(chargerID: chargedDevice.id)
355
                ) {
356
                    Label("View Saved Measurements", systemImage: "clock.arrow.trianglehead.counterclockwise.rotate.90")
357
                        .font(.subheadline.weight(.semibold))
358
                        .foregroundColor(.blue)
359
                }
360
                .buttonStyle(.plain)
361
            }
362
        }
363
    }
364

            
Bogdan Timofte authored a month ago
365
    private func activeSessionCard(
366
        _ activeSession: ChargeSessionSummary,
367
        chargedDevice: ChargedDeviceSummary
368
    ) -> some View {
Bogdan Timofte authored a month ago
369
        MeterInfoCardView(title: "Open Session", tint: .green) {
Bogdan Timofte authored a month ago
370
            MeterInfoRowView(label: "Started", value: activeSession.startedAt.format())
Bogdan Timofte authored a month ago
371
            MeterInfoRowView(label: "Status", value: activeSession.status.title)
Bogdan Timofte authored a month ago
372
            MeterInfoRowView(label: "Charging Type", value: activeSession.chargingTransportMode.title)
Bogdan Timofte authored a month ago
373
            MeterInfoRowView(label: "Charging Mode", value: activeSession.chargingStateMode.title)
Bogdan Timofte authored a month ago
374
            MeterInfoRowView(label: "Battery Energy", value: "\(activeSession.effectiveOrMeasuredEnergyWh.format(decimalDigits: 3)) Wh")
375
            if activeSession.chargingTransportMode == .wireless,
376
               let effectiveBatteryEnergyWh = activeSession.effectiveBatteryEnergyWh,
377
               abs(effectiveBatteryEnergyWh - activeSession.measuredEnergyWh) > 0.01 {
378
                MeterInfoRowView(label: "Charger Energy", value: "\(activeSession.measuredEnergyWh.format(decimalDigits: 3)) Wh")
379
            }
Bogdan Timofte authored a month ago
380
            MeterInfoRowView(label: "Auto Stop", value: autoStopDescription(for: activeSession))
Bogdan Timofte authored a month ago
381
            MeterInfoRowView(label: "Source", value: activeSession.sourceMode.title)
382
            if chargedDevice.isCharger == false,
383
               let chargerID = activeSession.chargerID,
384
               let charger = appData.chargedDeviceSummary(id: chargerID) {
385
                MeterInfoRowView(label: "Wireless Charger", value: charger.name)
386
            }
387
            if let maximumObservedCurrentAmps = activeSession.maximumObservedCurrentAmps {
388
                MeterInfoRowView(label: "Max Current", value: "\(maximumObservedCurrentAmps.format(decimalDigits: 2)) A")
389
            }
390
            if let maximumObservedPowerWatts = activeSession.maximumObservedPowerWatts {
391
                MeterInfoRowView(label: "Max Power", value: "\(maximumObservedPowerWatts.format(decimalDigits: 2)) W")
392
            }
393
            if activeSession.chargingTransportMode == .wired,
394
               let maximumObservedVoltageVolts = activeSession.maximumObservedVoltageVolts {
395
                MeterInfoRowView(label: "Max Voltage", value: "\(maximumObservedVoltageVolts.format(decimalDigits: 2)) V")
396
            }
397
            if chargedDevice.isCharger, let selectedSourceVoltageVolts = activeSession.selectedSourceVoltageVolts {
398
                MeterInfoRowView(label: "Selected Voltage", value: "\(selectedSourceVoltageVolts.format(decimalDigits: 2)) V")
399
            }
400
            if let targetBatteryPercent = activeSession.targetBatteryPercent {
401
                MeterInfoRowView(
402
                    label: "Target Notification",
403
                    value: "\(targetBatteryPercent.format(decimalDigits: 0))%"
404
                )
405
            }
Bogdan Timofte authored a month ago
406
            if let sessionWarning = sessionWarning(for: activeSession) {
407
                Text(sessionWarning)
408
                    .font(.caption2)
409
                    .foregroundColor(.orange)
410
            }
Bogdan Timofte authored a month ago
411
            if let wirelessSessionHint = wirelessSessionHint(for: activeSession) {
412
                Text(wirelessSessionHint)
413
                    .font(.caption2)
414
                    .foregroundColor(activeSession.shouldWarnAboutLowWirelessEfficiency ? .orange : .secondary)
415
            }
416
            if let batteryPrediction = chargedDevice.batteryLevelPrediction(for: activeSession) {
417
                MeterInfoRowView(
418
                    label: "Predicted Battery",
419
                    value: "\(batteryPrediction.predictedPercent.format(decimalDigits: 0))%"
420
                )
421
                Text(
422
                    "Anchored to \(batteryPrediction.anchorDescription) at \(batteryPrediction.anchorPercent.format(decimalDigits: 0))% using \(batteryPrediction.estimatedCapacityWh.format(decimalDigits: 2)) Wh estimated capacity."
423
                )
424
                .font(.caption2)
425
                .foregroundColor(.secondary)
426
            }
427

            
428
            Button("Add Battery Checkpoint") {
429
                checkpointEditorVisibility = true
430
            }
431
            .frame(maxWidth: .infinity)
432
            .padding(.vertical, 10)
433
            .meterCard(tint: .green, fillOpacity: 0.16, strokeOpacity: 0.22, cornerRadius: 14)
434
            .buttonStyle(.plain)
435

            
436
            Button(activeSession.targetBatteryPercent == nil ? "Set Target Notification" : "Change Target Notification") {
437
                targetNotificationEditorVisibility = true
438
            }
439
            .frame(maxWidth: .infinity)
440
            .padding(.vertical, 10)
441
            .meterCard(tint: .indigo, fillOpacity: 0.16, strokeOpacity: 0.22, cornerRadius: 14)
442
            .buttonStyle(.plain)
443

            
444
            if activeSession.targetBatteryPercent != nil {
445
                Button("Clear Target Notification") {
446
                    _ = appData.setTargetBatteryPercent(nil, for: activeSession.id)
447
                }
448
                .frame(maxWidth: .infinity)
449
                .padding(.vertical, 10)
450
                .meterCard(tint: .secondary, fillOpacity: 0.12, strokeOpacity: 0.18, cornerRadius: 14)
451
                .buttonStyle(.plain)
452
            }
453

            
Bogdan Timofte authored a month ago
454
            if activeSession.status == .active {
455
                Button("Pause Session") {
456
                    _ = appData.pauseChargeSession(sessionID: activeSession.id)
457
                }
458
                .frame(maxWidth: .infinity)
459
                .padding(.vertical, 10)
460
                .meterCard(tint: .orange, fillOpacity: 0.16, strokeOpacity: 0.22, cornerRadius: 14)
461
                .buttonStyle(.plain)
462
            } else if activeSession.status == .paused {
463
                Button("Resume Session") {
464
                    _ = appData.resumeChargeSession(sessionID: activeSession.id)
465
                }
466
                .frame(maxWidth: .infinity)
467
                .padding(.vertical, 10)
468
                .meterCard(tint: .blue, fillOpacity: 0.16, strokeOpacity: 0.22, cornerRadius: 14)
469
                .buttonStyle(.plain)
470

            
471
                Text("Paused sessions close automatically after 10 minutes.")
472
                    .font(.caption2)
473
                    .foregroundColor(.secondary)
474
            }
475

            
476
            Button(activeSession.requiresCompletionConfirmation ? "Finish Session With Checkpoint" : "Stop Session") {
477
                pendingSessionStopRequest = DeviceSessionStopRequest(
478
                    sessionID: activeSession.id,
479
                    title: activeSession.requiresCompletionConfirmation ? "Finish Session" : "Stop Session",
480
                    confirmTitle: activeSession.requiresCompletionConfirmation ? "Finish" : "Stop",
481
                    explanation: "Add the final battery checkpoint before closing this session."
482
                )
Bogdan Timofte authored a month ago
483
            }
484
            .frame(maxWidth: .infinity)
485
            .padding(.vertical, 10)
Bogdan Timofte authored a month ago
486
            .meterCard(tint: .red, fillOpacity: 0.16, strokeOpacity: 0.22, cornerRadius: 14)
Bogdan Timofte authored a month ago
487
            .buttonStyle(.plain)
488

            
489
            if activeSession.requiresCompletionConfirmation {
490
                Divider()
491
                if let contradictionPercent = activeSession.completionContradictionPercent {
492
                    Text("Current says charging may have stopped, but the estimated battery level is only \(contradictionPercent.format(decimalDigits: 0))%.")
493
                        .font(.caption2)
494
                        .foregroundColor(.secondary)
495
                }
496

            
497
                Button("Keep Monitoring") {
498
                    _ = appData.continueChargeSessionMonitoring(sessionID: activeSession.id)
499
                }
500
                .frame(maxWidth: .infinity)
501
                .padding(.vertical, 10)
502
                .meterCard(tint: .blue, fillOpacity: 0.16, strokeOpacity: 0.22, cornerRadius: 14)
503
                .buttonStyle(.plain)
504
            }
505
        }
506
    }
507

            
508
    private func capacityEvolutionCard(_ chargedDevice: ChargedDeviceSummary) -> some View {
509
        VStack(alignment: .leading, spacing: 12) {
510
            Text("Capacity Evolution")
511
                .font(.headline)
512

            
513
            ForEach(chargedDevice.capacityHistory.suffix(6)) { point in
514
                HStack {
515
                    Text(point.timestamp.format())
516
                        .font(.caption)
517
                        .foregroundColor(.secondary)
518
                    Spacer()
519
                    Text(point.chargingTransportMode.title)
520
                        .font(.caption2)
521
                        .foregroundColor(.secondary)
522
                    Text("•")
523
                        .foregroundColor(.secondary)
524
                    Text("\(point.capacityWh.format(decimalDigits: 2)) Wh")
525
                        .font(.footnote.weight(.semibold))
526
                }
527
            }
528
        }
529
        .frame(maxWidth: .infinity, alignment: .leading)
530
        .padding(18)
531
        .meterCard(tint: .orange, fillOpacity: 0.14, strokeOpacity: 0.20, cornerRadius: 18)
532
    }
533

            
534
    private func preferredStoredCurveSession(for chargedDevice: ChargedDeviceSummary) -> ChargeSessionSummary? {
535
        if let activeSession = chargedDevice.activeSession, !activeSession.aggregatedSamples.isEmpty {
536
            return activeSession
537
        }
538

            
539
        return chargedDevice.sessions.first(where: { !$0.aggregatedSamples.isEmpty })
540
    }
541

            
542
    private func storedCurveCard(_ session: ChargeSessionSummary) -> some View {
543
        let currentSeries = storedSeriesSnapshot(
544
            from: session.aggregatedSamples,
545
            minimumYSpan: 0.15
546
        ) { $0.averageCurrentAmps }
547
        let energySeries = storedSeriesSnapshot(
548
            from: session.aggregatedSamples,
549
            minimumYSpan: 0.2
550
        ) { $0.measuredEnergyWh }
551

            
552
        return VStack(alignment: .leading, spacing: 14) {
553
            HStack(alignment: .firstTextBaseline) {
554
                VStack(alignment: .leading, spacing: 4) {
Bogdan Timofte authored a month ago
555
                    HStack(spacing: 8) {
556
                        Text("Stored Session Curve")
557
                            .font(.headline)
558
                        ContextInfoButton(
559
                            title: "Stored Session Curve",
560
                            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."
561
                        )
562
                    }
Bogdan Timofte authored a month ago
563
                    Text(session.status.isOpen ? "Open session, persisted as aggregated samples." : "Most recent persisted session at aggregated resolution.")
Bogdan Timofte authored a month ago
564
                        .font(.caption)
565
                        .foregroundColor(.secondary)
566
                }
567

            
568
                Spacer()
569

            
570
                Text("\(session.aggregatedSamples.count) points")
571
                    .font(.caption.weight(.semibold))
572
                    .foregroundColor(.secondary)
573
            }
574

            
575
            if let currentSeries {
576
                storedSeriesChart(
577
                    title: "Current",
578
                    unit: "A",
579
                    strokeColor: .blue,
580
                    snapshot: currentSeries
581
                )
582
            }
583

            
584
            if let energySeries {
585
                storedSeriesChart(
586
                    title: "Energy",
587
                    unit: "Wh",
588
                    strokeColor: .teal,
589
                    areaChart: true,
590
                    snapshot: energySeries
591
                )
592
            }
593

            
594
        }
595
        .frame(maxWidth: .infinity, alignment: .leading)
596
        .padding(18)
597
        .meterCard(tint: .indigo, fillOpacity: 0.12, strokeOpacity: 0.18, cornerRadius: 18)
598
    }
599

            
600
    private func typicalCurveCard(_ chargedDevice: ChargedDeviceSummary) -> some View {
601
        VStack(alignment: .leading, spacing: 12) {
602
            Text("Typical Charge Curve")
603
                .font(.headline)
604

            
605
            ForEach(chargedDevice.typicalCurve) { point in
606
                HStack {
607
                    Text("\(point.percentBin)%")
608
                        .font(.footnote.weight(.semibold))
609
                    Spacer()
610
                    Text("\(point.averageEnergyWh.format(decimalDigits: 2)) Wh")
611
                        .font(.caption.weight(.semibold))
612
                    Text("•")
613
                        .foregroundColor(.secondary)
614
                    Text("\(point.sampleCount) sample\(point.sampleCount == 1 ? "" : "s")")
615
                        .font(.caption2)
616
                        .foregroundColor(.secondary)
617
                }
618
            }
619
        }
620
        .frame(maxWidth: .infinity, alignment: .leading)
621
        .padding(18)
622
        .meterCard(tint: .blue, fillOpacity: 0.14, strokeOpacity: 0.20, cornerRadius: 18)
623
    }
624

            
625
    private func sessionsCard(_ chargedDevice: ChargedDeviceSummary) -> some View {
626
        VStack(alignment: .leading, spacing: 12) {
Bogdan Timofte authored a month ago
627
            HStack(spacing: 8) {
628
                Text("Charge Sessions")
629
                    .font(.headline)
630
                ContextInfoButton(
631
                    title: "Charge Sessions",
632
                    message: "Use these summaries to spot odd sessions quickly before they influence device estimates."
633
                )
634
            }
Bogdan Timofte authored a month ago
635

            
636
            ForEach(chargedDevice.sessions, id: \.id) { session in
637
                VStack(alignment: .leading, spacing: 6) {
638
                    HStack(alignment: .firstTextBaseline, spacing: 10) {
639
                        Text(session.startedAt.format())
640
                            .font(.caption.weight(.semibold))
641
                        Text(session.status.title)
642
                            .font(.caption2.weight(.semibold))
643
                            .padding(.horizontal, 8)
644
                            .padding(.vertical, 4)
645
                            .background(
646
                                Capsule()
647
                                    .fill(statusTint(for: session).opacity(0.16))
648
                            )
649
                        Spacer()
650
                        Button {
651
                            pendingSessionDeletion = session
652
                        } label: {
653
                            Image(systemName: "trash")
654
                                .font(.caption.weight(.semibold))
655
                                .foregroundColor(.red)
656
                                .padding(8)
657
                                .background(
658
                                    Circle()
659
                                        .fill(Color.red.opacity(0.10))
660
                                )
661
                        }
662
                        .buttonStyle(.plain)
663
                    }
664

            
665
                    Text(sessionSummaryLine(session))
666
                        .font(.caption2)
667
                        .foregroundColor(.secondary)
668

            
669
                    MeterInfoRowView(
670
                        label: "Duration",
671
                        value: sessionDurationText(session)
672
                    )
673
                    MeterInfoRowView(
674
                        label: "Energy",
675
                        value: "\(session.effectiveOrMeasuredEnergyWh.format(decimalDigits: 2)) Wh"
676
                    )
677
                    if session.chargingTransportMode == .wireless,
678
                       let effectiveBatteryEnergyWh = session.effectiveBatteryEnergyWh,
679
                       abs(effectiveBatteryEnergyWh - session.measuredEnergyWh) > 0.01 {
680
                        MeterInfoRowView(
681
                            label: "Charger Energy",
682
                            value: "\(session.measuredEnergyWh.format(decimalDigits: 2)) Wh"
683
                        )
684
                    }
685
                    if let maximumObservedCurrentAmps = session.maximumObservedCurrentAmps {
686
                        MeterInfoRowView(
687
                            label: "Max Current",
688
                            value: "\(maximumObservedCurrentAmps.format(decimalDigits: 2)) A"
689
                        )
690
                    }
691
                    if let maximumObservedPowerWatts = session.maximumObservedPowerWatts {
692
                        MeterInfoRowView(
693
                            label: "Max Power",
694
                            value: "\(maximumObservedPowerWatts.format(decimalDigits: 2)) W"
695
                        )
696
                    }
697
                    if session.chargingTransportMode == .wired,
698
                       let maximumObservedVoltageVolts = session.maximumObservedVoltageVolts {
699
                        MeterInfoRowView(
700
                            label: "Max Voltage",
701
                            value: "\(maximumObservedVoltageVolts.format(decimalDigits: 2)) V"
702
                        )
703
                    }
704
                    if chargedDevice.isCharger, let selectedSourceVoltageVolts = session.selectedSourceVoltageVolts {
705
                        MeterInfoRowView(
706
                            label: "Selected Voltage",
707
                            value: "\(selectedSourceVoltageVolts.format(decimalDigits: 2)) V"
708
                        )
709
                    }
710
                    if chargedDevice.isCharger == false,
711
                       let chargerID = session.chargerID,
712
                       let charger = appData.chargedDeviceSummary(id: chargerID) {
713
                        MeterInfoRowView(
714
                            label: "Wireless Charger",
715
                            value: charger.name
716
                        )
717
                    }
718
                    if let wirelessSessionHint = wirelessSessionHint(for: session) {
719
                        Text(wirelessSessionHint)
720
                            .font(.caption2)
721
                            .foregroundColor(session.shouldWarnAboutLowWirelessEfficiency ? .orange : .secondary)
722
                    }
723
                }
724
                .padding(14)
725
                .meterCard(
726
                    tint: statusTint(for: session),
727
                    fillOpacity: 0.10,
728
                    strokeOpacity: 0.16,
729
                    cornerRadius: 16
730
                )
731
            }
732
        }
733
        .frame(maxWidth: .infinity, alignment: .leading)
734
        .padding(18)
735
        .meterCard(tint: .teal, fillOpacity: 0.14, strokeOpacity: 0.20, cornerRadius: 18)
736
    }
737

            
738
    private func sessionSummaryLine(_ session: ChargeSessionSummary) -> String {
739
        var components: [String] = []
740

            
741
        if let batteryDeltaPercent = session.batteryDeltaPercent {
742
            components.append("\(batteryDeltaPercent.format(decimalDigits: 0))%")
743
        }
744

            
745
        if let capacityEstimateWh = session.capacityEstimateWh {
746
            components.append("\(capacityEstimateWh.format(decimalDigits: 2)) Wh capacity")
747
        }
748

            
749
        components.append(session.chargingTransportMode.title)
Bogdan Timofte authored a month ago
750
        components.append(session.chargingStateMode.title)
Bogdan Timofte authored a month ago
751
        components.append(session.sourceMode.title)
752
        return components.joined(separator: " • ")
753
    }
754

            
755
    private func wirelessSessionHint(for session: ChargeSessionSummary) -> String? {
756
        guard session.chargingTransportMode == .wireless else {
757
            return nil
758
        }
759

            
760
        var components: [String] = []
761
        if let wirelessEfficiencyFactor = session.wirelessEfficiencyFactor {
762
            components.append("Efficiency \(Int((wirelessEfficiencyFactor * 100).rounded()))%")
763
        }
764
        if session.usesEstimatedWirelessEfficiency {
765
            components.append("Estimated from wired baseline and checkpoints")
766
        }
767
        if session.shouldWarnAboutLowWirelessEfficiency {
768
            components.append("Low wireless efficiency, so capacity confidence is reduced")
769
        }
770

            
771
        return components.isEmpty ? nil : components.joined(separator: " • ")
772
    }
773

            
774
    private func sessionDurationText(_ session: ChargeSessionSummary) -> String {
775
        let formatter = DateComponentsFormatter()
Bogdan Timofte authored a month ago
776
        let effectiveDuration = max(session.effectiveDuration, 0)
777
        formatter.allowedUnits = effectiveDuration >= 3600 ? [.hour, .minute] : [.minute, .second]
Bogdan Timofte authored a month ago
778
        formatter.unitsStyle = .abbreviated
779
        formatter.zeroFormattingBehavior = .dropAll
Bogdan Timofte authored a month ago
780
        return formatter.string(from: effectiveDuration) ?? "0m"
Bogdan Timofte authored a month ago
781
    }
782

            
783
    private func statusTint(for session: ChargeSessionSummary) -> Color {
784
        switch session.status {
785
        case .active:
786
            return .green
Bogdan Timofte authored a month ago
787
        case .paused:
788
            return .orange
Bogdan Timofte authored a month ago
789
        case .completed:
790
            return .teal
791
        case .abandoned:
Bogdan Timofte authored a month ago
792
            return .secondary
Bogdan Timofte authored a month ago
793
        }
794
    }
795

            
796
    private func tint(for chargedDevice: ChargedDeviceSummary) -> Color {
797
        switch chargedDevice.deviceClass {
798
        case .iphone:
799
            return .blue
800
        case .watch:
801
            return .green
802
        case .powerbank:
803
            return .orange
804
        case .charger:
805
            return .pink
806
        case .other:
807
            return .secondary
808
        }
809
    }
810

            
Bogdan Timofte authored a month ago
811
    private func standbyEnergyLabel(_ wattHours: Double) -> String {
812
        if wattHours >= 1000 {
813
            return "\((wattHours / 1000).format(decimalDigits: 3)) kWh"
814
        }
815
        return "\(wattHours.format(decimalDigits: 2)) Wh"
816
    }
817

            
818
    private var standbyMeasurementMeters: [AppData.MeterSummary] {
819
        appData.meterSummaries.filter { $0.meter != nil }
820
    }
821

            
Bogdan Timofte authored a month ago
822
    private func completionCurrentDescription(
823
        for chargedDevice: ChargedDeviceSummary,
Bogdan Timofte authored a month ago
824
        sessionKind: ChargeSessionKind
Bogdan Timofte authored a month ago
825
    ) -> String {
Bogdan Timofte authored a month ago
826
        if let configuredCurrent = chargedDevice.configuredCompletionCurrentAmps(for: sessionKind) {
827
            if let learnedCurrent = chargedDevice.learnedCompletionCurrentAmps(for: sessionKind),
Bogdan Timofte authored a month ago
828
               abs(configuredCurrent - learnedCurrent) >= 0.01 {
829
                return "\(configuredCurrent.format(decimalDigits: 2)) A configured • \(learnedCurrent.format(decimalDigits: 2)) A learned"
830
            }
831
            return "\(configuredCurrent.format(decimalDigits: 2)) A configured"
832
        }
833

            
Bogdan Timofte authored a month ago
834
        if let learnedCurrent = chargedDevice.learnedCompletionCurrentAmps(for: sessionKind) {
Bogdan Timofte authored a month ago
835
            return "\(learnedCurrent.format(decimalDigits: 2)) A learned"
836
        }
837

            
838
        return "Learning"
839
    }
840

            
Bogdan Timofte authored a month ago
841
    private func completionSessionKinds(for chargedDevice: ChargedDeviceSummary) -> [ChargeSessionKind] {
842
        chargedDevice.supportedChargingModes.flatMap { chargingTransportMode in
843
            chargedDevice.supportedChargingStateModes.map { chargingStateMode in
844
                ChargeSessionKind(
845
                    chargingTransportMode: chargingTransportMode,
846
                    chargingStateMode: chargingStateMode
847
                )
848
            }
849
        }
850
    }
851

            
852
    private func autoStopDescription(for session: ChargeSessionSummary) -> String {
853
        if session.autoStopEnabled == false {
854
            return "Manual"
855
        }
856

            
857
        if let sessionWarning = sessionWarning(for: session),
858
           sessionWarning.contains("idle-current") {
859
            return "Blocked by charger setup"
860
        }
861

            
862
        if session.stopThresholdAmps > 0 {
863
            return "\(session.stopThresholdAmps.format(decimalDigits: 2)) A"
864
        }
865

            
866
        return "Learning"
867
    }
868

            
869
    private func sessionWarning(for session: ChargeSessionSummary) -> String? {
870
        guard session.chargingTransportMode == .wireless,
871
              let chargerID = session.chargerID,
872
              let charger = appData.chargedDeviceSummary(id: chargerID),
873
              charger.chargerIdleCurrentAmps == nil else {
874
            return nil
875
        }
876

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

            
Bogdan Timofte authored a month ago
880
    private var deletionTitle: String {
881
        appData.chargedDeviceSummary(id: chargedDeviceID)?.isCharger == true ? "charger" : "device"
882
    }
883

            
884
    private var deletionMessage: String {
885
        if appData.chargedDeviceSummary(id: chargedDeviceID)?.isCharger == true {
886
            return "This removes the charger from the library and unlinks it from wireless sessions that used it."
887
        }
888
        return "This removes the device and its stored charging history from the library."
889
    }
890

            
891
    private func storedSeriesSnapshot(
892
        from samples: [ChargeSessionSampleSummary],
893
        minimumYSpan: Double,
894
        value: (ChargeSessionSampleSummary) -> Double
895
    ) -> StoredSeriesSnapshot? {
896
        let sortedSamples = samples.sorted { lhs, rhs in
897
            if lhs.bucketIndex != rhs.bucketIndex {
898
                return lhs.bucketIndex < rhs.bucketIndex
899
            }
900
            return lhs.timestamp < rhs.timestamp
901
        }
902

            
903
        guard
904
            let firstSample = sortedSamples.first,
905
            let lastSample = sortedSamples.last
906
        else {
907
            return nil
908
        }
909

            
910
        let points = sortedSamples.enumerated().map { index, sample in
911
            Measurements.Measurement.Point(
912
                id: index,
913
                timestamp: sample.timestamp,
914
                value: value(sample),
915
                kind: .sample
916
            )
917
        }
918

            
919
        let minimumValue = points.map(\.value).min() ?? 0
920
        let maximumValue = points.map(\.value).max() ?? minimumValue
921
        let context = ChartContext()
922
        context.setBounds(
923
            xMin: CGFloat(firstSample.timestamp.timeIntervalSince1970),
924
            xMax: CGFloat(max(lastSample.timestamp.timeIntervalSince1970, firstSample.timestamp.timeIntervalSince1970 + 1)),
925
            yMin: CGFloat(minimumValue),
926
            yMax: CGFloat(maximumValue)
927
        )
928
        context.ensureMinimumSize(width: 1, height: CGFloat(max(maximumValue - minimumValue, minimumYSpan)))
929

            
930
        return StoredSeriesSnapshot(
931
            points: points,
932
            context: context,
933
            minimumValue: minimumValue,
934
            maximumValue: maximumValue
935
        )
936
    }
937

            
938
    private func storedSeriesChart(
939
        title: String,
940
        unit: String,
941
        strokeColor: Color,
942
        areaChart: Bool = false,
943
        snapshot: StoredSeriesSnapshot
944
    ) -> some View {
945
        VStack(alignment: .leading, spacing: 8) {
946
            HStack(alignment: .firstTextBaseline) {
947
                Text(title)
948
                    .font(.subheadline.weight(.semibold))
949
                Spacer()
950
                Text(
951
                    "Latest \(snapshot.lastValue.format(decimalDigits: 2)) \(unit) • \(snapshot.minimumValue.format(decimalDigits: 2))-\(snapshot.maximumValue.format(decimalDigits: 2)) \(unit)"
952
                )
953
                .font(.caption2)
954
                .foregroundColor(.secondary)
955
            }
956

            
957
            Chart(
958
                points: snapshot.points,
959
                context: snapshot.context,
960
                areaChart: areaChart,
961
                strokeColor: strokeColor
962
            )
963
            .frame(height: 118)
964
            .padding(.horizontal, 6)
965
            .padding(.vertical, 8)
966
            .background(
967
                RoundedRectangle(cornerRadius: 16)
968
                    .fill(strokeColor.opacity(areaChart ? 0.08 : 0.06))
969
            )
970

            
971
            HStack {
972
                Text(snapshot.startLabel)
973
                Spacer()
974
                Text(snapshot.endLabel)
975
            }
976
            .font(.caption2)
977
            .foregroundColor(.secondary)
978
        }
979
    }
980
}
981

            
982
private struct StoredSeriesSnapshot {
983
    let points: [Measurements.Measurement.Point]
984
    let context: ChartContext
985
    let minimumValue: Double
986
    let maximumValue: Double
987

            
988
    var lastValue: Double {
989
        points.last?.value ?? 0
990
    }
991

            
992
    var startLabel: String {
993
        guard let firstTimestamp = points.first?.timestamp else { return "" }
994
        return firstTimestamp.formatted(date: .omitted, time: .shortened)
995
    }
996

            
997
    var endLabel: String {
998
        guard let lastTimestamp = points.last?.timestamp else { return "" }
999
        return lastTimestamp.formatted(date: .omitted, time: .shortened)
1000
    }
1001
}
1002

            
1003
private struct ChargedDeviceCheckpointEditorSheetView: View {
1004
    @Environment(\.dismiss) private var dismiss
1005
    @EnvironmentObject private var appData: AppData
1006

            
1007
    let sessionID: UUID
1008

            
1009
    @State private var batteryPercent = ""
1010
    @State private var label = ""
Bogdan Timofte authored a month ago
1011
    @State private var confirmationWarning: BatteryCheckpointPlausibilityWarning?
1012

            
1013
    private var plausibilityWarning: BatteryCheckpointPlausibilityWarning? {
1014
        guard let percent = Double(batteryPercent) else {
1015
            return nil
1016
        }
1017
        return appData.batteryCheckpointPlausibilityWarning(percent: percent, for: sessionID)
1018
    }
Bogdan Timofte authored a month ago
1019

            
1020
    var body: some View {
1021
        NavigationView {
1022
            Form {
Bogdan Timofte authored a month ago
1023
                Section(
1024
                    header: ContextInfoHeader(
1025
                        title: "Checkpoint",
1026
                        message: "The checkpoint is stored on the active charge session and is used for capacity estimation and charge-level prediction."
1027
                    )
1028
                ) {
Bogdan Timofte authored a month ago
1029
                    TextField("Battery %", text: $batteryPercent)
1030
                        .keyboardType(.decimalPad)
1031
                    TextField("Label (optional)", text: $label)
1032
                }
1033

            
Bogdan Timofte authored a month ago
1034
                if let plausibilityWarning {
1035
                    Section(header: Text(plausibilityWarning.title)) {
1036
                        Text(plausibilityWarning.message)
1037
                            .font(.footnote)
1038
                            .foregroundColor(.orange)
1039
                    }
Bogdan Timofte authored a month ago
1040
                }
1041
            }
1042
            .navigationTitle("Battery Checkpoint")
1043
            .navigationBarTitleDisplayMode(.inline)
1044
            .toolbar {
1045
                ToolbarItem(placement: .cancellationAction) {
1046
                    Button("Cancel") {
1047
                        dismiss()
1048
                    }
1049
                }
1050

            
1051
                ToolbarItem(placement: .confirmationAction) {
1052
                    Button("Save") {
Bogdan Timofte authored a month ago
1053
                        saveCheckpoint()
Bogdan Timofte authored a month ago
1054
                    }
1055
                    .disabled(
1056
                        (Double(batteryPercent) ?? -1) < 0
1057
                            || (Double(batteryPercent) ?? 101) > 100
1058
                    )
1059
                }
1060
            }
1061
        }
1062
        .navigationViewStyle(StackNavigationViewStyle())
Bogdan Timofte authored a month ago
1063
        .alert(item: $confirmationWarning) { warning in
1064
            Alert(
1065
                title: Text(warning.title),
1066
                message: Text(warning.message),
1067
                primaryButton: .destructive(Text("Save Anyway")) {
1068
                    saveCheckpoint(forceOverride: true)
1069
                },
1070
                secondaryButton: .cancel()
1071
            )
1072
        }
1073
    }
1074

            
1075
    private func saveCheckpoint(forceOverride: Bool = false) {
1076
        guard let percent = Double(batteryPercent) else {
1077
            return
1078
        }
1079

            
1080
        if !forceOverride, let plausibilityWarning {
1081
            confirmationWarning = plausibilityWarning
1082
            return
1083
        }
1084

            
1085
        if appData.addBatteryCheckpoint(percent: percent, label: label, for: sessionID) {
1086
            dismiss()
1087
        }
Bogdan Timofte authored a month ago
1088
    }
1089
}
1090

            
1091
private struct ChargedDeviceTargetNotificationEditorSheetView: View {
1092
    @Environment(\.dismiss) private var dismiss
1093
    @EnvironmentObject private var appData: AppData
1094

            
1095
    let sessionID: UUID
1096
    let initialTargetPercent: Double?
1097

            
1098
    @State private var targetPercent: Double
1099

            
1100
    init(sessionID: UUID, initialTargetPercent: Double?) {
1101
        self.sessionID = sessionID
1102
        self.initialTargetPercent = initialTargetPercent
1103
        _targetPercent = State(initialValue: initialTargetPercent ?? 80)
1104
    }
1105

            
1106
    var body: some View {
1107
        NavigationView {
1108
            Form {
Bogdan Timofte authored a month ago
1109
                Section(
1110
                    header: ContextInfoHeader(
1111
                        title: "Target Level",
1112
                        message: "A local notification will be generated on synced devices when the estimated battery level reaches this target."
1113
                    )
1114
                ) {
Bogdan Timofte authored a month ago
1115
                    VStack(alignment: .leading, spacing: 12) {
1116
                        Text("\(targetPercent.format(decimalDigits: 0))%")
1117
                            .font(.title3.weight(.bold))
1118
                        Slider(value: $targetPercent, in: 20...100, step: 1)
1119
                    }
1120
                }
1121
            }
1122
            .navigationTitle("Battery Target")
1123
            .navigationBarTitleDisplayMode(.inline)
1124
            .toolbar {
1125
                ToolbarItem(placement: .cancellationAction) {
1126
                    Button("Cancel") {
1127
                        dismiss()
1128
                    }
1129
                }
1130

            
1131
                ToolbarItem(placement: .confirmationAction) {
1132
                    Button("Save") {
1133
                        if appData.setTargetBatteryPercent(targetPercent, for: sessionID) {
1134
                            dismiss()
1135
                        }
1136
                    }
1137
                }
1138
            }
1139
        }
1140
        .navigationViewStyle(StackNavigationViewStyle())
1141
    }
1142
}
Bogdan Timofte authored a month ago
1143

            
1144
private struct DeviceSessionStopRequest: Identifiable {
1145
    let sessionID: UUID
1146
    let title: String
1147
    let confirmTitle: String
1148
    let explanation: String
1149

            
1150
    var id: UUID {
1151
        sessionID
1152
    }
1153
}