Просмотр исходного кода

Enhance chart interaction by pausing auto-scroll on user scroll

Jonas Björkert 2 лет назад
Родитель
Сommit
74a5fda9eb

+ 4 - 1
LoopFollow/Controllers/Graphs.swift

@@ -527,7 +527,10 @@ extension MainViewController {
         }
         
         // Move to current reading everytime new readings load
-        BGChart.moveViewToAnimated(xValue: dateTimeUtils.getNowTimeIntervalUTC() - (BGChart.visibleXRange * 0.7), yValue: 0.0, axis: .right, duration: 1, easingOption: .easeInBack)
+        // Check if auto-scrolling should be performed
+        if autoScrollPauseUntil == nil || Date() > autoScrollPauseUntil! {
+            BGChart.moveViewToAnimated(xValue: dateTimeUtils.getNowTimeIntervalUTC() - (BGChart.visibleXRange * 0.7), yValue: 0.0, axis: .right, duration: 1, easingOption: .easeInBack)
+        }
     }
     
     func updatePredictionGraph(color: UIColor? = nil) {

+ 11 - 2
LoopFollow/ViewControllers/MainViewController.swift

@@ -148,6 +148,8 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
     // This is a temporary safeguard until the issue with multiple calls to speakBG is fixed.
     var lastSpeechTime: Date?
 
+    var autoScrollPauseUntil: Date? = nil
+
     override func viewDidLoad() {
         super.viewDidLoad()
 
@@ -824,6 +826,13 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
         
     }
     
-    
+    // User has scrolled the chart
+    func chartTranslated(_ chartView: ChartViewBase, dX: CGFloat, dY: CGFloat) {
+        let isViewingLatestData = abs(BGChart.highestVisibleX - BGChart.chartXMax) < 0.001
+        if isViewingLatestData {
+            autoScrollPauseUntil = nil // User is back at the latest data, allow auto-scrolling
+        } else {
+            autoScrollPauseUntil = Date().addingTimeInterval(5 * 60) // User is viewing historical data, pause auto-scrolling
+        }
+    }
 }
-