USB-Meter / USB Meter / Views / ChargedDevices / Details / ChargedDeviceDetailView.swift
Newer Older
825 lines | 32.388kb
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 {
Bogdan Timofte authored a month ago
11
    private enum DetailTab: Hashable {
12
        case overview
13
        case standby
14
        case sessions
15
        case trends
16
        case settings
17
    }
18

            
Bogdan Timofte authored a month ago
19
    @EnvironmentObject private var appData: AppData
20
    @Environment(\.dismiss) private var dismiss
Bogdan Timofte authored a month ago
21

            
Bogdan Timofte authored a month ago
22
    @State private var editorVisibility = false
23
    @State private var deleteConfirmationVisibility = false
Bogdan Timofte authored a month ago
24
    @State private var selectedTab: DetailTab = .overview
Bogdan Timofte authored a month ago
25

            
26
    let chargedDeviceID: UUID
27

            
28
    var body: some View {
29
        Group {
30
            if let chargedDevice = appData.chargedDeviceSummary(id: chargedDeviceID) {
Bogdan Timofte authored a month ago
31
                tabbedDetailView(chargedDevice)
Bogdan Timofte authored a month ago
32
                .navigationTitle(chargedDevice.name)
33
                .toolbar {
34
                    ToolbarItemGroup(placement: .primaryAction) {
Bogdan Timofte authored a month ago
35
                        Button("Edit", action: showEditor)
36
                        Button(role: .destructive, action: showDeleteConfirmation) {
Bogdan Timofte authored a month ago
37
                            Image(systemName: "trash")
38
                        }
39
                    }
40
                }
41
            } else {
42
                Text("This device is no longer available.")
43
                    .foregroundColor(.secondary)
44
                    .navigationTitle("Device")
45
            }
46
        }
47
        .sheet(isPresented: $editorVisibility) {
48
            if let chargedDevice = appData.chargedDeviceSummary(id: chargedDeviceID) {
Bogdan Timofte authored a month ago
49
                if chargedDevice.isCharger {
Bogdan Timofte authored a month ago
50
                    ChargerEditorSheetView(chargedDevice: chargedDevice)
51
                        .environmentObject(appData)
Bogdan Timofte authored a month ago
52
                } else {
53
                    ChargedDeviceEditorSheetView(
54
                        meterMACAddress: nil,
55
                        chargedDevice: chargedDevice
56
                    )
57
                    .environmentObject(appData)
58
                }
Bogdan Timofte authored a month ago
59
            }
60
        }
61
        .confirmationDialog("Delete \(deletionTitle)?", isPresented: $deleteConfirmationVisibility, titleVisibility: .visible) {
62
            Button("Delete", role: .destructive) {
63
                if appData.deleteChargedDevice(id: chargedDeviceID) {
64
                    dismiss()
65
                }
66
            }
67
            Button("Cancel", role: .cancel) {}
68
        } message: {
69
            Text(deletionMessage)
70
        }
71
    }
72

            
Bogdan Timofte authored a month ago
73
    private func tabbedDetailView(_ chargedDevice: ChargedDeviceSummary) -> some View {
74
        GeometryReader { proxy in
75
            let tabs = availableTabs(for: chargedDevice)
76
            let displayedTab = displayedTab(from: tabs)
77
            let tabBarPresentation = AdaptiveTabBarPresentation.standard(for: proxy.size)
78

            
79
            VStack(spacing: 0) {
80
                ChargedDeviceDetailTabBarView(
81
                    tabs: tabs,
82
                    selection: $selectedTab,
83
                    tint: tint(for: chargedDevice),
84
                    presentation: tabBarPresentation,
85
                    title: title(for:),
86
                    systemImage: systemImage(for:)
87
                )
88

            
89
                ScrollView {
90
                    tabContent(displayedTab, chargedDevice: chargedDevice)
91
                        .padding()
92
                }
93
                .id(displayedTab)
94
                .transition(.opacity.combined(with: .move(edge: .trailing)))
95
                .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .top)
96
            }
97
            .animation(.easeInOut(duration: 0.22), value: displayedTab)
98
            .animation(.easeInOut(duration: 0.22), value: tabs)
99
        }
100
        .background(detailBackground(for: chargedDevice))
101
        .onAppear {
102
            ensureSelectedTabExists(for: chargedDevice)
103
        }
104
        .onChange(of: chargedDevice.isCharger) { _ in
105
            ensureSelectedTabExists(for: chargedDevice)
106
        }
107
    }
108

            
109
    @ViewBuilder
