Преглед изворни кода

set end of cone dynamically with a min of 2h, fix thread error

polscm32 пре 1 година
родитељ
комит
10a3ab8c1b

+ 5 - 4
FreeAPS/Sources/Modules/Bolus/BolusStateModel.swift

@@ -107,6 +107,7 @@ extension Bolus {
 
         @Published var minForecast: [Int] = []
         @Published var maxForecast: [Int] = []
+        @Published var minCount: Int = 12 // count of Forecasts drawn in 5 min distances, i.e. 12 means a min of 1 hour
 
         let now = Date.now
 
@@ -802,14 +803,14 @@ extension Bolus.StateModel {
             return
         }
 
-        let maxCount = min(36, nonEmptyArrays.map(\.count).max() ?? 0)
-        guard maxCount > 0 else { return }
+        minCount = min(12, nonEmptyArrays.map(\.count).min() ?? 0)
+        guard minCount > 0 else { return }
 
-        minForecast = (0 ..< maxCount).map { index in
+        minForecast = (0 ..< minCount).map { index in
             nonEmptyArrays.compactMap { $0.indices.contains(index) ? $0[index] : nil }.min() ?? 0
         }
 
-        maxForecast = (0 ..< maxCount).map { index in
+        maxForecast = (0 ..< minCount).map { index in
             nonEmptyArrays.compactMap { $0.indices.contains(index) ? $0[index] : nil }.max() ?? 0
         }
     }

+ 4 - 1
FreeAPS/Sources/Modules/Bolus/View/ForeCastChart.swift

@@ -9,7 +9,10 @@ struct ForeCastChart: View {
     @Binding var units: GlucoseUnits
 
     @State private var startMarker = Date(timeIntervalSinceNow: -4 * 60 * 60)
-    @State private var endMarker = Date(timeIntervalSinceNow: 3 * 60 * 60)
+
+    private var endMarker: Date {
+        Date(timeIntervalSinceNow: TimeInterval(2 * 5 * state.minCount * 60)) // min is 2h -> (2*1h = 2*(5*12*60))
+    }
 
     var body: some View {
         VStack {

+ 14 - 12
FreeAPS/Sources/Modules/Home/HomeStateModel.swift

@@ -84,6 +84,7 @@ extension Home {
 
         @Published var minForecast: [Int] = []
         @Published var maxForecast: [Int] = []
+        @Published var minCount: Int = 12 // count of Forecasts drawn in 5 min distances, i.e. 12 means a min of 1 hour
 
         let context = CoreDataStack.shared.newTaskContext()
         let viewContext = CoreDataStack.shared.persistentContainer.viewContext
@@ -955,18 +956,19 @@ extension Home.StateModel {
         for data: (id: UUID, forecastID: NSManagedObjectID, forecastValueIDs: [NSManagedObjectID]),
         in context: NSManagedObjectContext
     ) async -> (UUID, Forecast?, [ForecastValue]) {
-        // Initialize the variables for the forecast and forecast values
         var forecast: Forecast?
         var forecastValues: [ForecastValue] = []
 
         do {
-            // Try to fetch the forecast object
-            forecast = try context.existingObject(with: data.forecastID) as? Forecast
-
-            // Fetch the forecast values, limiting to the first 36 values, i.e. 3 hours from the current time
-            for forecastValueID in data.forecastValueIDs.prefix(36) {
-                if let forecastValue = try context.existingObject(with: forecastValueID) as? ForecastValue {
-                    forecastValues.append(forecastValue)
+            try await context.perform {
+                // Fetch the forecast object
+                forecast = try context.existingObject(with: data.forecastID) as? Forecast
+
+                // Fetch the forecast values
+                for forecastValueID in data.forecastValueIDs.prefix(36) {
+                    if let forecastValue = try context.existingObject(with: forecastValueID) as? ForecastValue {
+                        forecastValues.append(forecastValue)
+                    }
                 }
             }
         } catch {
@@ -1012,15 +1014,15 @@ extension Home.StateModel {
             return
         }
 
-        let maxCount = min(36, allForecastValues.map(\.count).max() ?? 0)
-        guard maxCount > 0 else { return }
+        minCount = min(12, allForecastValues.map(\.count).min() ?? 0)
+        guard minCount > 0 else { return }
 
         // Calculate min and max forecast values
-        minForecast = (0 ..< maxCount).map { index in
+        minForecast = (0 ..< minCount).map { index in
             allForecastValues.compactMap { $0.indices.contains(index) ? Int($0[index].value) : nil }.min() ?? 0
         }
 
-        maxForecast = (0 ..< maxCount).map { index in
+        maxForecast = (0 ..< minCount).map { index in
             allForecastValues.compactMap { $0.indices.contains(index) ? Int($0[index].value) : nil }.max() ?? 0
         }
     }

+ 5 - 3
FreeAPS/Sources/Modules/Home/View/Chart/MainChartView.swift

@@ -60,8 +60,6 @@ struct MainChartView: View {
     @State private var count: Decimal = 1
     @State private var startMarker =
         Date(timeIntervalSince1970: TimeInterval(NSDate().timeIntervalSince1970 - 60 * 60 * 24)) // 24 hours
-    @State private var endMarker =
-        Date(timeIntervalSince1970: TimeInterval(NSDate().timeIntervalSince1970 + 60 * 60 * 3)) // 3 hours
     @State private var minValue: Decimal = 45
     @State private var maxValue: Decimal = 270
     @State private var selection: Date? = nil
@@ -77,6 +75,10 @@ struct MainChartView: View {
     @Environment(\.colorScheme) var colorScheme
     @Environment(\.calendar) var calendar
 
+    private var endMarker: Date {
+        Date(timeIntervalSinceNow: TimeInterval(2 * 5 * state.minCount * 60)) // min is 2h -> (2*1h = 2*(5*12*60))
+    }
+
     private var bolusFormatter: NumberFormatter {
         let formatter = NumberFormatter()
         formatter.numberStyle = .decimal
@@ -977,7 +979,7 @@ extension MainChartView {
     /// update start and  end marker to fix scroll update problem with x axis
     private func updateStartEndMarkers() {
         startMarker = Date(timeIntervalSince1970: TimeInterval(NSDate().timeIntervalSince1970 - 86400))
-        endMarker = Date(timeIntervalSince1970: TimeInterval(NSDate().timeIntervalSince1970 + 10800))
+//        endMarker = Date(timeIntervalSince1970: TimeInterval(NSDate().timeIntervalSince1970 + 10800))
     }
 
     private func calculateBasals() {