Skip to content

Commit

Permalink
Add semi-working RPC toggle button
Browse files Browse the repository at this point in the history
  • Loading branch information
jake158 committed May 3, 2024
1 parent 6c5934b commit 5acc9c8
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def on_tab_change(self):

class PomodoroApp(ctk.CTk):
WIDTH = 350
HEIGHT = 400
HEIGHT = 450

def __init__(self):
super().__init__()
Expand Down
41 changes: 32 additions & 9 deletions src/frames/pomodoro_frame.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import time
import threading
import customtkinter as ctk
from CTkMessagebox import CTkMessagebox
from datetime import datetime, timedelta
from src.utils import load_config, load_data, save_data, beep, DEF_POMODORO_MINS, DEF_SB_MINS, DEF_LB_MINS, DEF_SB_BEFORE_L
from src.logic.richpresence import RichPresence
Expand All @@ -9,6 +10,8 @@
BREAK_HOVER = "#adaaaa"
RESET_BTN_COLOR = "#cca508"
RESET_HOVER = "#e3b707"
CONNECTED_TEXT = "#45b54e"
DISCONNECTED_TEXT = "#f75a4f"


class PomodoroFrame(ctk.CTkFrame):
Expand Down Expand Up @@ -44,7 +47,11 @@ def initialize_ui(self, config):
self.lb_button.pack(pady=(0, 10))

self.reset_button = ctk.CTkButton(self, text="Reset", font=("Roboto", 17), fg_color=RESET_BTN_COLOR, hover_color=RESET_HOVER, command=self.reset)
self.reset_button.pack()
self.reset_button.pack(pady=(0, 10))

self.discord_button = ctk.CTkButton(self, text="Connected to Discord [click to disconnect]", font=("Roboto", 12),
fg_color="transparent", text_color=CONNECTED_TEXT, width=70, command=self.toggle_rpc)
self.discord_button.pack(pady=(20, 0))

def initialize_state(self, config):
self.running = False
Expand Down Expand Up @@ -73,16 +80,32 @@ def initialize_rpc(self):
self.rpc_thread = threading.Thread(target=self.update_rpc, daemon=True)
self.rpc_thread.start()

def toggle_rpc(self):
if not self.rpc.connected:
if self.rpc.connect():
self.discord_button.configure(text="Connected to Discord [click to disconnect]", text_color=CONNECTED_TEXT)
else:
CTkMessagebox(title="Error", message="Reconnecting to Discord failed\nCheck console for error output", icon="cancel")
else:
if self.rpc.disconnect():
self.discord_button.configure(text="Not connected to Discord [click to connect]", text_color=DISCONNECTED_TEXT)
else:
CTkMessagebox(title="Error", message="Reconnecting to Discord failed\nCheck console for error output", icon="cancel")

def update_rpc(self):
while True:
if self.break_running:
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.idling_state()
if self.rpc.connected:
try:
if self.break_running:
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.idling_state()
except Exception as e:
print(f"Error updating Rich Presence: {e}")

# Discord-imposed rate limit
time.sleep(15)
Expand Down
22 changes: 20 additions & 2 deletions src/logic/richpresence.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,29 @@ def __init__(self):
super().__init__(client_id=CLIENT_ID)
self.launch_time = datetime.now().timestamp()
# Can only update every 15 seconds
self.connected = False
self.connect()

def connect(self):
try:
self.connect()
super().connect()
self.idling_state()
self.connected = True
except Exception as e:
print(f"Failed to reconnect to Discord: {e}")
self.connected = False
return self.connected

def disconnect(self):
if not self.connected:
return True
try:
super().close()
self.connected = False
except Exception as e:
print(f"Failed to connect to Discord: {e}")
print(f"Failed to disconnect from Discord: {e}")
self.connected = True
return not self.connected

def format_time(self, seconds_studied):
total_seconds = seconds_studied
Expand Down

0 comments on commit 5acc9c8

Please sign in to comment.