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

Change order of task invocation to fix bogus carbsRequired notification

Co-Authored-By: avouspierre <avouspierre@users.noreply.github.com>
Deniz Cengiz 1 год назад
Родитель
Сommit
68928ad628
1 измененных файлов с 9 добавлено и 30 удалено
  1. 9 30
      Trio/Sources/Modules/Treatments/TreatmentsStateModel.swift

+ 9 - 30
Trio/Sources/Modules/Treatments/TreatmentsStateModel.swift

@@ -380,26 +380,6 @@ extension Treatments {
 
         /// Calculate insulin recommendation
         @MainActor func calculateInsulin() async -> Decimal {
-//            let input = CalculationInput(
-//                carbs: carbs,
-//                currentBG: currentBG,
-//                deltaBG: deltaBG,
-//                target: target,
-//                isf: isf,
-//                carbRatio: carbRatio,
-//                iob: iob,
-//                cob: cob,
-//                useFattyMealCorrectionFactor: useFattyMealCorrectionFactor,
-//                fattyMealFactor: fattyMealFactor,
-//                useSuperBolus: useSuperBolus,
-//                sweetMealFactor: sweetMealFactor,
-//                basal: basal,
-//                fraction: fraction,
-//                maxBolus: maxBolus
-//            )
-//
-//            let result = await bolusCalculationManager.calculateInsulin(input: input)
-
             let result = await bolusCalculationManager.handleBolusCalculation(
                 carbs: carbs,
                 useFattyMealCorrection: useFattyMealCorrectionFactor,
@@ -432,19 +412,17 @@ extension Treatments {
                 let isFatPresent = fat > 0
                 let isProteinPresent = protein > 0
 
+                if isCarbsPresent || isFatPresent || isProteinPresent {
+                    await saveMeal()
+                }
+
                 if isInsulinGiven {
                     try await handleInsulin(isExternal: externalInsulin)
-                } else if isCarbsPresent || isFatPresent || isProteinPresent {
-                    await MainActor.run {
-                        self.waitForSuggestion = true
-                    }
                 } else {
                     hideModal()
                     return
                 }
 
-                await saveMeal()
-
                 // If glucose data is stale end the custom loading animation by hiding the modal
                 // Get date on Main thread
                 let date = await MainActor.run {
@@ -560,11 +538,12 @@ extension Treatments {
             )]
             await carbsStorage.storeCarbs(carbsToStore, areFetchedFromRemote: false)
 
-            if carbs > 0 || fat > 0 || protein > 0 {
-                // only perform determine basal sync if the user doesn't use the pump bolus, otherwise the enact bolus func in the APSManger does a sync
-                if amount <= 0 {
-                    await apsManager.determineBasalSync()
+            // only perform determine basal sync if the user doesn't use the pump bolus, otherwise the enact bolus func in the APSManger does a sync
+            if amount <= 0 {
+                await MainActor.run {
+                    self.waitForSuggestion = true
                 }
+                await apsManager.determineBasalSync()
             }
         }