USB-Meter / USB Meter / Views / MeterMappingDebugView.swift
Newer Older
84 lines | 2.826kb
Bogdan Timofte authored 2 months ago
1
//  MeterMappingDebugView.swift
2
//  USB Meter
3
//
4
//  Created by Codex on 2026.
5
//
6

            
7
import SwiftUI
8

            
9
struct MeterMappingDebugView: View {
10
    @State private var records: [MeterNameRecord] = []
11
    private let store = MeterNameStore.shared
12
    private let changePublisher = NotificationCenter.default.publisher(for: .meterNameStoreDidChange)
13

            
14
    var body: some View {
Bogdan Timofte authored 2 months ago
15
        List {
16
            Section {
17
                VStack(alignment: .leading, spacing: 8) {
18
                    Text(store.currentCloudAvailability.helpTitle)
19
                        .font(.headline)
20
                    Text(store.currentCloudAvailability.helpMessage)
21
                        .font(.caption)
22
                        .foregroundColor(.secondary)
Bogdan Timofte authored 2 months ago
23
                }
Bogdan Timofte authored 2 months ago
24
                .padding(.vertical, 6)
25
            } header: {
Bogdan Timofte authored a month ago
26
                ContextInfoHeader(
27
                    title: "Sync Status",
28
                    message: "This screen is limited to meter sync metadata visible on this device through the local store and iCloud KVS."
29
                )
Bogdan Timofte authored 2 months ago
30
            }
31

            
32
            Section {
33
                ForEach(records) { record in
34
                    VStack(alignment: .leading, spacing: 6) {
35
                        Text(record.customName)
36
                            .font(.headline)
37
                        Text(record.macAddress)
38
                            .font(.caption.monospaced())
39
                            .foregroundColor(.secondary)
40
                        HStack {
41
                            Text("TC66 unit:")
42
                                .font(.caption.weight(.semibold))
43
                            Text(record.temperatureUnit)
44
                                .font(.caption.monospaced())
45
                                .foregroundColor(.blue)
46
                        }
47
                    }
48
                    .padding(.vertical, 8)
49
                }
50
            } header: {
51
                Text("KVS Meter Mapping")
Bogdan Timofte authored 2 months ago
52
            }
53
        }
54
        .listStyle(.insetGrouped)
Bogdan Timofte authored 2 months ago
55
        .navigationTitle("Meter Sync Debug")
Bogdan Timofte authored a month ago
56
        .navigationBarTitleDisplayMode(.inline)
Bogdan Timofte authored 2 months ago
57
        .onAppear(perform: reload)
58
        .onReceive(changePublisher) { _ in reload() }
59
        .toolbar {
60
            Button("Refresh") {
61
                reload()
62
            }
63
        }
Bogdan Timofte authored a month ago
64
        .sidebarToggleToolbarItem()
Bogdan Timofte authored 2 months ago
65
    }
66

            
67
    private func reload() {
68
        records = store.allRecords().map { record in
69
            MeterNameRecord(
70
                id: record.id,
71
                macAddress: record.macAddress,
72
                customName: record.customName ?? "<unnamed>",
73
                temperatureUnit: record.temperatureUnit ?? "n/a"
74
            )
75
        }
76
    }
77
}
78

            
79
private struct MeterNameRecord: Identifiable {
80
    let id: String
81
    let macAddress: String
82
    let customName: String
83
    let temperatureUnit: String
84
}