110
    private func tabContent(_ tab: DetailTab, chargedDevice: ChargedDeviceSummary) -> some View {
111
        VStack(spacing: 18) {
112
            switch tab {
113
            case .overview:
114
                overviewTab(chargedDevice)
115
            case .standby:
116
                standbyTab(chargedDevice)
117
            case .sessions:
118
                sessionsTab(chargedDevice)
119
            case .trends:
120
                trendsTab(chargedDevice)
121
            case .settings:
122
                settingsTab(chargedDevice)
123
            }
124
        }
125
    }
126

            
127
    @ViewBuilder
128
    private func overviewTab(_ chargedDevice: ChargedDeviceSummary) -> some View {
129
        headerCard(chargedDevice)
130
        insightsCard(chargedDevice)
131

            
132
        if let activeSession = chargedDevice.activeSession {
133
            activeSessionSummaryCard(activeSession, chargedDevice: chargedDevice)
134
        }
135
    }
136

            
137
    @ViewBuilder
138
    private func standbyTab(_ chargedDevice: ChargedDeviceSummary) -> some View {
139
        standbyPowerCard(chargedDevice)
140
    }
141

            
142
    @ViewBuilder
143
    private func sessionsTab(_ chargedDevice: ChargedDeviceSummary) -> some View {
144
        if let activeSession = chargedDevice.activeSession {
145
            activeSessionSummaryCard(activeSession, chargedDevice: chargedDevice)
146
        }
147

            
148
        if !closedSessions(for: chargedDevice).isEmpty {
149
            sessionHistorySummaryCard(chargedDevice)
150
        } else if chargedDevice.activeSession == nil {
151
            emptyStateCard(
152
                title: "No Sessions",
153
                message: "Charging sessions will appear here after this \(chargedDevice.isCharger ? "charger" : "device") is used in a recording.",
154
                tint: .teal
155
            )
156
        }
157
    }
158

            
159
    @ViewBuilder
160
    private func trendsTab(_ chargedDevice: ChargedDeviceSummary) -> some View {
161
        if !chargedDevice.capacityHistory.isEmpty {
162
            capacityEvolutionCard(chargedDevice)
163
        }
164

            
165
        if !chargedDevice.typicalCurve.isEmpty {
166
            typicalCurveCard(chargedDevice)
167
        }
168

            
169
        if chargedDevice.capacityHistory.isEmpty && chargedDevice.typicalCurve.isEmpty {
170
            emptyStateCard(
171
                title: "Learning Trends",
172
                message: "Capacity history and charge curves will appear after enough completed sessions are available.",
173
                tint: .blue
174
            )
175
        }
176
    }
177

            
178
    @ViewBuilder
179
    private func settingsTab(_ chargedDevice: ChargedDeviceSummary) -> some View {
180
        settingsCard(chargedDevice)
181
    }
182

            
Bogdan Timofte authored a month ago
183
    private func headerCard(_ chargedDevice: ChargedDeviceSummary) -> some View {
184
        HStack(alignment: .top, spacing: 18) {
185
            ChargedDeviceQRCodeView(qrIdentifier: chargedDevice.qrIdentifier, side: 118)
186

            
187
            VStack(alignment: .leading, spacing: 10) {
Bogdan Timofte authored a month ago
188
                ChargedDeviceIdentityLabelView(
189
                    chargedDevice: chargedDevice,
190
                    iconPointSize: 22
191
                )
192
                .font(.title3.weight(.bold))
Bogdan Timofte authored a month ago
193

            
Bogdan Timofte authored a month ago
194
                Text(chargedDevice.identityTitle)
Bogdan Timofte authored a month ago
195
                    .font(.subheadline.weight(.semibold))
196
                    .foregroundColor(.secondary)
197

            
198
                if let meterMAC = chargedDevice.lastAssociatedMeterMAC {
199
                    Text("Default meter: \(meterMAC)")
200
                        .font(.caption)
201
                        .foregroundColor(.secondary)
202
                }
203

            
204
                Text(chargedDevice.qrIdentifier)
205
                    .font(.caption2.monospaced())
206
                    .foregroundColor(.secondary)
207
                    .textSelection(.enabled)
208
            }
209

            
210
            Spacer(minLength: 0)
211
        }
212
        .frame(maxWidth: .infinity, alignment: .leading)
213
        .padding(18)
214
        .meterCard(tint: tint(for: chargedDevice), fillOpacity: 0.20, strokeOpacity: 0.26, cornerRadius: 20)
215
    }
