/ BalanceKit / GoalSettingsView.swift
GoalSettingsView.swift
  1  //
  2  //  GoalSettingsView.swift
  3  //  BalanceKit
  4  //
  5  //  Created by Alexander Kunau on 12.07.25.
  6  //
  7  
  8  import SwiftUI
  9  
 10  struct GoalSettingsView: View {
 11      @ObservedObject var dataManager: FoodDataManager
 12      
 13      // Temporäre Werte für die Bearbeitung
 14      @State private var calorieGoal: Int
 15      @State private var proteinGoal: Double
 16      @State private var carbsGoal: Double
 17      @State private var fatGoal: Double
 18      
 19      init(dataManager: FoodDataManager) {
 20          self.dataManager = dataManager
 21          // Initialisiere die State-Variablen mit den aktuellen Werten
 22          _calorieGoal = State(initialValue: dataManager.dailyCalorieGoal)
 23          _proteinGoal = State(initialValue: dataManager.dailyProteinGoal)
 24          _carbsGoal = State(initialValue: dataManager.dailyCarbsGoal)
 25          _fatGoal = State(initialValue: dataManager.dailyFatGoal)
 26      }
 27      
 28      var body: some View {
 29          List {
 30              Section(header: Text("Tägliche Zielwerte")) {
 31                  HStack {
 32                      Text("Kalorien")
 33                      Spacer()
 34                      TextField("Kalorien", value: $calorieGoal, formatter: NumberFormatter())
 35                          .keyboardType(.numberPad)
 36                          .multilineTextAlignment(.trailing)
 37                      Text("kcal")
 38                  }
 39                  
 40                  HStack {
 41                      Text("Protein")
 42                      Spacer()
 43                      TextField("Protein", value: $proteinGoal, formatter: NumberFormatter())
 44                          .keyboardType(.decimalPad)
 45                          .multilineTextAlignment(.trailing)
 46                      Text("g")
 47                  }
 48                  
 49                  HStack {
 50                      Text("Kohlenhydrate")
 51                      Spacer()
 52                      TextField("Kohlenhydrate", value: $carbsGoal, formatter: NumberFormatter())
 53                          .keyboardType(.decimalPad)
 54                          .multilineTextAlignment(.trailing)
 55                      Text("g")
 56                  }
 57                  
 58                  HStack {
 59                      Text("Fett")
 60                      Spacer()
 61                      TextField("Fett", value: $fatGoal, formatter: NumberFormatter())
 62                          .keyboardType(.decimalPad)
 63                          .multilineTextAlignment(.trailing)
 64                      Text("g")
 65                  }
 66              }
 67              
 68              Section {
 69                  Button(action: saveGoals) {
 70                      Text("Zielwerte speichern")
 71                          .frame(maxWidth: .infinity)
 72                          .foregroundColor(.white)
 73                          .padding()
 74                          .background(Color.blue)
 75                          .cornerRadius(10)
 76                  }
 77              }
 78              
 79              Section(header: Text("Empfehlungen"), footer: Text("Diese Werte sind allgemeine Empfehlungen. Bitte passe sie an deine individuellen Bedürfnisse an.")) {
 80                  Button(action: setLowCarbDiet) {
 81                      Text("Low-Carb Diät (1800 kcal)")
 82                  }
 83                  
 84                  Button(action: setBalancedDiet) {
 85                      Text("Ausgewogene Ernährung (2000 kcal)")
 86                  }
 87                  
 88                  Button(action: setHighProteinDiet) {
 89                      Text("Proteinreiche Ernährung (2200 kcal)")
 90                  }
 91              }
 92              
 93              Section(header: Text("Custom Werte"), footer: Text("Verwende die obigen Felder, um eigene Zielwerte festzulegen und speichere sie mit dem blauen Button.")) {
 94                  Text("Passe die Werte oben individuell an und tippe auf 'Zielwerte speichern'.")
 95                      .foregroundColor(.secondary)
 96                      .italic()
 97              }
 98          }
 99          .navigationTitle("Zielwerte")
100      }
101      
102      private func saveGoals() {
103          // Aktualisiere die Werte im DataManager
104          dataManager.dailyCalorieGoal = calorieGoal
105          dataManager.dailyProteinGoal = proteinGoal
106          dataManager.dailyCarbsGoal = carbsGoal
107          dataManager.dailyFatGoal = fatGoal
108      }
109      
110      private func setLowCarbDiet() {
111          calorieGoal = 1800
112          proteinGoal = 135.0
113          carbsGoal = 90.0
114          fatGoal = 100.0
115          saveGoals()
116      }
117      
118      private func setBalancedDiet() {
119          calorieGoal = 2000
120          proteinGoal = 100.0
121          carbsGoal = 250.0
122          fatGoal = 70.0
123          saveGoals()
124      }
125      
126      private func setHighProteinDiet() {
127          calorieGoal = 2200
128          proteinGoal = 180.0
129          carbsGoal = 165.0
130          fatGoal = 75.0
131          saveGoals()
132      }
133  }
134  
135  #Preview {
136      NavigationStack {
137          GoalSettingsView(dataManager: FoodDataManager())
138      }
139  }