Procházet zdrojové kódy

Add explicit duplicate filtering in storeCarbs() addressing #44

Deniz Cengiz před 1 rokem
rodič
revize
0786f73745

+ 22 - 2
FreeAPS/Sources/APS/Storage/CarbsStorage.swift

@@ -29,8 +29,28 @@ final class BaseCarbsStorage: CarbsStorage, Injectable {
     }
 
     func storeCarbs(_ entries: [CarbsEntry], areFetchedFromRemote: Bool) async {
-        await saveCarbEquivalents(entries: entries, areFetchedFromRemote: areFetchedFromRemote)
-        await saveCarbsToCoreData(entries: entries, areFetchedFromRemote: areFetchedFromRemote)
+        var entriesToStore = entries
+        
+        if areFetchedFromRemote {
+            let existing24hCarbEntries: [CarbEntryStored] = await CoreDataStack.shared.fetchEntitiesAsync(
+                ofType: CarbEntryStored.self,
+                onContext: self.coredataContext,
+                predicate: NSPredicate.predicateForOneDayAgo,
+                key: "date",
+                ascending: false,
+                batchSize: 50
+            )
+            
+            if !existing24hCarbEntries.isEmpty {
+                // compare entries createdAt and existing24hCarbEntries date and remove duplicates in entries
+                // TODO: refactor this when properties-to-fetch has landed to only fetch dates
+                let existingTimestamps = Set(existing24hCarbEntries.map { $0.date })
+                entriesToStore = entriesToStore.filter { !existingTimestamps.contains($0.actualDate ?? $0.createdAt) || !existingTimestamps.contains($0.createdAt) }
+            }
+        }
+        
+        await saveCarbEquivalents(entries: entriesToStore, areFetchedFromRemote: areFetchedFromRemote)
+        await saveCarbsToCoreData(entries: entriesToStore, areFetchedFromRemote: areFetchedFromRemote)
     }
 
     /**

+ 2 - 56
Trio.xcworkspace/xcshareddata/swiftpm/Package.resolved

@@ -1,5 +1,5 @@
 {
-  "originHash" : "f5c836c216c4ca7d356e3777e58d6d4f9502b03f3974891349eb775f4c4cf750",
+  "originHash" : "cef813f4bbb01679d4ac9bf4a9f82c1a0a61e44dc839643e81aa92e4d00642bc",
   "pins" : [
     {
       "identity" : "cryptoswift",
@@ -11,15 +11,6 @@
       }
     },
     {
-      "identity" : "mkringprogressview",
-      "kind" : "remoteSourceControl",
-      "location" : "https://github.com/maxkonovalov/MKRingProgressView.git",
-      "state" : {
-        "branch" : "master",
-        "revision" : "660888aab1d2ab0ed7eb9eb53caec12af4955fa7"
-      }
-    },
-    {
       "identity" : "slidebutton",
       "kind" : "remoteSourceControl",
       "location" : "https://github.com/no-comment/SlideButton",
@@ -29,60 +20,15 @@
       }
     },
     {
-      "identity" : "swift-algorithms",
-      "kind" : "remoteSourceControl",
-      "location" : "https://github.com/apple/swift-algorithms",
-      "state" : {
-        "revision" : "2327673b0e9c7e90e6b1826376526ec3627210e4",
-        "version" : "0.2.1"
-      }
-    },
-    {
-      "identity" : "swift-numerics",
-      "kind" : "remoteSourceControl",
-      "location" : "https://github.com/apple/swift-numerics",
-      "state" : {
-        "revision" : "6583ac70c326c3ee080c1d42d9ca3361dca816cd",
-        "version" : "0.1.0"
-      }
-    },
-    {
       "identity" : "swiftcharts",
       "kind" : "remoteSourceControl",
-      "location" : "https://github.com/ivanschuetz/SwiftCharts",
+      "location" : "https://github.com/ivanschuetz/SwiftCharts.git",
       "state" : {
         "branch" : "master",
         "revision" : "c354c1945bb35a1f01b665b22474f6db28cba4a2"
       }
     },
     {
-      "identity" : "swiftdate",
-      "kind" : "remoteSourceControl",
-      "location" : "https://github.com/malcommac/SwiftDate",
-      "state" : {
-        "revision" : "6190d0cefff3013e77ed567e6b074f324e5c5bf5",
-        "version" : "6.3.1"
-      }
-    },
-    {
-      "identity" : "swiftmessages",
-      "kind" : "remoteSourceControl",
-      "location" : "https://github.com/SwiftKickMobile/SwiftMessages",
-      "state" : {
-        "revision" : "62e12e138fc3eedf88c7553dd5d98712aa119f40",
-        "version" : "9.0.9"
-      }
-    },
-    {
-      "identity" : "swinject",
-      "kind" : "remoteSourceControl",
-      "location" : "https://github.com/Swinject/Swinject",
-      "state" : {
-        "revision" : "be9dbcc7b86811bc131539a20c6f9c2d3e56919f",
-        "version" : "2.9.1"
-      }
-    },
-    {
       "identity" : "tidepoolkit",
       "kind" : "remoteSourceControl",
       "location" : "https://github.com/tidepool-org/TidepoolKit",