Browse Source

Refresh clears predictions

Jonas Björkert 2 years ago
parent
commit
610cb13b5c

+ 3 - 3
LoopFollow/Controllers/Nightscout/DeviceStatus.swift

@@ -124,12 +124,12 @@ extension MainViewController {
             }
         }
         
-        // Loop
+        // Loop - handle new data
         if let lastLoopRecord = lastDeviceStatus?["loop"] as! [String : AnyObject]? {
             DeviceStatusLoop(formatter: formatter, lastLoopRecord: lastLoopRecord)
         }
-        
-        // Trio
+
+        // OpenAPS - handle new data
         if let lastLoopRecord = lastDeviceStatus?["openaps"] as! [String : AnyObject]? {
             DeviceStatusOpenAPS(formatter: formatter, lastDeviceStatus: lastDeviceStatus, lastLoopRecord: lastLoopRecord)
         }

+ 0 - 16
LoopFollow/Controllers/Nightscout/DeviceStatusLoop.swift

@@ -12,22 +12,6 @@ import Charts
 
 extension MainViewController {
     func DeviceStatusLoop(formatter: ISO8601DateFormatter, lastLoopRecord: [String: AnyObject]) {
-        // Check if OpenAPS prediction data exists and clear it if necessary
-        let openAPSDataIndices = [12, 13, 14, 15]
-        
-        for dataIndex in openAPSDataIndices {
-            let mainChart = BGChart.lineData!.dataSets[dataIndex] as! LineChartDataSet
-            let smallChart = BGChartFull.lineData!.dataSets[dataIndex] as! LineChartDataSet
-            
-            if !mainChart.entries.isEmpty || !smallChart.entries.isEmpty {
-                updatePredictionGraphGeneric(
-                    dataIndex: dataIndex,
-                    predictionData: [],
-                    chartLabel: "",
-                    color: UIColor.systemGray
-                )
-            }
-        }
         
         if let lastLoopTime = formatter.date(from: (lastLoopRecord["timestamp"] as! String))?.timeIntervalSince1970  {
             UserDefaultsRepository.alertLastLoopTime.value = lastLoopTime

+ 0 - 5
LoopFollow/Controllers/Nightscout/DeviceStatusOpenAPS.swift

@@ -11,11 +11,6 @@ import UIKit
 
 extension MainViewController {
     func DeviceStatusOpenAPS(formatter: ISO8601DateFormatter, lastDeviceStatus: [String: AnyObject]?, lastLoopRecord: [String: AnyObject]) {
-        // Check if Loop prediction data exists and clear it if necessary
-        if !predictionData.isEmpty {
-            predictionData.removeAll()
-            updatePredictionGraph()
-        }
         
         if let lastLoopTime = formatter.date(from: (lastDeviceStatus?["created_at"] as! String))?.timeIntervalSince1970  {
             UserDefaultsRepository.alertLastLoopTime.value = lastLoopTime

+ 24 - 0
LoopFollow/ViewControllers/MainViewController.swift

@@ -274,6 +274,30 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
     // Clean all timers and start new ones when refreshing
     @objc func refresh() {
         print("Refreshing")
+
+        // Clear prediction for both Loop or OpenAPS
+
+        // Check if Loop prediction data exists and clear it if necessary
+        if !predictionData.isEmpty {
+            predictionData.removeAll()
+            updatePredictionGraph()
+        }
+
+        // Check if OpenAPS prediction data exists and clear it if necessary
+        let openAPSDataIndices = [12, 13, 14, 15]
+        for dataIndex in openAPSDataIndices {
+            let mainChart = BGChart.lineData!.dataSets[dataIndex] as! LineChartDataSet
+            let smallChart = BGChartFull.lineData!.dataSets[dataIndex] as! LineChartDataSet
+            if !mainChart.entries.isEmpty || !smallChart.entries.isEmpty {
+                updatePredictionGraphGeneric(
+                    dataIndex: dataIndex,
+                    predictionData: [],
+                    chartLabel: "",
+                    color: UIColor.systemGray
+                )
+            }
+        }
+
         MinAgoText.text = "Refreshing"
         invalidateTimers()
         restartAllTimers()