From 90ca437321cef093a489904f6152ddf09ed9a456 Mon Sep 17 00:00:00 2001 From: freeram Date: Thu, 4 Apr 2024 04:43:32 -0600 Subject: [PATCH] Show time studied in Rich Presence --- pomodorodiscord/src/pomodoro_frame.py | 6 ++++-- pomodorodiscord/src/richpresence.py | 19 +++++++++++++++---- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/pomodorodiscord/src/pomodoro_frame.py b/pomodorodiscord/src/pomodoro_frame.py index ea9a963..cf436e4 100644 --- a/pomodorodiscord/src/pomodoro_frame.py +++ b/pomodorodiscord/src/pomodoro_frame.py @@ -69,6 +69,7 @@ def initialize_state(self, config): self.start_time_timestamp = None self.end_time_timestamp = None self.session_counter = 0 + self.seconds_studied = 0 def initialize_rpc(self): """Start the rich presence in a separate thread""" @@ -79,11 +80,11 @@ def initialize_rpc(self): def update_rpc(self): while True: if self.break_running: - self.rpc.break_state(self.session_counter, self.start_time_timestamp, self.end_time_timestamp) + 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) else: - self.rpc.default_state() + self.rpc.default_state(self.end_time_timestamp) # Discord-imposed rate limit time.sleep(15) @@ -111,6 +112,7 @@ def update_timer(self): self.remaining_time -= 1 if not self.break_running: self.track_second() + self.seconds_studied += 1 minutes, seconds = divmod(self.remaining_time, 60) self.timer_display.configure(text=f"{minutes:02d}:{seconds:02d}") self.next_timer_update = self.after(1000, self.update_timer) diff --git a/pomodorodiscord/src/richpresence.py b/pomodorodiscord/src/richpresence.py index aac0c4f..847caac 100644 --- a/pomodorodiscord/src/richpresence.py +++ b/pomodorodiscord/src/richpresence.py @@ -1,4 +1,5 @@ import pypresence +from datetime import datetime CLIENT_ID = '1215345125002059836' @@ -6,6 +7,7 @@ class RichPresence(pypresence.Presence): def __init__(self): super().__init__(client_id=CLIENT_ID) + self.launch_time = datetime.now().timestamp() # Can only update every 15 seconds try: self.connect() @@ -13,14 +15,23 @@ def __init__(self): except Exception as e: print(f"Failed to connect to Discord: {e}") - def default_state(self): - self.update(state="Idling", details=None, large_image="graytomato", + def format_time(self, seconds_studied): + total_seconds = seconds_studied + total_hours = seconds_studied / 3600 + + if total_hours < 1: + return f"{total_seconds // 60} minute{'s' if total_seconds // 60 != 1 else ''}" + else: + return f"{total_hours:.1f} hours" + + def default_state(self, start_time=None): + self.update(state="Idling", details=None, start=start_time or self.launch_time, large_image="graytomato", large_text="github.com/freeram/pomodoro-discord") 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 break_state(self, complete, start_time, end_time): - self.update(state=f"On break. Sessions completed: {complete}", details=None, start=start_time, + 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")