소스 검색

fixes, to do: refactoring, use combined sheet also in old calc, fix init of currentTab that causes an initial jump to the home view when pressing the plus in tabbar before going on to the bolus view"

polscm32 2 년 전
부모
커밋
a35687fff6
2개의 변경된 파일142개의 추가작업 그리고 15개의 파일을 삭제
  1. 140 13
      FreeAPS/Sources/Modules/Bolus/View/AlternativeBolusCalcRootView.swift
  2. 2 2
      FreeAPS/Sources/Modules/Home/View/HomeRootView.swift

+ 140 - 13
FreeAPS/Sources/Modules/Bolus/View/AlternativeBolusCalcRootView.swift

@@ -12,6 +12,7 @@ extension Bolus {
         let override: Bool
         let override: Bool
         @StateObject var state: StateModel
         @StateObject var state: StateModel
         @State private var showInfo = false
         @State private var showInfo = false
+        @State private var showAlert = false
         @State private var exceededMaxBolus = false
         @State private var exceededMaxBolus = false
         @State private var keepForNextWiew: Bool = false
         @State private var keepForNextWiew: Bool = false
         @State private var autofocus: Bool = true
         @State private var autofocus: Bool = true
@@ -45,6 +46,11 @@ extension Bolus {
             sortDescriptors: [NSSortDescriptor(key: "createdAt", ascending: false)]
             sortDescriptors: [NSSortDescriptor(key: "createdAt", ascending: false)]
         ) var meal: FetchedResults<Meals>
         ) var meal: FetchedResults<Meals>
 
 
+        @FetchRequest(
+            entity: Presets.entity(),
+            sortDescriptors: [NSSortDescriptor(key: "dish", ascending: true)]
+        ) var carbPresets: FetchedResults<Presets>
+
         private var formatter: NumberFormatter {
         private var formatter: NumberFormatter {
             let formatter = NumberFormatter()
             let formatter = NumberFormatter()
             formatter.numberStyle = .decimal
             formatter.numberStyle = .decimal
@@ -123,6 +129,129 @@ extension Bolus {
             }
             }
         }
         }
 
 
+        private var minusButton: some View {
+            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.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: { Image(systemName: "minus.circle.fill")
+                .font(.system(size: 20))
+            }
+            .disabled(
+                state
+                    .selection == nil ||
+                    (
+                        !state.summation
+                            .contains(state.selection?.dish ?? "") && (state.selection?.dish ?? "") != ""
+                    )
+            )
+            .buttonStyle(.borderless)
+            .tint(.blue)
+        }
+
+        private var plusButton: some View {
+            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()
+            }
+            label: { Image(systemName: "plus.circle.fill")
+                .font(.system(size: 20))
+            }
+            .disabled(state.selection == nil)
+            .buttonStyle(.borderless)
+            .tint(.blue)
+        }
+
+        private var mealPresets: some View {
+            Section {
+                HStack {
+                    if state.selection != nil {
+                        minusButton
+                    }
+                    Picker("Preset", selection: $state.selection) {
+                        Text("Saved Food").tag(nil as Presets?)
+                        ForEach(carbPresets, id: \.self) { (preset: Presets) in
+                            Text(preset.dish ?? "").tag(preset as Presets?)
+                        }
+                    }
+                    .labelsHidden()
+                    .frame(maxWidth: .infinity, alignment: .center)
+                    ._onBindingChange($state.selection) { _ in
+                        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.addToSummation()
+                    }
+                    if state.selection != nil {
+                        plusButton
+                    }
+                }
+
+                HStack {
+                    Button("Delete Preset") {
+                        showAlert.toggle()
+                    }
+                    .disabled(state.selection == nil)
+                    .tint(.orange)
+                    .buttonStyle(.borderless)
+                    .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
+
+                                state.addPresetToNewMeal()
+                            }
+                        }
+                    )
+
+                    Spacer()
+
+                    Button {
+                        isPromptPresented = true
+                    }
+                    label: { Text("Save as Preset") }
+                        .buttonStyle(.borderless)
+                        .disabled(
+                            empty ||
+                                (
+                                    (((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
+                                )
+                        )
+                }
+            }
+        }
+
         @ViewBuilder private func proteinAndFat() -> some View {
         @ViewBuilder private func proteinAndFat() -> some View {
             HStack {
             HStack {
                 Text("Fat").foregroundColor(.orange)
                 Text("Fat").foregroundColor(.orange)
@@ -221,9 +350,9 @@ extension Bolus {
                     //                        }
                     //                        }
                     //                    }
                     //                    }
                     //                    .focused($isFocused)
                     //                    .focused($isFocused)
-                    //                    .popover(isPresented: $isPromptPresented) {
-                    //                        presetPopover
-                    //                    }
+                    .popover(isPresented: $isPromptPresented) {
+                        presetPopover
+                    }
 
 
                     HStack {
                     HStack {
                         Spacer()
                         Spacer()
@@ -246,20 +375,17 @@ extension Bolus {
                             } else {
                             } else {
                                 Text("Edit meal")
                                 Text("Edit meal")
                             }
                             }
-                        }.buttonStyle(.bordered)
-                            .disabled(empty)
+                        }.disabled(empty)
 
 
                         Spacer()
                         Spacer()
                     }
                     }
                 } header: { Text("Carbs") }.listRowBackground(Color.chart)
                 } header: { Text("Carbs") }.listRowBackground(Color.chart)
 
 
