Jelajahi Sumber

Meal presets. Bug fix and more clean up (still a bit of a mess...)

(cherry picked from commit 27071ef834e986b5e78a70cdb265a1ad9e028200)
Jon Mårtensson 3 tahun lalu
induk
melakukan
b345996ca3

+ 14 - 7
FreeAPS/Sources/Modules/AddCarbs/AddCarbsStateModel.swift

@@ -129,13 +129,6 @@ extension AddCarbs {
             if a != nil, summation[a ?? 0] != "" {
                 summation.remove(at: a!)
             }
-            if (selection?.carbs ?? 0) as Decimal == carbs, (selection?.fat ?? 0) as Decimal == fat,
-               (selection?.protein ?? 0) as Decimal == protein
-            {
-                carbs = 0
-                fat = 0
-                protein = 0
-            }
         }
 
         func addPresetToNewMeal() {
@@ -149,8 +142,21 @@ extension AddCarbs {
             summation.append(dish)
         }
 
+        func addToSummation() {
+            summation.append(selection?.dish ?? "")
+        }
+
         func waitersNotepad() -> String {
             var filteredArray = summation.filter { !$0.isEmpty }
+
+            if carbs == 0, protein == 0, fat == 0 {
+                filteredArray = []
+            }
+
+            guard filteredArray != [] else {
+                return ""
+            }
+
             var carbs_: Decimal = 0.0
             var fat_: Decimal = 0.0
             var protein_: Decimal = 0.0
@@ -214,6 +220,7 @@ extension AddCarbs {
                     } else { waitersNotepadString += " " + each }
                 }
             }
+
             return waitersNotepadString
         }
     }

+ 15 - 7
FreeAPS/Sources/Modules/AddCarbs/View/AddCarbsRootView.swift

@@ -138,7 +138,7 @@ extension AddCarbs {
                         state.carbs += ((state.selection?.carbs ?? 0) as NSDecimalNumber) as Decimal
                         state.fat += ((state.selection?.fat ?? 0) as NSDecimalNumber) as Decimal
                         state.protein += ((state.selection?.protein ?? 0) as NSDecimalNumber) as Decimal
-                        state.summation.append(state.selection?.dish ?? "")
+                        state.addToSummation()
                     }
                 }
                 HStack {
@@ -155,6 +155,12 @@ extension AddCarbs {
                             Button("No", role: .cancel) {}
                             Button("Yes", role: .destructive) {
                                 state.deletePreset()
+
+                                state.carbs += ((state.selection?.carbs ?? 0) as NSDecimalNumber) as Decimal
+                                state.fat += ((state.selection?.fat ?? 0) as NSDecimalNumber) as Decimal
+                                state.protein += ((state.selection?.protein ?? 0) as NSDecimalNumber) as Decimal
+
+                                state.addPresetToNewMeal()
                             }
                         }
                     )
@@ -178,14 +184,16 @@ extension AddCarbs {
                         } else { state.protein = 0 }
 
                         state.removePresetFromNewMeal()
+                        if state.carbs == 0, state.fat == 0, state.protein == 0 { state.summation = [] }
                     }
                     label: { Text("[ -1 ]") }
-                        .disabled(state.selection == nil || (
-                            (((state.selection?.carbs ?? 0) as NSDecimalNumber) as Decimal) == state
-                                .carbs && (((state.selection?.fat ?? 0) as NSDecimalNumber) as Decimal) == state
-                                .fat && (((state.selection?.protein ?? 0) as NSDecimalNumber) as Decimal) == state
-                                .protein
-                        ))
+                        .disabled(
+                            state
+                                .selection == nil ||
+                                (
+                                    !state.summation.contains(state.selection?.dish ?? "") && (state.selection?.dish ?? "") != ""
+                                )
+                        )
                         .buttonStyle(BorderlessButtonStyle())
                         .frame(maxWidth: .infinity, alignment: .trailing)
                         .accentColor(.minus)