Browse Source

Add glucose import logic

polscm32 aka Marvout 1 year ago
parent
commit
62c5eb62a5

+ 4 - 2
FreeAPS/Sources/Application/FreeAPSApp.swift

@@ -74,9 +74,11 @@ import Swinject
             let importer = JSONImporter(context: coreDataStack.newTaskContext())
             async let importPumpHistory: () = importer.importPumpHistoryIfNeeded()
             async let importCarbHistory: () = importer.importCarbHistoryIfNeeded()
-            
+            async let importGlucoseHistory: () = importer.importGlucoseHistoryIfNeeded()
+
             await importPumpHistory
             await importCarbHistory
+            await importGlucoseHistory
         }
     }
 
@@ -88,7 +90,7 @@ import Swinject
                 .environmentObject(Icons())
                 .onOpenURL(perform: handleURL)
         }
-        .onChange(of: scenePhase) { oldScenePhase, newScenePhase in
+        .onChange(of: scenePhase) { _, newScenePhase in
             debug(.default, "APPLICATION PHASE: \(newScenePhase)")
 
             /// If the App goes to the background we should ensure that all the changes are saved from the viewContext to the Persistent Container

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

@@ -111,6 +111,56 @@ extension NSPredicate {
     }
 }
 
+struct GlucoseEntryDTO: Decodable, ImportableDTO {
+    var id: UUID?
+    var date: Date?
+    var glucose: Int
+    var direction: String?
+    var isManual: Bool?
+
+    // Custom initializer to handle numeric dates
+    init(from decoder: Decoder) throws {
+        let container = try decoder.container(keyedBy: CodingKeys.self)
+        id = try container.decodeIfPresent(UUID.self, forKey: .id)
+        glucose = try container.decode(Int.self, forKey: .glucose)
+        direction = try container.decodeIfPresent(String.self, forKey: .direction)
+        isManual = try container.decodeIfPresent(Bool.self, forKey: .isManual) ?? false
+
+        // Handle numeric date
+        if let timestamp = try? container.decode(Double.self, forKey: .date) {
+            // Assuming the timestamp is in milliseconds
+            date = Date(timeIntervalSince1970: timestamp / 1000)
+        } else {
+            date = nil
+        }
+    }
+
+    enum CodingKeys: String, CodingKey {
+        case id
+        case date
+        case glucose
+        case direction
+        case isManual
+    }
+
+    // Conformance to ImportableDTO
+    typealias ManagedObject = GlucoseStored
+
+    func store(in context: NSManagedObjectContext) -> GlucoseStored {
+        let glucoseEntry = GlucoseStored(context: context)
+        glucoseEntry.id = id ?? UUID()
+        glucoseEntry.date = date ?? Date()
+        glucoseEntry.glucose = Int16(glucose)
+        glucoseEntry.direction = direction
+        glucoseEntry.isManual = isManual ?? false
+        glucoseEntry.isUploadedToNS = false
+        glucoseEntry.isUploadedToHealth = false
+        glucoseEntry.isUploadedToTidepool = false
+
+        return glucoseEntry
+    }
+}
+
 extension GlucoseStored: Encodable {
     enum CodingKeys: String, CodingKey {
         case date

+ 9 - 0
Model/MigrationScript.swift

@@ -108,4 +108,13 @@ extension JSONImporter {
             dateDecodingStrategy: .iso8601
         )
     }
+
+    func importGlucoseHistoryIfNeeded() async {
+        await importDataIfNeeded(
+            userDefaultsKey: "glucoseHistoryImported",
+            filePathComponent: OpenAPS.Monitor.glucose,
+            dtoType: GlucoseEntryDTO.self,
+            dateDecodingStrategy: .iso8601
+        )
+    }
 }