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

Add Fat and Protein.
Convert it to carb equivalents.
Thanks Max Power!

Jon Mårtensson 3 лет назад
Родитель
Сommit
78f8c58251

+ 6 - 0
FreeAPS/Sources/Localizations/Main/en.lproj/Localizable.strings

@@ -951,6 +951,12 @@ Enact a temp Basal or a temp target */
 /* Online or internal server */
 "Online or internal server" = "Online or internal server";
 
+/* Add Fat */
+"Fat" = "Fat";
+
+/* Add Protein */
+"Protein" = "Protein";
+
 /* -------------- Developer settings ---------------------- */
 
 /* Debug options */

+ 6 - 0
FreeAPS/Sources/Localizations/Main/sv.lproj/Localizable.strings

@@ -951,6 +951,12 @@ Enact a temp Basal or a temp target */
 /* Online or internal server */
 "Online or internal server" = "Online eller intern server";
 
+/* Add Fat */
+"Fat" = "Fett";
+
+/* Add Protein */
+"Protein" = "Protein";
+
 /* -------------- Developer settings ---------------------- */
 /* Debug options */
 

+ 23 - 4
FreeAPS/Sources/Modules/AddCarbs/AddCarbsStateModel.swift

@@ -6,6 +6,8 @@ extension AddCarbs {
         @Injected() var apsManager: APSManager!
         @Published var carbs: Decimal = 0
         @Published var date = Date()
+        @Published var protein: Decimal = 0
+        @Published var fat: Decimal = 0
         @Published var carbsRequired: Decimal?
 
         override func subscribe() {
@@ -13,14 +15,31 @@ extension AddCarbs {
         }
 
         func add() {
-            guard carbs > 0 else {
+            guard carbs > 0 || fat > 0 || protein > 0 else {
                 showModal(for: nil)
                 return
             }
 
-            carbsStorage.storeCarbs([
-                CarbsEntry(createdAt: date, carbs: carbs, enteredBy: CarbsEntry.manual)
-            ])
+            // Convert fat and protein to carb equivalents and store as future carbs
+            let fpucarb = (0.4 * protein) + (0.9 * fat)
+            let fpus = ((fat * 9.0) + (protein * 4.0)) / 100.0
+            var counter: Decimal = (fpus * 2) - 1.0
+            var roundedCounter: Decimal = 0
+            NSDecimalRound(&roundedCounter, &counter, 0, .up)
+            let carbequiv = fpucarb / roundedCounter
+            while counter > 0 {
+                let newdate = 1.0 + trunc(Double(truncating: counter as NSNumber))
+                carbsStorage.storeCarbs([
+                    CarbsEntry(
+                        createdAt: date + (newdate * 3600), carbs: carbequiv, enteredBy: CarbsEntry.manual
+                    )
+                ])
+                counter -= 1
+            }
+            // Store the real carbs
+            if carbs > 0 {
+                carbsStorage.storeCarbs([CarbsEntry(createdAt: date, carbs: carbs, enteredBy: CarbsEntry.manual)])
+            }
 
             if settingsManager.settings.skipBolusScreenAfterCarbs {
                 apsManager.determineBasalSync()

+ 35 - 8
FreeAPS/Sources/Modules/AddCarbs/View/AddCarbsRootView.swift

@@ -25,19 +25,46 @@ extension AddCarbs {
                     }
                 }
                 Section {
-                    HStack {
-                        Text("Amount")
-                        Spacer()
-                        DecimalTextField("0", value: $state.carbs, formatter: formatter, autofocus: true, cleanInput: true)
-                        Text("grams").foregroundColor(.secondary)
+                    Section {
+                        HStack {
+                            Text("Carbs").fontWeight(.semibold)
+                            Spacer()
+                            DecimalTextField("0", value: $state.carbs, formatter: formatter, autofocus: true, cleanInput: true)
+                            Text("grams").foregroundColor(.secondary)
+                        }.padding(.vertical)
+                        // Adding Protein and Fat. Test
+                        HStack {
+                            Text("Protein").foregroundColor(.loopRed).fontWeight(.thin)
+                            Spacer()
+                            DecimalTextField(
+                                "0",
+                                value: $state.protein,
+                                formatter: formatter,
+                                autofocus: false,
+                                cleanInput: true
+                            ).foregroundColor(.loopRed)
+                            Text("grams").foregroundColor(.secondary)
+                        }
+                        HStack {
+                            Text("Fat").foregroundColor(.loopYellow).fontWeight(.thin)
+                            Spacer()
+                            DecimalTextField(
+                                "0",
+                                value: $state.fat,
+                                formatter: formatter,
+                                autofocus: false,
+                                cleanInput: true
+                            )
+                            Text("grams").foregroundColor(.secondary)
+                        }
+
+                        DatePicker("Date", selection: $state.date)
                     }
-                    DatePicker("Date", selection: $state.date)
                 }
-
                 Section {
                     Button { state.add() }
                     label: { Text("Add") }
-                        .disabled(state.carbs <= 0)
+                        .disabled(state.carbs <= 0 && state.fat <= 0 && state.protein <= 0)
                 }
             }
             .onAppear(perform: configureView)