diff --git a/ios/App/Shared/player/AudioPlayer.swift b/ios/App/Shared/player/AudioPlayer.swift index 6c22dc1c..f4199465 100644 --- a/ios/App/Shared/player/AudioPlayer.swift +++ b/ios/App/Shared/player/AudioPlayer.swift @@ -178,6 +178,11 @@ class AudioPlayer: NSObject { if self.isSleepTimerSet() { // Update the UI NotificationCenter.default.post(name: NSNotification.Name(PlayerEvents.sleepSet.rawValue), object: nil) + + // Handle a sitation where the user skips past the chapter end + if self.isChapterSleepTimerBeforeTime(currentTime) { + self.removeSleepTimer() + } } } } diff --git a/ios/App/Shared/player/AudioPlayerSleepTimer.swift b/ios/App/Shared/player/AudioPlayerSleepTimer.swift index 59511e94..b5995bc9 100644 --- a/ios/App/Shared/player/AudioPlayerSleepTimer.swift +++ b/ios/App/Shared/player/AudioPlayerSleepTimer.swift @@ -27,12 +27,6 @@ extension AudioPlayer { sleepTimeRemaining = self.sleepTimeRemaining } - // Guard against invalid sleep timers, but give it a chance to go 1 second negative to prevent a raise condition - if sleepTimeRemaining?.isLess(than: -1) ?? false { - self.removeSleepTimer() - return nil - } - return sleepTimeRemaining } @@ -110,14 +104,8 @@ extension AudioPlayer { self.removeChapterSleepTimer() self.sleepTimeRemaining = nil - // Update the UI after a delay, to avoid a race condition when changing chapters - DispatchQueue.runOnMainQueue { - Timer.scheduledTimer(withTimeInterval: 0.1, repeats: false) { _ in - if !self.isSleepTimerSet() { - NotificationCenter.default.post(name: NSNotification.Name(PlayerEvents.sleepEnded.rawValue), object: self) - } - } - } + // Update the UI + NotificationCenter.default.post(name: NSNotification.Name(PlayerEvents.sleepEnded.rawValue), object: self) } @@ -149,7 +137,7 @@ extension AudioPlayer { self.sleepTimeChapterStopAt = nil } - private func isChapterSleepTimerBeforeTime(_ time: Double) -> Bool { + internal func isChapterSleepTimerBeforeTime(_ time: Double) -> Bool { if let chapterStopAt = self.sleepTimeChapterStopAt { return chapterStopAt <= time } @@ -157,11 +145,11 @@ extension AudioPlayer { return false } - private func isCountdownSleepTimerSet() -> Bool { + internal func isCountdownSleepTimerSet() -> Bool { return self.sleepTimeRemaining != nil } - private func isChapterSleepTimerSet() -> Bool { + internal func isChapterSleepTimerSet() -> Bool { return self.sleepTimeChapterStopAt != nil }