/ BalanceKit / WaterDashboardView.swift
WaterDashboardView.swift
  1  import SwiftUI
  2  
  3  // Wasserkachel mit Plus-Button für schnelles Hinzufügen
  4  struct WaterDashboardView: View {
  5      @ObservedObject var healthManager: HealthManager
  6      @State private var showingQuickAdd = false
  7      @State private var quickWaterAmount: String = ""
  8      @State private var waterOptions: [Int] = [200, 330, 500]
  9      
 10      var body: some View {
 11          Button(action: {
 12              showingQuickAdd = true
 13          }) {
 14              // Invertiertes Kachel-Layout mit blauem Hintergrund mit Plus-Button unten rechts
 15              ZStack {
 16                  // Hauptinhalt
 17                  VStack(alignment: .center, spacing: 4) {
 18                      Text("Wasser")
 19                          .font(.headline)
 20                          .fontWeight(.medium)
 21                          .lineLimit(1)
 22                          .minimumScaleFactor(0.8)
 23                          .foregroundColor(.white)
 24                          .padding(.top, 8)
 25                      
 26                      Spacer()
 27                      
 28                      // Zentrierte Anzeige des Wasserverbrauchs
 29                      VStack(alignment: .center, spacing: 0) {
 30                          Text(String(format: "%.0f", healthManager.waterConsumed))
 31                              .font(.largeTitle)
 32                              .fontWeight(.bold)
 33                              .foregroundColor(.white)
 34                          
 35                          Text("ml")
 36                              .font(.caption)
 37                              .foregroundColor(.white.opacity(0.8))
 38                      }
 39                      
 40                      Spacer()
 41                  }
 42                  .frame(maxWidth: .infinity)
 43                  
 44                  // Plus-Button unten rechts positioniert
 45                  VStack {
 46                      Spacer()
 47                      HStack {
 48                          Spacer()
 49                          Image(systemName: "plus.circle.fill")
 50                              .foregroundColor(.white)
 51                              .font(.title2)
 52                              .padding(0)
 53                      }
 54                  }
 55              }
 56              .frame(maxWidth: .infinity, minHeight: 140)
 57              .padding()
 58              .background(
 59                  LinearGradient(
 60                      gradient: Gradient(colors: [Color.blue, Color.blue.opacity(0.8)]),
 61                      startPoint: .topLeading,
 62                      endPoint: .bottomTrailing
 63                  )
 64              )
 65              .cornerRadius(16)
 66          }
 67          .buttonStyle(PlainButtonStyle()) // Damit die Karte nicht wie ein Button aussieht
 68          .sheet(isPresented: $showingQuickAdd) {
 69              NavigationView {
 70                  VStack(spacing: 20) {
 71                      Text("Wasser hinzufügen")
 72                          .font(.headline)
 73                          .padding(.top)
 74                      
 75                      // Schnelle Optionen
 76                      VStack(alignment: .leading) {
 77                          Text("Schnellauswahl:")
 78                              .font(.subheadline)
 79                              .foregroundColor(.secondary)
 80                              .padding(.horizontal)
 81                          
 82                          HStack(spacing: 10) {
 83                              ForEach(waterOptions, id: \.self) { amount in
 84                                  Button(action: {
 85                                      addWater(Double(amount))
 86                                      showingQuickAdd = false
 87                                  }) {
 88                                      Text("\(amount) ml")
 89                                          .padding(.vertical, 10)
 90                                          .padding(.horizontal, 15)
 91                                          .frame(maxWidth: .infinity)
 92                                          .background(Color.blue.opacity(0.2))
 93                                          .foregroundColor(.primary)
 94                                          .cornerRadius(10)
 95                                  }
 96                              }
 97                          }
 98                          .padding(.horizontal)
 99                      }
100                      
101                      Divider()
102                          .padding(.vertical)
103                      
104                      // Benutzerdefinierte Menge
105                      VStack(alignment: .leading) {
106                          Text("Benutzerdefinierte Menge:")
107                              .font(.subheadline)
108                              .foregroundColor(.secondary)
109                              .padding(.horizontal)
110                          
111                          HStack {
112                              TextField("Menge", text: $quickWaterAmount)
113                                  .keyboardType(.numberPad)
114                                  .textFieldStyle(RoundedBorderTextFieldStyle())
115                                  .frame(maxWidth: .infinity)
116                              
117                              Text("ml")
118                                  .foregroundColor(.secondary)
119                                  .padding(.trailing)
120                          }
121                          .padding(.horizontal)
122                          
123                          Button(action: {
124                              if let amount = Double(quickWaterAmount), amount > 0 {
125                                  addWater(amount)
126                                  quickWaterAmount = ""
127                                  showingQuickAdd = false
128                              }
129                          }) {
130                              Text("Hinzufügen")
131                                  .padding(.vertical, 12)
132                                  .frame(maxWidth: .infinity)
133                                  .background(Color.blue)
134                                  .foregroundColor(.white)
135                                  .cornerRadius(10)
136                          }
137                          .disabled(Double(quickWaterAmount) == nil || Double(quickWaterAmount)! <= 0)
138                          .padding()
139                      }
140                      
141                      Spacer()
142                  }
143                  .padding()
144                  .navigationBarItems(trailing: Button("Schließen") {
145                      showingQuickAdd = false
146                  })
147              }
148              .presentationDetents([.medium])
149          }
150      }
151      
152      private func addWater(_ amount: Double) {
153          healthManager.addWater(milliliters: amount)
154      }
155  }