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

            
8
import SwiftUI
9

            
10
struct SidebarChargedDevicesSectionView: View {
11
    let title: String
12
    let chargedDevices: [ChargedDeviceSummary]
13
    let emptyStateText: String
14
    let tint: Color
15
    let onAdd: () -> Void
16

            
17
    var body: some View {
18
        Section(header: headerView) {
19
            if chargedDevices.isEmpty {
20
                Text(emptyStateText)
21
                    .font(.footnote)
22
                    .foregroundColor(.secondary)
23
                    .frame(maxWidth: .infinity, alignment: .leading)
24
                    .padding(18)
25
                    .meterCard(tint: tint, fillOpacity: 0.12, strokeOpacity: 0.18)
26
            } else {
27
                ForEach(chargedDevices) { chargedDevice in
28
                    NavigationLink(destination: ChargedDeviceDetailView(chargedDeviceID: chargedDevice.id)) {
29
                        ChargedDeviceSidebarCardView(chargedDevice: chargedDevice)
30
                    }
31
                    .buttonStyle(.plain)
32
                }
33
            }
34
        }
35
    }
36

            
37
    private var headerView: some View {
38
        HStack(alignment: .firstTextBaseline, spacing: 10) {
39
            Text(title)
40
                .font(.headline)
41
            Spacer()
42
            Button(action: onAdd) {
43
                Image(systemName: "plus.circle.fill")
44
                    .font(.body.weight(.semibold))
45
                    .foregroundColor(tint)
46
            }
47
            .buttonStyle(.plain)
48
            Text("\(chargedDevices.count)")
49
                .font(.caption.weight(.bold))
50
                .padding(.horizontal, 10)
51
                .padding(.vertical, 6)
52
                .meterCard(tint: tint, fillOpacity: 0.18, strokeOpacity: 0.24, cornerRadius: 999)
53
        }
54
    }
55
}
56

            
57
private struct ChargedDeviceSidebarCardView: View {
58
    let chargedDevice: ChargedDeviceSummary
59

            
60
    var body: some View {
61
        HStack(alignment: .top, spacing: 12) {
62
            ChargedDeviceQRCodeView(qrIdentifier: chargedDevice.qrIdentifier, side: 54)
63

            
64
            VStack(alignment: .leading, spacing: 6) {
65
                HStack {
Bogdan Timofte authored a month ago
66
                    Label(chargedDevice.name, systemImage: chargedDevice.identitySymbolName)
Bogdan Timofte authored a month ago
67
                        .font(.headline)
68
                    if chargedDevice.activeSession != nil {
69
                        Spacer()
70
                        Text("Live")
71
                            .font(.caption.weight(.bold))
72
                            .foregroundColor(.green)
73
                    }
74
                }
75

            
Bogdan Timofte authored a month ago
76
                Text(chargedDevice.identityTitle)
Bogdan Timofte authored a month ago
77
                    .font(.caption.weight(.semibold))
78
                    .foregroundColor(.secondary)
79

            
Bogdan Timofte authored a month ago
80
                if chargedDevice.isCharger {
81
                    if let chargerMaximumPowerWatts = chargedDevice.chargerMaximumPowerWatts {
82
                        Text("Max power: \(chargerMaximumPowerWatts.format(decimalDigits: 2)) W")
83
                            .font(.caption2)
84
                            .foregroundColor(.secondary)
85
                    } else {
86
                        Text("Wireless charger")
87
                            .font(.caption2)
88
                            .foregroundColor(.secondary)
89
                    }
Bogdan Timofte authored a month ago
90
                } else {
Bogdan Timofte authored a month ago
91
                    Text(chargedDevice.supportedChargingModes.map(\.title).joined(separator: " + "))
Bogdan Timofte authored a month ago
92
                        .font(.caption2)
93
                        .foregroundColor(.secondary)
Bogdan Timofte authored a month ago
94

            
95
                    if let estimatedCapacityWh = chargedDevice.estimatedBatteryCapacityWh {
96
                        Text("Capacity: \(estimatedCapacityWh.format(decimalDigits: 2)) Wh")
97
                            .font(.caption2)
98
                            .foregroundColor(.secondary)
99
                    } else {
100
                        Text("Capacity: learning")
101
                            .font(.caption2)
102
                            .foregroundColor(.secondary)
103
                    }
Bogdan Timofte authored a month ago
104
                }
105
            }
106
        }
107
        .padding(.vertical, 4)
108
    }
109
}