diff --git a/CHANGELOG.md b/CHANGELOG.md index a95b2901..3b4d5528 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Fixed +- Fixed the sleep timer freezing and the app closing when the timer was set again and then canceled ([#345]) ## [1.8.1] - 2026-02-14 ### Changed @@ -124,6 +126,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 [#261]: https://github.com/FossifyOrg/Music-Player/issues/261 [#269]: https://github.com/FossifyOrg/Music-Player/issues/269 [#298]: https://github.com/FossifyOrg/Music-Player/issues/298 +[#345]: https://github.com/FossifyOrg/Music-Player/issues/345 [#361]: https://github.com/FossifyOrg/Music-Player/issues/361 [Unreleased]: https://github.com/FossifyOrg/Music-Player/compare/1.8.1...HEAD diff --git a/app/src/main/kotlin/org/fossify/musicplayer/activities/MainActivity.kt b/app/src/main/kotlin/org/fossify/musicplayer/activities/MainActivity.kt index 9865320b..13437365 100644 --- a/app/src/main/kotlin/org/fossify/musicplayer/activities/MainActivity.kt +++ b/app/src/main/kotlin/org/fossify/musicplayer/activities/MainActivity.kt @@ -481,14 +481,14 @@ class MainActivity : SimpleMusicActivity() { private fun startSleepTimer() { binding.sleepTimerHolder.fadeIn() withPlayer { - sendCommand(CustomCommands.TOGGLE_SLEEP_TIMER) + sendCommand(CustomCommands.START_SLEEP_TIMER) } } private fun stopSleepTimer() { binding.sleepTimerHolder.fadeOut() withPlayer { - sendCommand(CustomCommands.TOGGLE_SLEEP_TIMER) + sendCommand(CustomCommands.STOP_SLEEP_TIMER) } } diff --git a/app/src/main/kotlin/org/fossify/musicplayer/playback/CustomCommands.kt b/app/src/main/kotlin/org/fossify/musicplayer/playback/CustomCommands.kt index 2b01e934..63418094 100644 --- a/app/src/main/kotlin/org/fossify/musicplayer/playback/CustomCommands.kt +++ b/app/src/main/kotlin/org/fossify/musicplayer/playback/CustomCommands.kt @@ -12,7 +12,8 @@ import org.fossify.musicplayer.helpers.PATH enum class CustomCommands(val customAction: String) { CLOSE_PLAYER(customAction = PATH + "CLOSE_PLAYER"), RELOAD_CONTENT(customAction = PATH + "RELOAD_CONTENT"), - TOGGLE_SLEEP_TIMER(customAction = PATH + "TOGGLE_SLEEP_TIMER"), + START_SLEEP_TIMER(customAction = PATH + "START_SLEEP_TIMER"), + STOP_SLEEP_TIMER(customAction = PATH + "STOP_SLEEP_TIMER"), SET_NEXT_ITEM(customAction = PATH + "SET_NEXT_ITEM"), SET_SHUFFLE_ORDER(customAction = PATH + "SET_SHUFFLE_ORDER"); diff --git a/app/src/main/kotlin/org/fossify/musicplayer/playback/MediaSessionCallback.kt b/app/src/main/kotlin/org/fossify/musicplayer/playback/MediaSessionCallback.kt index 82f589ba..8dc635c0 100644 --- a/app/src/main/kotlin/org/fossify/musicplayer/playback/MediaSessionCallback.kt +++ b/app/src/main/kotlin/org/fossify/musicplayer/playback/MediaSessionCallback.kt @@ -74,7 +74,8 @@ internal fun PlaybackService.getMediaSessionCallback() = object : MediaLibrarySe when (command) { CustomCommands.CLOSE_PLAYER -> stopService() CustomCommands.RELOAD_CONTENT -> reloadContent() - CustomCommands.TOGGLE_SLEEP_TIMER -> toggleSleepTimer() + CustomCommands.START_SLEEP_TIMER -> startSleepTimer() + CustomCommands.STOP_SLEEP_TIMER -> stopSleepTimer() CustomCommands.SET_SHUFFLE_ORDER -> setShuffleOrder(args) CustomCommands.SET_NEXT_ITEM -> setNextItem(args) } diff --git a/app/src/main/kotlin/org/fossify/musicplayer/playback/SleepTimer.kt b/app/src/main/kotlin/org/fossify/musicplayer/playback/SleepTimer.kt index c468dd51..cfbb1a71 100644 --- a/app/src/main/kotlin/org/fossify/musicplayer/playback/SleepTimer.kt +++ b/app/src/main/kotlin/org/fossify/musicplayer/playback/SleepTimer.kt @@ -5,17 +5,8 @@ import org.fossify.musicplayer.extensions.config import org.fossify.musicplayer.models.Events import org.greenrobot.eventbus.EventBus -private var isActive = false private var sleepTimer: CountDownTimer? = null -internal fun PlaybackService.toggleSleepTimer() { - if (isActive) { - stopSleepTimer() - } else { - startSleepTimer() - } -} - internal fun PlaybackService.startSleepTimer() { val millisInFuture = config.sleepInTS - System.currentTimeMillis() + 1000L sleepTimer?.cancel() @@ -34,12 +25,10 @@ internal fun PlaybackService.startSleepTimer() { } sleepTimer?.start() - isActive = true } internal fun PlaybackService.stopSleepTimer() { sleepTimer?.cancel() sleepTimer = null - isActive = false config.sleepInTS = 0 }