Marvin Polscheit 1 год назад
Родитель
Сommit
3963b810c1

+ 8 - 12
Trio/Sources/Modules/Treatments/TreatmentsStateModel.swift

@@ -89,6 +89,7 @@ extension Treatments {
         var note: String = ""
 
         var date = Date()
+        let defaultDate = Date()
 
         var carbsRequired: Decimal?
         var useFPUconversion: Bool = false
@@ -381,18 +382,12 @@ extension Treatments {
                 minPredBG
             }
 
-            // Check if carbs are actually backdated (more than 15 minutes in the past)
-            // This ensures we only consider it backdated if the user has deliberately changed the date
-            let minutesThreshold = 15.0 // 15 minutes threshold
-            let isBackdated = date.timeIntervalSinceNow < -minutesThreshold * 60 && simulatedDetermination != nil
+            // Use the cob value of the simulation if we have a simulated determination
+            let simulatedCOB: Int16? =
+                Int16(truncating: NSNumber(value: (simulatedDetermination?.cob as NSDecimalNumber?)?.doubleValue ?? 0))
 
-            // Use the cob value of the simulation if carbs are backdated, otherwise set `simulatedCOB` to nil so that the cob value of the most recent determination gets used in the Bolus Calc Manager
-            let simulatedCOB: Int16? = isBackdated ?
-                Int16(truncating: NSNumber(value: (simulatedDetermination?.cob as NSDecimalNumber?)?.doubleValue ?? 0)) : nil
-            // If the user backdates carbs the current carb value must be set to 0
-            // otherwise the calc would recommend insulin for carbs + backdated carbs
-            // e.g. you enter 50g carbs and backdate them 1h -> calc would recommend insulin for 50g + ~45g (where ~45g is the partially absorbed cob)
-            let carbs: Decimal = isBackdated ? 0 : self.carbs
+            // Check if this is a backdated entry by comparing with the default date
+            let isBackdated = date != defaultDate
 
             let result = await bolusCalculationManager.handleBolusCalculation(
                 carbs: carbs,
@@ -400,7 +395,8 @@ extension Treatments {
                 useSuperBolus: useSuperBolus,
                 lastLoopDate: apsManager.lastLoopDate,
                 minPredBG: localMinPredBG,
-                simulatedCOB: simulatedCOB
+                simulatedCOB: simulatedCOB,
+                isBackdated: isBackdated
             )
 
             // Update state properties with calculation results on main thread

+ 15 - 6
Trio/Sources/Services/BolusCalculator/BolusCalculationManager.swift

@@ -10,7 +10,8 @@ protocol BolusCalculationManager {
         useSuperBolus: Bool,
         lastLoopDate: Date,
         minPredBG: Decimal?,
-        simulatedCOB: Int16?
+        simulatedCOB: Int16?,
+        isBackdated: Bool
     ) async -> CalculationResult
 }
 
@@ -290,7 +291,8 @@ final class BaseBolusCalculationManager: BolusCalculationManager, Injectable {
         useSuperBolus: Bool,
         lastLoopDate: Date,
         minPredBG: Decimal?,
-        simulatedCOB: Int16?
+        simulatedCOB: Int16?,
+        isBackdated: Bool
     ) async throws -> CalculationInput {
         do {
             // Get settings
@@ -338,15 +340,20 @@ final class BaseBolusCalculationManager: BolusCalculationManager, Injectable {
                 )
             }
 
+            // If the entry is backdated (user explicitly changed the date), set carbs to 0
+            // This prevents double-counting of carbs (entered carbs + COB from backdated entry)
+            let effectiveCarbs = isBackdated ? 0 : carbs
+            let effectiveCob = isBackdated ? simulatedCOB : bolusVars.cob
+
             return CalculationInput(
-                carbs: carbs,
+                carbs: effectiveCarbs,
                 currentBG: glucoseVars.currentBG,
                 deltaBG: glucoseVars.deltaBG,
                 target: bolusVars.target,
                 isf: bolusVars.isf,
                 carbRatio: bolusVars.carbRatio,
                 iob: bolusVars.iob,
-                cob: simulatedCOB ?? bolusVars.cob,
+                cob: effectiveCob ?? bolusVars.cob,
                 useFattyMealCorrectionFactor: useFattyMealCorrection,
                 fattyMealFactor: settings.fattyMealFactor,
                 useSuperBolus: useSuperBolus,
@@ -492,7 +499,8 @@ final class BaseBolusCalculationManager: BolusCalculationManager, Injectable {
         useSuperBolus: Bool,
         lastLoopDate: Date,
         minPredBG: Decimal? = nil,
-        simulatedCOB: Int16? = nil
+        simulatedCOB: Int16? = nil,
+        isBackdated: Bool = false
     ) async -> CalculationResult {
         do {
             let input = try await prepareCalculationInput(
@@ -501,7 +509,8 @@ final class BaseBolusCalculationManager: BolusCalculationManager, Injectable {
                 useSuperBolus: useSuperBolus,
                 lastLoopDate: lastLoopDate,
                 minPredBG: minPredBG,
-                simulatedCOB: simulatedCOB
+                simulatedCOB: simulatedCOB,
+                isBackdated: isBackdated
             )
             let result = await calculateInsulin(input: input)
             return result

+ 2 - 1
Trio/Sources/Services/WatchManager/AppleWatchManager.swift

@@ -623,7 +623,8 @@ final class BaseWatchManager: NSObject, WCSessionDelegate, Injectable, WatchMana
                         useSuperBolus: false,
                         lastLoopDate: apsManager.lastLoopDate,
                         minPredBG: minPredBG,
-                        simulatedCOB: nil // TODO:
+                        simulatedCOB: nil,
+                        isBackdated: false // we cannot backdate carbs via watch
                     )
 
                     // Send recommendation back to watch