USB-Meter / USB Meter / Views / MeterMappingDebugView.swift
1 contributor
60 lines | 1.797kb
//  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(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)
        }
        .listStyle(.insetGrouped)
        .navigationTitle("Meter Name Mapping")
        .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
}