Skip to content

Commit 086b5fa

Browse files
committed
closing app keeps track of countdown timer/sprint counter
1 parent b088564 commit 086b5fa

File tree

5 files changed

+77
-40
lines changed

5 files changed

+77
-40
lines changed

app/index.js

Lines changed: 69 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import document from "document";
55
import * as messaging from "messaging";
66
import { display } from "display";
7+
import { me } from "appbit";
78
import * as fs from "fs";
89
import clock from "clock";
910
clock.granularity = "minutes";
@@ -24,7 +25,8 @@ const restartSkipButton = document.getElementById("restartSkipButton");
2425
const restartSkipIcon = restartSkipButton.getElementById("combo-button-icon");
2526

2627

27-
const totalFlowInSeconds = 5; //1500;
28+
//Default these incase they haven't setup custom times on mobile app
29+
const totalFlowInSeconds = 1500;
2830
const totalShortBreakInSeconds = 10;//300;
2931
const totalLongBreakInSeconds = 15;//900;
3032
const totalSprints = 4;
@@ -35,13 +37,14 @@ let counting = false;
3537
let countdownSeconds = totalFlowInSeconds;
3638
let flow = true; //used to toggle between flow intervals and breaks
3739
let currentIntervalTime;
38-
let currentIntervalText;
3940
let currentSprint = 1;
4041

