-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
154 lines (142 loc) · 4.65 KB
/
script.js
File metadata and controls
154 lines (142 loc) · 4.65 KB
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
let focusTime = 0;
let currentFocusTime = 0;
let breakTime = 0;
let totalBreakTime = 0;
let untrackedTime = 0;
let focusSessions = 0;
let breakSessions = 0;
let untrackedSessions = 0;
let divisor = 3;
let mode = "Untracked";
let timer;
let titleEmoji = "🟡";
window.onload = function() {
loadState();
updateDisplay();
}
document.addEventListener('DOMContentLoaded', function () {
if (!Notification) {
alert('Desktop notifications not available in your browser. Try Chromium.');
return;
}
if (Notification.permission !== 'granted')
Notification.requestPermission();
});
function startFocus() {
clearInterval(timer);
mode = "Focus";
currentFocusTime = 0;
timer = setInterval(function() {
focusTime++;
currentFocusTime++;
if (currentFocusTime % divisor === 0) {
breakTime++;
}
titleEmoji = titleEmoji === "🔴" ? "🔴" : "🔴";
updateDisplay();
}, 1000);
focusSessions++;
}
function startBreak() {
if (breakTime > 0) {
clearInterval(timer);
mode = "Break";
timer = setInterval(function() {
breakTime--;
totalBreakTime++;
if (breakTime === 0) {
startUntracked();
if (Notification.permission === "granted") {
new Notification('Break ended', {
body: 'Your break has ended. Time to get back to work!',
});
}
}
updateDisplay();
}, 1000);
breakSessions++;
}
}
function startUntracked() {
clearInterval(timer);
mode = "Untracked";
timer = setInterval(function() {
untrackedTime++;
updateDisplay();
}, 1000);
untrackedSessions++;
}
function applyDivisor() {
divisor = document.getElementById("divisorInput").value;
updateDisplay();
}
function reset() {
clearInterval(timer);
focusTime = 0;
currentFocusTime = 0;
breakTime = 0;
totalBreakTime = 0;
untrackedTime = 0;
focusSessions = 0;
breakSessions = 0;
untrackedSessions = 0;
mode = "Untracked";
updateDisplay();
}
function updateDisplay() {
let clockTime = formatTime(mode === "Focus" ? currentFocusTime : mode === "Break" ? breakTime : untrackedTime);
document.getElementById("clock").innerText = clockTime;
let statusText = document.getElementById("statusText");
statusText.innerText = mode;
if (mode === "Focus") {
statusText.style.color = "var(--color-dark-red)";
} else if (mode === "Break") {
statusText.style.color = "var(--color-dark-green)";
} else {
statusText.style.color = "var(--color-dark-yellow)";
}
document.getElementById("availableBreak").innerText = formatTime(breakTime);
document.getElementById("totalFocus").innerText = formatTime(focusTime);
document.getElementById("focusSessions").innerText = focusSessions;
document.getElementById("totalBreak").innerText = formatTime(totalBreakTime);
document.getElementById("breakSessions").innerText = breakSessions;
document.getElementById("totalUntracked").innerText = formatTime(untrackedTime);
document.getElementById("untrackedSessions").innerText = untrackedSessions;
document.title = mode === "Focus" ? `${titleEmoji} F ${clockTime}` : mode === "Break" ? `🟢 B ${clockTime}` : `🟡 U`;
saveState();
}
function formatTime(time) {
let hours = Math.floor(time / 3600);
let minutes = Math.floor((time % 3600) / 60);
let seconds = time % 60;
return `${hours.toString().padStart(2, "0")}:${minutes.toString().padStart(2, "0")}:${seconds.toString().padStart(2, "0")}`;
}
function saveState() {
localStorage.setItem("flowtime", JSON.stringify({
focusTime: focusTime,
currentFocusTime: currentFocusTime,
breakTime: breakTime,
totalBreakTime: totalBreakTime,
untrackedTime: untrackedTime,
focusSessions: focusSessions,
breakSessions: breakSessions,
untrackedSessions: untrackedSessions,
divisor: divisor,
mode: mode
}));
}
function loadState() {
let savedState = JSON.parse(localStorage.getItem("flowtime"));
if (savedState) {
focusTime = savedState.focusTime;
currentFocusTime = savedState.currentFocusTime;
breakTime = savedState.breakTime;
totalBreakTime = savedState.totalBreakTime;
untrackedTime = savedState.untrackedTime;
focusSessions = savedState.focusSessions;
breakSessions = savedState.breakSessions;
untrackedSessions = savedState.untrackedSessions;
divisor = savedState.divisor;
mode = savedState.mode;
}
}