Skip to content

Commit 2583ba0

Browse files
committed
big fixes related to reloading
1 parent 7c700b9 commit 2583ba0

File tree

1 file changed

+54
-9
lines changed

1 file changed

+54
-9
lines changed

app/index.js

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ const restartSkipButton = document.getElementById("restartSkipButton");
2929
const restartSkipIcon = restartSkipButton.getElementById("combo-button-icon");
3030

3131
//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;
3535
const totalSets = 4;
3636
const flowText = "Flow";
3737
const breakText = "Break";
@@ -41,7 +41,7 @@ let flow = true; //used to toggle between flow intervals and breaks
4141
let currentSprintTotalTime;
4242
let currentSet = 1;
4343
let deadlineInSeconds;
44-
let timeLeft = totalFlowInSeconds;
44+
let timeLeft; //= totalFlowInSeconds;
4545
var countdown;
4646

4747
//setup clock
@@ -72,12 +72,30 @@ display.onchange = () => { // optimize battery life
7272
messaging.peerSocket.onmessage = (evt)=>{
7373
//persist
7474
writeToFile(evt.data, "flowSettings.txt");
75+
//determine time left
76+
paused = true;
77+
saveStateToFile();
7578
setupWithUserSettings();
79+
restart();
7680
}
7781
me.onunload = () => {
7882
//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");
8199
}
82100

83101

@@ -127,6 +145,7 @@ function nextSprint(){
127145

128146
if (flow){
129147
currentSet < totalSets ? currentSet++ : currentSet = 1;
148+
console.log(`nextSprint`);
130149
setupSprint(totalFlowInSeconds, totalFlowInSeconds, flowText, 0, (Date.now()/1000) + totalFlowInSeconds);
131150

132151
}
@@ -166,6 +185,7 @@ function play(){
166185
}
167186

168187
function pause(){
188+
console.log(`pause`);
169189
paused = true;
170190
//stop the clock
171191
clearInterval(countdown);
@@ -183,6 +203,7 @@ function skip(){
183203

184204
function restart(){
185205
pause();
206+
console.log(`restart`);
186207
setupSprint(currentSprintTotalTime, currentSprintTotalTime, flow ? flowText : breakText, 0, (Date.now()/1000) + currentSprintTotalTime);
187208
}
188209

@@ -193,19 +214,22 @@ function setupWithUserSettings(){
193214
}
194215
catch(err){
195216
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");
197218
}
198219
//setup consts based on user settings
199220
totalFlowInSeconds = settings.flowTime == 0 ? totalFlowInSeconds : parseInt(settings.flowTime)*60;
200221
totalShortBreakInSeconds = settings.shortBreakTime == 0? totalShortBreakInSeconds : parseInt(settings.shortBreakTime)*60;
201222
totalLongBreakInSeconds = settings.longBreakTime == 0 ? totalLongBreakInSeconds : parseInt(settings.longBreakTime)*60;
223+
currentSprintTotalTime = totalFlowInSeconds;
224+
timeLeft = currentSprintTotalTime;
202225

203226
//pick up where the user ended
204227
try { //read settings about past states
205228
restorePreviousSession(fs.readFileSync("saveState.txt", "cbor"));
206229
}
207230
catch (err) {
208231
console.log(err);
232+
console.log(`err`);
209233
setupSprint(totalFlowInSeconds, totalFlowInSeconds, flowText, 0, (Date.now()/1000) + totalFlowInSeconds);
210234
}
211235
}
@@ -218,14 +242,35 @@ function restorePreviousSession(saveState){
218242

219243

220244
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+
222266
currentSet = saveState.set;
223267

224268
let text = flow ? flowText : breakText;
225269
deadlineInSeconds = saveState.paused ? (Date.now()/1000) + saveState.timeLeft : saveState.deadlineInSeconds;
226270
saveState.paused ? pause() : play();
227271
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);
229274
}
230275
else{ //finished sprint while our of app, so move to the next
231276
skip();

0 commit comments

Comments
 (0)