Procházet zdrojové kódy

Switch to BloodGlucose type

Sam King před 1 rokem
rodič
revize
99a351e247

+ 0 - 18
Model/Helper/GlucoseStored+helper.swift

@@ -174,21 +174,3 @@ extension GlucoseStored {
         BloodGlucose.Direction(rawValue: direction ?? "")
     }
 }
-
-extension Glucose {
-    func store(in context: NSManagedObjectContext) throws {
-        guard let glucoseValue = glucose ?? sgv else {
-            throw JSONImporterError.missingGlucoseValueInGlucoseEntry
-        }
-
-        let glucoseEntry = GlucoseStored(context: context)
-        glucoseEntry.id = _id.flatMap({ UUID(uuidString: $0) }) ?? UUID()
-        glucoseEntry.date = date
-        glucoseEntry.glucose = Int16(glucoseValue)
-        glucoseEntry.direction = direction?.rawValue
-        glucoseEntry.isManual = type == .manual
-        glucoseEntry.isUploadedToNS = true
-        glucoseEntry.isUploadedToHealth = true
-        glucoseEntry.isUploadedToTidepool = true
-    }
-}

+ 20 - 3
Model/JSONImporter.swift

@@ -35,7 +35,6 @@ class JSONImporter {
     private func readJsonFile<T: Decodable>(url: URL) throws -> T {
         let data = try Data(contentsOf: url)
         let decoder = JSONCoding.decoder
-        decoder.dateDecodingStrategy = .millisecondsSince1970
         return try decoder.decode(T.self, from: data)
     }
 
@@ -67,10 +66,10 @@ class JSONImporter {
     ///   - An error if the file cannot be read or decoded.
     ///   - An error if the CoreData operation fails.
     func importGlucoseHistory(url: URL) async throws {
-        let glucoseHistory: [Glucose] = try readJsonFile(url: url)
+        let glucoseHistory: [BloodGlucose] = try readJsonFile(url: url)
         let existingDates = try await fetchGlucoseDates()
         for glucoseEntry in glucoseHistory {
-            if !existingDates.contains(glucoseEntry.date) {
+            if !existingDates.contains(glucoseEntry.dateString) {
                 try glucoseEntry.store(in: context)
             }
         }
@@ -79,6 +78,24 @@ class JSONImporter {
 
 // MARK: - Extension for Specific Import Functions
 
+extension BloodGlucose {
+    func store(in context: NSManagedObjectContext) throws {
+        guard let glucoseValue = glucose ?? sgv else {
+            throw JSONImporterError.missingGlucoseValueInGlucoseEntry
+        }
+
+        let glucoseEntry = GlucoseStored(context: context)
+        glucoseEntry.id = _id.flatMap({ UUID(uuidString: $0) }) ?? UUID()
+        glucoseEntry.date = dateString
+        glucoseEntry.glucose = Int16(glucoseValue)
+        glucoseEntry.direction = direction?.rawValue
+        glucoseEntry.isManual = type == "Manual"
+        glucoseEntry.isUploadedToNS = true
+        glucoseEntry.isUploadedToHealth = true
+        glucoseEntry.isUploadedToTidepool = true
+    }
+}
+
 extension JSONImporter {
     func importGlucoseHistoryIfNeeded() async {}
 }

+ 0 - 4
Trio.xcodeproj/project.pbxproj

@@ -104,7 +104,6 @@
 		383420D625FFE38C002D46C1 /* LoopView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 383420D525FFE38C002D46C1 /* LoopView.swift */; };
 		383420D925FFEB3F002D46C1 /* Popup.swift in Sources */ = {isa = PBXBuildFile; fileRef = 383420D825FFEB3F002D46C1 /* Popup.swift */; };
 		383948D625CD4D8900E91849 /* FileStorage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 383948D525CD4D8900E91849 /* FileStorage.swift */; };
-		383948DA25CD64D500E91849 /* Glucose.swift in Sources */ = {isa = PBXBuildFile; fileRef = 383948D925CD64D500E91849 /* Glucose.swift */; };
 		384E803425C385E60086DB71 /* JavaScriptWorker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 384E803325C385E60086DB71 /* JavaScriptWorker.swift */; };
 		384E803825C388640086DB71 /* Script.swift in Sources */ = {isa = PBXBuildFile; fileRef = 384E803725C388640086DB71 /* Script.swift */; };
 		38569347270B5DFB0002C50D /* CGMType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 38569344270B5DFA0002C50D /* CGMType.swift */; };
@@ -922,7 +921,6 @@
 		383420D525FFE38C002D46C1 /* LoopView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoopView.swift; sourceTree = "<group>"; };
 		383420D825FFEB3F002D46C1 /* Popup.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Popup.swift; sourceTree = "<group>"; };
 		383948D525CD4D8900E91849 /* FileStorage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileStorage.swift; sourceTree = "<group>"; };
-		383948D925CD64D500E91849 /* Glucose.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Glucose.swift; sourceTree = "<group>"; };
 		384E803325C385E60086DB71 /* JavaScriptWorker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JavaScriptWorker.swift; sourceTree = "<group>"; };
 		384E803725C388640086DB71 /* Script.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Script.swift; sourceTree = "<group>"; };
 		38569344270B5DFA0002C50D /* CGMType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CGMType.swift; sourceTree = "<group>"; };
@@ -2321,7 +2319,6 @@
 				3811DF0125CA9FEA00A708ED /* Credentials.swift */,
 				E592A36F2CEEC01E009A472C /* ContactTrickEntry.swift */,
 				38AEE73C25F0200C0013F05B /* TrioSettings.swift */,
-				383948D925CD64D500E91849 /* Glucose.swift */,
 				382C133625F13A1E00715CE1 /* InsulinSensitivities.swift */,
 				38887CCD25F5725200944304 /* IOBEntry.swift */,
 				DD68889C2C386E17006E3C44 /* NightscoutExercise.swift */,
@@ -4061,7 +4058,6 @@
 				DDBD53FC2DAA903100F940A6 /* OverviewStepView.swift in Sources */,
 				38A0364225ED069400FCBB52 /* TempBasal.swift in Sources */,
 				3811DE1725C9D40400A708ED /* Screen.swift in Sources */,
-				383948DA25CD64D500E91849 /* Glucose.swift in Sources */,
 				CE94598029E9E3BD0047C9C6 /* WatchConfigDataFlow.swift in Sources */,
 				388E596C25AD95110019842D /* OpenAPS.swift in Sources */,
 				E00EEC0527368630002FF094 /* StorageAssembly.swift in Sources */,

+ 7 - 5
Trio/Sources/Models/BloodGlucose.swift

@@ -79,11 +79,13 @@ struct BloodGlucose: JSON, Identifiable, Hashable, Codable {
         let container = try decoder.container(keyedBy: CodingKeys.self)
         _id = try container.decode(String.self, forKey: ._id)
 
-        do {
-            sgv = try container.decode(Int.self, forKey: .sgv)
-        } catch {
-            // The nightscout API returns a double instead of an int
-            sgv = Int(try container.decode(Double.self, forKey: .sgv))
+        sgv = try? container.decodeIfPresent(Int.self, forKey: .sgv)
+        if sgv == nil {
+            // The nightscout API might return a double instead of an int, or the key might be missing
+            if let doubleValue = try? container.decodeIfPresent(Double.self, forKey: .sgv) {
+                sgv = Int(doubleValue)
+            }
+            // If both attempts fail, sgv remains nil
         }
 
         direction = try container.decodeIfPresent(Direction.self, forKey: .direction)

+ 2 - 2
TrioTests/JSONImporterTests.swift

@@ -44,9 +44,9 @@ class BundleReference {}
 
         #expect(allReadings.count == 274)
         #expect(allReadings.first?.glucose == 115)
-        #expect(allReadings.first?.date == Date(timeIntervalSince1970: 1_745_868_771.726578))
+        #expect(allReadings.first?.date == Date("2025-04-28T19:32:51.727Z"))
         #expect(allReadings.last?.glucose == 127)
-        #expect(allReadings.last?.date == Date(timeIntervalSince1970: 1_745_782_670.3270996))
+        #expect(allReadings.last?.date == Date("2025-04-27T19:37:50.327Z"))
 
         let manualCount = allReadings.filter({ $0.isManual }).count
         #expect(manualCount == 1)