2
2
* Entry point for the watch app
3
3
*/
4
4
import document from "document" ;
5
- import clock from "clock" ;
6
- clock . granularity = "seconds" ;
5
+ import * as messaging from "messaging" ;
6
+ import * as fs from "fs" ;
7
+ import { vibration } from "haptics" ;
7
8
import { style } from "./style.js"
8
9
9
10
//grab screen elements
@@ -27,26 +28,39 @@ const breakText = "Break";
27
28
let formattedHours = 0 ;
28
29
let formattedMinutes = 0 ;
29
30
let formattedSeconds = 0 ;
30
- let seconds = flowInSeconds ;
31
31
let counting = false ;
32
+ let countdownSeconds = flowInSeconds ;
32
33
33
34
//used to toggle between flow sessions and breaks
34
35
let flow = true ;
35
36
let sessionTime = flowInSeconds ;
36
37
let currentSprint = 1 ;
37
38
38
- console . log ( style ) ;
39
+ readFile ( ) ;
39
40
style ( ) ;
40
- clock . ontick = ( ) => progress ( ) ;
41
+ //clock.tick does not run in background so use setInterval instead
42
+ setInterval ( ( ) => progress ( ) , 1000 )
41
43
playPauseButton . onactivate = ( evt ) => {
42
- //countingdown
43
- if ( counting ) {
44
- pause ( ) ;
45
- } //paused
46
- else {
47
- play ( ) ;
48
- }
49
-
44
+ counting ? pause ( ) : play ( ) ;
45
+ }
46
+
47
+ messaging . peerSocket . onmessage = ( evt ) => {
48
+ //persist
49
+ writeToFile ( evt ) ;
50
+ readFile ( ) ;
51
+ }
52
+
53
+ function writeToFile ( evt ) {
54
+ fs . writeFileSync ( "flowSettings.txt" , evt . data , "cbor" ) ;
55
+ }
56
+
57
+ function readFile ( ) {
58
+ 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 ;
50
64
}
51
65
52
66
function secondsToAngle ( seconds ) {
@@ -56,14 +70,18 @@ function secondsToAngle(seconds){
56
70
57
71
function progress ( ) {
58
72
if ( counting ) {
59
- if ( ! isSprintOver ( seconds ) ) {
60
- seconds -- ;
73
+ if ( ! isSprintOver ( countdownSeconds ) ) {
74
+ countdownSeconds -- ;
61
75
//calculate and update angle
62
- progressArc . sweepAngle = secondsToAngle ( sessionTime - seconds ) ;
76
+ const sweepAnimation = progressArc . getElementById ( "sweepAnimation" ) ;
77
+ sweepAnimation . final = secondsToAngle ( sessionTime - countdownSeconds ) ;
78
+ sweepAnimation . easing = "ease-in" ;
79
+ sweepAnimation . dur = "1" ;
80
+ progressArc . sweepAngle = secondsToAngle ( sessionTime - countdownSeconds ) ;
63
81
}
64
82
}
65
83
66
- formatCountdown ( seconds ) ;
84
+ formatCountdown ( countdownSeconds ) ;
67
85
}
68
86
69
87
function isSprintOver ( seconds ) {
@@ -81,7 +99,8 @@ function nextSprint(){
81
99
82
100
if ( flow ) {
83
101
setupSession ( flowInSeconds , flowText ) ;
84
- currentSprint ++ ;
102
+ currentSprint < sprints ? currentSprint ++ : currentSprint = 1 ;
103
+
85
104
sprintCounter . text = `${ currentSprint } of ${ sprints } `
86
105
}
87
106
else {
@@ -92,6 +111,8 @@ function nextSprint(){
92
111
setupSession ( longBreakInSeconds , breakText ) ;
93
112
}
94
113
}
114
+
115
+ vibration . start ( "nudge" ) ;
95
116
}
96
117
97
118
function setupSession ( nextSessionSeconds , text ) {
@@ -100,7 +121,7 @@ function setupSession(nextSessionSeconds, text){
100
121
//update session text
101
122
sessionText . text = text ;
102
123
//set the correct seconds for progress
103
- sessionTime = seconds = nextSessionSeconds ;
124
+ sessionTime = countdownSeconds = nextSessionSeconds ;
104
125
105
126
}
106
127
0 commit comments