216

            
Bogdan Timofte authored a month ago
217
    private func settingsCard(_ chargedDevice: ChargedDeviceSummary) -> some View {
218
        MeterInfoCardView(title: "Settings", tint: tint(for: chargedDevice)) {
219
            MeterInfoRowView(
220
                label: "Kind",
221
                value: chargedDevice.isCharger ? "Charger" : chargedDevice.deviceClass.title
222
            )
223
            MeterInfoRowView(label: "Template", value: chargedDevice.identityTitle)
224
            MeterInfoRowView(label: "QR ID", value: chargedDevice.qrIdentifier)
225

            
226
            if let meterMAC = chargedDevice.lastAssociatedMeterMAC {
227
                MeterInfoRowView(label: "Default Meter", value: meterMAC)
228
            }
229

            
230
            MeterInfoRowView(label: "Created", value: chargedDevice.createdAt.format())
231
            MeterInfoRowView(label: "Updated", value: chargedDevice.updatedAt.format())
232

            
233
            Divider()
234

            
235
            Button(action: showEditor) {
236
                Label("Edit \(chargedDevice.isCharger ? "Charger" : "Device")", systemImage: "pencil")
237
                    .font(.subheadline.weight(.semibold))
238
                    .frame(maxWidth: .infinity)
239
                    .padding(.vertical, 10)
240
                    .meterCard(tint: tint(for: chargedDevice), fillOpacity: 0.16, strokeOpacity: 0.22, cornerRadius: 14)
241
            }
242
            .buttonStyle(.plain)
243

            
244
            Button(role: .destructive, action: showDeleteConfirmation) {
245
                Label("Delete \(chargedDevice.isCharger ? "Charger" : "Device")", systemImage: "trash")
246
                    .font(.subheadline.weight(.semibold))
247
                    .frame(maxWidth: .infinity)
248
                    .padding(.vertical, 10)
249
                    .meterCard(tint: .red, fillOpacity: 0.10, strokeOpacity: 0.18, cornerRadius: 14)
250
            }
251
            .buttonStyle(.plain)
252
        }
253
    }
254

            
255
    private func emptyStateCard(title: String, message: String, tint: Color) -> some View {
256
        VStack(alignment: .leading, spacing: 8) {
257
            Text(title)
258
                .font(.headline)
259
            Text(message)
260
                .font(.footnote)
261
                .foregroundColor(.secondary)
262
                .fixedSize(horizontal: false, vertical: true)
263
        }
264
        .frame(maxWidth: .infinity, alignment: .leading)
265
        .padding(18)
266
        .meterCard(tint: tint, fillOpacity: 0.12, strokeOpacity: 0.18, cornerRadius: 18)
267
    }
268

            
Bogdan Timofte authored a month ago
269
    private func insightsCard(_ chargedDevice: ChargedDeviceSummary) -> some View {
270
        MeterInfoCardView(title: "Insights", tint: tint(for: chargedDevice)) {
Bogdan Timofte authored a month ago
271
            if chargedDevice.isCharger {
272
                chargerInsights(chargedDevice)
273
            } else {
274
                deviceInsights(chargedDevice)
275
            }
276

            
277
            if let notes = chargedDevice.notes, !notes.isEmpty {
278
                Divider()
279
                Text(notes)
280
                    .font(.footnote)
281
                    .foregroundColor(.secondary)
282
                    .frame(maxWidth: .infinity, alignment: .leading)
283
            }
284
        }
285
    }
286

            
287
    @ViewBuilder
288
    private func deviceInsights(_ chargedDevice: ChargedDeviceSummary) -> some View {
Bogdan Timofte authored a month ago
289
        if chargedDevice.hasMultipleChargingStateModes {
290
            MeterInfoRowView(
291
                label: "Charge Modes",
292
                value: chargedDevice.chargingStateAvailability.title
293
            )
294
        }
295
        if chargedDevice.hasMultipleChargingTransports {
296
            MeterInfoRowView(
297
                label: "Charging Support",
298
                value: chargedDevice.supportedChargingModes.map(\.title).joined(separator: " + ")
299
            )
300
        }
301
        if chargedDevice.showsWirelessProfileDetails {
Bogdan Timofte authored a month ago
302
            MeterInfoRowView(
Bogdan Timofte authored a month ago
303
                label: "Wireless Profile",
304
                value: chargedDevice.wirelessChargingProfile.title
Bogdan Timofte authored a month ago
305
            )
Bogdan Timofte authored a month ago
306
        }
307

            
308
        ForEach(completionSessionKinds(for: chargedDevice), id: \.rawValue) { sessionKind in
Bogdan Timofte authored a month ago
309
            MeterInfoRowView(
Bogdan Timofte authored a month ago
310
                label: completionCurrentLabel(for: chargedDevice, sessionKind: sessionKind),
Bogdan Timofte authored a month ago
311
                value: completionCurrentDescription(for: chargedDevice, sessionKind: sessionKind)
Bogdan Timofte authored a month ago
312
            )
Bogdan Timofte authored a month ago
313
        }
314
        MeterInfoRowView(
315
            label: "Estimated Capacity",
316
            value: chargedDevice.estimatedBatteryCapacityWh.map { "\($0.format(decimalDigits: 2)) Wh" } ?? "Not enough data"
317
        )
318
        if let wiredCapacity = chargedDevice.wiredEstimatedBatteryCapacityWh {
Bogdan Timofte authored a month ago
319
            if chargedDevice.hasMultipleChargingTransports {
320
                MeterInfoRowView(
321
                    label: "Wired Capacity",
322
                    value: "\(wiredCapacity.format(decimalDigits: 2)) Wh"
323
                )
324
            }
Bogdan Timofte authored a month ago
325
        }
326
        if let wirelessCapacity = chargedDevice.wirelessEstimatedBatteryCapacityWh {
Bogdan Timofte authored a month ago
327
            if chargedDevice.hasMultipleChargingTransports {
328
                MeterInfoRowView(
329
                    label: "Wireless Capacity",
330
                    value: "\(wirelessCapacity.format(decimalDigits: 2)) Wh"
331
                )
332
            }
Bogdan Timofte authored a month ago
333
        }
Bogdan Timofte authored a month ago
334
        if let wirelessEfficiencyFactor = chargedDevice.wirelessChargerEfficiencyFactor,
335
           chargedDevice.showsWirelessProfileDetails {
Bogdan Timofte authored a month ago
336
            MeterInfoRowView(
337
                label: "Wireless Efficiency",
338
                value: "\(Int((wirelessEfficiencyFactor * 100).rounded()))%"
339
            )
340
        }
341
        MeterInfoRowView(
342
            label: "Charge Sessions",
343
            value: "\(chargedDevice.sessionCount)"
344
        )
345
    }
