USB-Meter / USB Meter / Views / Meter / Tabs / Connection / Components / ConnectionHomeInfoPreviewView.swift
Newer Older
78 lines | 3.46kb
Bogdan Timofte authored a week ago
1
//
2
//  ConnectionHomeInfoPreviewView.swift
3
//  USB Meter
4
//
5
//  Created by Bogdan Timofte on 29/03/2026.
6
//  Co-authored-by: GPT-5.3-Codex.
7
//  Copyright © 2026 Bogdan Timofte. All rights reserved.
8
//
9

            
10
import SwiftUI
11

            
12
struct ConnectionHomeInfoPreviewView: View {
13
    let meter: Meter
14

            
15
    var body: some View {
16
        VStack(spacing: 14) {
17
            MeterInfoCard(title: "Overview", tint: meter.color) {
18
                MeterInfoRow(label: "Name", value: meter.name)
19
                MeterInfoRow(label: "Device Model", value: meter.deviceModelName)
20
                MeterInfoRow(label: "Advertised Model", value: meter.modelString)
21
                MeterInfoRow(label: "Working Voltage", value: meter.documentedWorkingVoltage)
22
                MeterInfoRow(label: "Temperature Unit", value: meter.temperatureUnitDescription)
23
                MeterInfoRow(label: "Last Seen", value: meterHistoryText(for: meter.lastSeen))
24
                MeterInfoRow(label: "Last Connected", value: meterHistoryText(for: meter.lastConnectedAt))
25
            }
26

            
27
            MeterInfoCard(title: "Identifiers", tint: .blue) {
28
                MeterInfoRow(label: "MAC", value: meter.btSerial.macAddress.description)
29
                if meter.modelNumber != 0 {
30
                    MeterInfoRow(label: "Model Identifier", value: "\(meter.modelNumber)")
31
                }
32
            }
33

            
34
            MeterInfoCard(title: "Screen Reporting", tint: .orange) {
35
                if meter.reportsCurrentScreenIndex {
36
                    MeterInfoRow(label: "Current Screen", value: meter.currentScreenDescription)
37
                    Text("The active screen index is reported by the meter and mapped by the app to a known label.")
38
                        .font(.footnote)
39
                        .foregroundColor(.secondary)
40
                } else {
41
                    MeterInfoRow(label: "Current Screen", value: "Not Reported")
42
                    Text("The current screen is not reported by the device payload, or we have not yet identified where and how the protocol announces it.")
43
                        .font(.footnote)
44
                        .foregroundColor(.secondary)
45
                }
46
            }
47

            
48
            MeterInfoCard(title: "Live Device Details", tint: .indigo) {
49
                if meter.operationalState == .dataIsAvailable {
50
                    if !meter.firmwareVersion.isEmpty {
51
                        MeterInfoRow(label: "Firmware", value: meter.firmwareVersion)
52
                    }
53
                    if meter.supportsChargerDetection {
54
                        MeterInfoRow(label: "Detected Charger", value: meter.chargerTypeDescription)
55
                    }
56
                    if meter.serialNumber != 0 {
57
                        MeterInfoRow(label: "Serial", value: "\(meter.serialNumber)")
58
                    }
59
                    if meter.bootCount != 0 {
60
                        MeterInfoRow(label: "Boot Count", value: "\(meter.bootCount)")
61
                    }
62
                } else {
63
                    Text("Connect to the meter to load firmware, serial, and boot details.")
64
                        .font(.footnote)
65
                        .foregroundColor(.secondary)
66
                }
67
            }
68
        }
69
        .padding(.horizontal, 12)
70
    }
71

            
72
    private func meterHistoryText(for date: Date?) -> String {
73
        guard let date else {
74
            return "Never"
75
        }
76
        return date.format(as: "yyyy-MM-dd HH:mm")
77
    }
78
}