From a224f313494dc00f077e76f90ff1bcfd8e911a9a Mon Sep 17 00:00:00 2001 From: freeram Date: Fri, 8 Mar 2024 03:32:38 -0700 Subject: [PATCH] Integrate rich presence --- pomodorodiscord/src/pomodoro_frame.py | 32 ++++++++++++++++++++++++--- pomodorodiscord/src/richpresence.py | 15 ++++++++----- 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/pomodorodiscord/src/pomodoro_frame.py b/pomodorodiscord/src/pomodoro_frame.py index 04935ae..e629456 100644 --- a/pomodorodiscord/src/pomodoro_frame.py +++ b/pomodorodiscord/src/pomodoro_frame.py @@ -1,8 +1,9 @@ import os import json +import time import threading import customtkinter as ctk -from datetime import datetime +from datetime import datetime, timedelta from src.utils import load_config, DEF_POMODORO_MINS, DEF_SB_MINS, DEF_LB_MINS, beep from src.richpresence import RichPresence @@ -52,14 +53,38 @@ def __init__(self, master): self.break_running = False self.next_timer_update = None self.remaining_time = self.pomodoro_time + # States used for Rich Presence + self.start_time_timestamp = None + self.end_time_timestamp = None + self.session_counter = 0 def init_rpc(self): self.rpc = RichPresence() + self.rpc_thread = threading.Thread(target=self.update_rpc, daemon=True) + self.rpc_thread.start() + + def update_rpc(self): + while True: + if self.break_running: + self.rpc.break_state(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() + + # Discord-imposed rate limit + time.sleep(15) def start_timer(self): if self.next_timer_update: self.after_cancel(self.next_timer_update) + # Rich presence info + start_time = datetime.now() + end_time = start_time + timedelta(seconds=self.remaining_time) + self.start_time_timestamp = start_time.timestamp() + self.end_time_timestamp = end_time.timestamp() + self.running = not self.running btn_text = "Pause" if self.running else "Resume" btn_fg = "transparent" if self.running else self.start_color @@ -78,6 +103,7 @@ def update_timer(self): self.session_ended() def track_second(self): + # TODO: I don't like the if check every second if os.path.exists(self.data_file): with open(self.data_file, 'r') as file: data = json.load(file) @@ -117,10 +143,11 @@ def session_ended(self): if was_break: return + + self.session_counter += 1 current_date = datetime.now().strftime("%Y-%m-%d") - # TODO: I don't like the if check every second if os.path.exists(self.data_file): with open(self.data_file, 'r') as file: data = json.load(file) @@ -151,4 +178,3 @@ def long_break(self): self.break_running = True self.break_text.set("Long break") self.start_timer() - diff --git a/pomodorodiscord/src/richpresence.py b/pomodorodiscord/src/richpresence.py index 1c2e90d..0dc7977 100644 --- a/pomodorodiscord/src/richpresence.py +++ b/pomodorodiscord/src/richpresence.py @@ -7,6 +7,7 @@ class RichPresence(pypresence.Presence): def __init__(self): super().__init__(client_id=CLIENT_ID) + self.state = None try: self.connect() self.default_state() @@ -14,11 +15,13 @@ def __init__(self): print(f"Failed to connect to Discord: {e}") def default_state(self): - self.update(state="Placeholder", - details="Placeholder details", large_image="tomato") + self.update(state="Idling", details=None, large_image="graytomato") + self.state = "default" - def begin_pomodoro(self): - pass + 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") + self.state = "pomodoro" - def begin_break(self): - pass + def break_state(self, start_time, end_time): + self.update(state="On break", details=None, start=start_time, end=end_time, large_image="greentomato") + self.state = "break"