USB-Meter / USB Meter / Views / MeterMappingDebugView.swift
1 contributor
82 lines | 2.773kb
//  MeterMappingDebugView.swift
//  USB Meter
//
//  Created by Codex on 2026.
//

import SwiftUI

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

    var body: some View {
        List {
            Section {
                VStack(alignment: .leading, spacing: 8) {
                    Text(store.currentCloudAvailability.helpTitle)
                        .font(.headline)
                    Text("This screen is limited to meter sync metadata visible on this device through the local store and iCloud KVS.")
                        .font(.caption)
                        .foregroundColor(.secondary)
                    Text(store.currentCloudAvailability.helpMessage)
                        .font(.caption)
                        .foregroundColor(.secondary)
                }
                .padding(.vertical, 6)
            } header: {
                Text("Sync Status")
            }

            Section {
                ForEach(records) { record in
                    VStack(alignment: .leading, spacing: 6) {
                        Text(record.customName)
                            .font(.headline)
                        Text(record.macAddress)
                            .font(.caption.monospaced())
                            .foregroundColor(.secondary)
                        HStack {
                            Text("TC66 unit:")
                                .font(.caption.weight(.semibold))
                            Text(record.temperatureUnit)
                                .font(.caption.monospaced())
                                .foregroundColor(.blue)
                        }
                    }
                    .padding(.vertical, 8)
                }
            } header: {
                Text("KVS Meter Mapping")
            }
        }
        .listStyle(.insetGrouped)
        .navigationTitle("Meter Sync Debug")
        .onAppear(perform: reload)
        .onReceive(changePublisher) { _ in reload() }
        .toolbar {
            Button("Refresh") {
                reload()
            }
        }
    }

    private func reload() {
        records = store.allRecords().map { record in
            MeterNameRecord(
                id: record.id,
                macAddress: record.macAddress,
                customName: record.customName ?? "<unnamed>",
                temperatureUnit: record.temperatureUnit ?? "n/a"
            )
        }
    }
}

private struct MeterNameRecord: Identifiable {
    let id: String
    let macAddress: String
    let customName: String
    let temperatureUnit: String
}