Explorar o código

filter at database level

marv-out suggestion
Robert hai 7 meses
pai
achega
c064c30ca2
Modificáronse 1 ficheiros con 12 adicións e 8 borrados
  1. 12 8
      Trio/Sources/Services/WatchManager/GarminManager.swift

+ 12 - 8
Trio/Sources/Services/WatchManager/GarminManager.swift

@@ -478,21 +478,26 @@ final class BaseGarminManager: NSObject, GarminManager, Injectable, @unchecked S
     /// Fetches recent temp basal events from CoreData pump history.
     /// Fetches recent temp basal events from CoreData pump history.
     /// - Returns: An array of `NSManagedObjectID`s for pump events with temp basals.
     /// - Returns: An array of `NSManagedObjectID`s for pump events with temp basals.
     private func fetchTempBasals() async throws -> [NSManagedObjectID] {
     private func fetchTempBasals() async throws -> [NSManagedObjectID] {
+        let tempBasalPredicate = NSPredicate(format: "tempBasal != nil")
+        let compoundPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: [
+            NSPredicate.pumpHistoryLast24h,
+            tempBasalPredicate
+        ])
+        
         let results = try await CoreDataStack.shared.fetchEntitiesAsync(
         let results = try await CoreDataStack.shared.fetchEntitiesAsync(
             ofType: PumpEventStored.self,
             ofType: PumpEventStored.self,
             onContext: backgroundContext,
             onContext: backgroundContext,
-            predicate: NSPredicate.pumpHistoryLast24h,
+            predicate: compoundPredicate,
             key: "timestamp",
             key: "timestamp",
             ascending: false, // Most recent first
             ascending: false, // Most recent first
-            fetchLimit: 5
+            fetchLimit: 1
         )
         )
 
 
         return try await backgroundContext.perform {
         return try await backgroundContext.perform {
             guard let pumpEvents = results as? [PumpEventStored] else {
             guard let pumpEvents = results as? [PumpEventStored] else {
                 throw CoreDataError.fetchError(function: #function, file: #file)
                 throw CoreDataError.fetchError(function: #function, file: #file)
             }
             }
-            // Filter only events that have a tempBasal
-            return pumpEvents.filter { $0.tempBasal != nil }.map(\.objectID)
+            return pumpEvents.map(\.objectID)
         }
         }
     }
     }
 
 
@@ -800,8 +805,7 @@ final class BaseGarminManager: NSObject, GarminManager, Injectable, @unchecked S
 
 
                 // Get display configuration from settings
                 // Get display configuration from settings
                 let displayPrimaryAttributeChoice = self.settingsManager.settings.garminSettings.primaryAttributeChoice.rawValue
                 let displayPrimaryAttributeChoice = self.settingsManager.settings.garminSettings.primaryAttributeChoice.rawValue
-                let displaySecondaryAttributeChoice = self.settingsManager.settings.garminSettings.secondaryAttributeChoice
-                    .rawValue
+                let displaySecondaryAttributeChoice = self.settingsManager.settings.garminSettings.secondaryAttributeChoice.rawValue
 
 
                 // Process glucose readings
                 // Process glucose readings
                 // For Trio: Process 2 readings (to calculate delta) but only send 1 entry
                 // For Trio: Process 2 readings (to calculate delta) but only send 1 entry
@@ -1603,7 +1607,7 @@ extension BaseGarminManager: SettingsObserver {
             // Re-register devices to add/remove watchface app based on enabled state
             // Re-register devices to add/remove watchface app based on enabled state
             registerDevices(devices)
             registerDevices(devices)
 
 
-            if !settings.garminSettings.isWatchfaceDataEnabled { // ← REVERSED LOGIC
+            if !settings.garminSettings.isWatchfaceDataEnabled {  // ← REVERSED LOGIC
                 debugGarmin("Garmin: Watchface app unregistered, datafield continues")
                 debugGarmin("Garmin: Watchface app unregistered, datafield continues")
             } else {
             } else {
                 debugGarmin("Garmin: Watchface app re-registered - sending immediate update")
                 debugGarmin("Garmin: Watchface app re-registered - sending immediate update")
@@ -1636,7 +1640,7 @@ extension BaseGarminManager: SettingsObserver {
         // Determine which type of update is needed (if any)
         // Determine which type of update is needed (if any)
         let needsImmediateUpdate = (
         let needsImmediateUpdate = (
             unitsChanged ||
             unitsChanged ||
-                (enabledChanged && settings.garminSettings.isWatchfaceDataEnabled) // ← REVERSED LOGIC
+                (enabledChanged && settings.garminSettings.isWatchfaceDataEnabled)  // ← REVERSED LOGIC
         ) &&
         ) &&
             !watchfaceChanged && !datafieldChanged // Don't send if only watchface or datafield changed
             !watchfaceChanged && !datafieldChanged // Don't send if only watchface or datafield changed