-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFocusApp.py
424 lines (357 loc) · 18.6 KB
/
FocusApp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
import datetime
import json
import threading
import tkinter as tk
from tkinter import messagebox
import customtkinter as ctk
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from playsound import playsound
from consts import COLORS
from utils import add_focus_session_to_calendar, get_resource_path
class FocusApp(ctk.CTk):
def __init__(self):
super().__init__()
self.title("Focus Tracker 2024")
self.geometry("600x800") # Increased height to accommodate new widgets
ctk.set_appearance_mode("dark")
ctk.set_default_color_theme("blue")
self.is_focus_running = False
self.is_break_running = False
self.start_pause_time = None
self.total_paused_time_in_seconds = 0
self.is_paused = False
self.focus_duration = 0
self.elapsed_time = 0
self.start_time = None
self.task = ""
self.total_focus_time = 0
self.sessions_completed = 0
self.daily_focus_times = {}
self.load_stats()
self.create_widgets()
self.after(1000, self.update_timers)
self.protocol("WM_DELETE_WINDOW", self.on_close)
def on_close(self):
if self.is_focus_running:
if messagebox.askyesno("Quit", "Do you really want to quit?"):
destroy = True
self.save_stats() # Save stats before closing
else:
destroy = True
if destroy:
self.quit()
self.destroy()
def create_widgets(self):
self.grid_columnconfigure(0, weight=1)
self.grid_rowconfigure(0, weight=1)
main_frame = ctk.CTkFrame(self, fg_color="transparent")
main_frame.grid(row=0, column=0, padx=20, pady=20, sticky="nsew")
main_frame.grid_columnconfigure(0, weight=1)
# Timer Frame
timer_frame = ctk.CTkFrame(main_frame, fg_color="#2a2d2e", corner_radius=15)
timer_frame.grid(row=0, column=0, pady=(0, 10), sticky="ew")
timer_frame.grid_columnconfigure(0, weight=1)
self.focus_timer_label = ctk.CTkLabel(timer_frame, text="00:00",
font=ctk.CTkFont(size=40, weight="bold"),
text_color="#4CAF50")
self.focus_timer_label.grid(row=0, column=0, pady=(10, 10))
self.break_timer_label = ctk.CTkLabel(timer_frame, text="00:00",
font=ctk.CTkFont(size=40, weight="bold"),
text_color="#4CAF50")
self.break_timer_label.grid(row=0, column=0, pady=(10, 10))
self.task_label = ctk.CTkLabel(timer_frame, text="Current Task: None",
font=ctk.CTkFont(size=16))
self.task_label.grid(row=1, column=0, pady=(0, 10))
self.setted_focus_time_label = ctk.CTkLabel(timer_frame, text="Setted Focus Time: 0 min",
font=ctk.CTkFont(size=16))
self.setted_focus_time_label.grid(row=2, column=0, pady=(0, 10))
plus_minus_frame = ctk.CTkFrame(timer_frame)
plus_minus_frame.grid(row=3, column=0, padx=5, pady=5)
self.plus_5min_button = ctk.CTkButton(plus_minus_frame, text="+5 min", command=self.add_5_minutes,
font=ctk.CTkFont(size=12), width=10)
self.plus_5min_button.grid(row=0, column=1, pady=(0, 10), padx=10)
self.minus_5min_button = ctk.CTkButton(plus_minus_frame, text="-5 min", command=self.minus_5_minutes,
font=ctk.CTkFont(size=12), width=10)
self.minus_5min_button.grid(row=0, column=0, pady=(0, 10), padx=10)
# Input Frame
input_frame = ctk.CTkFrame(main_frame)
input_frame.grid(row=1, column=0, pady=(0, 10), sticky="ew")
input_frame.grid_columnconfigure(1, weight=1)
# Task Entry
ctk.CTkLabel(input_frame, text="Task:").grid(row=0, column=0, padx=(10, 5), pady=5, sticky="w")
self.task_entry = ctk.CTkEntry(input_frame, width=300)
self.task_entry.grid(row=0, column=1, padx=(0, 10), pady=5, sticky="ew")
# Focus Duration Entry
ctk.CTkLabel(input_frame, text="Duration (min):").grid(row=1, column=0, padx=(10, 5), pady=5, sticky="w")
self.focus_entry = ctk.CTkEntry(input_frame, width=100)
self.focus_entry.grid(row=1, column=1, padx=(0, 10), pady=5, sticky="w")
# CC Email Entry
ctk.CTkLabel(input_frame, text="CC Email:").grid(row=2, column=0, padx=(10, 5), pady=5, sticky="w")
self.cc_email_entry = ctk.CTkEntry(input_frame, width=300, placeholder_text="Optional")
self.cc_email_entry.grid(row=2, column=1, padx=(0, 10), pady=5, sticky="ew")
# Buttons
button_frame = ctk.CTkFrame(main_frame, fg_color="transparent")
button_frame.grid(row=2, column=0, pady=(0, 20), sticky="ew")
button_frame.grid_columnconfigure((0, 1, 2), weight=1)
self.start_focus_button = ctk.CTkButton(button_frame, text="Start Focus", command=self.start_focus_session,
fg_color="#4CAF50", hover_color="#45a049")
self.start_focus_button.grid(row=0, column=0, padx=5)
self.pause_button = ctk.CTkButton(button_frame, text="Pause", command=self.pause_focus_session,
state="disabled", fg_color="#FFA500", hover_color="#FF8C00")
self.pause_button.grid(row=0, column=1, padx=5)
self.stop_button = ctk.CTkButton(button_frame, text="Stop", command=self.stop_focus_session,
state="disabled", fg_color="#F44336", hover_color="#D32F2F")
self.stop_button.grid(row=0, column=2, padx=5)
# Statistics Frame
stats_frame = ctk.CTkFrame(main_frame)
stats_frame.grid(row=3, column=0, pady=(0, 20), sticky="ew")
stats_frame.grid_columnconfigure(0, weight=1)
self.total_focus_label = ctk.CTkLabel(stats_frame,
text=f"Total Focus Time: {self.format_time(self.total_focus_time)}",
font=ctk.CTkFont(size=14, weight="bold"))
self.total_focus_label.grid(row=0, column=0, padx=10, pady=5, sticky="w")
self.sessions_label = ctk.CTkLabel(stats_frame, text=f"Sessions Completed: {self.sessions_completed}",
font=ctk.CTkFont(size=14, weight="bold"))
self.sessions_label.grid(row=1, column=0, padx=10, pady=5, sticky="w")
# Options Frame
options_frame = ctk.CTkFrame(main_frame, fg_color="transparent")
options_frame.grid(row=4, column=0, pady=(0, 20), sticky="ew")
options_frame.grid_columnconfigure((0, 1), weight=1)
self.reset_stats_button = ctk.CTkButton(options_frame, text="Reset Statistics", command=self.reset_stats,
fg_color="#607D8B", hover_color="#455A64")
self.reset_stats_button.grid(row=0, column=0, padx=5)
self.freq_sound_var = tk.BooleanVar(value=True)
self.freq_sound_toggle = ctk.CTkSwitch(options_frame, text="Enable Frequent Sounds",
variable=self.freq_sound_var)
self.freq_sound_toggle.grid(row=0, column=2, padx=5)
self.sound_var = tk.BooleanVar(value=True)
self.sound_toggle = ctk.CTkSwitch(options_frame, text="Enable Sounds", variable=self.sound_var)
self.sound_toggle.grid(row=0, column=1, padx=5)
# Productivity Graph
self.fig, self.ax = plt.subplots(figsize=(5, 3))
self.canvas = FigureCanvasTkAgg(self.fig, master=main_frame)
self.canvas_widget = self.canvas.get_tk_widget()
self.canvas_widget.grid(row=5, column=0, pady=(0, 20), sticky="ew")
self.update_productivity_graph()
# Dropdown label
label = ctk.CTkLabel(input_frame, text="Color:", font=("Arial", 14))
label.grid(row=3, column=0, padx=(10, 5), pady=5, sticky="w")
# Dropdown menu for selecting colors
self.color_var = ctk.StringVar(value="1: Light Blue") # Default selection
self.selected_color_id = 1
color_info = COLORS[self.selected_color_id]
self.color_dropdown = ctk.CTkOptionMenu(
input_frame,
variable=self.color_var,
values=[f"{color_id}: {color_info['name']}" for color_id, color_info in COLORS.items()],
command=self.on_color_select, # Called when a selection is made
fg_color=color_info["background"],
text_color="#000000", # Set the text color to black
button_color=color_info["background"],
)
self.color_dropdown.grid(row=3, column=1, pady=(0, 10), sticky="ew")
def add_5_minutes(self, force=False):
if self.focus_duration == 0 and not force:
messagebox.showwarning("Warning", "Please Start a session first.")
else:
self.focus_duration += 5
self.setted_focus_time_label.configure(text=f"Setted Focus Time: {self.focus_duration} min")
def minus_5_minutes(self):
if self.focus_duration == 0:
messagebox.showwarning("Warning", "Please Start a session first.")
elif self.focus_duration > 5:
self.focus_duration -= 5
else:
self.focus_duration = 0
self.setted_focus_time_label.configure(text=f"Setted Focus Time: {self.focus_duration} min")
def start_focus_session(self):
if not self.is_focus_running:
try:
self.focus_duration = int(self.focus_entry.get())
self.task = self.task_entry.get()
if not self.task:
messagebox.showwarning("Warning", "Please enter a task description.")
return
self.elapsed_time = 0
self.is_paused = False
self.task_label.configure(text=f"Current Task: {self.task}")
self.pause_button.configure(state="normal")
self.stop_button.configure(state="normal")
self.start_time = datetime.datetime.utcnow()
self.is_focus_running = True
self.is_break_running = False
self.break_timer_label.configure(text="")
threading.Thread(target=self.run_timer, daemon=True).start()
self.setted_focus_time_label.configure(text=f"Setted Focus Time: {self.focus_duration} min")
except ValueError:
messagebox.showerror("Error", "Please enter a valid number for focus duration.")
def pause_focus_session(self):
if self.is_focus_running:
if self.is_paused:
if self.start_pause_time is None:
self.start_pause_time = datetime.datetime.utcnow()
self.total_paused_time_in_seconds += int((
datetime.datetime.utcnow() - self.start_pause_time).total_seconds())
self.is_paused = False
self.pause_button.configure(text="Pause")
self.start_pause_time = None
else:
self.start_pause_time = datetime.datetime.utcnow()
self.is_paused = True
self.pause_button.configure(text="Resume")
def update_focus_timer(self):
text = self.format_time(self.elapsed_time)
self.focus_timer_label.configure(text=f"Focus Timer: {text}")
def stop_focus_session(self):
if self.is_focus_running:
self.is_focus_running = False
self.update_stats()
self.log_focus_to_calendar()
self.reset_focus_timer()
self.start_break_timer()
self.total_paused_time_in_seconds = 0
self.start_pause_time = None
def reset_focus_timer(self):
self.elapsed_time = 0
self.is_focus_running = False
self.is_paused = False
self.focus_timer_label.configure(text="Focus Timer: 00:00")
self.pause_button.configure(state="disabled")
self.stop_button.configure(state="disabled")
self.start_focus_button.configure(state="normal")
def log_focus_to_calendar(self):
if self.start_time:
elapsed_minutes = self.elapsed_time // 60
cc_email = self.cc_email_entry.get()
add_focus_session_to_calendar(self.start_time, elapsed_minutes, self.task, cc_email if cc_email else None,
self.selected_color_id)
def ask_add_more_time_on_complete(self):
# Ask the user whether to add 5 more minutes
add_more_time = messagebox.askyesno("Add More Time",
"Focus Session Complete! Would you like to add 5 more minutes?")
if add_more_time:
self.add_5_minutes(force=True)
return add_more_time
def run_timer(self):
self.start_time = datetime.datetime.utcnow()
self.last_notification = 0
self.update_timers()
def update_timers(self):
if self.is_focus_running and not self.is_paused:
current_time = datetime.datetime.utcnow()
self.elapsed_time = int(
(current_time - self.start_time).total_seconds()) - self.total_paused_time_in_seconds
self.update_focus_timer()
# Play sound every 15 minutes
if self.elapsed_time - self.last_notification >= 900 and self.freq_sound_var.get(): # 900 seconds = 15 minutes
self.play_sound(get_resource_path("sounds/wow-171498.mp3"))
self.last_notification = self.elapsed_time
if self.elapsed_time >= self.focus_duration * 60:
self.play_sound(get_resource_path("sounds/success-fanfare-trumpets-6185.mp3"))
if self.ask_add_more_time_on_complete():
self.after(1000, self.update_timers)
else:
self.is_focus_running = False
self.log_focus_to_calendar()
self.update_stats()
self.reset_focus_timer()
self.start_break_timer()
else:
self.after(1000, self.update_timers)
elif self.is_focus_running and self.is_paused:
self.after(1000, self.update_timers)
elif self.is_break_running:
text = self.format_time(int((datetime.datetime.utcnow() - self.start_time).total_seconds()))
self.break_timer_label.configure(text=f"Break Timer: {text}")
self.after(1000, self.update_timers)
def start_break_timer(self):
self.start_time = datetime.datetime.utcnow()
self.is_focus_running = False
self.is_break_running = True
self.break_timer_label.configure(text="Break Timer: 00:00")
self.update_timers()
def update_stats(self):
self.total_focus_time += self.elapsed_time
self.sessions_completed += 1
self.total_focus_label.configure(text=f"Total Focus Time: {self.format_time(self.total_focus_time)}")
self.sessions_label.configure(text=f"Sessions Completed: {self.sessions_completed}")
# Update daily focus times
today = datetime.date.today().isoformat()
if today in self.daily_focus_times:
self.daily_focus_times[today] += self.elapsed_time
else:
self.daily_focus_times[today] = self.elapsed_time
self.save_stats()
self.update_productivity_graph()
def reset_stats(self):
if messagebox.askyesno("Reset Statistics", "Are you sure you want to reset your statistics?"):
self.total_focus_time = 0
self.sessions_completed = 0
self.daily_focus_times = {}
self.total_focus_label.configure(text="Total Focus Time: 00:00:00")
self.sessions_label.configure(text="Sessions Completed: 0")
self.save_stats()
self.update_productivity_graph()
def save_stats(self):
stats = {
"total_focus_time": self.total_focus_time,
"sessions_completed": self.sessions_completed,
"daily_focus_times": self.daily_focus_times
}
with open("focus_stats.json", "w") as f:
json.dump(stats, f)
def load_stats(self):
try:
with open("focus_stats.json", "r") as f:
stats = json.load(f)
self.total_focus_time = stats.get("total_focus_time", 0)
self.sessions_completed = stats.get("sessions_completed", 0)
self.daily_focus_times = stats.get("daily_focus_times", {})
except FileNotFoundError:
self.total_focus_time = 0
self.sessions_completed = 0
self.daily_focus_times = {}
@staticmethod
def format_time(seconds):
hours, remainder = divmod(seconds, 3600)
minutes, seconds = divmod(remainder, 60)
if hours == 0:
text = f"{minutes:02}:{seconds:02}"
else:
text = f"{hours:02}:{minutes:02}:{seconds:02}"
return text
def play_sound(self, sound_file):
if self.sound_var.get():
try:
playsound(sound_file)
except Exception as e:
print(f"Error playing sound: {e}")
def update_productivity_graph(self):
self.ax.clear()
dates = list(self.daily_focus_times.keys())[-7:] # Last 7 days
times = [self.daily_focus_times[date] / 3600 for date in dates] # Convert to hours
dates = [date.split("-")[1] + "-" + date.split("-")[2] for date in dates]
if len(times) == 0:
average_time = 0
else:
average_time = sum(times) / len(times)
self.ax.bar(dates, times, color='skyblue')
self.ax.axhline(average_time, color='red', linestyle='--', label='Average Time')
self.ax.set_xlabel('Date')
self.ax.set_ylabel('Focus Time (hours)')
self.ax.set_title('Daily Focus Time (Last 7 Days)')
self.ax.tick_params(axis='x')
max_time = max(times) if times else 1
self.ax.set_yticks([i for i in range(0, int(max_time) + 1)]) # Set y-ticks to be every hour
self.fig.tight_layout()
self.canvas.draw()
def on_color_select(self, event):
"""Update the preview label with the selected color."""
self.selected_color_id = int(self.color_var.get().split(':')[0])
color_info = COLORS[self.selected_color_id]
self.color_dropdown.configure(fg_color=color_info["background"],
button_color=color_info["background"], ) # Update background color
if __name__ == "__main__":
app = FocusApp()
app.mainloop()