Bogdan Timofte authored a month ago
346

            
Bogdan Timofte authored a month ago
347
    @ViewBuilder
348
    private func chargerInsights(_ chargedDevice: ChargedDeviceSummary) -> some View {
Bogdan Timofte authored a month ago
349
        if let chargerType = chargedDevice.chargerType {
350
            MeterInfoRowView(
351
                label: "Type",
352
                value: chargerType.title
353
            )
354
        }
Bogdan Timofte authored a month ago
355
        if !chargedDevice.chargerObservedVoltageSelections.isEmpty {
Bogdan Timofte authored a month ago
356
            MeterInfoRowView(
Bogdan Timofte authored a month ago
357
                label: "Observed Voltages",
358
                value: chargedDevice.chargerObservedVoltageSelections
359
                    .map { "\($0.format(decimalDigits: 1)) V" }
360
                    .joined(separator: ", ")
Bogdan Timofte authored a month ago
361
            )
Bogdan Timofte authored a month ago
362
        }
363
        if let chargerIdleCurrentAmps = chargedDevice.chargerIdleCurrentAmps {
Bogdan Timofte authored a month ago
364
            MeterInfoRowView(
Bogdan Timofte authored a month ago
365
                label: "Idle Current",
366
                value: "\(chargerIdleCurrentAmps.format(decimalDigits: 2)) A"
Bogdan Timofte authored a month ago
367
            )
Bogdan Timofte authored a month ago
368
        }
369
        if let chargerEfficiencyFactor = chargedDevice.chargerEfficiencyFactor {
Bogdan Timofte authored a month ago
370
            MeterInfoRowView(
Bogdan Timofte authored a month ago
371
                label: "Efficiency",
372
                value: "\(Int((chargerEfficiencyFactor * 100).rounded()))%"
Bogdan Timofte authored a month ago
373
            )
Bogdan Timofte authored a month ago
374
        }
375
        if let chargerMaximumPowerWatts = chargedDevice.chargerMaximumPowerWatts {
376
            MeterInfoRowView(
377
                label: "Max Power",
378
                value: "\(chargerMaximumPowerWatts.format(decimalDigits: 2)) W"
379
            )
380
        }
Bogdan Timofte authored a month ago
381
        if let latestStandbyPowerMeasurement = chargedDevice.latestStandbyPowerMeasurement {
382
            MeterInfoRowView(
383
                label: "Standby Power",
384
                value: "\(latestStandbyPowerMeasurement.averagePowerWatts.format(decimalDigits: 3)) W"
385
            )
386
            MeterInfoRowView(
387
                label: "Standby Projection",
388
                value: standbyEnergyLabel(latestStandbyPowerMeasurement.projectedYearlyEnergyWh) + " / year"
389
            )
390
        }
Bogdan Timofte authored a month ago
391
        MeterInfoRowView(
392
            label: "Wireless Sessions",
393
            value: "\(chargedDevice.sessionCount)"
394
        )
Bogdan Timofte authored a month ago
395

            
Bogdan Timofte authored a month ago
396
        if chargedDevice.chargerIdleCurrentAmps == nil {
397
            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.")
398
                .font(.caption2)
399
                .foregroundColor(.orange)
Bogdan Timofte authored a month ago
400
        }
401
    }
