polscm32 2 лет назад
Родитель
Сommit
b469e2e1c3

+ 17 - 11
FreeAPS/Sources/APS/Storage/GlucoseStorage.swift

@@ -66,28 +66,34 @@ final class BaseGlucoseStorage: GlucoseStorage, Injectable {
                     }
                 }
 
-                // MARK: Save to CoreData.
+                // MARK: - Save to CoreData.
 
                 var bg_ = 0
-                var bgDate = Date()
-                var id = ""
                 var direction = ""
 
                 if glucose.isNotEmpty {
                     bg_ = glucose[0].glucose ?? 0
-                    bgDate = glucose[0].dateString
-                    id = glucose[0].id
                     direction = glucose[0].direction?.symbol ?? "↔︎"
                 }
 
                 if bg_ != 0 {
                     self.coredataContext.perform {
-                        let dataForForStats = Readings(context: self.coredataContext)
-                        dataForForStats.date = bgDate
-                        dataForForStats.glucose = Int16(bg_)
-                        dataForForStats.id = id
-                        dataForForStats.direction = direction
-                        try? self.coredataContext.save()
+                        let newItem = GlucoseStored(context: self.coredataContext)
+                        newItem.id = UUID()
+                        newItem.glucose = Int16(bg_)
+                        newItem.date = Date()
+                        newItem.direction = direction
+
+                        do {
+                            try self.coredataContext.save()
+                            debugPrint(
+                                "Glucose Storage: \(CoreDataStack.identifier) \(DebuggingIdentifiers.succeeded) saved glucose to core data"
+                            )
+                        } catch {
+                            debugPrint(
+                                "Glucose Storage: \(CoreDataStack.identifier) \(DebuggingIdentifiers.failed) failed to save glucose to core data"
+                            )
+                        }
                     }
                 }
             }

+ 40 - 37
FreeAPS/Sources/Modules/Home/View/Chart/MainChartView.swift

@@ -103,6 +103,13 @@ struct MainChartView: View {
         animation: Animation.bouncy
     ) var insulinFromPersistence: FetchedResults<InsulinStored>
 
