/ BalanceKit / WaterIntakeView.swift
WaterIntakeView.swift
  1  import SwiftUI
  2  
  3  struct WaterIntakeView: View {
  4      @ObservedObject var healthManager: HealthManager
  5      @State private var waterAmount: String = ""
  6      @State private var showingAddView = false
  7      @Environment(\.colorScheme) var colorScheme
  8      
  9      var body: some View {
 10          VStack(alignment: .leading, spacing: 8) {
 11              HStack {
 12                  Image(systemName: "drop.fill")
 13                      .foregroundColor(.blue)
 14                      .font(.title3)
 15                  Text("Wasserkonsum")
 16                      .foregroundColor(.primary)
 17                      .font(.headline)
 18                  Spacer()
 19                  
 20                  Button(action: {
 21                      // Debug-Info im Konsolenprotokoll
 22                      print("DEBUG: Wasser-Tracking - Button gedrückt, aktueller Wert: \(healthManager.waterConsumed) ml")
 23                      showingAddView = true
 24                  }) {
 25                      Image(systemName: "plus.circle.fill")
 26                          .foregroundColor(.blue)
 27                          .font(.title2)
 28                  }
 29              }
 30              
 31              HStack(alignment: .firstTextBaseline) {
 32                  Text("\(Int(healthManager.waterConsumed))")
 33                      .font(.title3)
 34                      .fontWeight(.bold)
 35                      .foregroundColor(.primary)
 36                  Text("ml")
 37                      .font(.caption)
 38                      .foregroundColor(.secondary)
 39                      .padding(.leading, 2)
 40              }
 41              
 42              // Fortschrittsleiste für visuelle Darstellung
 43              VStack(alignment: .leading, spacing: 2) {
 44                  ProgressView(value: min(healthManager.waterConsumed, 2500), total: 2500)
 45                      .progressViewStyle(LinearProgressViewStyle(tint: .blue))
 46                  
 47                  HStack {
 48                      Spacer()
 49                      Text("Ziel: 2500 ml")
 50                          .font(.caption2)
 51                          .foregroundColor(.secondary)
 52                  }
 53              }
 54              .padding(.vertical, 4)
 55              
 56              // Debug-Button zum manuellen Aktualisieren
 57              Button(action: {
 58                  print("DEBUG: Wasser-Tracking - Manuelles Aktualisieren")
 59                  healthManager.fetchWaterIntake()
 60              }) {
 61                  Label("Aktualisieren", systemImage: "arrow.triangle.2.circlepath")
 62                      .font(.caption)
 63                      .foregroundColor(.secondary)
 64              }
 65              .buttonStyle(.borderless)
 66              .padding(.top, 4)
 67              
 68              Spacer(minLength: 0) // Flexible Spacer für gleiche Höhe
 69          }
 70          .padding()
 71          .background(Color.blue.opacity(0.15))
 72          .cornerRadius(10)
 73          .frame(maxWidth: .infinity, minHeight: 100)
 74          .overlay(
 75              RoundedRectangle(cornerRadius: 10)
 76                  .stroke(Color.blue.opacity(0.4), lineWidth: 2)
 77          )
 78                      .padding(.vertical)
 79              .sheet(isPresented: $showingAddView) {
 80                  AddWaterView(healthManager: healthManager, isPresented: $showingAddView)
 81              }
 82              .animation(.spring(), value: healthManager.waterConsumed)
 83      }
 84  }
 85  
 86  struct AddWaterView: View {
 87      @ObservedObject var healthManager: HealthManager
 88      @Binding var isPresented: Bool
 89      @State private var waterAmount: String = ""
 90      
 91      // Voreingestellte Wassermengen
 92      let presetAmounts = [200, 300, 500, 750]
 93      
 94      var body: some View {
 95          NavigationView {
 96              VStack(spacing: 20) {
 97                  Text("Wasser hinzufügen")
 98                      .font(.title2)
 99                      .fontWeight(.bold)
100                      .padding(.top)
101                  
102                  // Voreingestellte Mengen
103                  VStack(spacing: 12) {
104                      Text("Schnellauswahl:")
105                          .font(.headline)
106                          .frame(maxWidth: .infinity, alignment: .leading)
107                      
108                      LazyVGrid(columns: [GridItem(.flexible()), GridItem(.flexible())], spacing: 12) {
109                          ForEach(presetAmounts, id: \.self) { amount in
110                              Button(action: {
111                                  addWater(amount: Double(amount))
112                              }) {
113                                  Text("\(amount) ml")
114                                      .frame(maxWidth: .infinity)
115                                      .padding()
116                                      .background(Color.blue.opacity(0.2))
117                                      .foregroundColor(.primary)
118                                      .cornerRadius(8)
119                              }
120                          }
121                      }
122                  }
123                  .padding()
124                  
125                  Divider()
126                  
127                  // Benutzerdefinierte Menge
128                  VStack(spacing: 12) {
129                      Text("Benutzerdefinierte Menge:")
130                          .font(.headline)
131                          .frame(maxWidth: .infinity, alignment: .leading)
132                      
133                      HStack {
134                          TextField("Menge", text: $waterAmount)
135                              .keyboardType(.numberPad)
136                              .padding()
137                              .background(Color(.systemGray6))
138                              .cornerRadius(8)
139                          
140                          Text("ml")
141                              .padding(.trailing)
142                      }
143                      
144                      Button(action: {
145                          if let amount = Double(waterAmount), amount > 0 {
146                              addWater(amount: amount)
147                          }
148                      }) {
149                          Text("Hinzufügen")
150                              .frame(maxWidth: .infinity)
151                              .padding()
152                              .background(Color.blue)
153                              .foregroundColor(.white)
154                              .cornerRadius(8)
155                      }
156                      .disabled(Double(waterAmount) == nil || Double(waterAmount) == 0)
157                  }
158                  .padding()
159                  
160                  Spacer()
161              }
162              .padding()
163              .navigationBarItems(trailing: Button("Schließen") {
164                  isPresented = false
165              })
166          }
167      }
168      
169      func addWater(amount: Double) {
170          healthManager.addWater(milliliters: amount)
171          isPresented = false
172      }
173  }