Skip to content

Commit

Permalink
Meal presets. Bug fix and more clean up (still a bit of a mess...)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon-b-m committed Apr 8, 2023
1 parent a6e2eae commit 27071ef
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
21 changes: 14 additions & 7 deletions FreeAPS/Sources/Modules/AddCarbs/AddCarbsStateModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
Expand Down Expand Up @@ -214,6 +220,7 @@ extension AddCarbs {
} else { waitersNotepadString += " " + each }
}
}

return waitersNotepadString
}
}
Expand Down
22 changes: 15 additions & 7 deletions FreeAPS/Sources/Modules/AddCarbs/View/AddCarbsRootView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()
}
}
)
Expand All @@ -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)
Expand Down

0 comments on commit 27071ef

Please sign in to comment.