Skip to content

Commit 400e41b

Browse files
Add config support with hourly vs daily statistics toggle, Fix issue 1
1 parent 2e856e4 commit 400e41b

14 files changed

Lines changed: 121 additions & 26 deletions

ClockClassic/app/index.js

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,10 @@
11
import clock from "clock";
22
import document from "document";
33
import * as health from "user-activity";
4-
import {HeartRateSensor} from "heart-rate";
5-
import {display} from "display";
6-
import {vibration} from "haptics";
7-
import {peerSocket} from "messaging";
8-
import {units} from "user-settings";
9-
import * as fs from "fs";
104
import {battery} from "power";
11-
import {decode} from "cbor";
12-
import {inbox} from "file-transfer";
13-
14-
const THEMES = {
15-
white: ["FFFFFF", "FFFFFF", "FFFFFF"]
16-
};
17-
let weekNames = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"];
5+
import * as messaging from "messaging";
6+
import document from "document";
7+
import * as simpleSettings from "./simple/device-settings";
188

199
var myDate = $("mydate");
2010
var myTime = $("mytime");
@@ -25,6 +15,8 @@ var mySteps = $("mysteps");
2515
var batColor = $("batColor");
2616
var batText = $("batText");
2717
var myCal = $("mycal");
18+
var hourlyStats = false
19+
2820

