Forráskód Böngészése

Add Edit Meals also for the default Bolus View

Jon Mårtensson 2 éve
szülő
commit
29d1e06d3b

+ 2 - 2
FreeAPS/Sources/Modules/Bolus/View/AlternativeBolusCalcRootView.swift

@@ -215,9 +215,9 @@ extension Bolus {
                 }
                 }
             }
             }
             .onDisappear {
             .onDisappear {
-                if fetch, hasFatOrProtein, !keepForNextWiew {
+                if fetch, hasFatOrProtein, !keepForNextWiew, state.useCalc {
                     state.delete(deleteTwice: true, id: meal.first?.id ?? "")
                     state.delete(deleteTwice: true, id: meal.first?.id ?? "")
-                } else if fetch, !keepForNextWiew {
+                } else if fetch, !keepForNextWiew, state.useCalc {
                     state.delete(deleteTwice: false, id: meal.first?.id ?? "")
                     state.delete(deleteTwice: false, id: meal.first?.id ?? "")
                 }
                 }
             }
             }

+ 82 - 2
FreeAPS/Sources/Modules/Bolus/View/DefaultBolusCalcRootView.swift

@@ -11,9 +11,15 @@ extension Bolus {
         @State private var isAddInsulinAlertPresented = false
         @State private var isAddInsulinAlertPresented = false
         @State private var presentInfo = false
         @State private var presentInfo = false
         @State private var displayError = false
         @State private var displayError = false
+        @State private var keepForNextWiew: Bool = false
 
 
         @Environment(\.colorScheme) var colorScheme
         @Environment(\.colorScheme) var colorScheme
 
 
+        @FetchRequest(
+            entity: Meals.entity(),
+            sortDescriptors: [NSSortDescriptor(key: "createdAt", ascending: false)]
+        ) var meal: FetchedResults<Meals>
+
         private var formatter: NumberFormatter {
         private var formatter: NumberFormatter {
             let formatter = NumberFormatter()
             let formatter = NumberFormatter()
             formatter.numberStyle = .decimal
             formatter.numberStyle = .decimal
@@ -29,6 +35,57 @@ extension Bolus {
 
 
         var body: some View {
         var body: some View {
             Form {
             Form {
+                if fetch {
+                    Section {
+                        VStack {
+                            if let carbs = meal.first?.carbs, carbs > 0 {
+                                HStack {
+                                    Text("Carbs")
+                                    Spacer()
+                                    Text(carbs.formatted())
+                                    Text("g")
+                                }.foregroundColor(.secondary)
+                            }
+                            if let fat = meal.first?.fat, fat > 0 {
+                                HStack {
+                                    Text("Fat")
+                                    Spacer()
+                                    Text(fat.formatted())
+                                    Text("g")
+                                }.foregroundColor(.secondary)
+                            }
+                            if let protein = meal.first?.protein, protein > 0 {
+                                HStack {
+                                    Text("Protein")
+                                    Spacer()
+                                    Text(protein.formatted())
+                                    Text("g")
+                                }.foregroundColor(.secondary)
+                            }
+                            if let note = meal.first?.note, note != "" {
+                                HStack {
+                                    Text("Note")
+                                    Spacer()
+                                    Text(note)
+                                }.foregroundColor(.secondary)
+                            }
+                        }
+                    } header: { Text("Meal Summary") }
+                }
+
+                Section {
+                    Button {
+                        let id_ = meal.first?.id ?? ""
+                        if fetch {
+                            keepForNextWiew = true
+                            state.backToCarbsView(complexEntry: fetch, id_)
+                        } else {
+                            state.showModal(for: .addCarbs(editMode: false))
+                        }
+                    }
+                    label: { Text(fetch ? "Edit Meal" : "Add Meal") }.frame(maxWidth: .infinity, alignment: .center)
+                } header: { Text(!fetch ? "Meal Summary" : "") }
+
                 Section {
                 Section {
                     if state.waitForSuggestion {
                     if state.waitForSuggestion {
                         HStack {
                         HStack {
@@ -78,7 +135,10 @@ extension Bolus {
                     }
                     }
                     header: { Text("Bolus") }
                     header: { Text("Bolus") }
                     Section {
                     Section {
-                        Button { state.add() }
+                        Button {
+                            keepForNextWiew = true
+                            state.add()
+                        }
                         label: { Text(!(state.amount > state.maxBolus) ? "Enact bolus" : "Max Bolus exceeded!") }
                         label: { Text(!(state.amount > state.maxBolus) ? "Enact bolus" : "Max Bolus exceeded!") }
                             .frame(maxWidth: .infinity, alignment: .center)
                             .frame(maxWidth: .infinity, alignment: .center)
                             .disabled(
                             .disabled(
@@ -88,7 +148,10 @@ extension Bolus {
 
 
                     if waitForSuggestion {
                     if waitForSuggestion {
                         Section {
                         Section {
-                            Button { state.showModal(for: nil) }
+                            Button {
+                                keepForNextWiew = true
+                                state.showModal(for: nil)
+                            }
                             label: { Text("Continue without bolus") }.frame(maxWidth: .infinity, alignment: .center)
                             label: { Text("Continue without bolus") }.frame(maxWidth: .infinity, alignment: .center)
                         }
                         }
                     }
                     }
@@ -113,6 +176,15 @@ extension Bolus {
                     state.waitForSuggestion = waitForSuggestion
                     state.waitForSuggestion = waitForSuggestion
                 }
                 }
             }
             }
+
+            .onDisappear {
+                if fetch, hasFatOrProtein, !keepForNextWiew, !state.useCalc {
+                    state.delete(deleteTwice: true, id: meal.first?.id ?? "")
+                } else if fetch, !keepForNextWiew, !state.useCalc {
+                    state.delete(deleteTwice: false, id: meal.first?.id ?? "")
+                }
+            }
+
             .navigationTitle("Enact Bolus")
             .navigationTitle("Enact Bolus")
             .navigationBarTitleDisplayMode(.inline)
             .navigationBarTitleDisplayMode(.inline)
             .navigationBarItems(leading: Button("Close", action: state.hideModal))
             .navigationBarItems(leading: Button("Close", action: state.hideModal))
@@ -121,6 +193,14 @@ extension Bolus {
             }
             }
         }
         }
 
 
+        var changed: Bool {
+            ((meal.first?.carbs ?? 0) > 0) || ((meal.first?.fat ?? 0) > 0) || ((meal.first?.protein ?? 0) > 0)
+        }
+
+        var hasFatOrProtein: Bool {
+            ((meal.first?.fat ?? 0) > 0) || ((meal.first?.protein ?? 0) > 0)
+        }
+
         var bolusInfo: some View {
         var bolusInfo: some View {
             VStack {
             VStack {
                 // Variables
                 // Variables