4
4
import document from "document" ;
5
5
import * as messaging from "messaging" ;
6
6
import * as fs from "fs" ;
7
+ import clock from "clock" ;
8
+ clock . granularity = "minutes" ;
9
+ import { preferences } from "user-settings" ;
7
10
import { vibration } from "haptics" ;
8
11
import { style } from "./style.js"
9
12
10
13
//grab screen elements
14
+ const time = document . getElementById ( "time" ) ;
11
15
const progressArc = document . getElementById ( "progressArc" ) ;
12
16
const countdown = document . getElementById ( "countdown" ) ;
13
17
const sprintCounter = document . getElementById ( "sprintCounter" ) ;
@@ -31,12 +35,22 @@ let formattedSeconds = 0;
31
35
let counting = false ;
32
36
let countdownSeconds = flowInSeconds ;
33
37
34
- //used to toggle between flow sessions and breaks
35
- let flow = true ;
38
+
39
+ let flow = true ; //used to toggle between flow sessions and breaks
36
40
let sessionTime = flowInSeconds ;
37
41
let currentSprint = 1 ;
38
42
39
- readFile ( ) ;
43
+ //setup clock
44
+ clock . ontick = ( evt ) => {
45
+ let hours = evt . date . getHours ( ) ;
46
+ let minutes = evt . date . getMinutes ( ) ;
47
+ hours = hours > 12 && preferences . clockDisplay === "12h" ? hours - 12 : hours ;
48
+ hours = hours < 10 ? "0" + hours : hours ;
49
+ minutes = minutes < 10 ? "0" + minutes : minutes ;
50
+ time . text = `${ hours } :${ minutes } ` ;
51
+ }
52
+
53
+ setupWithUserSettings ( ) ;
40
54
style ( ) ;
41
55
//clock.tick does not run in background so use setInterval instead
42
56
setInterval ( ( ) => progress ( ) , 1000 )
@@ -47,22 +61,33 @@ playPauseButton.onactivate = (evt) => {
47
61
messaging . peerSocket . onmessage = ( evt ) => {
48
62
//persist
49
63
writeToFile ( evt ) ;
50
- readFile ( ) ;
64
+ setupWithUserSettings ( ) ;
51
65
}
52
66
67
+
68
+
69
+
53
70
function writeToFile ( evt ) {
54
71
fs . writeFileSync ( "flowSettings.txt" , evt . data , "cbor" ) ;
55
72
}
56
73
57
- function readFile ( ) {
74
+ function setupWithUserSettings ( ) {
75
+ try {
76
+ fs . readFileSync ( "flowSettings.txt" , "cbor" )
77
+ }
78
+ catch ( err ) {
79
+ writeToFile ( { data : { flowTime : 0 , shortBreakTime : 0 , longBreakTime : 0 } } ) ;
80
+ }
58
81
let settings = fs . readFileSync ( "flowSettings.txt" , "cbor" ) ;
59
- flowInSeconds = parseInt ( settings . flowTime ) * 60 ;
60
- shortBreakInSeconds = parseInt ( settings . shortBreakTime ) * 60 ;
61
- longBreakInSeconds = parseInt ( settings . longBreakTime ) * 60 ;
62
- countdownSeconds = flowInSeconds ;
63
- sessionTime = flowInSeconds ;
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 ) ;
64
88
}
65
89
90
+
66
91
function secondsToAngle ( seconds ) {
67
92
//degree per second * elapsedseconds
68
93
return ( 360 / sessionTime ) * seconds ;
@@ -73,10 +98,6 @@ function progress(){
73
98
if ( ! isSprintOver ( countdownSeconds ) ) {
74
99
countdownSeconds -- ;
75
100
//calculate and update angle
76
- const sweepAnimation = progressArc . getElementById ( "sweepAnimation" ) ;
77
- sweepAnimation . final = secondsToAngle ( sessionTime - countdownSeconds ) ;
78
- sweepAnimation . easing = "ease-in" ;
79
- sweepAnimation . dur = "1" ;
80
101
progressArc . sweepAngle = secondsToAngle ( sessionTime - countdownSeconds ) ;
81
102
}
82
103
}
@@ -122,7 +143,6 @@ function setupSession(nextSessionSeconds, text){
122
143
sessionText . text = text ;
123
144
//set the correct seconds for progress
124
145
sessionTime = countdownSeconds = nextSessionSeconds ;
125
-
126
146
}
127
147
128
148
function play ( ) {
0 commit comments