From b09c781bfaa2d7a4bad4925181b9c8e09411752b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A9v=C3=A9nyi=20Benedek?= Date: Sun, 1 Dec 2024 17:42:48 +0100 Subject: [PATCH] Reuse tick from player for sleep timer This makes everything much simpler, and also makes it sure, that the two counters are synchronized visually --- cozy/media/player.py | 3 --- cozy/view_model/sleep_timer_view_model.py | 25 ++++++++--------------- 2 files changed, 9 insertions(+), 19 deletions(-) diff --git a/cozy/media/player.py b/cozy/media/player.py index 4647d7a6..558c189e 100644 --- a/cozy/media/player.py +++ b/cozy/media/player.py @@ -207,11 +207,8 @@ def _fadeout_callback(self) -> None: GLib.source_remove(self._fade_timeout) self._fade_timeout = None - self.pause() self._volume_fader.props.volume = 1.0 - self.emit_event("fadeout-finished", None) - def fadeout(self, length: int) -> None: if not self._is_player_loaded(): return diff --git a/cozy/view_model/sleep_timer_view_model.py b/cozy/view_model/sleep_timer_view_model.py index c5f9ce90..858294e2 100644 --- a/cozy/view_model/sleep_timer_view_model.py +++ b/cozy/view_model/sleep_timer_view_model.py @@ -33,7 +33,7 @@ def __init__(self): self._remaining_seconds: int = 0 self._system_power_control: SystemPowerControl = SystemPowerControl.OFF - self._sleep_timer: Optional[IntervalTimer] = None + self._timer_running = False self._fadeout_running = False self._player.add_listener(self._on_player_changed) @@ -95,20 +95,14 @@ def destroy(self): def _start_timer(self): self.stop_after_chapter = False - if self._sleep_timer or self._remaining_seconds < 1 or not self._player.playing: - return + self._timer_running = True log.info("Start Sleep Timer") - self._sleep_timer = IntervalTimer(1, self._on_timer_tick) - self._sleep_timer.start() def _stop_timer(self): - if not self._sleep_timer: - return + self._timer_running = False log.info("Stop Sleep Timer") - self._sleep_timer.stop() - self._sleep_timer = None self._notify("timer_enabled") def _stop_playback(self): @@ -116,6 +110,7 @@ def _stop_playback(self): def _on_timer_tick(self): self._remaining_seconds -= 1 + self._notify_main_thread("remaining_seconds") if self._remaining_seconds <= FADEOUT_DURATION and not self._fadeout_running: self._fadeout_running = True @@ -125,16 +120,14 @@ def _on_timer_tick(self): self._stop_timer() self._stop_playback() - self._notify_main_thread("remaining_seconds") - def _on_player_changed(self, event, _): - if event == "chapter-changed": + if event == "position": + if self._timer_running and self._player._play_next_chapter: + # Protected attribute access above, because I don't feel like going through two layers of properties + self._on_timer_tick() + elif event == "chapter-changed": self.stop_after_chapter = False self._notify("stop_after_chapter") - elif event == "play": - self._start_timer() - elif event in {"pause", "stop"}: - self._stop_timer() def _handle_system_power_event(self): # TODO: This doesn't work in Flatpak. Either remove it completely, or make it conditional