Procházet zdrojové kódy

Bug fix for empty glucose data

(cherry picked from commit c74ab7c24148ab55351c3709528f6f3ce01dcbd6)
(cherry picked from commit 421793cb4ba154e5df70647d8c1371885c8dd380)
Jon Mårtensson před 2 roky
rodič
revize
6c052a348e
1 změnil soubory, kde provedl 65 přidání a 62 odebrání
  1. 65 62
      FreeAPS/Sources/Modules/Stat/View/ChartsView.swift

+ 65 - 62
FreeAPS/Sources/Modules/Stat/View/ChartsView.swift

@@ -215,81 +215,84 @@ struct ChartsView: View {
         VStack(alignment: .leading, spacing: 20) {
             let glucose = fetchRequest
             let mapGlucose = glucose.compactMap({ each in each.glucose })
-            let mapGlucoseAcuteLow = mapGlucose.filter({ $0 < Int16(3.3 / 0.0555) })
-            let mapGlucoseHigh = mapGlucose.filter({ $0 > Int16(11 / 0.0555) })
-            let mapGlucoseNormal = mapGlucose.filter({ $0 > Int16(3.8 / 0.0555) && $0 < Int16(7.9 / 0.0555) })
+            if !mapGlucose.isEmpty {
+                let mapGlucoseAcuteLow = mapGlucose.filter({ $0 < Int16(3.3 / 0.0555) })
+                let mapGlucoseHigh = mapGlucose.filter({ $0 > Int16(11 / 0.0555) })
+                let mapGlucoseNormal = mapGlucose.filter({ $0 > Int16(3.8 / 0.0555) && $0 < Int16(7.9 / 0.0555) })
 
-            HStack {
-                let value = Double(mapGlucoseHigh.count * 100 / mapGlucose.count)
-                if value != 0 {
-                    Text(units == .mmolL ? ">  11  " : ">  200 ").foregroundColor(.secondary)
-                    Text(value.formatted()).foregroundColor(.orange)
-                    Text("%").foregroundColor(.secondary)
-                }
-            }.font(.caption)
+                HStack {
+                    let value = Double(mapGlucoseHigh.count * 100 / mapGlucose.count)
+                    if value != 0 {
+                        Text(units == .mmolL ? ">  11  " : ">  200 ").foregroundColor(.secondary)
+                        Text(value.formatted()).foregroundColor(.orange)
+                        Text("%").foregroundColor(.secondary)
+                    }
+                }.font(.caption)
 
-            HStack {
-                let value = Double(mapGlucoseNormal.count * 100 / mapGlucose.count)
-                if value != 0 {
-                    Text(units == .mmolL ? "3.9-7.8" : "70-140").foregroundColor(.secondary)
-                    Text(value.formatted()).foregroundColor(.green)
-                    Text("%").foregroundColor(.secondary)
-                }
-            }.font(.caption)
+                HStack {
+                    let value = Double(mapGlucoseNormal.count * 100 / mapGlucose.count)
+                    if value != 0 {
+                        Text(units == .mmolL ? "3.9-7.8" : "70-140").foregroundColor(.secondary)
+                        Text(value.formatted()).foregroundColor(.green)
+                        Text("%").foregroundColor(.secondary)
+                    }
+                }.font(.caption)
 
-            HStack {
-                let value = Double(mapGlucoseAcuteLow.count * 100 / mapGlucose.count)
-                if value != 0 {
-                    Text(units == .mmolL ? "<  3.3 " : "<  59  ").foregroundColor(.secondary)
-                    Text(value.formatted()).foregroundColor(.red)
-                    Text("%").foregroundColor(.secondary)
-                }
-            }.font(.caption)
+                HStack {
+                    let value = Double(mapGlucoseAcuteLow.count * 100 / mapGlucose.count)
+                    if value != 0 {
+                        Text(units == .mmolL ? "<  3.3 " : "<  59  ").foregroundColor(.secondary)
+                        Text(value.formatted()).foregroundColor(.red)
+                        Text("%").foregroundColor(.secondary)
+                    }
+                }.font(.caption)
+            }
         }
     }
 
     var groupedGlucoseStatsLaying: some View {
         HStack {
             let glucose = fetchRequest
-
             let mapGlucose = glucose.compactMap({ each in each.glucose })
-            let mapGlucoseLow = mapGlucose.filter({ $0 < Int16(3.3 / 0.0555) })
-            let mapGlucoseAcuteLow = mapGlucose.filter({ $0 < Int16(2.6 / 0.0555) })
+            if !mapGlucose.isEmpty {
+                let mapGlucoseLow = mapGlucose.filter({ $0 < Int16(3.3 / 0.0555) })
+                let mapGlucoseAcuteLow = mapGlucose.filter({ $0 < Int16(2.6 / 0.0555) })
 
-            let mapGlucoseHigh = mapGlucose.filter({ $0 > Int(7.8 / 0.0555) })
-            let mapGlucoseAcuteHigh = mapGlucose.filter({ $0 > Int16(11 / 0.0555) })
+                let mapGlucoseHigh = mapGlucose.filter({ $0 > Int(7.8 / 0.0555) })
+                let mapGlucoseAcuteHigh = mapGlucose.filter({ $0 > Int16(11 / 0.0555) })
 
-            HStack {
-                let value = Double(mapGlucoseAcuteLow.count * 100 / mapGlucose.count)
-                if value != 0 {
-                    Text(units == .mmolL ? "< 2.6" : "< 47").font(.caption2).foregroundColor(.secondary)
-                    Text(value.formatted()).font(.caption).foregroundColor(value == 0 ? .green : .red)
-                    Text("%").font(.caption)
-                }
-            }.padding(.horizontal, 10)
-            HStack {
-                let value = Double(mapGlucoseLow.count * 100 / mapGlucose.count)
-                if value != 0 {
-                    Text(units == .mmolL ? "< 3.3" : "< 59").font(.caption2).foregroundColor(.secondary)
-                    Text(value.formatted()).font(.caption).foregroundColor(value == 0 ? .green : .orange)
-                    Text("%").font(.caption)
-                }
-            }
-            Spacer()
-            HStack {
-                let value = Double(mapGlucoseHigh.count * 100 / mapGlucose.count)
-                if value != 0 {
-                    Text(units == .mmolL ? "> 7.8" : "> 140").font(.caption).foregroundColor(.secondary)
-                    Text(value.formatted()).font(.caption).foregroundColor(value == 0 ? .green : .orange)
-                    Text("%").font(.caption)
+                HStack {
+                    let value = Double(mapGlucoseAcuteLow.count * 100 / mapGlucose.count)
+                    if value != 0 {
+                        Text(units == .mmolL ? "< 2.6" : "< 47").font(.caption2).foregroundColor(.secondary)
+                        Text(value.formatted()).font(.caption).foregroundColor(value == 0 ? .green : .red)
+                        Text("%").font(.caption)
+                    }
+                }.padding(.horizontal, 10)
+                HStack {
+                    let value = Double(mapGlucoseLow.count * 100 / mapGlucose.count)
+                    if value != 0 {
+                        Text(units == .mmolL ? "< 3.3" : "< 59").font(.caption2).foregroundColor(.secondary)
+                        Text(value.formatted()).font(.caption).foregroundColor(value == 0 ? .green : .orange)
+                        Text("%").font(.caption)
+                    }
                 }
-            }.padding(.horizontal, 10)
-            HStack {
-                let value = Double(mapGlucoseAcuteHigh.count * 100 / mapGlucose.count)
-                if value != 0 {
-                    Text(units == .mmolL ? "> 11.0" : "> 216").font(.caption).foregroundColor(.secondary)
-                    Text(value.formatted()).font(.caption).foregroundColor(value == 0 ? .green : .red)
-                    Text("%").font(.caption)
+                Spacer()
+                HStack {
+                    let value = Double(mapGlucoseHigh.count * 100 / mapGlucose.count)
+                    if value != 0 {
+                        Text(units == .mmolL ? "> 7.8" : "> 140").font(.caption).foregroundColor(.secondary)
+                        Text(value.formatted()).font(.caption).foregroundColor(value == 0 ? .green : .orange)
+                        Text("%").font(.caption)
+                    }
+                }.padding(.horizontal, 10)
+                HStack {
+                    let value = Double(mapGlucoseAcuteHigh.count * 100 / mapGlucose.count)
+                    if value != 0 {
+                        Text(units == .mmolL ? "> 11.0" : "> 216").font(.caption).foregroundColor(.secondary)
+                        Text(value.formatted()).font(.caption).foregroundColor(value == 0 ? .green : .red)
+                        Text("%").font(.caption)
+                    }
                 }
             }
         }