Yakov Karpov 5 лет назад
Родитель
Сommit
7f30831881

+ 1 - 3
FreeAPS/Sources/Charts/Models/PredictionLineData.swift

@@ -3,7 +3,5 @@ import Foundation
 public struct PredictionLineData: Identifiable, Hashable {
     public var id = UUID()
     let type: PredictionType
-    let values: [BloodGlucose]
-
-    func count() -> Int { values.count }
+    var values: [BloodGlucose]
 }

+ 32 - 28
FreeAPS/Sources/Charts/Views/Charts/MainChartView.swift

@@ -4,7 +4,7 @@ struct MainChartView: View {
     let maxWidth: CGFloat
     @Binding var showHours: Int
     @Binding var glucoseData: [BloodGlucose]
-    @Binding var predictionsData: [PredictionLineData]?
+    @Binding var predictionsData: [PredictionLineData]
     var body: some View {
         let allValues = getAllValues()
         let minValue = allValues.min() ?? 40
@@ -41,34 +41,38 @@ extension MainChartView {
     }
 
     func getPredictionValues() -> [Int]? {
-        if let predictions = predictionsData {
-            return predictions.flatMap { prediction in
-                prediction.values.compactMap(\.sgv)
-            }
+        guard !predictionsData.isEmpty else {
+            return nil
         }
-        return nil
-    }
-}
-
-struct MainChartView_Previews: PreviewProvider {
-    static let glucoseData = Array(SampleData.sampleData[0 ... 70])
-    static let predictionsData = [
-        PredictionLineData(
-            type: .iob,
-            values: Array(SampleData.sampleData[0 ... 10])
-        ),
-        PredictionLineData(type: .cob, values: Array(SampleData.sampleData[1 ... 21])),
-        PredictionLineData(
-            type: .uam,
-            values: Array(SampleData.sampleData[21 ... 30])
-        ),
-        PredictionLineData(type: .zt, values: Array(SampleData.sampleData[31 ... 40]))
-    ]
-
-    static var previews: some View {
-        ScrollView(.horizontal) {
-            MainChartView(maxWidth: 400, showHours: 1, glucoseData: glucoseData, predictionsData: predictionsData)
+        return predictionsData.flatMap { prediction in
+            prediction.values.compactMap(\.sgv)
         }
-        .preferredColorScheme(/*@START_MENU_TOKEN@*/ .dark/*@END_MENU_TOKEN@*/)
     }
 }
+
+// struct MainChartView_Previews: PreviewProvider {
+//    static let glucoseData = Array(SampleData.sampleData[0 ... 70])
+//
+//    static let predictionsData = [
+//        PredictionLineData(
+//            type: .iob,
+//            values: Array(SampleData.sampleData[0 ... 10])
+//        ),
+//
+//        PredictionLineData(type: .cob, values: Array(SampleData.sampleData[1 ... 21])),
+//
+//        PredictionLineData(
+//            type: .uam,
+//            values: Array(SampleData.sampleData[21 ... 30])
+//        ),
+//
+//        PredictionLineData(type: .zt, values: Array(SampleData.sampleData[31 ... 40]))
+//    ]
+//
+//    static var previews: some View {
+//        ScrollView(.horizontal) {
+//            MainChartView(maxWidth: 400, showHours: 1, glucoseData: glucoseData, predictionsData: predictionsData)
+//        }
+//        .preferredColorScheme(/*@START_MENU_TOKEN@*/ .dark/*@END_MENU_TOKEN@*/)
+//    }
+// }

+ 22 - 20
FreeAPS/Sources/Charts/Views/Charts/PredictionsChartView.swift

@@ -4,30 +4,32 @@ public struct PredictionsChartView: View {
     let minValue: Int
     let maxValue: Int
     let maxWidth: CGFloat
-    @Binding var data: [PredictionLineData]?
+    @Binding var data: [PredictionLineData]
     @Binding var showHours: Int
 
+    var chartsData: some View {
+        ForEach(0 ..< data.count, id: \.self) { index -> AnyView in
+            HStack {
+                PointChartView(
+                    minValue: minValue,
+                    maxValue: maxValue,
+                    maxWidth: maxWidth,
+                    showHours: $showHours,
+                    glucoseData: $data[index].values
+                ) { value in
+                    PredictionPointView(
+                        predictionType: data[index].type,
+                        value: value
+                    )
+                }
+                Spacer()
+            }.asAny()
+        }
+    }
+
     public var body: some View {
         ZStack {
-            if data != nil {
-                ForEach(0 ..< data!.count, id: \.self) { index in
-                    HStack {
-                        PointChartView(
-                            minValue: minValue,
-                            maxValue: maxValue,
-                            maxWidth: maxWidth,
-                            showHours: $showHours,
-                            glucoseData: $data[index].values
-                        ) { value in
-                            PredictionPointView(
-                                predictionType: data?[index].type ?? .cob,
-                                value: value
-                            )
-                        }
-                        Spacer()
-                    }
-                }
-            }
+            chartsData
         }
     }
 }

+ 5 - 1
FreeAPS/Sources/Models/BloodGlucose.swift

@@ -16,7 +16,11 @@ struct BloodGlucose: JSON, Identifiable, Hashable {
         case rateOutOfRange = "RATE OUT OF RANGE"
     }
 
-    var id = UUID().uuidString
+    var _id = UUID().uuidString
+    var id: String {
+        _id
+    }
+
     var sgv: Int?
     let direction: Direction?
     let date: UInt64

+ 18 - 14
FreeAPS/Sources/Modules/Home/View/HomeRootView.swift

@@ -5,6 +5,22 @@ extension Home {
         @EnvironmentObject var viewModel: ViewModel<Provider>
         @State var showHours = 1
 
+        var chart: some View {
+            GeometryReader { geo in
+                ScrollView(.horizontal, showsIndicators: false) {
+                    PointChartView(
+                        minValue: 20,
+                        maxValue: 300,
+                        maxWidth: geo.size.width,
+                        showHours: $showHours,
+                        glucoseData: $viewModel.glucose
+                    ) { value in
+                        GlucosePointView(value: value)
+                    }
+                }
+            }
+        }
+
         var body: some View {
             viewModel.setFilteredGlucoseHours(hours: 24)
             return GeometryReader { geo in
@@ -13,20 +29,8 @@ extension Home {
                         Text("Header")
                     }
                     ScrollView(.vertical, showsIndicators: false) {
-                        HoursPickerView(selectedHour: $showHours)
-                        ScrollView(.horizontal, showsIndicators: false) {
-                            PointChartView(
-                                minValue: 20,
-                                maxValue: 300,
-                                maxWidth: geo.size.width,
-                                showHours: showHours,
-                                glucoseData: SampleData.sampleData
-                            ) { value in
-                                GlucosePointView(value: value)
-                            }
-                        }.frame(height: 300)
-
-                        // GlucoseChartView(glucose: $viewModel.glucose, suggestion: $viewModel.suggestion).frame(height: 150)
+                        // chart.frame(height: 300)
+                        GlucoseChartView(glucose: $viewModel.glucose, suggestion: $viewModel.suggestion).frame(height: 150)
                         if let reason = viewModel.suggestion?.reason {
                             Text(reason).font(.caption).padding()
                         }

+ 2 - 2
FreeAPS/Sources/Services/Network/NightscoutAPI.swift

@@ -73,8 +73,8 @@ extension NightscoutAPI {
         return service.run(request)
             .retry(Config.retryCount)
             .decode(type: [BloodGlucose].self, decoder: JSONCoding.decoder)
-            .map {
-                $0.filter { $0.isStateValid }
+            .map { glucose in
+                glucose
                     .map {
                         var reading = $0
                         reading.glucose = $0.sgv

+ 4 - 0
FreeAPS/Sources/Services/Network/NightscoutManager.swift

@@ -54,6 +54,10 @@ final class BaseNightscoutManager: NightscoutManager, Injectable {
 
         let since = glucoseStorage.syncDate()
         return nightscout.fetchLastGlucose(288, sinceDate: since)
+            .tryCatch({ (error) -> AnyPublisher<[BloodGlucose], Error> in
+                print(error.localizedDescription)
+                return Just([]).setFailureType(to: Error.self).eraseToAnyPublisher()
+            })
             .replaceError(with: [])
             .map {
                 self.glucoseStorage.storeGlucose($0)