USB-Meter / USB Meter / Views / Sidebar / SidebarList / Sections / SidebarUSBMetersSectionView.swift
Newer Older
128 lines | 4.662kb
Bogdan Timofte authored 2 months ago
1
//
2
//  SidebarUSBMetersSectionView.swift
3
//  USB Meter
4
//
5

            
6
import SwiftUI
7
import CoreBluetooth
8

            
9
struct SidebarUSBMetersSectionView: View {
10
    let meters: [AppData.MeterSummary]
11
    let managerState: CBManagerState
12
    let hasLiveMeters: Bool
13
    let scanStartedAt: Date?
14
    let now: Date
15
    let noDevicesHelpDelay: TimeInterval
Bogdan Timofte authored a month ago
16
    let isExpanded: Bool
17
    let onToggle: () -> Void
Bogdan Timofte authored a month ago
18
    let onAddMeter: () -> Void
Bogdan Timofte authored 2 months ago
19

            
20
    var body: some View {
21
        Section(header: usbSectionHeader) {
Bogdan Timofte authored a month ago
22
            if isExpanded {
23
                if meters.isEmpty {
24
                    Text(devicesEmptyStateText)
25
                        .font(.footnote)
26
                        .foregroundColor(.secondary)
27
                        .frame(maxWidth: .infinity, alignment: .leading)
28
                        .padding(18)
29
                        .meterCard(
30
                            tint: isWaitingForFirstDiscovery ? .blue : .secondary,
31
                            fillOpacity: 0.14,
32
                            strokeOpacity: 0.20
33
                        )
34
                        .transition(.opacity.combined(with: .move(edge: .top)))
35
                } else {
36
                    ForEach(meters) { meterSummary in
37
                        if let meter = meterSummary.meter {
38
                            NavigationLink(destination: MeterView().environmentObject(meter)) {
Bogdan Timofte authored a month ago
39
                                SidebarMeterCardView()
Bogdan Timofte authored a month ago
40
                                    .environmentObject(meter)
41
                            }
42
                            .buttonStyle(.plain)
43
                            .transition(.opacity.combined(with: .move(edge: .top)))
Bogdan Timofte authored 2 months ago
44
                        }
45
                    }
46
                }
47
            }
48
        }
49
    }
50

            
51
    private var isWaitingForFirstDiscovery: Bool {
52
        guard managerState == .poweredOn else {
53
            return false
54
        }
55
        guard hasLiveMeters == false else {
56
            return false
57
        }
58
        guard let scanStartedAt else {
59
            return false
60
        }
61
        return now.timeIntervalSince(scanStartedAt) < noDevicesHelpDelay
62
    }
63

            
64
    private var devicesEmptyStateText: String {
65
        if isWaitingForFirstDiscovery {
66
            return "Scanning for nearby supported meters..."
67
        }
68
        return "No meters yet. Nearby supported meters will appear here and remain available after they disappear."
69
    }
70

            
71
    private var usbSectionHeader: some View {
Bogdan Timofte authored 2 months ago
72
        HStack(alignment: .firstTextBaseline) {
Bogdan Timofte authored a month ago
73
            Button(action: onToggle) {
74
                HStack(alignment: .firstTextBaseline, spacing: 4) {
75
                    Image(systemName: "chevron.right")
76
                        .font(.caption.weight(.semibold))
Bogdan Timofte authored 2 months ago
77
                        .foregroundColor(.secondary)
Bogdan Timofte authored a month ago
78
                        .rotationEffect(.degrees(isExpanded ? 90 : 0))
79
                        .animation(.easeInOut(duration: 0.22), value: isExpanded)
80
                    VStack(alignment: .leading, spacing: 2) {
81
                        Text("USB & Known Meters")
82
                            .font(.headline)
83
                        if meters.isEmpty == false {
84
                            Text(sectionSubtitleText)
85
                                .font(.caption)
86
                                .foregroundColor(.secondary)
87
                                .lineLimit(1)
88
                        }
89
                    }
Bogdan Timofte authored 2 months ago
90
                }
91
            }
Bogdan Timofte authored a month ago
92
            .buttonStyle(.plain)
Bogdan Timofte authored 2 months ago
93
            Spacer()
Bogdan Timofte authored a month ago
94
            Button(action: onAddMeter) {
95
                Image(systemName: "plus.circle.fill")
96
                    .font(.body.weight(.semibold))
97
                    .foregroundColor(.blue)
98
            }
99
            .buttonStyle(.plain)
Bogdan Timofte authored 2 months ago
100
            Text("\(meters.count)")
101
                .font(.caption.weight(.bold))
102
                .padding(.horizontal, 10)
103
                .padding(.vertical, 6)
104
                .meterCard(tint: .blue, fillOpacity: 0.18, strokeOpacity: 0.24, cornerRadius: 999)
105
        }
106
    }
Bogdan Timofte authored 2 months ago
107

            
108
    private var sectionSubtitleText: String {
109
        switch (liveMeterCount, offlineMeterCount) {
110
        case let (live, offline) where live > 0 && offline > 0:
111
            return "\(live) live • \(offline) stored"
112
        case let (live, _) where live > 0:
113
            return "\(live) live meter\(live == 1 ? "" : "s")"
114
        case let (_, offline) where offline > 0:
115
            return "\(offline) known meter\(offline == 1 ? "" : "s")"
116
        default:
117
            return ""
118
        }
119
    }
120

            
121
    private var liveMeterCount: Int {
122
        meters.filter { $0.meter != nil }.count
123
    }
124

            
125
    private var offlineMeterCount: Int {
126
        max(0, meters.count - liveMeterCount)
127
    }
Bogdan Timofte authored 2 months ago
128
}