Skip to content

Commit

Permalink
Reuse tick from player for sleep timer
Browse files Browse the repository at this point in the history
This makes everything much simpler, and also makes it sure, that the two
counters are synchronized visually
  • Loading branch information
rdbende committed Dec 1, 2024
1 parent a0c4a77 commit b09c781
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 19 deletions.
3 changes: 0 additions & 3 deletions cozy/media/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 9 additions & 16 deletions cozy/view_model/sleep_timer_view_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -95,27 +95,22 @@ 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):
self._player.pause()

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
Expand All @@ -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
Expand Down

0 comments on commit b09c781

Please sign in to comment.