-                // MARK: ADDING END
+                Section {
+                    mealPresets
+                }.listRowBackground(Color.chart)
 
 
-                if fetch {
-                    Section {
-                        mealEntries
-                    } header: { Text("Meal Summary") }.listRowBackground(Color.chart)
-                }
+                // MARK: ADDING END
 
 
                 Section {
                 Section {
                     HStack {
                     HStack {
@@ -382,6 +508,7 @@ extension Bolus {
                     ToolbarItem(placement: .topBarLeading) {
                     ToolbarItem(placement: .topBarLeading) {
                         Button {
                         Button {
                             state.hideModal()
                             state.hideModal()
+                            state.backToCarbsView(complexEntry: true, meal, override: false)
                         } label: {
                         } label: {
                             Text("Close")
                             Text("Close")
                         }
                         }
@@ -395,9 +522,9 @@ extension Bolus {
                     }
                     }
                 }
                 }
                 .onDisappear {
                 .onDisappear {
-                    if fetch, hasFatOrProtein, !keepForNextWiew, state.useCalc {
+                    if hasFatOrProtein, !keepForNextWiew, state.useCalc {
                         state.delete(deleteTwice: true, meal: meal)
                         state.delete(deleteTwice: true, meal: meal)
-                    } else if fetch, !keepForNextWiew, state.useCalc {
+                    } else if !keepForNextWiew, state.useCalc {
                         state.delete(deleteTwice: false, meal: meal)
                         state.delete(deleteTwice: false, meal: meal)
                     }
                     }
                 }
                 }

+ 2 - 2
FreeAPS/Sources/Modules/Home/View/HomeRootView.swift

@@ -981,9 +981,9 @@ extension Home {
                     action: {
                     action: {
                         state.showModal(for: .bolus(waitForSuggestion: false, fetch: false, editMode: false, override: false)) },
                         state.showModal(for: .bolus(waitForSuggestion: false, fetch: false, editMode: false, override: false)) },
                     label: {
                     label: {
-                        Image(systemName: "plus.circle.fill").font(.system(size: 40)).foregroundStyle(Color.gray.opacity(0.9))
+                        Image(systemName: "plus").font(.system(size: 40)).foregroundStyle(Color.gray.opacity(0.8))
                     }
                     }
-                ).padding(.bottom, 2)
+                ).padding(.bottom, 5)
             }
             }
         }
         }