4
4
import document from "document" ;
5
5
import * as messaging from "messaging" ;
6
6
import { display } from "display" ;
7
+ import { me } from "appbit" ;
7
8
import * as fs from "fs" ;
8
9
import clock from "clock" ;
9
10
clock . granularity = "minutes" ;
@@ -24,7 +25,8 @@ const restartSkipButton = document.getElementById("restartSkipButton");
24
25
const restartSkipIcon = restartSkipButton . getElementById ( "combo-button-icon" ) ;
25
26
26
27
27
- const totalFlowInSeconds = 5 ; //1500;
28
+ //Default these incase they haven't setup custom times on mobile app
29
+ const totalFlowInSeconds = 1500 ;
28
30
const totalShortBreakInSeconds = 10 ; //300;
29
31
const totalLongBreakInSeconds = 15 ; //900;
30
32
const totalSprints = 4 ;
@@ -35,13 +37,14 @@ let counting = false;
35
37
let countdownSeconds = totalFlowInSeconds ;
36
38
let flow = true ; //used to toggle between flow intervals and breaks
37
39
let currentIntervalTime ;
38
- let currentIntervalText ;
39
40
let currentSprint = 1 ;
40
41
41
42
//setup clock
42
43
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 ( ) ;
45
48
hours = hours > 12 && preferences . clockDisplay === "12h" ? hours - 12 : hours ;
46
49
hours = hours < 10 ? "0" + hours : hours ;
47
50
minutes = minutes < 10 ? "0" + minutes : minutes ;
@@ -66,20 +69,25 @@ display.onchange = () => { // optimize battery life
66
69
67
70
messaging . peerSocket . onmessage = ( evt ) => {
68
71
//persist
69
- writeToFile ( evt ) ;
72
+ writeToFile ( evt . data , "flowSettings.txt" ) ;
70
73
setupWithUserSettings ( ) ;
71
74
}
72
75
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
+ }
73
81
74
82
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 ] } ` ;
80
88
}
81
89
console . log ( log + " }" ) ;
82
- fs . writeFileSync ( "flowSettings.txt" , evt . data , "cbor" ) ;
90
+ fs . writeFileSync ( fileName , data , "cbor" ) ;
83
91
}
84
92
85
93
function secondsToAngle ( seconds ) {
@@ -116,47 +124,73 @@ function nextSprint(){
116
124
flow = ! flow ;
117
125
118
126
if ( flow ) {
119
- setupNextInterval ( totalFlowInSeconds , flowText ) ;
120
127
currentSprint < totalSprints ? currentSprint ++ : currentSprint = 1 ;
121
-
122
- sprintCounter . text = ` ${ currentSprint } of ${ totalSprints } `
128
+ setupNextInterval ( totalFlowInSeconds , totalFlowInSeconds , flowText , 0 , currentSprint ) ;
129
+
123
130
}
124
131
else {
125
132
if ( currentSprint < totalSprints ) {
126
- setupNextInterval ( totalShortBreakInSeconds , breakText ) ;
133
+ setupNextInterval ( totalShortBreakInSeconds , totalShortBreakInSeconds , breakText , 0 , currentSprint ) ;
127
134
}
128
135
else {
129
- setupNextInterval ( totalLongBreakInSeconds , breakText ) ;
136
+ setupNextInterval ( totalLongBreakInSeconds , totalLongBreakInSeconds , breakText , 0 , currentSprint ) ;
130
137
}
131
138
}
132
139
133
140
vibration . start ( "nudge" ) ;
134
141
}
135
142
136
143
function setupWithUserSettings ( ) {
144
+ let settings ;
137
145
try {
138
- fs . readFileSync ( "flowSettings.txt" , "cbor" )
146
+ settings = fs . readFileSync ( "flowSettings.txt" , "cbor" ) ;
139
147
}
140
148
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" ) ;
142
151
}
143
- let settings = fs . readFileSync ( "flowSettings.txt" , "cbor" ) ;
144
-
145
152
//setup consts based on user settings
146
153
totalFlowInSeconds = settings . flowTime == 0 ? totalFlowInSeconds : parseInt ( settings . flowTime ) * 60 ;
147
154
totalShortBreakInSeconds = settings . shortBreakTime == 0 ? totalShortBreakInSeconds : parseInt ( settings . shortBreakTime ) * 60 ;
148
155
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
+ }
150
182
}
151
183
152
- function setupNextInterval ( nextIntervalSeconds , text ) {
184
+ function setupNextInterval ( nextIntervalSeconds , secondsLeft , text , angleLeft , currentSprint ) {
153
185
//change the arc back to 0
154
- progressArc . sweepAngle = 0 ;
186
+ progressArc . sweepAngle = angleLeft ;
155
187
//update interval text
156
188
currentIntervalText . text = text ;
157
189
//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 } ` ;
160
194
}
161
195
162
196
function play ( ) {
@@ -182,20 +216,20 @@ function skip(){
182
216
183
217
function restart ( ) {
184
218
pause ( ) ;
185
- setupNextInterval ( currentIntervalTime , currentIntervalText ) ;
219
+ setupNextInterval ( currentIntervalTime , currentIntervalTime , flow ? flowText : breakText , 0 , currentSprint ) ;
186
220
}
187
221
188
222
function formatCountdown ( seconds ) {
189
223
//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 ) ;
193
227
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 ;
198
232
199
- //update countdown text
200
- countdown . text = `${ formattedHours } :${ formattedMinutes } :${ formattedSeconds } ` ;
233
+ //update countdown text
234
+ countdown . text = `${ formattedHours } :${ formattedMinutes } :${ formattedSeconds } ` ;
201
235
}
0 commit comments