Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7f0dba8

Browse files
committedJun 24, 2018
cleaned up some of the existing code and added some battery life optimizations
1 parent abab749 commit 7f0dba8

File tree

4 files changed

+72
-63
lines changed

4 files changed

+72
-63
lines changed
 

‎app/index.js

Lines changed: 57 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,36 @@
33
*/
44
import document from "document";
55
import * as messaging from "messaging";
6+
import { display } from "display";
67
import * as fs from "fs";
78
import clock from "clock";
89
clock.granularity = "minutes";
910
import { preferences } from "user-settings";
1011
import { vibration } from "haptics";
11-
import { style } from "./style.js"
12+
import { style, stopStyle } from "./style.js"
1213

1314
//grab screen elements
1415
const time = document.getElementById("time");
1516
const progressArc = document.getElementById("progressArc");
1617
const countdown = document.getElementById("countdown");
1718
const sprintCounter = document.getElementById("sprintCounter");
18-
const sessionText = document.getElementById("sessionText");
19+
const currentIntervalText = document.getElementById("currentIntervalText");
1920
const playPauseButton = document.getElementById("playPauseButton");
2021
const playPauseIcon = playPauseButton.getElementById("combo-button-icon");
2122
const playPauseIconPressed = playPauseButton.getElementById("combo-button-icon-press");
2223

2324

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;
2829
const flowText = "Flow";
2930
const breakText = "Break";
3031

31-
32-
let formattedHours = 0;
33-
let formattedMinutes = 0;
34-
let formattedSeconds = 0;
3532
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;
4136
let currentSprint = 1;
4237

4338
//setup clock
@@ -51,13 +46,17 @@ clock.ontick = (evt) => {
5146
}
5247

5348
setupWithUserSettings();
54-
style();
49+
style(display);
5550
//clock.tick does not run in background so use setInterval instead
5651
setInterval(() => progress(), 1000)
5752
playPauseButton.onactivate = (evt) => {
5853
counting ? pause() : play();
5954
}
6055

