polscm32 2 лет назад
Родитель
Сommit
06e5b07003
1 измененных файлов с 16 добавлено и 7 удалено
  1. 16 7
      FreeAPS/Sources/Modules/Home/View/Chart/MainChartView.swift

+ 16 - 7
FreeAPS/Sources/Modules/Home/View/Chart/MainChartView.swift

@@ -96,6 +96,13 @@ struct MainChartView: View {
         predicate: NSPredicate.predicateForOneDayAgo
         predicate: NSPredicate.predicateForOneDayAgo
     ) var carbsFromPersistence: FetchedResults<MealsStored>
     ) var carbsFromPersistence: FetchedResults<MealsStored>
 
 
+    @FetchRequest(
+        entity: InsulinStored.entity(),
+        sortDescriptors: [NSSortDescriptor(keyPath: \InsulinStored.date, ascending: true)],
+        predicate: NSPredicate.predicateForOneDayAgo,
+        animation: Animation.bouncy
+    ) var insulinFromPersistence: FetchedResults<InsulinStored>
+
     private var bolusFormatter: NumberFormatter {
     private var bolusFormatter: NumberFormatter {
         let formatter = NumberFormatter()
         let formatter = NumberFormatter()
         formatter.numberStyle = .decimal
         formatter.numberStyle = .decimal
@@ -345,21 +352,23 @@ extension MainChartView {
 extension MainChartView {
 extension MainChartView {
     private func drawBoluses() -> some ChartContent {
     private func drawBoluses() -> some ChartContent {
         /// smbs in triangle form
         /// smbs in triangle form
-        ForEach(boluses) { bolus in
-            let bolusAmount = bolus.amount ?? 0
-            let glucose = timeToNearestGlucose(time: bolus.timestamp.timeIntervalSince1970)
+        ForEach(insulinFromPersistence) { bolus in
+            let bolusAmount = bolus.amount ?? 0 as NSDecimalNumber
+            let bolusDate = bolus.date ?? Date()
+            let glucose = timeToNearestGlucose(time: bolusDate.timeIntervalSince1970)
             let yPosition = (Decimal(glucose.sgv ?? defaultBolusPosition) * conversionFactor) + bolusOffset
             let yPosition = (Decimal(glucose.sgv ?? defaultBolusPosition) * conversionFactor) + bolusOffset
-            let size = (Config.bolusSize + CGFloat(bolusAmount) * Config.bolusScale) * 1.8
+            let size = (Config.bolusSize + CGFloat(truncating: bolusAmount) * Config.bolusScale) * 1.8
 
 
-            return PointMark(
-                x: .value("Time", bolus.timestamp, unit: .second),
+            PointMark(
+                x: .value("Time", bolus.date ?? Date(), unit: .second),
                 y: .value("Value", yPosition)
                 y: .value("Value", yPosition)
             )
             )
             .symbol {
             .symbol {
                 Image(systemName: "arrowtriangle.down.fill").font(.system(size: size)).foregroundStyle(Color.insulin)
                 Image(systemName: "arrowtriangle.down.fill").font(.system(size: size)).foregroundStyle(Color.insulin)
             }
             }
             .annotation(position: .top) {
             .annotation(position: .top) {
-                Text(bolusFormatter.string(from: bolusAmount as NSNumber)!).font(.caption2)
+                Text(bolusFormatter.string(from: bolusAmount) ?? "")
+                    .font(.caption2)
                     .foregroundStyle(Color.insulin)
                     .foregroundStyle(Color.insulin)
             }
             }
         }
         }