Przeglądaj źródła

Implement deleteGlucose functionality in NightscoutManager and update HistoryProvider

Rhys 2 miesięcy temu
rodzic
commit
61280f68c7

+ 1 - 0
Trio/Sources/Modules/History/HistoryDataFlow.swift

@@ -244,6 +244,7 @@ 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 deleteGlucoseFromHealth(withSyncID id: String)
     func deleteMealDataFromHealth(byID id: String, sampleType: HKSampleType)

+ 7 - 0
Trio/Sources/Modules/History/HistoryProvider.swift

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

+ 5 - 1
Trio/Sources/Modules/History/HistoryStateModel.swift

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

+ 31 - 1
Trio/Sources/Services/Network/Nightscout/NightscoutAPI.swift

@@ -188,7 +188,7 @@ extension NightscoutAPI {
         return
     }
 
-    func deleteManualGlucose(withId id: String) async throws {
+    func deleteGlucose(withId id: String) async throws {
         var components = URLComponents()
         components.scheme = url.scheme
         components.host = url.host
@@ -216,6 +216,36 @@ extension NightscoutAPI {
         guard let httpResponse = response as? HTTPURLResponse, (200 ... 299).contains(httpResponse.statusCode) else {
             throw URLError(.badServerResponse)
         }
+    }
+
+    func deleteManualGlucose(withId id: String) 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)
+        ]
+
+        guard let url = components.url else {
+            throw URLError(.badURL)
+        }
+
+        var request = URLRequest(url: url)
+        request.allowsConstrainedNetworkAccess = false
+        request.timeoutInterval = Config.timeout
+        request.httpMethod = "DELETE"
+
+        if let secret = secret {
+            request.addValue(secret.sha1(), forHTTPHeaderField: "api-secret")
+        }
+
+        let (_, response) = try await URLSession.shared.data(for: request)
+
+        guard let httpResponse = response as? HTTPURLResponse, (200 ... 299).contains(httpResponse.statusCode) else {
+            throw URLError(.badServerResponse)
+        }
 
         debugPrint("Delete successful for ID \(id)")
     }

+ 15 - 0
Trio/Sources/Services/Network/Nightscout/NightscoutManager.swift

@@ -11,6 +11,7 @@ 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 uploadDeviceStatus() async throws
     func uploadGlucose() async
@@ -339,6 +340,20 @@ final class BaseNightscoutManager: NightscoutManager, Injectable {
         }
     }
 
+    func deleteGlucose(withID id: String) async {
+        guard let nightscout = nightscoutAPI, isUploadEnabled else { return }
+
+        do {
+            try await nightscout.deleteGlucose(withId: id)
+            debug(.nightscout, "Glucose deleted")
+        } catch {
+            debug(
+                .nightscout,
+                "\(DebuggingIdentifiers.failed) Failed to delete Glucose from Nightscout with error: \(error)"
+            )
+        }
+    }
+
     func deleteManualGlucose(withID id: String) async {
         guard let nightscout = nightscoutAPI, isUploadEnabled else { return }
 

+ 1 - 0
xDrip

@@ -0,0 +1 @@
+Subproject commit 0fe97c6d08bfd7025b792c64aff47da4fe80d2af