Skip to content

Commit 273da35

Browse files
committed
interval times are now configurable through settings in FitBit mobile app
1 parent 5be5fd8 commit 273da35

File tree

4 files changed

+109
-21
lines changed

4 files changed

+109
-21
lines changed

app/index.js

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
* Entry point for the watch app
33
*/
44
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";
78
import { style } from "./style.js"
89

910
//grab screen elements
@@ -27,26 +28,39 @@ const breakText = "Break";
2728
let formattedHours = 0;
2829
let formattedMinutes = 0;
2930
let formattedSeconds = 0;
30-
let seconds = flowInSeconds;
3131
let counting = false;
32+
let countdownSeconds = flowInSeconds;
3233

3334
//used to toggle between flow sessions and breaks
3435
let flow = true;
3536
let sessionTime = flowInSeconds;
3637
let currentSprint = 1;
3738

38-
console.log(style);
39+
readFile();
3940
style();
40-
clock.ontick = () => progress();
41+
//clock.tick does not run in background so use setInterval instead
42+
setInterval(() => progress(), 1000)
4143
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;
5064
}
5165

5266
function secondsToAngle(seconds){
@@ -56,14 +70,18 @@ function secondsToAngle(seconds){
5670

5771
function progress(){
5872
if (counting){
59-
if( !isSprintOver(seconds) ) {
60-
seconds--;
73+
if( !isSprintOver(countdownSeconds) ) {
74+
countdownSeconds--;
6175
//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);
6381
}
6482
}
6583

66-
formatCountdown(seconds);
84+
formatCountdown(countdownSeconds);
6785
}
6886

6987
function isSprintOver(seconds){
@@ -81,7 +99,8 @@ function nextSprint(){
8199

82100
if (flow){
83101
setupSession(flowInSeconds, flowText);
84-
currentSprint++;
102+
currentSprint < sprints ? currentSprint++ : currentSprint = 1;
103+
85104
sprintCounter.text = `${currentSprint} of ${sprints}`
86105
}
87106
else {
@@ -92,6 +111,8 @@ function nextSprint(){
92111
setupSession(longBreakInSeconds, breakText);
93112
}
94113
}
114+
115+
vibration.start("nudge");
95116
}
96117

97118
function setupSession(nextSessionSeconds, text){
@@ -100,7 +121,7 @@ function setupSession(nextSessionSeconds, text){
100121
//update session text
101122
sessionText.text = text;
102123
//set the correct seconds for progress
103-
sessionTime = seconds = nextSessionSeconds;
124+
sessionTime = countdownSeconds = nextSessionSeconds;
104125

105126
}
106127

companion/index.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,32 @@
11
/*
22
* Entry point for the companion app
33
*/
4+
import { settingsStorage } from "settings";
5+
import { me } from "companion";
6+
import * as messaging from "messaging";
47

5-
console.log("Companion code started");
8+
// Event fires when a setting is changed
9+
settingsStorage.onchange = (evt) => { sendSettings(); }
10+
11+
// Settings were changed while the companion was not running
12+
if (me.launchReasons.settingsChanged) {
13+
sendSettings();
14+
}
15+
16+
function sendSettings(){
17+
let flowTime = settingsStorage.getItem("flowTimeSlider");
18+
let shortTime = settingsStorage.getItem("shortBreakTimeSlider")
19+
let longTime = settingsStorage.getItem("longBreakTimeSlider")
20+
let data = {
21+
flowTime : flowTime.substring(1, flowTime.length-1),
22+
shortBreakTime : shortTime.substring(1, shortTime.length-1),
23+
longBreakTimeSlider : longTime.substring(1, longTime.length-1)
24+
}
25+
26+
// If we have a MessageSocket, send the data to the device
27+
if (messaging.peerSocket.readyState === messaging.peerSocket.OPEN) {
28+
messaging.peerSocket.send(data);
29+
} else {
30+
console.log("No peerSocket connection");
31+
}
32+
}

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"wipeColor": "#8bc34a",
88
"requestedPermissions": [],
99
"buildTargets": [
10-
"higgs",
1110
"meson"
1211
],
1312
"i18n": {

settings/index.jsx

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
function Settings(props){
2+
//setup because first time through will crash otherwise
3+
props.settings.flowTimeSlider = !props.settings.flowTimeSlider ? "0" : props.settings.flowTimeSlider;
4+
props.settings.shortBreakTimeSlider = !props.settings.shortBreakTimeSlider ? "0" : props.settings.shortBreakTimeSlider;
5+
props.settings.longBreakTimeSlider = !props.settings.longBreakTimeSlider ? "0" : props.settings.longBreakTimeSlider;
6+
7+
return(
8+
<Page>
9+
<Section
10+
title={<Text bold align="center">WorkFlow Settings</Text>}>
11+
<Text>Flow Time in minutes</Text>
12+
<Slider
13+
label= {props.settings.flowTimeSlider.substring(1,props.settings.flowTimeSlider.length-1)}
14+
settingsKey="flowTimeSlider"
15+
min="0"
16+
max="240"
17+
/>
18+
<Text>Short Break Time in minutes</Text>
19+
20+
<Slider
21+
label= {props.settings.shortBreakTimeSlider.substring(1,props.settings.shortBreakTimeSlider.length-1)}
22+
settingsKey="shortBreakTimeSlider"
23+
min="0"
24+
max="60"
25+
/>
26+
27+
<Text>Long Break Time in minutes</Text>
28+
29+
<Slider
30+
label= {props.settings.longBreakTimeSlider.substring(1,props.settings.longBreakTimeSlider.length-1)}
31+
settingsKey="longBreakTimeSlider"
32+
min="0"
33+
max="60"
34+
/>
35+
</Section>
36+
</Page>
37+
);
38+
}
39+
40+
41+
registerSettingsPage(Settings);

0 commit comments

Comments
 (0)