Skip to content

Commit

Permalink
Add remaining time when adding +5 minutes to chapter end
Browse files Browse the repository at this point in the history
  • Loading branch information
rdbende committed Dec 1, 2024
1 parent 4d97f9b commit a0c4a77
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
13 changes: 7 additions & 6 deletions cozy/ui/widgets/sleep_timer.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ def close(self, *_):

@Gtk.Template.Callback()
def set_timer(self, *_):
super().close()

value = self.sleep_timer_action.get_state().unpack()
if value == -1:
self._view_model.remaining_seconds = self.custom_adjustment.get_value() * 60
Expand All @@ -125,20 +127,19 @@ def set_timer(self, *_):
else:
self._view_model.remaining_seconds = value * 60

super().close()

@Gtk.Template.Callback()
def plus_5_minutes(self, *_):
self._view_model.remaining_seconds += 300
super().close()
if self._view_model.stop_after_chapter:
self._view_model.remaining_seconds = self._view_model.get_remaining_from_chapter() + 300
else:
self._view_model.remaining_seconds += 300

@Gtk.Template.Callback()
def till_end_of_chapter(self, *_):
self._view_model.stop_after_chapter = True
super().close()

@Gtk.Template.Callback()
def cancel_timer(self, *_):
super().close()
self._view_model.remaining_seconds = 0
self._view_model.stop_after_chapter = False
super().close()
12 changes: 11 additions & 1 deletion cozy/view_model/sleep_timer_view_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from enum import Enum, auto
from typing import Optional

from gi.repository import Gst

import inject

from cozy.architecture.observable import Observable
Expand Down Expand Up @@ -80,6 +82,14 @@ def stop_after_chapter(self, new_value: bool):
self._notify("stop_after_chapter")
self._notify("timer_enabled")

def get_remaining_from_chapter(self) -> float | None:
book = self._player.loaded_book
if not book:
return 0

position = book.current_chapter.length - (book.current_chapter.position - book.current_chapter.start_position)
return int(position / Gst.SECOND / book.playback_speed)

def destroy(self):
self._stop_timer()

Expand Down Expand Up @@ -127,7 +137,7 @@ def _on_player_changed(self, event, _):
self._stop_timer()

def _handle_system_power_event(self):
# TODO: This doesn't work in Flatpak. Either remove it completely, or make it conditional'
# TODO: This doesn't work in Flatpak. Either remove it completely, or make it conditional
command = None

if self.system_power_control == SystemPowerControl.SHUTDOWN:
Expand Down
2 changes: 1 addition & 1 deletion data/ui/sleep_timer_dialog.blp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Adjustment timer_value {

template $SleepTimer: Adw.Dialog {
width-request: 180;
title: _("Set Sleep Timer");
title: _("Sleep Timer");

child: Adw.ToolbarView toolbarview{
[top]
Expand Down

0 comments on commit a0c4a77

Please sign in to comment.