Przeglądaj źródła

Merge pull request #193 from jonfawcett/dev

Stores the time of the last speech announcement to prevent repeat
Marion Barker 3 lat temu
rodzic
commit
4c52ca5d0f

+ 16 - 1
LoopFollow/Controllers/Alarms.swift

@@ -847,8 +847,23 @@ extension MainViewController {
         
     }
     
-    
+    // Speaks the current blood glucose value and the change from the previous value.
+    // Repeated calls to the function within 30 seconds are prevented.
     func speakBG(currentValue: Int, previousValue: Int) {
+        // Get the current time
+        let currentTime = Date()
+
+        // Check if speakBG was called less than 30 seconds ago. If so, prevent repeated announcements and return.
+        // If `lastSpeechTime` is `nil` (i.e., this is the first time `speakBG` is being called), use `Date.distantPast` as the default
+        // value to ensure that the `guard` statement passes and the announcement is made.
+        guard currentTime.timeIntervalSince(lastSpeechTime ?? .distantPast) >= 30 else {
+            print("Repeated calls to speakBG detected!")
+            return
+        }
+
+        // Update the last speech time
+        self.lastSpeechTime = currentTime
+
         let bloodGlucoseDifference = currentValue - previousValue
         let negligibleThreshold = 3
         let differenceText: String

+ 4 - 0
LoopFollow/ViewControllers/MainViewController.swift

@@ -141,6 +141,10 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
     
     var snoozeTabItem: UITabBarItem = UITabBarItem()
     
+    // Stores the time of the last speech announcement to prevent repeated announcements.
+    // This is a temporary safeguard until the issue with multiple calls to speakBG is fixed.
+    var lastSpeechTime: Date?
+
     override func viewDidLoad() {
         super.viewDidLoad()