@@ -29,9 +29,9 @@ const restartSkipButton = document.getElementById("restartSkipButton");
29
29
const restartSkipIcon = restartSkipButton . getElementById ( "combo-button-icon" ) ;
30
30
31
31
//Default these incase they haven't setup custom times on mobile app
32
- const totalFlowInSeconds = 5 ; // 1500;
33
- const totalShortBreakInSeconds = 10 ; // 300;
34
- const totalLongBreakInSeconds = 15 ; // 900;
32
+ const totalFlowInSeconds = 1500 ;
33
+ const totalShortBreakInSeconds = 300 ;
34
+ const totalLongBreakInSeconds = 900 ;
35
35
const totalSets = 4 ;
36
36
const flowText = "Flow" ;
37
37
const breakText = "Break" ;
@@ -41,7 +41,7 @@ let flow = true; //used to toggle between flow intervals and breaks
41
41
let currentSprintTotalTime ;
42
42
let currentSet = 1 ;
43
43
let deadlineInSeconds ;
44
- let timeLeft = totalFlowInSeconds ;
44
+ let timeLeft ; // = totalFlowInSeconds;
45
45
var countdown ;
46
46
47
47
//setup clock
@@ -72,12 +72,30 @@ display.onchange = () => { // optimize battery life
72
72
messaging . peerSocket . onmessage = ( evt ) => {
73
73
//persist
74
74
writeToFile ( evt . data , "flowSettings.txt" ) ;
75
+ //determine time left
76
+ paused = true ;
77
+ saveStateToFile ( ) ;
75
78
setupWithUserSettings ( ) ;
79
+ restart ( ) ;
76
80
}
77
81
me . onunload = ( ) => {
78
82
//save data on exit
79
- let text = flow ? flowText : breakText ;
80
- writeToFile ( { deadlineInSeconds : deadlineInSeconds , timeLeft : timeLeft , state : currentSprintTotalTime , flow : flow , set : currentSet , paused : paused } , "saveState.txt" ) ;
83
+ saveStateToFile ( ) ;
84
+ }
85
+
86
+ function saveStateToFile ( ) {
87
+ let text = flow ? flowText : breakText ;
88
+ let breakState = null ;
89
+ if ( ! flow ) {
90
+ console . log ( `${ currentSprintTotalTime } === ${ totalShortBreakInSeconds } ` ) ;
91
+ if ( currentSprintTotalTime === totalShortBreakInSeconds ) {
92
+ breakState = "short" ;
93
+ }
94
+ else {
95
+ breakState = "long" ;
96
+ }
97
+ }
98
+ writeToFile ( { deadlineInSeconds : deadlineInSeconds , timeLeft : timeLeft , breakState : breakState , flow : flow , set : currentSet , paused : paused } , "saveState.txt" ) ;
81
99
}
82
100
83
101
@@ -127,6 +145,7 @@ function nextSprint(){
127
145
128
146
if ( flow ) {
129
147
currentSet < totalSets ? currentSet ++ : currentSet = 1 ;
148
+ console . log ( `nextSprint` ) ;
130
149
setupSprint ( totalFlowInSeconds , totalFlowInSeconds , flowText , 0 , ( Date . now ( ) / 1000 ) + totalFlowInSeconds ) ;
131
150
132
151
}
@@ -166,6 +185,7 @@ function play(){
166
185
}
167
186
168
187
function pause ( ) {
188
+ console . log ( `pause` ) ;
169
189
paused = true ;
170
190
//stop the clock
171
191
clearInterval ( countdown ) ;
@@ -183,6 +203,7 @@ function skip(){
183
203
184
204
function restart ( ) {
185
205
pause ( ) ;
206
+ console . log ( `restart` ) ;
186
207
setupSprint ( currentSprintTotalTime , currentSprintTotalTime , flow ? flowText : breakText , 0 , ( Date . now ( ) / 1000 ) + currentSprintTotalTime ) ;
187
208
}
188
209
@@ -193,19 +214,22 @@ function setupWithUserSettings(){
193
214
}
194
215
catch ( err ) {
195
216
settings = { flowTime : 0 , shortBreakTime : 0 , longBreakTime : 0 }
196
- writeToFile ( { flowTime : 0 , shortBreakTime : 0 , longBreakTime : 0 } , "flowSettings.txt" ) ;
217
+ // writeToFile({flowTime: 0, shortBreakTime: 0, longBreakTime: 0}, "flowSettings.txt");
197
218
}
198
219
//setup consts based on user settings
199
220
totalFlowInSeconds = settings . flowTime == 0 ? totalFlowInSeconds : parseInt ( settings . flowTime ) * 60 ;
200
221
totalShortBreakInSeconds = settings . shortBreakTime == 0 ? totalShortBreakInSeconds : parseInt ( settings . shortBreakTime ) * 60 ;
201
222
totalLongBreakInSeconds = settings . longBreakTime == 0 ? totalLongBreakInSeconds : parseInt ( settings . longBreakTime ) * 60 ;
223
+ currentSprintTotalTime = totalFlowInSeconds ;
224
+ timeLeft = currentSprintTotalTime ;
202
225
203
226
//pick up where the user ended
204
227
try { //read settings about past states
205
228
restorePreviousSession ( fs . readFileSync ( "saveState.txt" , "cbor" ) ) ;
206
229
}
207
230
catch ( err ) {
208
231
console . log ( err ) ;
232
+ console . log ( `err` ) ;
209
233
setupSprint ( totalFlowInSeconds , totalFlowInSeconds , flowText , 0 , ( Date . now ( ) / 1000 ) + totalFlowInSeconds ) ;
210
234
}
211
235
}
@@ -218,14 +242,35 @@ function restorePreviousSession(saveState){
218
242
219
243
220
244
flow = saveState . flow ;
221
- currentSprintTotalTime = saveState . state ;
245
+ if ( flow ) {
246
+ currentSprintTotalTime = totalFlowInSeconds ;
247
+ }
248
+ else {
249
+ if ( saveState . breakState === "short" ) {
250
+ console . log ( saveState . breakState ) ;
251
+ currentSprintTotalTime = totalShortBreakInSeconds ;
252
+ }
253
+ else if ( saveState . breakState === "long" ) {
254
+ console . log ( saveState . breakState ) ;
255
+ currentSprintTotalTime = totalLongBreakInSeconds ;
256
+ }
257
+ else { //something went wrong
258
+ console . log ( "ERR" + saveState . breakState ) ;
259
+ console . log ( typeof saveState . breakState ) ;
260
+ console . log ( saveState . breakState == "short" ) ;
261
+ currentSprintTotalTime = 0 ;
262
+ skip ( ) ;
263
+ }
264
+ }
265
+
222
266
currentSet = saveState . set ;
223
267
224
268
let text = flow ? flowText : breakText ;
225
269
deadlineInSeconds = saveState . paused ? ( Date . now ( ) / 1000 ) + saveState . timeLeft : saveState . deadlineInSeconds ;
226
270
saveState . paused ? pause ( ) : play ( ) ;
227
271
if ( deadlineInSeconds >= ( Date . now ( ) / 1000 ) || saveState . paused ) {
228
- setupSprint ( currentSprintTotalTime , timeLeft , text , secondsToAngle ( currentSprintTotalTime - deadlineInSeconds ) , deadlineInSeconds ) ;
272
+ console . log ( `RESTORE setting up with total time ${ currentSprintTotalTime } , timeleft ${ timeLeft } and angle ${ secondsToAngle ( currentSprintTotalTime - timeLeft ) } ` ) ;
273
+ setupSprint ( currentSprintTotalTime , timeLeft , text , secondsToAngle ( currentSprintTotalTime - timeLeft ) , deadlineInSeconds ) ;
229
274
}
230
275
else { //finished sprint while our of app, so move to the next
231
276
skip ( ) ;
0 commit comments