Skip to content

Commit

Permalink
Integrate rich presence
Browse files Browse the repository at this point in the history
  • Loading branch information
jake158 committed Mar 8, 2024
1 parent 39048fa commit a224f31
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
32 changes: 29 additions & 3 deletions pomodorodiscord/src/pomodoro_frame.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -151,4 +178,3 @@ def long_break(self):
self.break_running = True
self.break_text.set("Long break")
self.start_timer()

15 changes: 9 additions & 6 deletions pomodorodiscord/src/richpresence.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,21 @@
class RichPresence(pypresence.Presence):
def __init__(self):
super().__init__(client_id=CLIENT_ID)
self.state = None
try:
self.connect()
self.default_state()
except Exception as e:
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"

0 comments on commit a224f31

Please sign in to comment.