4142
//setup clock
4243
clock.ontick = (evt) => {
43-
let hours = evt.date.getHours();
44-
let minutes = evt.date.getMinutes();
44+
let hours;
45+
let minutes;
46+
hours = evt.date.getHours();
47+
minutes = evt.date.getMinutes();
4548
hours = hours > 12 && preferences.clockDisplay === "12h" ? hours - 12 : hours;
4649
hours = hours < 10 ? "0" + hours : hours;
4750
minutes = minutes < 10 ? "0" + minutes : minutes;
@@ -66,20 +69,25 @@ display.onchange = () => { // optimize battery life
6669

6770
messaging.peerSocket.onmessage = (evt)=>{
6871
//persist
69-
writeToFile(evt);
72+
writeToFile(evt.data, "flowSettings.txt");
7073
setupWithUserSettings();
7174
}
7275

76+
me.onunload = () => {
77+
//save data on exit
78+
let text = flow ? flowText : breakText;
79+
writeToFile({closeTime: Date.now(), timeLeft: countdownSeconds, text: text, state: currentIntervalTime, flow: flow, sprint: currentSprint, counting: counting},"saveState.txt");
80+
}
7381

7482

75-
76-
function writeToFile(evt){
77-
let log = 'write to file: {';
78-
for (var prop in evt.data){
79-
log += ` ${prop}:${evt.data[prop]}`;
83+
function writeToFile(data, fileName){
84+
let log;
85+
log = 'write to file: {';
86+
for (var prop in data){
87+
log += ` ${prop}:${data[prop]}`;
8088
}
8189
console.log(log + " }");
82-
fs.writeFileSync("flowSettings.txt", evt.data, "cbor");
90+
fs.writeFileSync(fileName, data, "cbor");
8391
}
8492

8593
function secondsToAngle(seconds){
@@ -116,47 +124,73 @@ function nextSprint(){
116124
flow = !flow;
117125

118126
if (flow){
119-
setupNextInterval(totalFlowInSeconds, flowText);
120127
currentSprint < totalSprints ? currentSprint++ : currentSprint = 1;
121-
122-
sprintCounter.text = `${currentSprint} of ${totalSprints}`
128+
setupNextInterval(totalFlowInSeconds, totalFlowInSeconds, flowText, 0, currentSprint);
129+
123130
}
124131
else {
125132
if(currentSprint < totalSprints){
126-
setupNextInterval(totalShortBreakInSeconds, breakText);
133+
setupNextInterval(totalShortBreakInSeconds, totalShortBreakInSeconds, breakText, 0, currentSprint);
127134
}
128135
else{
129-
setupNextInterval(totalLongBreakInSeconds, breakText);
136+
setupNextInterval(totalLongBreakInSeconds, totalLongBreakInSeconds, breakText, 0, currentSprint);
130137
}
131138
}
132139

133140
vibration.start("nudge");
134141
}
135142

136143
function setupWithUserSettings(){
144+
let settings;
137145
try {
138-
fs.readFileSync("flowSettings.txt", "cbor")
146+
settings = fs.readFileSync("flowSettings.txt", "cbor");
139147
}
140148
catch(err){
141-
writeToFile({data : {flowTime: 0, shortBreakTime: 0, longBreakTime: 0}});
149+
settings = {flowTime: 0, shortBreakTime: 0, longBreakTime: 0}
150+
writeToFile({flowTime: 0, shortBreakTime: 0, longBreakTime: 0}, "flowSettings.txt");
142151
}
143-
let settings = fs.readFileSync("flowSettings.txt", "cbor");
144-
145152
//setup consts based on user settings
146153
totalFlowInSeconds = settings.flowTime == 0 ? totalFlowInSeconds : parseInt(settings.flowTime)*60;
147154
totalShortBreakInSeconds = settings.shortBreakTime == 0? totalShortBreakInSeconds : parseInt(settings.shortBreakTime)*60;
148155
totalLongBreakInSeconds = settings.longBreakTime == 0 ? totalLongBreakInSeconds : parseInt(settings.longBreakTime)*60;
149-
setupNextInterval(totalFlowInSeconds, flowText);
156+
157+
//pick up where the user ended
158+
try {
159+
//{closeTime: Date.now(), timeLeft: countdownSeconds, text: text, state: currentIntervalTime, sprint: currentSprint, counting: counting}
160+
let saveState = fs.readFileSync("saveState.txt", "cbor");
161+
//secondsLeft when app closed - seconds passed since exit
162+
let secondsLeft = saveState.timeLeft - ((Date.now() - parseInt(saveState.closeTime))/1000);
163+
secondsLeft <= 0 ? 0 : Math.ceil(secondsLeft);
164+
countdownSeconds = saveState.secondsLeft;
165+
flow = saveState.flow;
166+
currentIntervalTime = saveState.state;
167+
currentSprint = saveState.sprint;
168+
if (secondsLeft <= 0){
169+
pause();
170+
nextSprint();
171+
}
172+
else{
173+
saveState.counting ? play() : pause();
174+
currentIntervalTime = saveState.state;
175+
setupNextInterval(currentIntervalTime, countDownseconds, saveState.text, secondsToAngle(currentIntervalTime - countDownseconds), currentSprint);
176+
}
177+
}
178+
catch (err) {
179+
console.log(err);
180+
setupNextInterval(totalFlowInSeconds, totalFlowInSeconds, flowText, 0, currentSprint);
181+
}
150182
}
151183

152-
function setupNextInterval(nextIntervalSeconds, text){
184+
function setupNextInterval(nextIntervalSeconds, secondsLeft, text, angleLeft, currentSprint){
153185
//change the arc back to 0
154-
progressArc.sweepAngle = 0;
186+
progressArc.sweepAngle = angleLeft;
155187
//update interval text
156188
currentIntervalText.text = text;
157189
//set the correct seconds for progress
158-
currentIntervalTime = countdownSeconds = nextIntervalSeconds;
159-
currentIntervalText = text;
190+
currentIntervalTime = nextIntervalSeconds;
191+
countdownSeconds = secondsLeft;
192+
currentIntervalText.text = text;
193+
sprintCounter.text = `${currentSprint} of ${totalSprints}`;
160194
}
161195

162196
function play(){
@@ -182,20 +216,20 @@ function skip(){
182216

183217
function restart(){
184218
pause();
185-
setupNextInterval(currentIntervalTime, currentIntervalText);
219+
setupNextInterval(currentIntervalTime, currentIntervalTime, flow ? flowText : breakText, 0, currentSprint);
186220
}
187221

188222
function formatCountdown(seconds){
189223
//calculate time left
190-
let formattedHours = Math.floor((seconds/60/60) % 60);
191-
let formattedMinutes = Math.floor((seconds/60) % 60);
192-
let formattedSeconds = Math.floor(seconds % 60);
224+
let formattedHours = Math.floor((seconds/60/60) % 60);
225+
let formattedMinutes = Math.floor((seconds/60) % 60);
226+
let formattedSeconds = Math.floor(seconds % 60);
193227

194-
//pad with 0
195-
formattedHours = formattedHours < 10 ? "0" + formattedHours : formattedHours;
196-
formattedMinutes = formattedMinutes < 10 ? "0" + formattedMinutes : formattedMinutes;
197-
formattedSeconds = formattedSeconds < 10 ? "0" + formattedSeconds : formattedSeconds;
228+
//pad with 0
229+
formattedHours = formattedHours < 10 ? "0" + formattedHours : formattedHours;
230+
formattedMinutes = formattedMinutes < 10 ? "0" + formattedMinutes : formattedMinutes;
231+
formattedSeconds = formattedSeconds < 10 ? "0" + formattedSeconds : formattedSeconds;
198232

199-
//update countdown text
200-
countdown.text = `${formattedHours}:${formattedMinutes}:${formattedSeconds}`;
233+
//update countdown text
234+
countdown.text = `${formattedHours}:${formattedMinutes}:${formattedSeconds}`;
201235
}

app/style.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import document from "document";
66
const background = document.getElementById("background");
77
const backgroundColor = document.getElementsByClassName("backgroundColor")
88
const progressArc = document.getElementById("progressArc");
9-
const backgroundArc = document.getElementById("backgroundArc");
109
const countdown = document.getElementById("countdown");
1110
const sprintCounter = document.getElementById("sprintCounter");
1211
const playPauseButton = document.getElementById("playPauseButton");
12+
const restartSkipButton = document.getElementById("restartSkipButton");
1313
const oldBackground = background.value;
1414

1515
let styleInterval;
@@ -28,14 +28,16 @@ export function stopStyle(){ // optimize battery life
2828
clearInterval(styleInterval);
2929
}
3030

31+
let colorProfile;
3132
function applyStyle(value){
32-
let colorProfile = styleProfiles[value];
33+
colorProfile = styleProfiles[value];
3334

3435
backgroundColor[value].style.fill = colorProfile.background;
3536
progressArc.style.fill = colorProfile.accentColor;
3637
backgroundArc.style.fill = colorProfile.backgroundArc;
3738
countdown.style.fill = colorProfile.accentColor;
3839
playPauseButton.style.fill = colorProfile.accentColor;
40+
restartSkipButton.style.fill = colorProfile.accentColor;
3941
sprintCounter.style.fill = colorProfile.baseColor;
4042
}
4143

companion/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { settingsStorage } from "settings";
55
import { me } from "companion";
66
import * as messaging from "messaging";
77

8+
89
// Event fires when a setting is changed
910
settingsStorage.onchange = (evt) => { sendSettings(); }
1011

@@ -18,9 +19,9 @@ function sendSettings(){
1819
let shortTime = settingsStorage.getItem("shortBreakTimeSlider")
1920
let longTime = settingsStorage.getItem("longBreakTimeSlider")
2021
let data = {
21-
flowTime : flowTime.substring(1, flowTime.length-1),
22-
shortBreakTime : shortTime.substring(1, shortTime.length-1),
23-
longBreakTimeSlider : longTime.substring(1, longTime.length-1)
22+
flowTime : flowTime ? flowTime.substring(1, flowTime.length-1) : 0,
23+
shortBreakTime : shortTime ? shortTime.substring(1, shortTime.length-1) : 0,
24+
longBreakTime : longTime ? longTime.substring(1, longTime.length-1) : 0
2425
}
2526

2627
// If we have a MessageSocket, send the data to the device

photos.png

-2.57 KB
Binary file not shown.

resources/icon.png

193 Bytes
Loading

0 commit comments

Comments
 (0)