Просмотр исходного кода

Waiters Notepad

(cherry picked from commit 7f405e3aff224baa8d73639f1fcf4ae69190cf05)
Jon Mårtensson 3 лет назад
Родитель
Сommit
5b81e394d5

+ 1 - 1
FreeAPS/Resources/json/defaults/freeaps/freeaps_settings.json

@@ -22,7 +22,7 @@
     "useAppleHealth": false,
     "animatedBackground": false,
     "displayStatistics": false,
-    "useFPUconversion": false
+    "useFPUconversion": true
     "individualAdjustmentFactor": 0.5,
     "timeCap": 8,
     "minuteInterval": 30,

+ 1 - 1
FreeAPS/Sources/Models/FreeAPSSettings.swift

@@ -25,7 +25,7 @@ struct FreeAPSSettings: JSON, Equatable {
     var carbsRequiredThreshold: Decimal = 10
     var animatedBackground: Bool = false
     var displayStatistics: Bool = false
-    var useFPUconversion: Bool = false
+    var useFPUconversion: Bool = true
     var individualAdjustmentFactor: Decimal = 0.5
     var timeCap: Int = 8
     var minuteInterval: Int = 30

+ 30 - 1
FreeAPS/Sources/Modules/AddCarbs/AddCarbsStateModel.swift

@@ -11,9 +11,10 @@ extension AddCarbs {
         @Published var protein: Decimal = 0
         @Published var fat: Decimal = 0
         @Published var carbsRequired: Decimal?
-        @Published var useFPU: Bool = false
+        @Published var useFPU: Bool = true
         @Published var dish: String = ""
         @Published var selection: Presets?
+        @Published var summation: [String] = []
 
         let coredataContext = CoreDataStack.shared.persistentContainer.viewContext
         // @Environment(\.managedObjectContext) var moc
@@ -121,5 +122,33 @@ extension AddCarbs {
             }
             selection = nil
         }
+
+        func removePresetFromNewMeal() {
+            let a = summation.firstIndex(where: { $0 == selection?.dish! })
+
+            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() {
+            let test: String = selection?.dish ?? "dontAdd"
+            if test != "dontAdd" {
+                summation.append(test)
+            }
+        }
+
+        func fullMeal() -> [String] {
+            let filteredArray = summation.filter { !$0.isEmpty }
+            return filteredArray
+        }
     }
 }

+ 45 - 11
FreeAPS/Sources/Modules/AddCarbs/View/AddCarbsRootView.swift

@@ -41,23 +41,34 @@ extension AddCarbs {
                         HStack {
                             Text("Carbs").fontWeight(.semibold)
                             Spacer()
-                            DecimalTextField("0", value: $state.carbs, formatter: formatter, autofocus: true, cleanInput: true)
+                            DecimalTextField(
+                                "0",
+                                value: $state.carbs,
+                                formatter: formatter,
+                                autofocus: true,
+                                cleanInput: true
+                            )
                             Text("grams").foregroundColor(.secondary)
                         }.padding(.vertical)
 
-                        if state.useFPU { proteinAndFat() }
-                        DatePicker("Date", selection: $state.date)
+                        if state.useFPU {
+                            proteinAndFat()
+                        }
                     }
                 }
                 Section {
+                    mealPresets
+                }
+
+                Section {
+                    DatePicker("Date", selection: $state.date)
+                }
+
+                Section(footer: Text(state.fullMeal().description != "[]" ? state.fullMeal().description : "")) {
                     Button { state.add() }
                     label: { Text("Save and continue") }
                         .disabled(state.carbs <= 0 && state.fat <= 0 && state.protein <= 0)
                 }
-
-                if state.useFPU {
-                    mealPresets
-                }
             }
             .onAppear(perform: configureView)
             .navigationBarItems(leading: Button("Close", action: state.hideModal))
@@ -105,6 +116,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 ?? "")
                     }
                 }
                 HStack {
@@ -125,9 +137,25 @@ extension AddCarbs {
                         }
                     )
                     Button {
-                        if state.carbs != 0 { state.carbs -= ((state.selection?.carbs ?? 0) as NSDecimalNumber) as Decimal }
-                        if state.fat != 0 { state.fat -= ((state.selection?.fat ?? 0) as NSDecimalNumber) as Decimal }
-                        if state.protein != 0 { state.protein -= ((state.selection?.protein ?? 0) as NSDecimalNumber) as Decimal }
+                        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()
                     }
                     label: { Text("[ -1 ]") }
                         .disabled(state.selection == nil || (
@@ -142,7 +170,10 @@ extension AddCarbs {
                     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.protein += ((state.selection?.protein ?? 0) as NSDecimalNumber) as Decimal
+
+                        state.addPresetToNewMeal()
+                    }
                     label: { Text("[ +1 ]") }
                         .disabled(state.selection == nil)
                         .buttonStyle(BorderlessButtonStyle())
@@ -184,6 +215,9 @@ extension AddCarbs {
                 label: { Text("Save as Preset") }
             }
             .frame(maxWidth: .infinity, alignment: .trailing)
+            .controlSize(.mini)
+            .buttonStyle(BorderlessButtonStyle())
+
             .disabled(
                 (state.carbs <= 0 && state.fat <= 0 && state.protein <= 0) ||
                     (