polscm32 aka Marvout 1 год назад
Родитель
Сommit
0a545e5393
2 измененных файлов с 30 добавлено и 23 удалено
  1. 5 23
      FreeAPS/Sources/APS/APSManager.swift
  2. 25 0
      FreeAPS/Sources/APS/Storage/TDDStorage.swift

+ 5 - 23
FreeAPS/Sources/APS/APSManager.swift

@@ -356,9 +356,8 @@ final class BaseAPSManager: APSManager, Injectable {
     private func calculateAndStoreTDD() async {
         guard let pumpManager else { return }
 
-        // Get required data
-        let pumpHistory = await pumpHistoryStorage.getPumpHistory()
-        let basalProfile = await storage
+        async let pumpHistory = pumpHistoryStorage.getPumpHistory()
+        async let basalProfile = storage
             .retrieveAsync(OpenAPS.Settings.basalProfile, as: [BasalProfileEntry].self) ??
             [BasalProfileEntry](from: OpenAPS.defaults(for: OpenAPS.Settings.basalProfile)) ??
             [] // OpenAPS.defaults ensures we at least get default rate of 1u/hr for 24 hrs
@@ -367,32 +366,15 @@ final class BaseAPSManager: APSManager, Injectable {
 
         // Calculate TDD
         let tddResult = await tddStorage.calculateTDD(
-            pumpHistory: pumpHistory,
-            basalProfile: basalProfile,
+            pumpHistory: await pumpHistory,
+            basalProfile: await basalProfile,
             basalIncrement: Decimal(
                 basalIncrements.first(where: { $0 > 0.0 }) ?? 0.05
             ) // supportedBasalRates must be non-empty, so we could force-unwrap here… but apparently sim-pump does not like that?!
         )
 
-        // TODO: Move this to storage as well
         // Store TDD in Core Data
-        await privateContext.perform {
-            let tddStored = TDDStored(context: self.privateContext)
-            tddStored.id = UUID()
-            tddStored.date = Date()
-            tddStored.total = NSDecimalNumber(decimal: tddResult.total)
-            tddStored.bolus = NSDecimalNumber(decimal: tddResult.bolus)
-            tddStored.tempBasal = NSDecimalNumber(decimal: tddResult.tempBasal)
-            tddStored.scheduledBasal = NSDecimalNumber(decimal: tddResult.scheduledBasal)
-            tddStored.weightedAverage = tddResult.weightedAverage.map { NSDecimalNumber(decimal: $0) }
-
-            do {
-                guard self.privateContext.hasChanges else { return }
-                try self.privateContext.save()
-            } catch {
-                debug(.apsManager, "\(DebuggingIdentifiers.failed) Failed to save TDD: \(error.localizedDescription)")
-            }
-        }
+        await tddStorage.storeTDD(tddResult)
     }
 
     func determineBasal() async -> Bool {

+ 25 - 0
FreeAPS/Sources/APS/Storage/TDDStorage.swift

@@ -4,6 +4,7 @@ import Swinject
 protocol TDDStorage {
     func calculateTDD(pumpHistory: [PumpHistoryEvent], basalProfile: [BasalProfileEntry], basalIncrement: Decimal) async
         -> TDDResult
+    func storeTDD(_ tddResult: TDDResult) async
 }
 
 /// Structure containing the results of TDD calculations
@@ -22,6 +23,8 @@ final class BaseTDDStorage: TDDStorage, Injectable {
         injectServices(resolver)
     }
 
+    private let privateContext = CoreDataStack.shared.newTaskContext()
+
     /// Main function to calculate TDD from pump history and basal profile
     /// - Parameters:
     ///   - pumpHistory: Array of pump history events
@@ -86,6 +89,28 @@ final class BaseTDDStorage: TDDStorage, Injectable {
         )
     }
 
+    /// Stores the Total Daily Dose (TDD) result in Core Data
+    /// - Parameter tddResult: The TDD result to store, containing total insulin, bolus, temp basal, scheduled basal and weighted average
+    func storeTDD(_ tddResult: TDDResult) async {
+        await privateContext.perform {
+            let tddStored = TDDStored(context: self.privateContext)
+            tddStored.id = UUID()
+            tddStored.date = Date()
+            tddStored.total = NSDecimalNumber(decimal: tddResult.total)
+            tddStored.bolus = NSDecimalNumber(decimal: tddResult.bolus)
+            tddStored.tempBasal = NSDecimalNumber(decimal: tddResult.tempBasal)
+            tddStored.scheduledBasal = NSDecimalNumber(decimal: tddResult.scheduledBasal)
+            tddStored.weightedAverage = tddResult.weightedAverage.map { NSDecimalNumber(decimal: $0) }
+
+            do {
+                guard self.privateContext.hasChanges else { return }
+                try self.privateContext.save()
+            } catch {
+                debug(.apsManager, "\(DebuggingIdentifiers.failed) Failed to save TDD: \(error.localizedDescription)")
+            }
+        }
+    }
+
     /// Calculates the number of hours of available pump history data
     /// - Parameter pumpHistory: Array of pump history events
     /// - Returns: Number of hours of available data