402

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

            
406
        return MeterInfoCardView(
407
            title: "Standby Power",
408
            tint: .orange
409
        ) {
410
            if standbyMeasurementMeters.isEmpty {
411
                Text("Connect a meter first. Standby measurement is launched from a live meter feed, so only currently available meters can be selected here.")
412
                    .font(.footnote)
413
                    .foregroundColor(.secondary)
414
                    .frame(maxWidth: .infinity, alignment: .leading)
415
            } else {
416
                NavigationLink(
417
                    destination: ChargerStandbyPowerWizardView(
418
                        preferredChargerID: chargedDevice.id,
419
                        locksChargerSelection: true
420
                    )
421
                ) {
422
                    Label("New Measurement", systemImage: "plus.circle.fill")
423
                        .font(.subheadline.weight(.semibold))
424
                        .foregroundColor(.orange)
425
                }
426
                .buttonStyle(.plain)
427
            }
428

            
429
            if let latestMeasurement {
430
                Divider()
431

            
432
                NavigationLink(
433
                    destination: ChargerStandbyPowerMeasurementDetailView(
434
                        chargerID: chargedDevice.id,
435
                        measurementID: latestMeasurement.id
436
                    )
437
                ) {
438
                    VStack(alignment: .leading, spacing: 8) {
439
                        HStack {
440
                            Text("Latest Measurement")
441
                                .font(.subheadline.weight(.semibold))
442
                                .foregroundColor(.primary)
443
                            Spacer()
444
                            Text("\(latestMeasurement.averagePowerWatts.format(decimalDigits: 3)) W")
445
                                .font(.subheadline.weight(.bold))
446
                                .foregroundColor(.primary)
447
                                .monospacedDigit()
448
                        }
449

            
450
                        Text(
451
                            "\(latestMeasurement.endedAt.format()) • \(latestMeasurement.sampleCount) samples • \(standbyEnergyLabel(latestMeasurement.projectedYearlyEnergyWh)) / year"
452
                        )
453
                        .font(.caption)
454
                        .foregroundColor(.secondary)
455
                    }
456
                }
457
                .buttonStyle(.plain)
458
            }
459

            
460
            if chargedDevice.standbyPowerMeasurements.isEmpty == false {
461
                Divider()
462

            
463
                NavigationLink(
464
                    destination: ChargerStandbyPowerMeasurementsView(chargerID: chargedDevice.id)
465
                ) {
466
                    Label("View Saved Measurements", systemImage: "clock.arrow.trianglehead.counterclockwise.rotate.90")
467
                        .font(.subheadline.weight(.semibold))
468
                        .foregroundColor(.blue)
469
                }
470
                .buttonStyle(.plain)
471
            }
472
        }
473
    }
474

            
Bogdan Timofte authored a month ago
475
    private func activeSessionSummaryCard(
Bogdan Timofte authored a month ago
476
        _ activeSession: ChargeSessionSummary,
477
        chargedDevice: ChargedDeviceSummary
478
    ) -> some View {
Bogdan Timofte authored a month ago
479
        NavigationLink(
480
            destination: ChargedDeviceActiveSessionView(chargedDeviceID: chargedDevice.id)
481
        ) {
482
            VStack(alignment: .leading, spacing: 14) {
483
                HStack(alignment: .firstTextBaseline) {
484
                    VStack(alignment: .leading, spacing: 4) {
485
                        Text("Current Session")
486
                            .font(.headline)
487
                            .foregroundColor(.primary)
488
                        Text(activeSession.status.title)
489
                            .font(.caption.weight(.semibold))
490
                            .foregroundColor(statusTint(for: activeSession))
491
                    }
Bogdan Timofte authored a month ago
492

            
Bogdan Timofte authored a month ago
493
                    Spacer()
Bogdan Timofte authored a month ago
494

            
Bogdan Timofte authored a month ago
495
                    Image(systemName: "chevron.right")
496
                        .font(.caption.weight(.semibold))
497
                        .foregroundColor(.secondary)
Bogdan Timofte authored a month ago
498
                }
499

            
Bogdan Timofte authored a month ago
500
                LazyVGrid(columns: activeSessionSummaryColumns, spacing: 8) {
501
                    activeSessionMetricCell(
502
                        label: "Energy",
503
                        value: "\(activeSession.effectiveOrMeasuredEnergyWh.format(decimalDigits: 2)) Wh",
504
                        tint: .teal
505
                    )
506
                    activeSessionMetricCell(
507
                        label: "Duration",
508
                        value: sessionDurationText(activeSession),
509
                        tint: .orange
510
                    )
511
                    if let maximumObservedPowerWatts = activeSession.maximumObservedPowerWatts {
512
                        activeSessionMetricCell(
513
                            label: "Max Power",
514
                            value: "\(maximumObservedPowerWatts.format(decimalDigits: 2)) W",
515
                            tint: .blue
516
                        )
517
                    }
518
                    if let batteryPrediction = chargedDevice.batteryLevelPrediction(for: activeSession) {
519
                        activeSessionMetricCell(
520
                            label: "Battery",
521
                            value: "\(batteryPrediction.predictedPercent.format(decimalDigits: 0))%",
522
                            tint: .green
523
                        )
524
                    } else if let targetBatteryPercent = activeSession.targetBatteryPercent {
525
                        activeSessionMetricCell(
526
                            label: "Target",
527
                            value: "\(targetBatteryPercent.format(decimalDigits: 0))%",
528
                            tint: .indigo
529
                        )
530
                    }
Bogdan Timofte authored a month ago
531
                }
532

            
Bogdan Timofte authored a month ago
533
                Text("Started \(activeSession.startedAt.format())")
534
                    .font(.caption)
Bogdan Timofte authored a month ago
535
                    .foregroundColor(.secondary)
536
            }
Bogdan Timofte authored a month ago
537
        }
538
        .buttonStyle(.plain)
539
        .padding(18)
540
        .meterCard(tint: statusTint(for: activeSession), fillOpacity: 0.16, strokeOpacity: 0.22, cornerRadius: 18)
541
    }
