import SwiftUI struct PowerResourceCard: View { let snapshot: MetricsSnapshot let samples: [TrendSample] let chartInterval: TimeInterval let chartBoundaries: Set let chartIntervals: [TimeInterval] @Environment(\.locale) private var locale private func batteryPowerLabel(_ battery: BatteryMetrics) -> LocalizedStringKey { if battery.charging { return "battery.chargePower" } if !battery.externalPower { return "battery.dischargePower" } return "battery.power" } @ViewBuilder var body: some View { if snapshot.batteryState == .absent { ResourceCard(title: "power.system", symbol: "bolt.fill", color: Color("MetricBattery"), value: MetricFormat.watts(snapshot.systemPower), chart: { PanelTrendChart(dates: samples.map(\.date), first: samples.map(\.systemPower), color: Color("MetricBattery"), fixedScale: nil, firstValueFormat: .watts, firstLabel: "power.system", expectedInterval: chartInterval, boundaryDates: chartBoundaries, intervals: chartIntervals) }) { Text(snapshot.systemPower == nil ? "power.sensorUnavailable" : "panel.noBattery") .font(.system(size: 10)).foregroundStyle(.secondary) } } else { batteryChargeCard } } private var batteryChargeCard: some View { ResourceCard( title: "metric.battery", symbol: "battery.100percent", color: Color("MetricBattery"), value: MetricFormat.percent(snapshot.battery?.charge), status: snapshot.battery.map { AppLocalization.string($0.externalPower ? "battery.acPower" : "battery.batterySource", locale: locale) }, chartPlacement: .background, chart: { PanelTrendChart( dates: samples.map(\.date), first: samples.map(\.battery), second: samples.map(\.batteryPower), color: Color("MetricBattery"), secondColor: Color("SeriesUser"), fixedScale: 1, secondSeriesUsesAutonomousScale: true, secondValueFormat: .watts, firstLabel: "metric.battery", secondLabel: snapshot.battery.map(batteryPowerLabel) ?? "battery.power", expectedInterval: chartInterval, boundaryDates: chartBoundaries, intervals: chartIntervals ) } ) { InlineMetric("battery.powerShort", MetricFormat.watts(snapshot.battery?.power)) .frame(height: 16) if let battery = snapshot.battery { batteryDetails(battery) } else { Text(snapshot.batteryState == .unknown ? "metric.waiting" : "panel.batteryFailed").foregroundStyle(.secondary) .font(.system(size: 10)).frame(maxWidth: .infinity).frame(height: 32) } } } private func batteryDetails(_ battery: BatteryMetrics) -> some View { VStack(alignment: .leading, spacing: 4) { HStack(spacing: 8) { InlineMetric("battery.healthShort", MetricFormat.percent(battery.health)) InlineMetric("battery.cyclesShort", battery.cycles.map(String.init) ?? "--") } HStack(spacing: 4) { Text("battery.timeShort").foregroundStyle(.secondary) if battery.externalPower { Text("battery.notApplicableShort").help(Text("battery.timeNotApplicable")) } else if let minutes = battery.minutesRemaining { MetricText(value: "\(minutes) min") } else { Text("battery.timeUnknown") } } } .font(.system(size: 10)).lineLimit(1).minimumScaleFactor(0.9) } }