+    @FetchRequest(
+        entity: GlucoseStored.entity(),
+        sortDescriptors: [NSSortDescriptor(keyPath: \GlucoseStored.date, ascending: true)],
+        predicate: NSPredicate.predicateForOneDayAgo,
+        animation: Animation.bouncy
+    ) var glucoseFromPersistence: FetchedResults<GlucoseStored>
+
     private var bolusFormatter: NumberFormatter {
         let formatter = NumberFormatter()
         formatter.numberStyle = .decimal
@@ -412,44 +419,40 @@ extension MainChartView {
     private func drawGlucose() -> some ChartContent {
         /// glucose point mark
         /// filtering for high and low bounds in settings
-        ForEach(glucose) { item in
-            if let sgv = item.sgv {
-                let sgvLimited = max(sgv, 0)
-
-                if smooth {
-                    if sgvLimited > Int(highGlucose) {
-                        PointMark(
-                            x: .value("Time", item.dateString, unit: .second),
-                            y: .value("Value", Decimal(sgvLimited) * conversionFactor)
-                        ).foregroundStyle(Color.orange.gradient).symbolSize(25).interpolationMethod(.cardinal)
-                    } else if sgvLimited < Int(lowGlucose) {
-                        PointMark(
-                            x: .value("Time", item.dateString, unit: .second),
-                            y: .value("Value", Decimal(sgvLimited) * conversionFactor)
-                        ).foregroundStyle(Color.red.gradient).symbolSize(25).interpolationMethod(.cardinal)
-                    } else {
-                        PointMark(
-                            x: .value("Time", item.dateString, unit: .second),
-                            y: .value("Value", Decimal(sgvLimited) * conversionFactor)
-                        ).foregroundStyle(Color.green.gradient).symbolSize(25).interpolationMethod(.cardinal)
-                    }
+        ForEach(glucoseFromPersistence) { item in
+            if smooth {
+                if item.glucose > Int(highGlucose) {
+                    PointMark(
+                        x: .value("Time", item.date ?? Date(), unit: .second),
+                        y: .value("Value", Decimal(item.glucose) * conversionFactor)
+                    ).foregroundStyle(Color.orange.gradient).symbolSize(25).interpolationMethod(.cardinal)
+                } else if item.glucose < Int(lowGlucose) {
+                    PointMark(
+                        x: .value("Time", item.date ?? Date(), unit: .second),
+                        y: .value("Value", Decimal(item.glucose) * conversionFactor)
+                    ).foregroundStyle(Color.red.gradient).symbolSize(25).interpolationMethod(.cardinal)
                 } else {
-                    if sgvLimited > Int(highGlucose) {
-                        PointMark(
-                            x: .value("Time", item.dateString, unit: .second),
-                            y: .value("Value", Decimal(sgvLimited) * conversionFactor)
-                        ).foregroundStyle(Color.orange.gradient).symbolSize(25)
-                    } else if sgvLimited < Int(lowGlucose) {
-                        PointMark(
-                            x: .value("Time", item.dateString, unit: .second),
-                            y: .value("Value", Decimal(sgvLimited) * conversionFactor)
-                        ).foregroundStyle(Color.red.gradient).symbolSize(25)
-                    } else {
-                        PointMark(
-                            x: .value("Time", item.dateString, unit: .second),
-                            y: .value("Value", Decimal(sgvLimited) * conversionFactor)
-                        ).foregroundStyle(Color.green.gradient).symbolSize(25)
-                    }
+                    PointMark(
+                        x: .value("Time", item.date ?? Date(), unit: .second),
+                        y: .value("Value", Decimal(item.glucose) * conversionFactor)
+                    ).foregroundStyle(Color.green.gradient).symbolSize(25).interpolationMethod(.cardinal)
+                }
+            } else {
+                if item.glucose > Int(highGlucose) {
+                    PointMark(
+                        x: .value("Time", item.date ?? Date(), unit: .second),
+                        y: .value("Value", Decimal(item.glucose) * conversionFactor)
+                    ).foregroundStyle(Color.orange.gradient).symbolSize(25)
+                } else if item.glucose < Int(lowGlucose) {
+                    PointMark(
+                        x: .value("Time", item.date ?? Date(), unit: .second),
+                        y: .value("Value", Decimal(item.glucose) * conversionFactor)
+                    ).foregroundStyle(Color.red.gradient).symbolSize(25)
+                } else {
+                    PointMark(
+                        x: .value("Time", item.date ?? Date(), unit: .second),
+                        y: .value("Value", Decimal(item.glucose) * conversionFactor)
+                    ).foregroundStyle(Color.green.gradient).symbolSize(25)
                 }
             }
         }

+ 7 - 0
FreeAPS/Sources/Modules/Home/View/HomeRootView.swift

@@ -63,6 +63,13 @@ extension Home {
             sortDescriptors: [NSSortDescriptor(key: "date", ascending: false)]
         ) var enactedSliderTT: FetchedResults<TempTargetsSlider>
 
+        @FetchRequest(
+            entity: GlucoseStored.entity(),
+            sortDescriptors: [NSSortDescriptor(keyPath: \GlucoseStored.date, ascending: true)],
+            predicate: NSPredicate.predicateFor30MinAgo,
+            animation: Animation.bouncy
+        ) var glucoseFromPersistence: FetchedResults<GlucoseStored>
+
         var bolusProgressFormatter: NumberFormatter {
             let formatter = NumberFormatter()
             formatter.numberStyle = .decimal

+ 6 - 0
Model/Core_Data.xcdatamodeld/Core_Data.xcdatamodel/contents

@@ -42,6 +42,12 @@
         <attribute name="enteredBy" optional="YES" attributeType="String"/>
         <attribute name="fat" optional="YES" attributeType="Decimal" defaultValueString="0.0"/>
     </entity>
+    <entity name="GlucoseStored" representedClassName="GlucoseStored" syncable="YES" codeGenerationType="class">
+        <attribute name="date" optional="YES" attributeType="Date" usesScalarValueType="NO"/>
+        <attribute name="direction" optional="YES" attributeType="String"/>
+        <attribute name="glucose" optional="YES" attributeType="Integer 16" defaultValueString="0" usesScalarValueType="YES"/>
+        <attribute name="id" optional="YES" attributeType="UUID" usesScalarValueType="NO"/>
+    </entity>
     <entity name="HbA1c" representedClassName="HbA1c" syncable="YES" codeGenerationType="class">
         <attribute name="date" optional="YES" attributeType="Date" usesScalarValueType="NO"/>
         <attribute name="hba1c" optional="YES" attributeType="Decimal" defaultValueString="0.0"/>