56+
display.onchange = () => { // optimize battery life
57+
display.on ? style() : stopStyle();
58+
}
59+
6160
messaging.peerSocket.onmessage = (evt)=>{
6261
//persist
6362
writeToFile(evt);
@@ -68,41 +67,32 @@ messaging.peerSocket.onmessage = (evt)=>{
6867

6968

7069
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]}`;
8073
}
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");
8876
}
8977

90-
9178
function secondsToAngle(seconds){
9279
//degree per second * elapsedseconds
93-
return (360/sessionTime) * seconds;
80+
return (360/currentIntervalTime) * seconds;
9481
}
9582

9683
function progress(){
9784
if (counting){
9885
if( !isSprintOver(countdownSeconds) ) {
9986
countdownSeconds--;
87+
}
88+
if (display.on) {
10089
//calculate and update angle
101-
progressArc.sweepAngle = secondsToAngle(sessionTime - countdownSeconds);
90+
progressArc.sweepAngle = secondsToAngle(currentIntervalTime - countdownSeconds);
10291
}
10392
}
104-
105-
formatCountdown(countdownSeconds);
93+
if (display.on) {
94+
formatCountdown(countdownSeconds);
95+
}
10696
}
10797

10898
function isSprintOver(seconds){
@@ -115,34 +105,50 @@ function isSprintOver(seconds){
115105
}
116106

117107
function nextSprint(){
118-
//swap session type
108+
//swap interval type
119109
flow = !flow;
120110

121111
if (flow){
122-
setupSession(flowInSeconds, flowText);
123-
currentSprint < sprints ? currentSprint++ : currentSprint = 1;
112+
setupNextInterval(totalFlowInSeconds, flowText);
113+
currentSprint < totalSprints ? currentSprint++ : currentSprint = 1;
124114

125-
sprintCounter.text = `${currentSprint} of ${sprints}`
115+
sprintCounter.text = `${currentSprint} of ${totalSprints}`
126116
}
127117
else {
128-
if(currentSprint < sprints){
129-
setupSession(shortBreakInSeconds, breakText);
118+
if(currentSprint < totalSprints){
119+
setupNextInterval(totalShortBreakInSeconds, breakText);
130120
}
131121
else{
132-
setupSession(longBreakInSeconds, breakText);
122+
setupNextInterval(totalLongBreakInSeconds, breakText);
133123
}
134124
}
135125

136126
vibration.start("nudge");
137127
}
138128

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){
140146
//change the arc back to 0
141147
progressArc.sweepAngle = 0;
142-
//update session text
143-
sessionText.text = text;
148+
//update interval text
149+
currentIntervalText.text = text;
144150
//set the correct seconds for progress
145-
sessionTime = countdownSeconds = nextSessionSeconds;
151+
currentIntervalTime = countdownSeconds = nextIntervalSeconds;
146152
}
147153

148154
function play(){
@@ -159,9 +165,9 @@ function pause(){
159165

160166
function formatCountdown(seconds){
161167
//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);
165171

166172
//pad with 0
167173
formattedHours = formattedHours < 10 ? "0" + formattedHours : formattedHours;

‎app/style.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,32 @@ const backgroundArc = document.getElementById("backgroundArc");
1010
const countdown = document.getElementById("countdown");
1111
const sprintCounter = document.getElementById("sprintCounter");
1212
const playPauseButton = document.getElementById("playPauseButton");
13-
const playPauseIcon = playPauseButton.getElementById("combo-button-icon");
14-
const playPauseIconPressed = playPauseButton.getElementById("combo-button-icon-press");
1513
const oldBackground = background.value;
1614

15+
let styleInterval;
16+
1717
export function style(){
18-
19-
2018
//check if they changed the background every 100 ms
21-
setInterval(()=>{
22-
if(background.value != oldBackground){
23-
applyStyle(background.value);
24-
oldBackground = background.value;
25-
}}, 100);
19+
styleInterval = setInterval(()=>{
20+
if(background.value != oldBackground){
21+
applyStyle(background.value);
22+
oldBackground = background.value;
23+
}}, 500);
24+
}
25+
26+
export function stopStyle(){ // optimize battery life
27+
console.log("screen off. No style interval necessary");
28+
clearInterval(styleInterval);
2629
}
2730

2831
function applyStyle(value){
2932
let colorProfile = styleProfiles[value];
33+
3034
backgroundColor[value].style.fill = colorProfile.background;
3135
progressArc.style.fill = colorProfile.accentColor;
3236
backgroundArc.style.fill = colorProfile.backgroundArc;
3337
countdown.style.fill = colorProfile.accentColor;
3438
playPauseButton.style.fill = colorProfile.accentColor;
35-
// pauseButton.style.fill = colorProfile.accentColor;
3639
sprintCounter.style.fill = colorProfile.baseColor;
3740
}
3841

‎resources/index.gui

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
<arc id="backgroundArc" class="backgroundArcColor" x="30" y="30" height="240" width="240" arc-width="20" start-angle="0" sweep-angle="360"/>
4040
<arc id="progressArc" class="accentColor" x="30" y="30" height="240" width="240" arc-width="20" start-angle="0" sweep-angle="0"/>
4141

42-
<text id="sessionText" class="baseColor" x="150" y="100">Flow</text>
42+
<text id="currentIntervalText" class="baseColor" x="150" y="100">Flow</text>
4343
<text id="countdown" class="accentColor" x="150" y="160">00:00:00</text>
4444
<text id="sprintCounter" class="baseColor" x="150" y="210">1 of 4</text>
4545

‎resources/styles.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
text-anchor: "middle";
2828
}
2929

30-
#sessionText{
30+
#currentIntervalText{
3131
font-family: "System-Regular";
3232
font-size: "30";
3333
text-anchor: "middle";

0 commit comments

Comments
 (0)
Please sign in to comment.