Skip to content

Commit

Permalink
De-spaghettify, separate threads for connecting/disconnecting RPC
Browse files Browse the repository at this point in the history
  • Loading branch information
jake158 committed May 3, 2024
1 parent 5acc9c8 commit 4ae4dfe
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 27 deletions.
48 changes: 28 additions & 20 deletions src/frames/pomodoro_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def initialize_ui(self, config):
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)
fg_color="transparent", text_color=CONNECTED_TEXT, hover=False, width=70, command=self.toggle_rpc)
self.discord_button.pack(pady=(20, 0))

def initialize_state(self, config):
Expand Down Expand Up @@ -81,31 +81,39 @@ def initialize_rpc(self):
self.rpc_thread.start()

def toggle_rpc(self):
self.discord_button.configure(state="disabled")
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")
self.discord_button.configure(text="Connecting...")
threading.Thread(target=self.connect_rpc, daemon=True).start()
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")
self.discord_button.configure(text="Disconnecting...")
threading.Thread(target=self.disconnect_rpc, daemon=True).start()

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

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

def update_rpc(self):
while True:
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}")
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()

# Discord-imposed rate limit
time.sleep(15)
Expand Down
13 changes: 6 additions & 7 deletions src/logic/richpresence.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,31 @@ def __init__(self):
# Can only update every 15 seconds
self.connected = False
self.connect()
self.idling_state()

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

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

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:
Expand Down

0 comments on commit 4ae4dfe

Please sign in to comment.