Bogdan Timofte authored a month ago
542

            
Bogdan Timofte authored a month ago
543
    private var activeSessionSummaryColumns: [GridItem] {
544
        [
545
            GridItem(.flexible(minimum: 92), spacing: 8),
546
            GridItem(.flexible(minimum: 92), spacing: 8)
547
        ]
548
    }
Bogdan Timofte authored a month ago
549

            
Bogdan Timofte authored a month ago
550
    private func activeSessionMetricCell(label: String, value: String, tint: Color) -> some View {
551
        VStack(alignment: .leading, spacing: 4) {
552
            Text(label)
553
                .font(.caption2)
554
                .foregroundColor(.secondary)
555
            Text(value)
556
                .font(.footnote.weight(.semibold))
557
                .foregroundColor(.primary)
558
                .monospacedDigit()
559
                .lineLimit(1)
560
                .minimumScaleFactor(0.8)
Bogdan Timofte authored a month ago
561
        }
Bogdan Timofte authored a month ago
562
        .frame(maxWidth: .infinity, alignment: .leading)
563
        .padding(10)
564
        .meterCard(tint: tint, fillOpacity: 0.08, strokeOpacity: 0.12, cornerRadius: 12)
Bogdan Timofte authored a month ago
565
    }
566

            
567
    private func capacityEvolutionCard(_ chargedDevice: ChargedDeviceSummary) -> some View {
568
        VStack(alignment: .leading, spacing: 12) {
569
            Text("Capacity Evolution")
570
                .font(.headline)
571

            
572
            ForEach(chargedDevice.capacityHistory.suffix(6)) { point in
573
                HStack {
574
                    Text(point.timestamp.format())
575
                        .font(.caption)
576
                        .foregroundColor(.secondary)
577
                    Spacer()
Bogdan Timofte authored a month ago
578
                    if chargedDevice.shouldShowChargingTransport(point.chargingTransportMode) {
579
                        Text(point.chargingTransportMode.title)
580
                            .font(.caption2)
581
                            .foregroundColor(.secondary)
582
                        Text("•")
583
                            .foregroundColor(.secondary)
584
                    }
Bogdan Timofte authored a month ago
585
                    Text("\(point.capacityWh.format(decimalDigits: 2)) Wh")
586
                        .font(.footnote.weight(.semibold))
587
                }
588
            }
589
        }
590
        .frame(maxWidth: .infinity, alignment: .leading)
591
        .padding(18)
592
        .meterCard(tint: .orange, fillOpacity: 0.14, strokeOpacity: 0.20, cornerRadius: 18)
593
    }
