/ BalanceKit / HealthActivityView.swift
HealthActivityView.swift
1 import SwiftUI 2 3 struct HealthActivityView: View { 4 @ObservedObject var healthManager: HealthManager 5 @Environment(\.colorScheme) var colorScheme 6 var date: Date = Date() // Standardmäßig heute 7 8 // Zustand für historische Daten 9 @State private var steps: Int = 0 10 @State private var activeCalories: Double = 0 11 12 // Formatiertes Datum 13 private var formattedDate: String { 14 let formatter = DateFormatter() 15 formatter.dateStyle = .medium 16 formatter.timeStyle = .none 17 formatter.locale = Locale(identifier: "de_DE") 18 return formatter.string(from: date) 19 } 20 21 var body: some View { 22 VStack(alignment: .leading, spacing: 12) { 23 Text(Calendar.current.isDateInToday(date) ? "Aktivität heute" : "Aktivität am \(formattedDate)") 24 .font(.headline) 25 26 HStack(spacing: 8) { 27 // Schritte 28 ActivityCardView( 29 icon: "figure.walk", 30 title: "Schritte", 31 value: Calendar.current.isDateInToday(date) ? "\(healthManager.steps)" : "\(steps)", 32 color: .green 33 ) 34 35 // Aktivitäts-Kalorien (ohne manuelle Workouts) 36 ActivityCardView( 37 icon: "flame.fill", 38 title: "Aktive Kalorien", 39 value: String(format: "%.1f", Calendar.current.isDateInToday(date) ? healthManager.activeCaloriesBurned : activeCalories), 40 unit: "kcal", 41 color: .orange 42 ) 43 } 44 .onAppear { 45 // Lade Daten für das ausgewählte Datum, wenn es nicht heute ist 46 if !Calendar.current.isDateInToday(date) { 47 loadDataForDate() 48 } 49 } 50 .onChange(of: date) { 51 // Lade neue Daten, wenn sich das Datum ändert 52 if Calendar.current.isDateInToday(date) { 53 steps = healthManager.steps 54 activeCalories = healthManager.activeCaloriesBurned 55 } else { 56 loadDataForDate() 57 } 58 } 59 60 // Refresh-Button 61 Button { 62 healthManager.fetchTodaysData() 63 } label: { 64 Label("Aktualisieren", systemImage: "arrow.clockwise") 65 .font(.caption) 66 .foregroundColor(.secondary) 67 } 68 .buttonStyle(.borderless) 69 } 70 } 71 72 private func loadDataForDate() { 73 // Lade Schritte für das ausgewählte Datum 74 healthManager.fetchStepsForDate(date) { fetchedSteps in 75 DispatchQueue.main.async { 76 self.steps = fetchedSteps 77 } 78 } 79 80 // Lade aktive Kalorien für das ausgewählte Datum 81 healthManager.fetchActiveCaloriesForDate(date) { fetchedCalories in 82 DispatchQueue.main.async { 83 self.activeCalories = fetchedCalories 84 } 85 } 86 } 87 } 88 89 struct ActivityCardView: View { 90 let icon: String 91 let title: String 92 let value: String 93 var unit: String = "" 94 let color: Color 95 96 var body: some View { 97 VStack(alignment: .leading, spacing: 8) { 98 HStack { 99 Image(systemName: icon) 100 .foregroundColor(color) 101 Text(title) 102 .foregroundColor(.primary) 103 .font(.subheadline) 104 Spacer() 105 } 106 107 Text(unit.isEmpty ? value : "\(value) \(unit)") 108 .font(.title3) 109 .fontWeight(.bold) 110 .foregroundColor(.primary) 111 .padding(.bottom, 4) // Kleine Polsterung am unteren Rand 112 113 Spacer(minLength: 0) // Flexible Spacer für gleiche Höhe 114 } 115 .padding() 116 .background(color.opacity(0.1)) 117 .cornerRadius(10) 118 .frame(maxWidth: .infinity, minHeight: 100) // Mindesthöhe für beide Karten 119 } 120 }