Explorar el Código

No longer push _id to NS; decode as legacyId

Deniz Cengiz hace 1 mes
padre
commit
149e3bb71f

+ 1 - 1
Model/JSONImporter.swift

@@ -355,7 +355,7 @@ extension BloodGlucose {
         }
 
         let glucoseEntry = GlucoseStored(context: context)
-        glucoseEntry.id = _id.flatMap({ UUID(uuidString: $0) }) ?? UUID()
+        glucoseEntry.id = UUID(uuidString: id) ?? UUID()
         glucoseEntry.date = dateString
         glucoseEntry.glucose = Int16(glucoseValue)
         glucoseEntry.direction = direction?.rawValue

+ 1 - 1
Trio/Sources/APS/CGM/GlucoseSimulatorSource.swift

@@ -217,7 +217,7 @@ class OscillatingGenerator: BloodGlucoseGenerator {
 
             // Create BloodGlucose with the correct constructor
             let bloodGlucose = BloodGlucose(
-                _id: UUID().uuidString,
+                id: UUID().uuidString,
                 sgv: glucose,
                 direction: direction,
                 date: Decimal(Int(currentDate.timeIntervalSince1970) * 1000),

+ 1 - 1
Trio/Sources/APS/CGM/PluginSource.swift

@@ -236,7 +236,7 @@ extension PluginSource: CGMManagerDelegate {
 
                 let value = Int(quantity.doubleValue(for: .milligramsPerDeciliter))
                 return BloodGlucose(
-                    _id: UUID().uuidString,
+                    id: UUID().uuidString,
                     sgv: value,
                     direction: .init(trendType: newGlucoseSample.trend),
                     date: Decimal(Int(newGlucoseSample.date.timeIntervalSince1970 * 1000)),

+ 1 - 1
Trio/Sources/APS/DeviceDataManager.swift

@@ -332,7 +332,7 @@ final class BaseDeviceDataManager: DeviceDataManager, Injectable {
                         let results = glucose.enumerated().map { index, sample -> BloodGlucose in
                             let value = Int(sample.quantity.doubleValue(for: .milligramsPerDeciliter))
                             return BloodGlucose(
-                                _id: sample.syncIdentifier,
+                                id: sample.syncIdentifier,
                                 sgv: value,
                                 direction: directions[index],
                                 date: Decimal(Int(sample.date.timeIntervalSince1970 * 1000)),

+ 5 - 5
Trio/Sources/APS/Storage/GlucoseStorage.swift

@@ -412,7 +412,7 @@ final class BaseGlucoseStorage: GlucoseStorage, Injectable {
 
             return fetchedResults.map { result in
                 BloodGlucose(
-                    _id: result.id?.uuidString ?? UUID().uuidString,
+                    id: result.id?.uuidString ?? UUID().uuidString,
                     sgv: Int(result.glucose),
                     direction: BloodGlucose.Direction(from: result.direction ?? ""),
                     date: Decimal(result.date?.timeIntervalSince1970 ?? Date().timeIntervalSince1970) * 1000,
@@ -501,7 +501,7 @@ final class BaseGlucoseStorage: GlucoseStorage, Injectable {
 
             return fetchedResults.map { result in
                 BloodGlucose(
-                    _id: result.id?.uuidString ?? UUID().uuidString,
+                    id: result.id?.uuidString ?? UUID().uuidString,
                     sgv: Int(result.glucose),
                     direction: BloodGlucose.Direction(from: result.direction ?? ""),
                     date: Decimal(result.date?.timeIntervalSince1970 ?? Date().timeIntervalSince1970) * 1000,
@@ -533,7 +533,7 @@ final class BaseGlucoseStorage: GlucoseStorage, Injectable {
 
             return fetchedResults.map { result in
                 BloodGlucose(
-                    _id: result.id?.uuidString ?? UUID().uuidString,
+                    id: result.id?.uuidString ?? UUID().uuidString,
                     sgv: Int(result.glucose),
                     direction: BloodGlucose.Direction(from: result.direction ?? ""),
                     date: Decimal(result.date?.timeIntervalSince1970 ?? Date().timeIntervalSince1970) * 1000,
@@ -565,7 +565,7 @@ final class BaseGlucoseStorage: GlucoseStorage, Injectable {
 
             return fetchedResults.map { result in
                 BloodGlucose(
-                    _id: result.id?.uuidString ?? UUID().uuidString,
+                    id: result.id?.uuidString ?? UUID().uuidString,
                     sgv: Int(result.glucose),
                     direction: BloodGlucose.Direction(from: result.direction ?? ""),
                     date: Decimal(result.date?.timeIntervalSince1970 ?? Date().timeIntervalSince1970) * 1000,
@@ -598,7 +598,7 @@ final class BaseGlucoseStorage: GlucoseStorage, Injectable {
 
             return fetchedResults.map { result in
                 BloodGlucose(
-                    _id: result.id?.uuidString ?? UUID().uuidString,
+                    id: result.id?.uuidString ?? UUID().uuidString,
                     sgv: Int(result.glucose),
                     direction: BloodGlucose.Direction(from: result.direction ?? ""),
                     date: Decimal(result.date?.timeIntervalSince1970 ?? Date().timeIntervalSince1970) * 1000,

+ 14 - 9
Trio/Sources/Models/BloodGlucose.swift

@@ -60,7 +60,8 @@ struct BloodGlucose: JSON, Identifiable, Hashable, Codable {
     }
 
     enum CodingKeys: String, CodingKey {
-        case _id
+        case legacyId = "_id"
+        case id
         case sgv
         case direction
         case date
@@ -77,7 +78,12 @@ struct BloodGlucose: JSON, Identifiable, Hashable, Codable {
 
     init(from decoder: Decoder) throws {
         let container = try decoder.container(keyedBy: CodingKeys.self)
-        _id = try container.decode(String.self, forKey: ._id)
+
+        let legacyId = try container.decodeIfPresent(String.self, forKey: .legacyId)
+        let explicitId = try container.decodeIfPresent(String.self, forKey: .id)
+
+        self.legacyId = legacyId
+        id = explicitId ?? legacyId ?? UUID().uuidString
 
         sgv = try? container.decodeIfPresent(Int.self, forKey: .sgv)
         if sgv == nil {
@@ -102,7 +108,8 @@ struct BloodGlucose: JSON, Identifiable, Hashable, Codable {
     }
 
     init(
-        _id: String = UUID().uuidString,
+        id: String = UUID().uuidString,
+        legacyId: String? = nil,
         sgv: Int? = nil,
         direction: Direction? = nil,
         date: Decimal,
@@ -116,7 +123,8 @@ struct BloodGlucose: JSON, Identifiable, Hashable, Codable {
         sessionStartDate: Date? = nil,
         transmitterID: String? = nil
     ) {
-        self._id = _id
+        self.id = id
+        self.legacyId = legacyId
         self.sgv = sgv
         self.direction = direction
         self.date = date
@@ -131,11 +139,8 @@ struct BloodGlucose: JSON, Identifiable, Hashable, Codable {
         self.transmitterID = transmitterID
     }
 
-    var _id: String?
-    var id: String {
-        _id ?? UUID().uuidString
-    }
-
+    let legacyId: String?
+    var id: String
     var sgv: Int?
     var direction: Direction?
     let date: Decimal

+ 2 - 2
Trio/Sources/Modules/History/HistoryDataFlow.swift

@@ -244,8 +244,8 @@ enum History {
 protocol HistoryProvider: Provider {
     func deleteCarbsFromNightscout(withID id: String)
     func deleteInsulinFromNightscout(withID id: String)
-    func deleteGlucoseFromNightscout(withID id: String)
-    func deleteManualGlucoseFromNightscout(withID id: String)
+    func deleteGlucoseFromNightscout(withID id: String, withDate date: Date)
+    func deleteManualGlucoseFromNightscout(withID id: String, withDate date: Date)
     func deleteGlucoseFromHealth(withSyncID id: String)
     func deleteMealDataFromHealth(byID id: String, sampleType: HKSampleType)
     func deleteInsulinFromHealth(withSyncID id: String)

+ 4 - 4
Trio/Sources/Modules/History/HistoryProvider.swift

@@ -35,17 +35,17 @@ extension History {
             }
         }
 
-        func deleteGlucoseFromNightscout(withID id: String) {
+        func deleteGlucoseFromNightscout(withID id: String, withDate date: Date) {
             Task.detached { [weak self] in
                 guard let self = self else { return }
-                await self.nightscoutManager.deleteGlucose(withID: id)
+                await self.nightscoutManager.deleteGlucose(withID: id, withDate: date)
             }
         }
 
-        func deleteManualGlucoseFromNightscout(withID id: String) {
+        func deleteManualGlucoseFromNightscout(withID id: String, withDate date: Date) {
             Task.detached { [weak self] in
                 guard let self = self else { return }
-                await self.nightscoutManager.deleteManualGlucose(withID: id)
+                await self.nightscoutManager.deleteManualGlucose(withID: id, withDate: date)
             }
         }
 

+ 3 - 3
Trio/Sources/Modules/History/HistoryStateModel.swift

@@ -73,11 +73,11 @@ extension History {
                     }
 
                     // Delete from Nightscout
-                    if let id = glucoseToDelete.id?.uuidString {
+                    if let id = glucoseToDelete.id?.uuidString, let date = glucoseToDelete.date {
                         if glucoseToDelete.isManual {
-                            self.provider.deleteManualGlucoseFromNightscout(withID: id)
+                            self.provider.deleteManualGlucoseFromNightscout(withID: id, withDate: date)
                         } else {
-                            self.provider.deleteGlucoseFromNightscout(withID: id)
+                            self.provider.deleteGlucoseFromNightscout(withID: id, withDate: date)
                         }
                     }
 

+ 5 - 5
Trio/Sources/Services/Network/Nightscout/NightscoutAPI.swift

@@ -188,14 +188,14 @@ extension NightscoutAPI {
         return
     }
 
-    func deleteGlucose(withId id: String) async throws {
+    func deleteGlucose(withId _: String, withDate date: Date) async throws {
         var components = URLComponents()
         components.scheme = url.scheme
         components.host = url.host
         components.port = url.port
         components.path = Config.uploadEntriesPath
         components.queryItems = [
-            URLQueryItem(name: "find[_id][$eq]", value: id)
+            URLQueryItem(name: "find[dateString][$eq]", value: Formatter.iso8601withFractionalSeconds.string(from: date))
         ]
 
         guard let url = components.url else {
@@ -218,14 +218,14 @@ extension NightscoutAPI {
         }
     }
 
-    func deleteManualGlucose(withId id: String) async throws {
+    func deleteManualGlucose(withId id: String, withDate date: Date) async throws {
         var components = URLComponents()
         components.scheme = url.scheme
         components.host = url.host
         components.port = url.port
         components.path = Config.uploadEntriesPath
         components.queryItems = [
-            URLQueryItem(name: "find[id][$eq]", value: id)
+            URLQueryItem(name: "find[dateString][$eq]", value: Formatter.iso8601withFractionalSeconds.string(from: date))
         ]
 
         guard let url = components.url else {
@@ -247,7 +247,7 @@ extension NightscoutAPI {
             throw URLError(.badServerResponse)
         }
 
-        debugPrint("Delete successful for ID \(id)")
+        debugPrint("Delete successful for ID \(id) at \(date)")
     }
 
     func deleteInsulin(withId id: String) async throws {

+ 6 - 6
Trio/Sources/Services/Network/Nightscout/NightscoutManager.swift

@@ -11,8 +11,8 @@ protocol NightscoutManager: GlucoseSource {
     func fetchTempTargets() async -> [TempTarget]
     func deleteCarbs(withID id: String) async
     func deleteInsulin(withID id: String) async
-    func deleteGlucose(withID id: String) async
-    func deleteManualGlucose(withID id: String) async
+    func deleteGlucose(withID id: String, withDate date: Date) async
+    func deleteManualGlucose(withID id: String, withDate date: Date) async
     func uploadDeviceStatus() async throws
     func uploadGlucose() async
     func uploadCarbs() async
@@ -340,11 +340,11 @@ final class BaseNightscoutManager: NightscoutManager, Injectable {
         }
     }
 
-    func deleteGlucose(withID id: String) async {
+    func deleteGlucose(withID id: String, withDate date: Date) async {
         guard let nightscout = nightscoutAPI, isUploadEnabled else { return }
 
         do {
-            try await nightscout.deleteGlucose(withId: id)
+            try await nightscout.deleteGlucose(withId: id, withDate: date)
             debug(.nightscout, "Glucose deleted")
         } catch {
             debug(
@@ -354,11 +354,11 @@ final class BaseNightscoutManager: NightscoutManager, Injectable {
         }
     }
 
-    func deleteManualGlucose(withID id: String) async {
+    func deleteManualGlucose(withID id: String, withDate date: Date) async {
         guard let nightscout = nightscoutAPI, isUploadEnabled else { return }
 
         do {
-            try await nightscout.deleteManualGlucose(withId: id)
+            try await nightscout.deleteManualGlucose(withId: id, withDate: date)
         } catch {
             debug(
                 .nightscout,