diff --git a/ios/App/Shared/player/AudioPlayer.swift b/ios/App/Shared/player/AudioPlayer.swift index e62c55ca..750bb03b 100644 --- a/ios/App/Shared/player/AudioPlayer.swift +++ b/ios/App/Shared/player/AudioPlayer.swift @@ -194,11 +194,6 @@ 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() - } } } } @@ -352,6 +347,9 @@ class AudioPlayer: NSObject { // Update the progress self.updateNowPlaying() + + // Handle a chapter sleep timer that may now be invalid + self.handleTrackChangeForChapterSleepTimer() } public func pause() { diff --git a/ios/App/Shared/player/AudioPlayerSleepTimer.swift b/ios/App/Shared/player/AudioPlayerSleepTimer.swift index baffbe39..3fba6206 100644 --- a/ios/App/Shared/player/AudioPlayerSleepTimer.swift +++ b/ios/App/Shared/player/AudioPlayerSleepTimer.swift @@ -47,9 +47,13 @@ extension AudioPlayer { NotificationCenter.default.post(name: NSNotification.Name(PlayerEvents.sleepSet.rawValue), object: nil) } - public func setChapterSleepTimer(stopAt: Double) { - logger.log("SLEEP TIMER: Scheduling for chapter end \(stopAt)") + public func setChapterSleepTimer(stopAt: Double?) { self.removeSleepTimer() + guard let stopAt = stopAt else { return } + guard let currentTime = self.getCurrentTime() else { return } + guard stopAt >= currentTime else { return } + + logger.log("SLEEP TIMER: Scheduling for chapter end \(stopAt)") // Schedule the observation time self.sleepTimeChapterStopAt = stopAt @@ -111,7 +115,12 @@ extension AudioPlayer { // MARK: - Internal helpers - internal func decrementSleepTimerIfRunning() { + internal func handleTrackChangeForChapterSleepTimer() { + // If no sleep timer is set, this does nothing + self.setChapterSleepTimer(stopAt: self.sleepTimeChapterStopAt) + } + + private func decrementSleepTimerIfRunning() { if var sleepTimeRemaining = self.sleepTimeRemaining { sleepTimeRemaining -= 1 self.sleepTimeRemaining = sleepTimeRemaining @@ -137,19 +146,11 @@ extension AudioPlayer { self.sleepTimeChapterStopAt = nil } - internal func isChapterSleepTimerBeforeTime(_ time: Double) -> Bool { - if let chapterStopAt = self.sleepTimeChapterStopAt { - return chapterStopAt <= time - } - - return false - } - - internal func isCountdownSleepTimerSet() -> Bool { + private func isCountdownSleepTimerSet() -> Bool { return self.sleepTimeRemaining != nil } - internal func isChapterSleepTimerSet() -> Bool { + private func isChapterSleepTimerSet() -> Bool { return self.sleepTimeChapterStopAt != nil } diff --git a/ios/App/Shared/util/Database.swift b/ios/App/Shared/util/Database.swift index 27ef58be..be39b5c8 100644 --- a/ios/App/Shared/util/Database.swift +++ b/ios/App/Shared/util/Database.swift @@ -244,6 +244,7 @@ class Database { public func getPlaybackSession(id: String) -> PlaybackSession? { do { let realm = try Realm() + realm.refresh() // Refresh, because working with stale sessions leads to wrong times return realm.object(ofType: PlaybackSession.self, forPrimaryKey: id) } catch { debugPrint(error)