4
4
import document from "document" ;
5
5
import clock from "clock" ;
6
6
clock . granularity = "seconds" ;
7
+ import { style } from "./style.js"
7
8
8
9
//grab screen elements
9
- let background = document . getElementById ( "background" ) ;
10
- let backgroundColor = document . getElementsByClassName ( "backgroundColor" )
11
- let progressArc = document . getElementById ( "progressArc" ) ;
12
- let backgroundArc = document . getElementById ( "backgroundArc" ) ;
13
- let countdown = document . getElementById ( "countdown" ) ;
14
- let sprintCounter = document . getElementById ( "sprintCounter" ) ;
15
- let playPauseButton = document . getElementById ( "playPauseButton" ) ;
16
- let playPauseIcon = playPauseButton . getElementById ( "combo-button-icon" ) ;
17
- let playPauseIconPressed = playPauseButton . getElementById ( "combo-button-icon-press" ) ;
18
- let oldBackground = background . value ;
19
- background . value = 0 ;
20
-
21
-
22
- let intervalInSeconds = 1500 ;
10
+ const progressArc = document . getElementById ( "progressArc" ) ;
11
+ const countdown = document . getElementById ( "countdown" ) ;
12
+ const sprintCounter = document . getElementById ( "sprintCounter" ) ;
13
+ const sessionText = document . getElementById ( "sessionText" ) ;
14
+ const playPauseButton = document . getElementById ( "playPauseButton" ) ;
15
+ const playPauseIcon = playPauseButton . getElementById ( "combo-button-icon" ) ;
16
+ const playPauseIconPressed = playPauseButton . getElementById ( "combo-button-icon-press" ) ;
17
+
18
+
19
+ const flowInSeconds = 5 ; //1500;
20
+ const shortBreakInSeconds = 10 ; //300;
21
+ const longBreakInSeconds = 15 ; //900;
22
+ const sprints = 4 ;
23
+ const flowText = "Flow" ;
24
+ const breakText = "Break" ;
25
+
26
+
23
27
let formattedHours = 0 ;
24
28
let formattedMinutes = 0 ;
25
29
let formattedSeconds = 0 ;
26
- let seconds = intervalInSeconds ;
30
+ let seconds = flowInSeconds ;
27
31
let counting = false ;
28
32
33
+ //used to toggle between flow sessions and breaks
34
+ let flow = true ;
35
+ let sessionTime = flowInSeconds ;
36
+ let currentSprint = 1 ;
37
+
38
+ console . log ( style ) ;
39
+ style ( ) ;
40
+ clock . ontick = ( ) => progress ( ) ;
41
+ playPauseButton . onactivate = ( evt ) => {
42
+ //countingdown
43
+ if ( counting ) {
44
+ pause ( ) ;
45
+ } //paused
46
+ else {
47
+ play ( ) ;
48
+ }
49
+
50
+ }
51
+
29
52
function secondsToAngle ( seconds ) {
30
53
//degree per second * elapsedseconds
31
- return ( 360 / intervalInSeconds ) * seconds ;
54
+ return ( 360 / sessionTime ) * seconds ;
32
55
}
33
56
34
57
function progress ( ) {
35
58
if ( counting ) {
36
- seconds -- ;
37
-
38
- //calculate and update angle
39
- let a = secondsToAngle ( intervalInSeconds - seconds ) ;
40
- progressArc . sweepAngle = a ;
41
-
42
- //calculate time left
43
- formattedHours = Math . floor ( ( seconds / 60 / 60 ) % 60 ) ;
44
- formattedMinutes = Math . floor ( ( seconds / 60 ) % 60 ) ;
45
- formattedSeconds = Math . floor ( seconds % 60 ) ;
59
+ if ( ! isSprintOver ( seconds ) ) {
60
+ seconds -- ;
61
+ //calculate and update angle
62
+ progressArc . sweepAngle = secondsToAngle ( sessionTime - seconds ) ;
63
+ }
64
+ }
65
+
66
+ formatCountdown ( seconds ) ;
67
+ }
46
68
47
- //pad with 0
48
- formattedHours = formattedHours < 10 ? "0" + formattedHours : formattedHours ;
49
- formattedMinutes = formattedMinutes < 10 ? "0" + formmatedMinutes : formattedMinutes ;
50
- formattedSeconds = formattedSeconds < 10 ? "0" + formattedSeconds : formattedSeconds ;
69
+ function isSprintOver ( seconds ) {
70
+ if ( seconds === 0 ) {
71
+ pause ( ) ;
72
+ nextSprint ( ) ;
73
+ return true ;
74
+ }
75
+ return false ;
76
+ }
51
77
52
- //update countdown text
53
- countdown . text = `${ formattedHours } :${ formattedMinutes } :${ formattedSeconds } ` ;
78
+ function nextSprint ( ) {
79
+ //swap session type
80
+ flow = ! flow ;
81
+
82
+ if ( flow ) {
83
+ setupSession ( flowInSeconds , flowText ) ;
84
+ currentSprint ++ ;
85
+ sprintCounter . text = `${ currentSprint } of ${ sprints } `
86
+ }
87
+ else {
88
+ if ( currentSprint < sprints ) {
89
+ setupSession ( shortBreakInSeconds , breakText ) ;
90
+ }
91
+ else {
92
+ setupSession ( longBreakInSeconds , breakText ) ;
93
+ }
54
94
}
55
95
}
56
96
57
- clock . ontick = ( ) => progress ( ) ;
97
+ function setupSession ( nextSessionSeconds , text ) {
98
+ //change the arc back to 0
99
+ progressArc . sweepAngle = 0 ;
100
+ //update session text
101
+ sessionText . text = text ;
102
+ //set the correct seconds for progress
103
+ sessionTime = seconds = nextSessionSeconds ;
104
+
105
+ }
58
106
59
- playPauseButton . onactivate = ( evt ) => {
60
- //countingdown
61
- if ( counting ) {
62
- counting = false
63
- playPauseIcon . image = "play.png" ;
64
- playPauseIconPressed . image = "play_press.png" ;
65
- } //paused
66
- else {
107
+ function play ( ) {
67
108
counting = true ;
68
109
playPauseIcon . image = "pause.png" ;
69
110
playPauseIconPressed . image = "pause_press.png" ;
70
- }
71
-
72
111
}
73
112
74
-
75
- //style functions
76
- setInterval ( ( ) => {
77
- if ( background . value != oldBackground ) {
78
- applyStyle ( background . value ) ;
79
- oldBackground = background . value ;
80
- } } , 100 ) ;
81
-
82
- function applyStyle ( value ) {
83
- let colorProfile = style [ value ] ;
84
- backgroundColor [ value ] . style . fill = colorProfile . background ;
85
- progressArc . style . fill = colorProfile . accentColor ;
86
- backgroundArc . style . fill = colorProfile . backgroundArc ;
87
- countdown . style . fill = colorProfile . accentColor ;
88
- playPauseButton . style . fill = colorProfile . accentColor ;
89
- // pauseButton.style.fill = colorProfile.accentColor;
90
- sprintCounter . style . fill = colorProfile . baseColor ;
113
+ function pause ( ) {
114
+ counting = false
115
+ playPauseIcon . image = "play.png" ;
116
+ playPauseIconPressed . image = "play_press.png" ;
91
117
}
92
118
93
- const style = [
94
- { background : "#000" ,
95
- baseColor : "#fff" ,
96
- accentColor : "#FF9F1E" ,
97
- backgroundArc : "#636366"
98
- } ,
99
- { background : "#000" ,
100
- baseColor : "#fff" ,
101
- accentColor : "#00FFFF" ,
102
- backgroundArc : "#636366"
103
- } ,
104
- { background : "#000" ,
105
- baseColor : "#fff" ,
106
- accentColor : "#48f442" ,
107
- backgroundArc : "#636366"
108
- } ,
109
- { background : "#000" ,
110
- baseColor : "#fff" ,
111
- accentColor : "#f727ca" ,
112
- backgroundArc : "#636366"
113
- } ,
114
- { background : "#44090C" ,
115
- baseColor : "#fff" ,
116
- accentColor : "#fff" ,
117
- backgroundArc : "#636366"
118
- } ,
119
- { background : "#51a3a3" ,
120
- baseColor : "#fff" ,
121
- accentColor : "#dfcc74" ,
122
- backgroundArc : "#636366"
123
- } ,
124
- { background : "#0b032d" ,
125
- baseColor : "#fff" ,
126
- accentColor : "#f67e7d" ,
127
- backgroundArc : "#636366"
128
- }
129
- ] ;
119
+ function formatCountdown ( seconds ) {
120
+ //calculate time left
121
+ formattedHours = Math . floor ( ( seconds / 60 / 60 ) % 60 ) ;
122
+ formattedMinutes = Math . floor ( ( seconds / 60 ) % 60 ) ;
123
+ formattedSeconds = Math . floor ( seconds % 60 ) ;
124
+
125
+ //pad with 0
126
+ formattedHours = formattedHours < 10 ? "0" + formattedHours : formattedHours ;
127
+ formattedMinutes = formattedMinutes < 10 ? "0" + formattedMinutes : formattedMinutes ;
128
+ formattedSeconds = formattedSeconds < 10 ? "0" + formattedSeconds : formattedSeconds ;
129
+
130
+ //update countdown text
131
+ countdown . text = `${ formattedHours } :${ formattedMinutes } :${ formattedSeconds } ` ;
132
+ }
0 commit comments