diff --git a/FreeAPS/Sources/Modules/AddCarbs/AddCarbsStateModel.swift b/FreeAPS/Sources/Modules/AddCarbs/AddCarbsStateModel.swift index 443a9c5b5b..91ab065329 100644 --- a/FreeAPS/Sources/Modules/AddCarbs/AddCarbsStateModel.swift +++ b/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 } } diff --git a/FreeAPS/Sources/Modules/AddCarbs/View/AddCarbsRootView.swift b/FreeAPS/Sources/Modules/AddCarbs/View/AddCarbsRootView.swift index 9a4fa23002..e4d62149ce 100644 --- a/FreeAPS/Sources/Modules/AddCarbs/View/AddCarbsRootView.swift +++ b/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)