|
|
@@ -63,71 +63,24 @@ extension AddCarbs {
|
|
|
.controlSize(.mini)
|
|
|
}
|
|
|
}.focused($isFocused)
|
|
|
- HStack {
|
|
|
- Button {
|
|
|
- state.useFPUconversion.toggle()
|
|
|
+ .popover(isPresented: $isPromptPresented) {
|
|
|
+ presetPopover
|
|
|
}
|
|
|
- label: {
|
|
|
- Text(
|
|
|
- state
|
|
|
- .useFPUconversion ? NSLocalizedString("Hide Fat & Protein", comment: "") :
|
|
|
- NSLocalizedString("Fat & Protein", comment: "")
|
|
|
- ) }
|
|
|
- .controlSize(.mini)
|
|
|
- .buttonStyle(BorderlessButtonStyle())
|
|
|
- Button {
|
|
|
- isPromptPresented = true
|
|
|
- }
|
|
|
- label: { Text("Save as Preset") }
|
|
|
- .frame(maxWidth: .infinity, alignment: .trailing)
|
|
|
- .controlSize(.mini)
|
|
|
- .buttonStyle(BorderlessButtonStyle())
|
|
|
- .foregroundColor(
|
|
|
- (state.carbs <= 0 && state.fat <= 0 && state.protein <= 0) ||
|
|
|
- (
|
|
|
- (((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
|
|
|
- ) ? .secondary : .orange
|
|
|
- )
|
|
|
- .disabled(
|
|
|
- (state.carbs <= 0 && state.fat <= 0 && state.protein <= 0) ||
|
|
|
- (
|
|
|
- (((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
|
|
|
- )
|
|
|
- )
|
|
|
- }
|
|
|
- .popover(isPresented: $isPromptPresented) {
|
|
|
- presetPopover
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if state.useFPUconversion {
|
|
|
- Section {
|
|
|
- mealPresets
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
Section {
|
|
|
- DatePicker("Date", selection: $state.date)
|
|
|
+ mealPresets
|
|
|
}
|
|
|
|
|
|
Section {
|
|
|
Button { state.add() }
|
|
|
- label: { Text(state.carbs > 0 ? "Continue" : "Save") }
|
|
|
+ label: { Text(state.skipBolus ? "Save" : "Continue") }
|
|
|
.disabled(state.carbs <= 0 && state.fat <= 0 && state.protein <= 0)
|
|
|
.frame(maxWidth: .infinity, alignment: .center)
|
|
|
} footer: { Text(state.waitersNotepad().description) }
|
|
|
|
|
|
- if !state.useFPUconversion {
|
|
|
- Section {
|
|
|
- mealPresets
|
|
|
- }
|
|
|
+ Section {
|
|
|
+ DatePicker("Date", selection: $state.date)
|
|
|
}
|
|
|
}
|
|
|
.onAppear {
|
|
|
@@ -135,7 +88,7 @@ extension AddCarbs {
|
|
|
state.loadEntries(editMode)
|
|
|
}
|
|
|
}
|
|
|
- .navigationTitle("Add Meals")
|
|
|
+ .navigationTitle("Add Meal")
|
|
|
.navigationBarTitleDisplayMode(.inline)
|
|
|
.navigationBarItems(leading: Button("Close", action: state.hideModal))
|
|
|
}
|
|
|
@@ -168,16 +121,36 @@ extension AddCarbs {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ var notEmpty: Bool {
|
|
|
+ state.carbs > 0 || state.protein > 0 || state.fat > 0
|
|
|
+ }
|
|
|
+
|
|
|
var mealPresets: some View {
|
|
|
Section {
|
|
|
- VStack {
|
|
|
- Picker("Meal Presets", selection: $state.selection) {
|
|
|
- Text("Empty").tag(nil as Presets?)
|
|
|
+ HStack {
|
|
|
+ Button {
|
|
|
+ isPromptPresented = true
|
|
|
+ }
|
|
|
+ label: { Text("Save as Preset") }
|
|
|
+ .buttonStyle(BorderlessButtonStyle())
|
|
|
+ .disabled(
|
|
|
+ !notEmpty ||
|
|
|
+ (
|
|
|
+ (((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
|
|
|
+ )
|
|
|
+ )
|
|
|
+
|
|
|
+ Picker("Select a Preset", selection: $state.selection) {
|
|
|
+ Text("Presets").tag(nil as Presets?)
|
|
|
ForEach(carbPresets, id: \.self) { (preset: Presets) in
|
|
|
Text(preset.dish ?? "").tag(preset as Presets?)
|
|
|
}
|
|
|
}
|
|
|
- .pickerStyle(.automatic)
|
|
|
+ .labelsHidden()
|
|
|
+ .frame(maxWidth: .infinity, alignment: .trailing)
|
|
|
._onBindingChange($state.selection) { _ in
|
|
|
state.carbs += ((state.selection?.carbs ?? 0) as NSDecimalNumber) as Decimal
|
|
|
state.fat += ((state.selection?.fat ?? 0) as NSDecimalNumber) as Decimal
|
|
|
@@ -185,80 +158,84 @@ extension AddCarbs {
|
|
|
state.addToSummation()
|
|
|
}
|
|
|
}
|
|
|
- HStack {
|
|
|
- Button("Delete Preset") {
|
|
|
- showAlert.toggle()
|
|
|
- }
|
|
|
- .disabled(state.selection == nil)
|
|
|
- .accentColor(.orange)
|
|
|
- .buttonStyle(BorderlessButtonStyle())
|
|
|
- .alert(
|
|
|
- "Delete preset '\(state.selection?.dish ?? "")'?",
|
|
|
- isPresented: $showAlert,
|
|
|
- actions: {
|
|
|
- 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
|
|
|
+ if state.selection != nil {
|
|
|
+ HStack {
|
|
|
+ Button("Delete Preset") {
|
|
|
+ showAlert.toggle()
|
|
|
+ }
|
|
|
+ .disabled(state.selection == nil)
|
|
|
+ .tint(.orange)
|
|
|
+ .buttonStyle(BorderlessButtonStyle())
|
|
|
+ .alert(
|
|
|
+ "Delete preset '\(state.selection?.dish ?? "")'?",
|
|
|
+ isPresented: $showAlert,
|
|
|
+ actions: {
|
|
|
+ Button("No", role: .cancel) {}
|
|
|
+ Button("Yes", role: .destructive) {
|
|
|
+ state.deletePreset()
|
|
|
|
|
|
- state.addPresetToNewMeal()
|
|
|
+ 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()
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
- )
|
|
|
- Button {
|
|
|
- if state.carbs != 0,
|
|
|
- (state.carbs - (((state.selection?.carbs ?? 0) as NSDecimalNumber) as Decimal) as Decimal) >= 0
|
|
|
- {
|
|
|
- state.carbs -= (((state.selection?.carbs ?? 0) as NSDecimalNumber) as Decimal)
|
|
|
- } else { state.carbs = 0 }
|
|
|
+ )
|
|
|
+ Button {
|
|
|
+ if state.carbs != 0,
|
|
|
+ (state.carbs - (((state.selection?.carbs ?? 0) as NSDecimalNumber) as Decimal) as Decimal) >= 0
|
|
|
+ {
|
|
|
+ state.carbs -= (((state.selection?.carbs ?? 0) as NSDecimalNumber) as Decimal)
|
|
|
+ } else { state.carbs = 0 }
|
|
|
|
|
|
- if state.fat != 0,
|
|
|
- (state.fat - (((state.selection?.fat ?? 0) as NSDecimalNumber) as Decimal) as Decimal) >= 0
|
|
|
- {
|
|
|
- state.fat -= (((state.selection?.fat ?? 0) as NSDecimalNumber) as Decimal)
|
|
|
- } else { state.fat = 0 }
|
|
|
+ if state.fat != 0,
|
|
|
+ (state.fat - (((state.selection?.fat ?? 0) as NSDecimalNumber) as Decimal) as Decimal) >= 0
|
|
|
+ {
|
|
|
+ state.fat -= (((state.selection?.fat ?? 0) as NSDecimalNumber) as Decimal)
|
|
|
+ } else { state.fat = 0 }
|
|
|
|
|
|
- if state.protein != 0,
|
|
|
- (state.protein - (((state.selection?.protein ?? 0) as NSDecimalNumber) as Decimal) as Decimal) >= 0
|
|
|
- {
|
|
|
- state.protein -= (((state.selection?.protein ?? 0) as NSDecimalNumber) as Decimal)
|
|
|
- } else { state.protein = 0 }
|
|
|
+ if state.protein != 0,
|
|
|
+ (state.protein - (((state.selection?.protein ?? 0) as NSDecimalNumber) as Decimal) as Decimal) >= 0
|
|
|
+ {
|
|
|
+ state.protein -= (((state.selection?.protein ?? 0) as NSDecimalNumber) as Decimal)
|
|
|
+ } 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.summation.contains(state.selection?.dish ?? "") && (state.selection?.dish ?? "") != ""
|
|
|
- )
|
|
|
- )
|
|
|
- .buttonStyle(BorderlessButtonStyle())
|
|
|
- .frame(maxWidth: .infinity, alignment: .trailing)
|
|
|
- .accentColor(.minus)
|
|
|
- Button {
|
|
|
- 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.removePresetFromNewMeal()
|
|
|
+ if state.carbs == 0, state.fat == 0, state.protein == 0 { state.summation = [] }
|
|
|
+ }
|
|
|
+ label: { Text("[ -1 ]") }
|
|
|
+ .disabled(
|
|
|
+ state
|
|
|
+ .selection == nil ||
|
|
|
+ (
|
|
|
+ !state.summation
|
|
|
+ .contains(state.selection?.dish ?? "") && (state.selection?.dish ?? "") != ""
|
|
|
+ )
|
|
|
+ )
|
|
|
+ .buttonStyle(BorderlessButtonStyle())
|
|
|
+ .frame(maxWidth: .infinity, alignment: .trailing)
|
|
|
+ .tint(.minus)
|
|
|
+ Button {
|
|
|
+ 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()
|
|
|
+ state.addPresetToNewMeal()
|
|
|
+ }
|
|
|
+ label: { Text("[ +1 ]") }
|
|
|
+ .disabled(state.selection == nil)
|
|
|
+ .buttonStyle(BorderlessButtonStyle())
|
|
|
+ .tint(.blue)
|
|
|
}
|
|
|
- label: { Text("[ +1 ]") }
|
|
|
- .disabled(state.selection == nil)
|
|
|
- .buttonStyle(BorderlessButtonStyle())
|
|
|
- .accentColor(.blue)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@ViewBuilder private func proteinAndFat() -> some View {
|
|
|
HStack {
|
|
|
- Text("Fat").foregroundColor(.orange) // .fontWeight(.thin)
|
|
|
+ Text("Fat").foregroundColor(.orange)
|
|
|
Spacer()
|
|
|
DecimalTextField(
|
|
|
"0",
|
|
|
@@ -270,7 +247,7 @@ extension AddCarbs {
|
|
|
Text("grams").foregroundColor(.secondary)
|
|
|
}
|
|
|
HStack {
|
|
|
- Text("Protein").foregroundColor(.red) // .fontWeight(.thin)
|
|
|
+ Text("Protein").foregroundColor(.red)
|
|
|
Spacer()
|
|
|
DecimalTextField(
|
|
|
"0",
|