Store number of steps when unloading from memory (fix #84)

pull/94/head
Gordon Williams 2020-02-07 13:44:55 +00:00
parent 8b8147a543
commit e33202122b
3 changed files with 58 additions and 42 deletions

View File

@ -694,7 +694,7 @@
{ "id": "wpedom",
"name": "Pedometer widget",
"icon": "pedometer_widget.png",
"version":"0.01",
"version":"0.02",
"description": "Daily pedometer widget",
"tags": "widget",
"type":"widget",

1
apps/wpedom/ChangeLog Normal file
View File

@ -0,0 +1 @@
0.02: Save number of steps when unloading from memory

View File

@ -1,50 +1,65 @@
(() => {
const PEDOMFILE = "@wpedom";
// add the width
// WIDGETPOS.tr is originally 208 without any widgets
var xpos = WIDGETPOS.tr; // draw string with right alignment
var width = 48;
WIDGETPOS.tr -= (width + 2);
// add the width
// WIDGETPOS.tr is originally 208 without any widgets
var xpos = WIDGETPOS.tr; // draw string with right alignment
var width = 48;
WIDGETPOS.tr -= (width + 2);
let lastUpdate = new Date();
let stp_today = 0;
let lastUpdate = new Date();
let stp_today = 0;
function erase() {
g.clearRect(xpos-width, 0, xpos, 23);
}
function erase() {
g.clearRect(xpos-width, 0, xpos, 23);
// draw your widget at xpos
function draw() {
// Widget (0,0,239,23)
if (stp_today > 99999){
stp_today = stp_today % 100000; // cap to five digits + comma = 6 characters
erase();
}
// draw your widget at xpos
function draw() {
// Widget (0,0,239,23)
if (stp_today > 99999){
stp_today = stp_today % 100000; // cap to five digits + comma = 6 characters
erase();
}
let stps = stp_today.toString();
if (stps.length > 3){
stps = stps.slice(0,-3) + "," + stps.slice(-3);
}
g.setColor(1,1,1);
g.setFont("4x6", 2);
g.setFontAlign(1, 0); // align to x: right, y: center
g.drawString(stps, xpos, 11, true); // 6 * 4*2 = 48
g.flip();
let stps = stp_today.toString();
if (stps.length > 3){
stps = stps.slice(0,-3) + "," + stps.slice(-3);
}
Bangle.on('step', (up) => {
let date = new Date();
if (lastUpdate.getDate() == date.getDate()){
stp_today += 1;
} else {
stp_today = 1;
erase();
}
lastUpdate = date;
//console.log("up: " + up + " stp: " + stp_today + " " + date.toString());
draw();
});
g.setColor(1,1,1);
g.setFont("4x6", 2);
g.setFontAlign(1, 0); // align to x: right, y: center
g.drawString(stps, xpos, 11, true); // 6 * 4*2 = 48
g.flip();
}
// add your widget
WIDGETS["wpedom"]={draw:draw};
Bangle.on('step', (up) => {
let date = new Date();
if (lastUpdate.getDate() == date.getDate()){
stp_today += 1;
} else {
stp_today = 1;
erase();
}
lastUpdate = date;
//console.log("up: " + up + " stp: " + stp_today + " " + date.toString());
draw();
});
// When unloading, save state
E.on('kill', () => {
let d = {
lastUpdate : lastUpdate.toString(),
stepsToday : stp_today
};
require("Storage").write(PEDOMFILE,d);
});
// add your widget
WIDGETS["wpedom"]={draw:draw};
// Load data at startup
let pedomData = require("Storage").readJSON(PEDOMFILE);
if (pedomData) {
if (pedomData.lastUpdate)
lastUpdate = new Date(pedomData.lastUpdate);
stp_today = pedomData.stepsToday|0;
}
})()