3
3
*/
4
4
import document from "document" ;
5
5
import * as messaging from "messaging" ;
6
+ import { display } from "display" ;
6
7
import * as fs from "fs" ;
7
8
import clock from "clock" ;
8
9
clock . granularity = "minutes" ;
9
10
import { preferences } from "user-settings" ;
10
11
import { vibration } from "haptics" ;
11
- import { style } from "./style.js"
12
+ import { style , stopStyle } from "./style.js"
12
13
13
14
//grab screen elements
14
15
const time = document . getElementById ( "time" ) ;
15
16
const progressArc = document . getElementById ( "progressArc" ) ;
16
17
const countdown = document . getElementById ( "countdown" ) ;
17
18
const sprintCounter = document . getElementById ( "sprintCounter" ) ;
18
- const sessionText = document . getElementById ( "sessionText " ) ;
19
+ const currentIntervalText = document . getElementById ( "currentIntervalText " ) ;
19
20
const playPauseButton = document . getElementById ( "playPauseButton" ) ;
20
21
const playPauseIcon = playPauseButton . getElementById ( "combo-button-icon" ) ;
21
22
const playPauseIconPressed = playPauseButton . getElementById ( "combo-button-icon-press" ) ;
22
23
23
24
24
- const flowInSeconds = 5 ; //1500;
25
- const shortBreakInSeconds = 10 ; //300;
26
- const longBreakInSeconds = 15 ; //900;
27
- const sprints = 4 ;
25
+ const totalFlowInSeconds = 5 ; //1500;
26
+ const totalShortBreakInSeconds = 10 ; //300;
27
+ const totalLongBreakInSeconds = 15 ; //900;
28
+ const totalSprints = 4 ;
28
29
const flowText = "Flow" ;
29
30
const breakText = "Break" ;
30
31
31
-
32
- let formattedHours = 0 ;
33
- let formattedMinutes = 0 ;
34
- let formattedSeconds = 0 ;
35
32
let counting = false ;
36
- let countdownSeconds = flowInSeconds ;
37
-
38
-
39
- let flow = true ; //used to toggle between flow sessions and breaks
40
- let sessionTime = flowInSeconds ;
33
+ let countdownSeconds = totalFlowInSeconds ;
34
+ let flow = true ; //used to toggle between flow intervals and breaks
35
+ let currentIntervalTime ;
41
36
let currentSprint = 1 ;
42
37
43
38
//setup clock
@@ -51,13 +46,17 @@ clock.ontick = (evt) => {
51
46
}
52
47
53
48
setupWithUserSettings ( ) ;
54
- style ( ) ;
49
+ style ( display ) ;
55
50
//clock.tick does not run in background so use setInterval instead
56
51
setInterval ( ( ) => progress ( ) , 1000 )
57
52
playPauseButton . onactivate = ( evt ) => {
58
53
counting ? pause ( ) : play ( ) ;
59
54
}
60
55
56
+ display . onchange = ( ) => { // optimize battery life
57
+ display . on ? style ( ) : stopStyle ( ) ;
58
+ }
59
+
61
60
messaging . peerSocket . onmessage = ( evt ) => {
62
61
//persist
63
62
writeToFile ( evt ) ;
@@ -68,41 +67,32 @@ messaging.peerSocket.onmessage = (evt)=>{
68
67
69
68
70
69
function writeToFile ( evt ) {
71
- fs . writeFileSync ( "flowSettings.txt" , evt . data , "cbor" ) ;
72
- }
73
-
74
- function setupWithUserSettings ( ) {
75
- try {
76
- fs . readFileSync ( "flowSettings.txt" , "cbor" )
77
- }
78
- catch ( err ) {
79
- writeToFile ( { data : { flowTime : 0 , shortBreakTime : 0 , longBreakTime : 0 } } ) ;
70
+ let log = 'write to file: {' ;
71
+ for ( var prop in evt . data ) {
72
+ log += ` ${ prop } :${ evt . data [ prop ] } ` ;
80
73
}
81
- let settings = fs . readFileSync ( "flowSettings.txt" , "cbor" ) ;
82
-
83
- //setup consts based on user settings
84
- flowInSeconds = settings . flowTime == 0 ? flowInSeconds : parseInt ( settings . flowTime ) * 60 ;
85
- shortBreakInSeconds = settings . shortBreakTime == 0 ? shortBreakInSeconds : parseInt ( settings . shortBreakTime ) * 60 ;
86
- longBreakInSeconds = settings . longBreakTime == 0 ? longBreakInSeconds : parseInt ( settings . longBreakTime ) * 60 ;
87
- setupSession ( flowInSeconds , flowText ) ;
74
+ console . log ( log + " }" ) ;
75
+ fs . writeFileSync ( "flowSettings.txt" , evt . data , "cbor" ) ;
88
76
}
89
77
90
-
91
78
function secondsToAngle ( seconds ) {
92
79
//degree per second * elapsedseconds
93
- return ( 360 / sessionTime ) * seconds ;
80
+ return ( 360 / currentIntervalTime ) * seconds ;
94
81
}
95
82
96
83
function progress ( ) {
97
84
if ( counting ) {
98
85
if ( ! isSprintOver ( countdownSeconds ) ) {
99
86
countdownSeconds -- ;
87
+ }
88
+ if ( display . on ) {
100
89
//calculate and update angle
101
- progressArc . sweepAngle = secondsToAngle ( sessionTime - countdownSeconds ) ;
90
+ progressArc . sweepAngle = secondsToAngle ( currentIntervalTime - countdownSeconds ) ;
102
91
}
103
92
}
104
-
105
- formatCountdown ( countdownSeconds ) ;
93
+ if ( display . on ) {
94
+ formatCountdown ( countdownSeconds ) ;
95
+ }
106
96
}
107
97
108
98
function isSprintOver ( seconds ) {
@@ -115,34 +105,50 @@ function isSprintOver(seconds){
115
105
}
116
106
117
107
function nextSprint ( ) {
118
- //swap session type
108
+ //swap interval type
119
109
flow = ! flow ;
120
110
121
111
if ( flow ) {
122
- setupSession ( flowInSeconds , flowText ) ;
123
- currentSprint < sprints ? currentSprint ++ : currentSprint = 1 ;
112
+ setupNextInterval ( totalFlowInSeconds , flowText ) ;
113
+ currentSprint < totalSprints ? currentSprint ++ : currentSprint = 1 ;
124
114
125
- sprintCounter . text = `${ currentSprint } of ${ sprints } `
115
+ sprintCounter . text = `${ currentSprint } of ${ totalSprints } `
126
116
}
127
117
else {
128
- if ( currentSprint < sprints ) {
129
- setupSession ( shortBreakInSeconds , breakText ) ;
118
+ if ( currentSprint < totalSprints ) {
119
+ setupNextInterval ( totalShortBreakInSeconds , breakText ) ;
130
120
}
131
121
else {
132
- setupSession ( longBreakInSeconds , breakText ) ;
122
+ setupNextInterval ( totalLongBreakInSeconds , breakText ) ;
133
123
}
134
124
}
135
125
136
126
vibration . start ( "nudge" ) ;
137
127
}
138
128
139
- function setupSession ( nextSessionSeconds , text ) {
129
+ function setupWithUserSettings ( ) {
130
+ try {
131
+ fs . readFileSync ( "flowSettings.txt" , "cbor" )
132
+ }
133
+ catch ( err ) {
134
+ writeToFile ( { data : { flowTime : 0 , shortBreakTime : 0 , longBreakTime : 0 } } ) ;
135
+ }
136
+ let settings = fs . readFileSync ( "flowSettings.txt" , "cbor" ) ;
137
+
138
+ //setup consts based on user settings
139
+ totalFlowInSeconds = settings . flowTime == 0 ? totalFlowInSeconds : parseInt ( settings . flowTime ) * 60 ;
140
+ totalShortBreakInSeconds = settings . shortBreakTime == 0 ? totalShortBreakInSeconds : parseInt ( settings . shortBreakTime ) * 60 ;
141
+ totalLongBreakInSeconds = settings . longBreakTime == 0 ? totalLongBreakInSeconds : parseInt ( settings . longBreakTime ) * 60 ;
142
+ setupNextInterval ( totalFlowInSeconds , flowText ) ;
143
+ }
144
+
145
+ function setupNextInterval ( nextIntervalSeconds , text ) {
140
146
//change the arc back to 0
141
147
progressArc . sweepAngle = 0 ;
142
- //update session text
143
- sessionText . text = text ;
148
+ //update interval text
149
+ currentIntervalText . text = text ;
144
150
//set the correct seconds for progress
145
- sessionTime = countdownSeconds = nextSessionSeconds ;
151
+ currentIntervalTime = countdownSeconds = nextIntervalSeconds ;
146
152
}
147
153
148
154
function play ( ) {
@@ -159,9 +165,9 @@ function pause(){
159
165
160
166
function formatCountdown ( seconds ) {
161
167
//calculate time left
162
- formattedHours = Math . floor ( ( seconds / 60 / 60 ) % 60 ) ;
163
- formattedMinutes = Math . floor ( ( seconds / 60 ) % 60 ) ;
164
- formattedSeconds = Math . floor ( seconds % 60 ) ;
168
+ let formattedHours = Math . floor ( ( seconds / 60 / 60 ) % 60 ) ;
169
+ let formattedMinutes = Math . floor ( ( seconds / 60 ) % 60 ) ;
170
+ let formattedSeconds = Math . floor ( seconds % 60 ) ;
165
171
166
172
//pad with 0
167
173
formattedHours = formattedHours < 10 ? "0" + formattedHours : formattedHours ;
0 commit comments