From 6c5934bc4eea9351dab86fd3f7fde9abdc627f71 Mon Sep 17 00:00:00 2001 From: freeram Date: Sat, 20 Apr 2024 20:10:45 -0600 Subject: [PATCH] Add paused state, improve time formatting --- src/frames/pomodoro_frame.py | 16 +++++++++++----- src/logic/richpresence.py | 10 +++++++--- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/frames/pomodoro_frame.py b/src/frames/pomodoro_frame.py index adf48de..7db699d 100644 --- a/src/frames/pomodoro_frame.py +++ b/src/frames/pomodoro_frame.py @@ -66,6 +66,7 @@ def initialize_state(self, config): self.end_time_timestamp = None self.session_counter = 0 self.seconds_studied = 0 + self.paused = False def initialize_rpc(self): self.rpc = RichPresence() @@ -78,8 +79,10 @@ def update_rpc(self): self.rpc.break_state(self.seconds_studied, self.start_time_timestamp, self.end_time_timestamp) elif self.running: self.rpc.running_state(self.session_counter + 1, self.start_time_timestamp, self.end_time_timestamp) + elif self.paused: + self.rpc.paused_state(self.start_time_timestamp) else: - self.rpc.default_state() + self.rpc.idling_state() # Discord-imposed rate limit time.sleep(15) @@ -88,18 +91,20 @@ def toggle_timer(self): if self.next_timer_update: self.after_cancel(self.next_timer_update) + self.running = not self.running + btn_text = "Pause" if self.running else "Resume" + btn_fg = "transparent" if self.running else self.start_color + self.start_button.configure(text=btn_text, fg_color=btn_fg, hover=not self.running) + # Rich presence info now = datetime.now() end_time = now + timedelta(seconds=self.remaining_time) self.start_time_timestamp = now.timestamp() self.end_time_timestamp = end_time.timestamp() + self.paused = not self.running # For tracking seconds studied by date self.current_date = now.strftime("%Y-%m-%d") - self.running = not self.running - btn_text = "Pause" if self.running else "Resume" - btn_fg = "transparent" if self.running else self.start_color - self.start_button.configure(text=btn_text, fg_color=btn_fg, hover=not self.running) self.update_timer() def update_timer(self): @@ -125,6 +130,7 @@ def reset(self, to:str="pomodoro_time", default:int=DEF_POMODORO_MINS): self.running = False self.break_running = False self.short_break_running = False + self.paused = False self.break_text.set("") if self.next_timer_update: diff --git a/src/logic/richpresence.py b/src/logic/richpresence.py index 543e80b..db1526f 100644 --- a/src/logic/richpresence.py +++ b/src/logic/richpresence.py @@ -11,7 +11,7 @@ def __init__(self): # Can only update every 15 seconds try: self.connect() - self.default_state() + self.idling_state() except Exception as e: print(f"Failed to connect to Discord: {e}") @@ -22,9 +22,9 @@ def format_time(self, seconds_studied): if total_hours < 1: return f"{total_seconds // 60} minute{'s' if total_seconds // 60 != 1 else ''}" else: - return f"{total_hours:.1f} hours" + return f"{round(total_hours, 1) if total_hours % 1 != 0 else int(total_hours)} hours" - def default_state(self): + def idling_state(self): self.update(state="Idling", details=None, start=self.launch_time, large_image="graytomato", large_text="github.com/freeram/pomodoro-discord") @@ -32,6 +32,10 @@ def running_state(self, session, start_time, end_time): self.update(state=f"Session {session}", details="Studying", start=start_time, end=end_time, large_image="tomato", large_text="github.com/freeram/pomodoro-discord") + def paused_state(self, start_time): + self.update(state="Paused", details=None, start=start_time, large_image="graytomato", + large_text="github.com/freeram/pomodoro-discord") + def break_state(self, seconds_studied, start_time, end_time): self.update(state=f"Time studied: {self.format_time(seconds_studied)}", details="On break", start=start_time, end=end_time, large_image="greentomato", large_text="github.com/freeram/pomodoro-discord")