594

            
595
    private func typicalCurveCard(_ chargedDevice: ChargedDeviceSummary) -> some View {
596
        VStack(alignment: .leading, spacing: 12) {
597
            Text("Typical Charge Curve")
598
                .font(.headline)
599

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

            
Bogdan Timofte authored a month ago
620
    private func sessionHistorySummaryCard(_ chargedDevice: ChargedDeviceSummary) -> some View {
621
        let sessions = closedSessions(for: chargedDevice)
622
        let latestSession = sessions.first
623
        let totalEnergyWh = sessions.reduce(0) { $0 + $1.effectiveOrMeasuredEnergyWh }
Bogdan Timofte authored a month ago
624

            
Bogdan Timofte authored a month ago
625
        return MeterInfoCardView(title: "Session History", tint: .teal) {
626
            MeterInfoRowView(label: "Closed Sessions", value: "\(sessions.count)")
627
            MeterInfoRowView(label: "Total Energy", value: "\(totalEnergyWh.format(decimalDigits: 2)) Wh")
628
            if let latestSession {
629
                MeterInfoRowView(label: "Latest", value: latestSession.startedAt.format())
630
            }
Bogdan Timofte authored a month ago
631

            
Bogdan Timofte authored a month ago
632
            NavigationLink(
633
                destination: ChargedDeviceSessionsView(chargedDeviceID: chargedDevice.id)
634
            ) {
635
                Label("Manage Sessions", systemImage: "clock.arrow.trianglehead.counterclockwise.rotate.90")
636
                    .font(.subheadline.weight(.semibold))
637
                    .frame(maxWidth: .infinity)
638
                    .padding(.vertical, 10)
639
                    .meterCard(tint: .teal, fillOpacity: 0.16, strokeOpacity: 0.22, cornerRadius: 14)
Bogdan Timofte authored a month ago
640
            }
Bogdan Timofte authored a month ago
641
            .buttonStyle(.plain)
Bogdan Timofte authored a month ago
642
        }
643
    }
644

            
Bogdan Timofte authored a month ago
645
    private func closedSessions(for chargedDevice: ChargedDeviceSummary) -> [ChargeSessionSummary] {
646
        chargedDevice.sessions.filter { !$0.status.isOpen }
Bogdan Timofte authored a month ago
647
    }
648

            
Bogdan Timofte authored a month ago
649
    private func availableTabs(for chargedDevice: ChargedDeviceSummary) -> [DetailTab] {
650
        if chargedDevice.isCharger {
651
            return [.overview, .standby, .sessions, .settings]
Bogdan Timofte authored a month ago
652
        }
Bogdan Timofte authored a month ago
653
        return [.overview, .sessions, .trends, .settings]
654
    }
Bogdan Timofte authored a month ago
655

            
Bogdan Timofte authored a month ago
656
    private func displayedTab(from tabs: [DetailTab]) -> DetailTab {
657
        if tabs.contains(selectedTab) {
658
            return selectedTab
Bogdan Timofte authored a month ago
659
        }
Bogdan Timofte authored a month ago
660
        return tabs.first ?? .overview
661
    }
662

            
663
    private func ensureSelectedTabExists(for chargedDevice: ChargedDeviceSummary) {
664
        let tabs = availableTabs(for: chargedDevice)
665
        if !tabs.contains(selectedTab) {
666
            selectedTab = tabs.first ?? .overview
Bogdan Timofte authored a month ago
667
        }
Bogdan Timofte authored a month ago
668
    }
669

            
670
    private func title(for tab: DetailTab) -> String {
671
        switch tab {
672
        case .overview:
673
            return "Overview"
674
        case .standby:
675
            return "Standby"
676
        case .sessions:
677
            return "Sessions"
678
        case .trends:
679
            return "Trends"
680
        case .settings:
681
            return "Settings"
Bogdan Timofte authored a month ago
682
        }
Bogdan Timofte authored a month ago
683
    }
Bogdan Timofte authored a month ago
684

            
Bogdan Timofte authored a month ago
685
    private func systemImage(for tab: DetailTab) -> String {
686
        switch tab {
687
        case .overview:
688
            return "house.fill"
689
        case .standby:
690
            return "bolt.badge.clock"
691
        case .sessions:
692
            return "clock.arrow.trianglehead.counterclockwise.rotate.90"
693
        case .trends:
694
            return "chart.xyaxis.line"
695
        case .settings:
696
            return "gearshape.fill"
697
        }
698
    }
699

            
700
    private func detailBackground(for chargedDevice: ChargedDeviceSummary) -> some View {
701
        LinearGradient(
702
            colors: [tint(for: chargedDevice).opacity(0.18), Color.clear],
703
            startPoint: .topLeading,
704
            endPoint: .bottomTrailing
705
        )
706
        .ignoresSafeArea()
Bogdan Timofte authored a month ago
707
    }
708

            
709
    private func tint(for chargedDevice: ChargedDeviceSummary) -> Color {
710
        switch chargedDevice.deviceClass {
711
        case .iphone:
712
            return .blue
713
        case .watch:
714
            return .green
715
        case .powerbank:
716
            return .orange
717
        case .charger:
718
            return .pink
719
        case .other:
720
            return .secondary
721
        }
722
    }
723

            
Bogdan Timofte authored a month ago
724
    private func statusTint(for session: ChargeSessionSummary) -> Color {
725
        switch session.status {
726
        case .active:
727
            return .green
728
        case .paused:
729
            return .orange
730
        case .completed:
731
            return .teal
732
        case .abandoned:
733
            return .secondary
734
        }
735
    }
736

            
737
    private func sessionDurationText(_ session: ChargeSessionSummary) -> String {
738
        let formatter = DateComponentsFormatter()
739
        let effectiveDuration = max(session.effectiveDuration, 0)
740
        formatter.allowedUnits = effectiveDuration >= 3600 ? [.hour, .minute] : [.minute, .second]
741
        formatter.unitsStyle = .abbreviated
742
        formatter.zeroFormattingBehavior = .dropAll
743
        return formatter.string(from: effectiveDuration) ?? "0m"
744
    }
745

            
Bogdan Timofte authored a month ago
746
    private func standbyEnergyLabel(_ wattHours: Double) -> String {
747
        if wattHours >= 1000 {
748
            return "\((wattHours / 1000).format(decimalDigits: 3)) kWh"
749
        }
750
        return "\(wattHours.format(decimalDigits: 2)) Wh"
751
    }
752

            
753
    private var standbyMeasurementMeters: [AppData.MeterSummary] {
754
        appData.meterSummaries.filter { $0.meter != nil }
755
    }
756

            
Bogdan Timofte authored a month ago
757
    private func completionCurrentDescription(
758
        for chargedDevice: ChargedDeviceSummary,
Bogdan Timofte authored a month ago
759
        sessionKind: ChargeSessionKind
Bogdan Timofte authored a month ago
760
    ) -> String {
Bogdan Timofte authored a month ago
761
        if let configuredCurrent = chargedDevice.configuredCompletionCurrentAmps(for: sessionKind) {
762
            if let learnedCurrent = chargedDevice.learnedCompletionCurrentAmps(for: sessionKind),
Bogdan Timofte authored a month ago
763
               abs(configuredCurrent - learnedCurrent) >= 0.01 {
764
                return "\(configuredCurrent.format(decimalDigits: 2)) A configured • \(learnedCurrent.format(decimalDigits: 2)) A learned"
765
            }
766
            return "\(configuredCurrent.format(decimalDigits: 2)) A configured"
767
        }
768

            
Bogdan Timofte authored a month ago
769
        if let learnedCurrent = chargedDevice.learnedCompletionCurrentAmps(for: sessionKind) {
Bogdan Timofte authored a month ago
770
            return "\(learnedCurrent.format(decimalDigits: 2)) A learned"
771
        }
772

            
773
        return "Learning"
774
    }
775

            
Bogdan Timofte authored a month ago
776
    private func completionCurrentLabel(
777
        for chargedDevice: ChargedDeviceSummary,
778
        sessionKind: ChargeSessionKind
779
    ) -> String {
780
        let showsTransport = chargedDevice.shouldShowChargingTransport(sessionKind.chargingTransportMode)
781
        let showsState = chargedDevice.shouldShowChargingStateMode(sessionKind.chargingStateMode)
782

            
783
        switch (showsTransport, showsState) {
784
        case (true, true):
785
            return "\(sessionKind.shortTitle) Stop Current"
786
        case (true, false):
787
            return "\(sessionKind.chargingTransportMode.title) Stop Current"
788
        case (false, true):
789
            return "\(sessionKind.chargingStateMode.title) Stop Current"
790
        case (false, false):
791
            return "Stop Current"
792
        }
793
    }
794

            
Bogdan Timofte authored a month ago
795
    private func completionSessionKinds(for chargedDevice: ChargedDeviceSummary) -> [ChargeSessionKind] {
796
        chargedDevice.supportedChargingModes.flatMap { chargingTransportMode in
797
            chargedDevice.supportedChargingStateModes.map { chargingStateMode in
798
                ChargeSessionKind(
799
                    chargingTransportMode: chargingTransportMode,
800
                    chargingStateMode: chargingStateMode
801
                )
802
            }
803
        }
804
    }
805

            
Bogdan Timofte authored a month ago
806
    private func showEditor() {
807
        editorVisibility = true
Bogdan Timofte authored a month ago
808
    }
809

            
Bogdan Timofte authored a month ago
810
    private func showDeleteConfirmation() {
811
        deleteConfirmationVisibility = true
Bogdan Timofte authored a month ago
812
    }
813

            
Bogdan Timofte authored a month ago
814
    private var deletionTitle: String {
815
        appData.chargedDeviceSummary(id: chargedDeviceID)?.isCharger == true ? "charger" : "device"
816
    }
817

            
818
    private var deletionMessage: String {
819
        if appData.chargedDeviceSummary(id: chargedDeviceID)?.isCharger == true {
820
            return "This removes the charger from the library and unlinks it from wireless sessions that used it."
821
        }
822
        return "This removes the device and its stored charging history from the library."
823
    }
824

            
Bogdan Timofte authored a month ago
825
}