فهرست منبع

Add: stop alarm after 5 minutes so next reading can trigger it again if needed

Jon Fawcett 5 سال پیش
والد
کامیت
079e430313

+ 16 - 38
LoopFollow/Controllers/AlarmSound.swift

@@ -41,6 +41,22 @@ class AlarmSound {
     
     fileprivate static var muted = false
     
+    fileprivate static var alarmPlayingForTimer = Timer()
+    fileprivate static let alarmPlayingForInterval = 290
+    
+    fileprivate func startAlarmPlayingForTimer(time: TimeInterval) {
+        AlarmSound.alarmPlayingForTimer = Timer.scheduledTimer(timeInterval: time,
+                                     target: self,
+                                     selector: #selector(AlarmSound.alarmPlayingForTimerDidEnd(_:)),
+                                     userInfo: nil,
+                                     repeats: true)
+    }
+    
+    @objc func alarmPlayingForTimerDidEnd(_ timer:Timer) {
+        if !AlarmSound.isPlaying { return }
+        AlarmSound.stop()
+    }
+    
     /*
      * Sets the audio volume to 0.
      */
@@ -162,8 +178,6 @@ class AlarmSound {
             }
             
             
-            //self.playingTimer = Timer.schedule(repeatInterval: 1.0, handler: self.onPlayingTimer)
-            
         } catch let error {
             NSLog("AlarmSound - unable to play sound; error: \(error)")
         }
@@ -212,43 +226,7 @@ class AlarmSound {
             NSLog("Terminate AlarmSound - unable to play sound; error: \(error)")
         }
     }
-    
-    fileprivate static func onPlayingTimer(timer: Timer?) {
-        
-        // player should be playing, not muted!
-        guard self.isPlaying && !self.isMuted else {
-            return
-        }
-        
-        // application should be in active state!
-        guard UIApplication.shared.applicationState == .active else {
-            return
-        }
-        
-        if UserDefaultsRepository.overrideSystemOutputVolume.value {
 
-            // keep the system output volume before overriding it
-            if self.systemOutputVolumeBeforeOverride == nil {
-                //self.systemOutputVolumeBeforeOverride = MPVolumeView.volume
-                self.systemOutputVolumeBeforeOverride = AVAudioSession.sharedInstance().outputVolume
-            }
-            
-             // override the system output volume
-            MPVolumeView.setVolume(UserDefaultsRepository.systemOutputVolume.value)
-           // if MPVolumeView.volume != UserDefaultsRepository.systemOutputVolume.value {
-            //    self.volumeChangeDetector.isActive = false
-            //    MPVolumeView.volume = UserDefaultsRepository.systemOutputVolume.value
-           // } else {
-            
-                // listen to user volume changes
-           //     self.volumeChangeDetector.isActive = true
-           // }
-        }
-            
-        if self.vibrate.value {
-            AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
-        }
-    }
     
     fileprivate static func restoreSystemOutputVolume() {
         

+ 17 - 0
LoopFollow/Controllers/Alarms.swift

@@ -327,6 +327,7 @@ extension MainViewController {
         snoozer.updateDisplayWhenTriggered(bgVal: bgUnits.toDisplayUnits(String(bgData[bgData.count - 1].sgv)), directionVal: latestDirectionString ?? "", deltaVal: bgUnits.toDisplayUnits(latestDeltaString) ?? "", minAgoVal: latestMinAgoString ?? "", alertLabelVal: AlarmSound.whichAlarm)
         AlarmSound.setSoundFile(str: sound)
         AlarmSound.play(overrideVolume: overrideVolume, numLoops: numLoops)
+        startAlarmPlayingTimer()
     }
     
     func triggerAlarm(sound: String, snooozedBGReadingTime: TimeInterval?, overrideVolume: Bool, numLoops: Int)
@@ -342,6 +343,22 @@ extension MainViewController {
         }
         AlarmSound.setSoundFile(str: sound)
         AlarmSound.play(overrideVolume: overrideVolume, numLoops: numLoops)
+        
+        let bgSeconds = bgData.last!.date
+        let now = Date().timeIntervalSince1970
+        let secondsAgo = now - bgSeconds
+        let timerLength = 290 - secondsAgo
+        startAlarmPlayingTimer(time: timerLength)
+    }
+    
+    func stopAlarmAtNextReading(){
+        
+        AlarmSound.whichAlarm = "none"
+        guard let snoozer = self.tabBarController!.viewControllers?[2] as? SnoozeViewController else { return }
+        snoozer.updateDisplayWhenTriggered(bgVal: bgUnits.toDisplayUnits(String(bgData[bgData.count - 1].sgv)), directionVal: latestDirectionString ?? "", deltaVal: bgUnits.toDisplayUnits(latestDeltaString) ?? "", minAgoVal: latestMinAgoString ?? "", alertLabelVal: AlarmSound.whichAlarm)
+        snoozer.SnoozeButton.isHidden = true
+        snoozer.AlertLabel.isHidden = true
+        AlarmSound.stop()
     }
     
     func clearOldSnoozes(){

+ 1 - 0
LoopFollow/ViewControllers/SnoozeViewController.swift

@@ -51,6 +51,7 @@ class SnoozeViewController: UIViewController, UNUserNotificationCenterDelegate {
         DeltaLabel.text = deltaVal
         MinAgoLabel.text = minAgoVal
         AlertLabel.text = alertLabelVal
+        if alertLabelVal == "none" { return }
         sendNotification(self, bgVal: bgVal, directionVal: directionVal, deltaVal: deltaVal, minAgoVal: minAgoVal, alertLabelVal: alertLabelVal)
     }