2921
function $(s) {
3022
return document.getElementById(s);
@@ -53,7 +45,7 @@ function formatAMPM(date) {
5345
var ampm = hours >= 12 ? 'pm' : 'am';
5446
hours = hours % 12;
5547
hours = hours ? hours : 12; // the hour '0' should be '12'
56-
minutes = minutes < 10 ? '0'+minutes : minutes;
48+
minutes = minutes < 10 ? '0' + minutes : minutes;
5749
var strTime = hours + ':' + minutes + ' ' + ampm;
5850
return strTime;
5951
}
@@ -62,7 +54,21 @@ function numberWithCommas(x) {
6254
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
6355
}
6456

65-
function updateSteps() {
57+
function updateSteps(mins) {
58+
if(hourlyStats) {
59+
const minuteRecords = health.minuteHistory.query({ limit: mins });
60+
let stepsThisHour = 0;
61+
let caloriesThisHour = 0;
62+
63+
minuteRecords.forEach((minute, index) => {
64+
stepsThisHour += (minute.steps || 0);
65+
caloriesThisHour += (minute.calories || 0);
66+
});
67+
mySteps.text = numberWithCommas(stepsThisHour);
68+
myCal.text = numberWithCommas(caloriesThisHour);;
69+
return;
70+
}
71+
6672
let today = health.today.adjusted;
6773
mySteps.text = numberWithCommas(today.steps);
6874
myCal.text = numberWithCommas(today.calories);;
@@ -73,14 +79,14 @@ function onTick() {
7379
myDate.text = formatDate(now);
7480
myTime.text = formatAMPM(now);
7581

76-
updateSteps();
77-
7882
let hours = now.getHours() % 12;
7983
let mins = now.getMinutes();
8084
let secs = now.getSeconds();
81-
myHours.groupTransform.rotate.angle = (hours + mins/60)*30;
82-
myMins.groupTransform.rotate.angle = mins*6;
83-
mySecs.groupTransform.rotate.angle = secs*6;
85+
myHours.groupTransform.rotate.angle = (hours + mins / 60) * 30;
86+
myMins.groupTransform.rotate.angle = mins * 6;
87+
mySecs.groupTransform.rotate.angle = secs * 6;
88+
89+
updateSteps(mins);
8490

8591
let chrg = battery.chargeLevel;
8692

@@ -94,10 +100,17 @@ function onTick() {
94100
batColor.style.fill = "#58aae2";
95101
}
96102

97-
batColor.width = Math.ceil((36 * chrg)/100);
103+
batColor.width = Math.ceil((36 * chrg) / 100);
98104
}
99105

106+
function settingsCallback(data) {
107+
if (!data) {
108+
return;
109+
}
110+
hourlyStats = data.hourlyStats;
111+
}
112+
simpleSettings.initialize(settingsCallback);
100113

101114

102115
clock.granularity = "seconds";
103-
clock.ontick = onTick;
116+
clock.addEventListener("tick", onTick);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { me } from "appbit";
2+
import { me as device } from "device";
3+
import * as fs from "fs";
4+
import * as messaging from "messaging";
5+
6+
const SETTINGS_TYPE = "cbor";
7+
const SETTINGS_FILE = "settings.cbor";
8+
9+
let settings, onsettingschange;
10+
11+
export function initialize(callback) {
12+
settings = loadSettings();
13+
onsettingschange = callback;
14+
onsettingschange(settings);
15+
}
16+
17+
messaging.peerSocket.addEventListener("message", function(evt) {
18+
settings[evt.data.key] = evt.data.value;
19+
onsettingschange(settings);
20+
})
21+
22+
me.addEventListener("unload", saveSettings);
23+
24+
function loadSettings() {
25+
try {
26+
return fs.readFileSync(SETTINGS_FILE, SETTINGS_TYPE);
27+
} catch (ex) {
28+
return {};
29+
}
30+
}
31+
32+
function saveSettings() {
33+
fs.writeFileSync(SETTINGS_FILE, settings, SETTINGS_TYPE);
34+
}

ClockClassic/companion/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import * as simpleSettings from "./simple/companion-settings";
2+
3+
simpleSettings.initialize();
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import * as messaging from "messaging";
2+
import { settingsStorage } from "settings";
3+
4+
export function initialize() {
5+
settingsStorage.addEventListener("change", evt => {
6+
if (evt.oldValue !== evt.newValue) {
7+
sendValue(evt.key, evt.newValue);
8+
}
9+
});
10+
}
11+
12+
function sendValue(key, val) {
13+
if (val) {
14+
sendSettingData({
15+
key: key,
16+
value: JSON.parse(val)
17+
});
18+
}
19+
}
20+
21+
function sendSettingData(data) {
22+
if (messaging.peerSocket.readyState === messaging.peerSocket.OPEN) {
23+
messaging.peerSocket.send(data);
24+
} else {
25+
console.log("No peerSocket connection");
26+
}
27+
}

ClockClassic/package.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
{
2+
"devDependencies": {
3+
"@fitbit/sdk": "~4.2.0"
4+
},
25
"fitbit": {
36
"appUUID": "5cd3d22e-5ff8-47dd-9cae-7d8ffa0d82dc",
47
"appType": "clockface",
@@ -11,16 +14,16 @@
1114
"access_exercise"
1215
],
1316
"buildTargets": [
17+
"meson",
18+
"gemini",
19+
"mira",
1420
"higgs"
1521
],
1622
"i18n": {
17-
"en": {
23+
"en-US": {
1824
"name": "ClockClassic"
1925
}
2026
},
2127
"defaultLanguage": "en-US"
22-
},
23-
"devDependencies": {
24-
"@fitbit/sdk": "~4.1.0"
2528
}
2629
}

ClockClassic/settings/index.jsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function ClockSettings(props) {
2+
return (
3+
<Page>
4+
<Section
5+
title={<Text bold align="center">Clock Settings</Text>}>
6+
<Toggle
7+
settingsKey="hourlyStats"
8+
label="Hourly Stats"
9+
/>
10+
</Section>
11+
</Page>
12+
);
13+
}
14+
15+
registerSettingsPage(ClockSettings);

images/v1.0.3/ionic_sample1.png

10.5 KB
Loading

images/v1.0.3/ionic_sample2.png

11 KB
Loading

images/v1.0.3/versa2_sample1.png

11.2 KB
Loading

images/v1.0.3/versa2_sample2.png

11.5 KB
Loading

0 commit comments

Comments
 (0)