소스 검색

Merge pull request #192 from bjorkert/bgspeak_repeat_safeguard

Stores the time of the last speech announcement to prevent repeated a…
Marion Barker 3 년 전
부모
커밋
fb71c662f9
2개의 변경된 파일20개의 추가작업 그리고 1개의 파일을 삭제
  1. 16 1
      LoopFollow/Controllers/Alarms.swift
  2. 4 0
      LoopFollow/ViewControllers/MainViewController.swift

+ 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()