From 4ae4dfe2f797b6a582c32db00ce763a740ed0ebb Mon Sep 17 00:00:00 2001 From: freeram Date: Fri, 3 May 2024 04:01:07 -0600 Subject: [PATCH] De-spaghettify, separate threads for connecting/disconnecting RPC --- src/frames/pomodoro_frame.py | 48 +++++++++++++++++++++--------------- src/logic/richpresence.py | 13 +++++----- 2 files changed, 34 insertions(+), 27 deletions(-) diff --git a/src/frames/pomodoro_frame.py b/src/frames/pomodoro_frame.py index 35797b9..d06758e 100644 --- a/src/frames/pomodoro_frame.py +++ b/src/frames/pomodoro_frame.py @@ -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): @@ -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) diff --git a/src/logic/richpresence.py b/src/logic/richpresence.py index 9084ebd..a40ada9 100644 --- a/src/logic/richpresence.py +++ b/src/logic/richpresence.py @@ -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: