SPB Git

Repository Public

MetricViews.swift 5.9 KB 122 lines swift

1import SwiftUI23/// Freshness of telemetry as computed by the server, with the observation time. Text + icon, never colour alone.4public struct FreshnessLabel: View {5    @Environment(\.mccTheme) private var theme6    private let freshness: API.Freshness7    private let observedAt: Date?8    private let showsTime: Bool9    public init(_ freshness: API.Freshness, observedAt: Date? = nil, showsTime: Bool = true) {10        self.freshness = freshness; self.observedAt = observedAt; self.showsTime = showsTime11    }12    private var detail: String? {13        guard showsTime else { return nil }14        guard let observedAt else { return freshness == .offline ? tr("freshness.never_seen") : nil }15        return Format.relative(observedAt)16    }17    public var body: some View {18        HStack(spacing: Tokens.Space.s2) {19            Image(systemName: freshness.tone.symbol).imageScale(.small).foregroundStyle(freshness.tone.color(theme)).accessibilityHidden(true)20            Text(freshness.label).foregroundStyle(theme.textPrimary)21            if let detail { Text("· " + detail).foregroundStyle(theme.textTertiary) }22        }23        .lineLimit(1)24        .accessibilityElement(children: .ignore)25        .accessibilityLabel(tr("freshness.title"))26        .accessibilityValue([freshness.label, detail].compactMap { $0 }.joined(separator: ", "))27    }28}2930/// A measured value, or the explicit reason why there is none (`unsupported`, `permission_denied`,31/// `unavailable`, `stale`, `offline`). Never a made-up number, never a silent zero.32public struct MetricValueView: View {33    @Environment(\.mccTheme) private var theme34    private let title: String?35    private let value: API.MetricValue?36    private let unit: String?37    private let large: Bool3839    /// - Parameters:40    ///   - value: nil means the server did not report this metric at all.41    ///   - unit: overrides the unit carried by the value.42    public init(_ title: String? = nil, value: API.MetricValue?, unit: String? = nil, large: Bool = false) {43        self.title = title; self.value = value; self.unit = unit; self.large = large44    }4546    public static func text(for value: API.MetricValue?, unit: String? = nil) -> (text: String, measured: Bool) {47        guard let value else { return (tr("metric.not_reported"), false) }48        if let v = value.value, value.availability == .available || value.availability == .stale {49            return (Format.metric(v, unit: unit ?? value.unit), true)50        }51        if value.availability == .available { return (tr("metric.not_reported"), false) }52        return (value.availability.label, false)53    }5455    public var body: some View {56        let rendered = Self.text(for: value, unit: unit)57        VStack(alignment: .leading, spacing: Tokens.Space.s1) {58            if let title { Text(title).font(.caption).foregroundStyle(theme.textSecondary) }59            if rendered.measured {60                HStack(alignment: .firstTextBaseline, spacing: Tokens.Space.s2) {61                    Text(rendered.text)62                        .font(large ? .title2.weight(.semibold).monospacedDigit() : .body.monospacedDigit())63                        .foregroundStyle(theme.textPrimary)64                    if value?.availability == .stale {65                        Image(systemName: StatusTone.stale.symbol).imageScale(.small).foregroundStyle(StatusTone.stale.color(theme))66                            .accessibilityLabel(API.Availability.stale.label)67                    }68                }69            } else {70                HStack(spacing: Tokens.Space.s2) {71                    Image(systemName: (value?.availability.tone ?? .neutral).symbol).imageScale(.small)72                        .foregroundStyle((value?.availability.tone ?? .neutral).color(theme)).accessibilityHidden(true)73                    Text(rendered.text).font(large ? .headline : .callout).foregroundStyle(theme.textSecondary)74                }75                if let reason = value?.reason, !reason.isEmpty {76                    Text(reason).font(.caption).foregroundStyle(theme.textTertiary).lineLimit(2)77                }78            }79        }80        .frame(maxWidth: .infinity, alignment: .leading)81        .accessibilityElement(children: .ignore)82        .accessibilityLabel(title ?? "")83        .accessibilityValue(rendered.text + (value?.reason.map { ", \($0)" } ?? ""))84    }85}8687/// An optional figure of the API (absent = not measured): shows the formatted value or "not measured".88public struct OptionalValueText: View {89    @Environment(\.mccTheme) private var theme90    private let text: String?91    public init(_ text: String?) { self.text = text }92    public var body: some View {93        if let text {94            Text(text).monospacedDigit().foregroundStyle(theme.textPrimary)95        } else {96            Text("—").foregroundStyle(theme.textTertiary).accessibilityLabel(tr("metric.not_measured"))97        }98    }99}100101/// Explicit state of an inference metric family according to `metrics_support`.102public struct MetricSupportRow: View {103    @Environment(\.mccTheme) private var theme104    private let title: String105    private let support: API.MetricSupport106    public init(_ title: String, support: API.MetricSupport) { self.title = title; self.support = support }107    public var body: some View {108        HStack(alignment: .firstTextBaseline, spacing: Tokens.Space.s3) {109            Text(title).font(.callout).foregroundStyle(theme.textSecondary)110            Spacer(minLength: Tokens.Space.s3)111            VStack(alignment: .trailing, spacing: 2) {112                StatusLabel(support.availability == .available ? tr("metric.instrumented") : tr("metric.not_instrumented", support.availability.label),113                            tone: support.availability.tone)114                if let r = support.reason, !r.isEmpty {115                    Text(r).font(.caption).foregroundStyle(theme.textTertiary).multilineTextAlignment(.trailing)116                }117            }118        }119        .accessibilityElement(children: .combine)120    }121}122
Files main
Menu

Theme