Skip to content

Commit

Permalink
Add paused state, improve time formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
jake158 committed Apr 21, 2024
1 parent c3de232 commit 6c5934b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
16 changes: 11 additions & 5 deletions src/frames/pomodoro_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def initialize_state(self, config):
self.end_time_timestamp = None
self.session_counter = 0
self.seconds_studied = 0
self.paused = False

def initialize_rpc(self):
self.rpc = RichPresence()
Expand All @@ -78,8 +79,10 @@ def update_rpc(self):
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.default_state()
self.rpc.idling_state()

# Discord-imposed rate limit
time.sleep(15)
Expand All @@ -88,18 +91,20 @@ def toggle_timer(self):
if self.next_timer_update:
self.after_cancel(self.next_timer_update)

self.running = not self.running
btn_text = "Pause" if self.running else "Resume"
btn_fg = "transparent" if self.running else self.start_color
self.start_button.configure(text=btn_text, fg_color=btn_fg, hover=not self.running)

# Rich presence info
now = datetime.now()
end_time = now + timedelta(seconds=self.remaining_time)
self.start_time_timestamp = now.timestamp()
self.end_time_timestamp = end_time.timestamp()
self.paused = not self.running
# For tracking seconds studied by date
self.current_date = now.strftime("%Y-%m-%d")

self.running = not self.running
btn_text = "Pause" if self.running else "Resume"
btn_fg = "transparent" if self.running else self.start_color
self.start_button.configure(text=btn_text, fg_color=btn_fg, hover=not self.running)
self.update_timer()

def update_timer(self):
Expand All @@ -125,6 +130,7 @@ def reset(self, to:str="pomodoro_time", default:int=DEF_POMODORO_MINS):
self.running = False
self.break_running = False
self.short_break_running = False
self.paused = False
self.break_text.set("")

if self.next_timer_update:
Expand Down
10 changes: 7 additions & 3 deletions src/logic/richpresence.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def __init__(self):
# Can only update every 15 seconds
try:
self.connect()
self.default_state()
self.idling_state()
except Exception as e:
print(f"Failed to connect to Discord: {e}")

Expand All @@ -22,16 +22,20 @@ def format_time(self, seconds_studied):
if total_hours < 1:
return f"{total_seconds // 60} minute{'s' if total_seconds // 60 != 1 else ''}"
else:
return f"{total_hours:.1f} hours"
return f"{round(total_hours, 1) if total_hours % 1 != 0 else int(total_hours)} hours"

def default_state(self):
def idling_state(self):
self.update(state="Idling", details=None, start=self.launch_time, large_image="graytomato",
large_text="github.com/freeram/pomodoro-discord")

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", large_text="github.com/freeram/pomodoro-discord")

def paused_state(self, start_time):
self.update(state="Paused", details=None, start=start_time, large_image="graytomato",
large_text="github.com/freeram/pomodoro-discord")

def break_state(self, seconds_studied, start_time, end_time):
self.update(state=f"Time studied: {self.format_time(seconds_studied)}", details="On break", start=start_time,
end=end_time, large_image="greentomato", large_text="github.com/freeram/pomodoro-discord")

0 comments on commit 6c5934b

Please sign in to comment.