widpedom 0.21: Memory usage improvements, fix widget initial width (fix #1170)

master
Gordon Williams 2022-01-06 17:04:24 +00:00
parent a9ea028ede
commit d1d478015e
3 changed files with 27 additions and 29 deletions

View File

@ -1590,7 +1590,7 @@
{ {
"id": "widpedom", "id": "widpedom",
"name": "Pedometer widget", "name": "Pedometer widget",
"version": "0.20", "version": "0.21",
"description": "Daily pedometer widget", "description": "Daily pedometer widget",
"icon": "widget.png", "icon": "widget.png",
"type": "widget", "type": "widget",

View File

@ -20,3 +20,4 @@
Fix issue with widget overwrite in large font mode Fix issue with widget overwrite in large font mode
Memory usage enhancements Memory usage enhancements
0.20: Fix issue where step count would randomly reset 0.20: Fix issue where step count would randomly reset
0.21: Memory usage improvements, fix widget initial width (fix #1170)

View File

@ -1,5 +1,4 @@
(() => { (() => {
const PEDOMFILE = "wpedom.json"
// Last time Bangle.on('step' was called // Last time Bangle.on('step' was called
let lastUpdate = new Date(); let lastUpdate = new Date();
// Last step count when Bangle.on('step' was called // Last step count when Bangle.on('step' was called
@ -8,19 +7,14 @@
let settings; let settings;
function loadSettings() { function loadSettings() {
const d = require('Storage').readJSON(PEDOMFILE, 1) || {}; const d = require('Storage').readJSON("wpedom.json", 1) || {};
settings = d.settings || {}; settings = Object.assign({
}
function setting(key) {
if (!settings) { loadSettings() }
const DEFAULTS = {
'goal': 10000, 'goal': 10000,
'progress': false, 'progress': false,
'large': false, 'large': false,
'hide': false 'hide': false
} }, d.settings || {});
return (key in settings) ? settings[key] : DEFAULTS[key]; return d;
} }
Bangle.on('step', stepCount => { Bangle.on('step', stepCount => {
@ -31,10 +25,10 @@
if (lastUpdate.getDate() == date.getDate()){ if (lastUpdate.getDate() == date.getDate()){
stp_today += steps; stp_today += steps;
} else { } else {
// TODO: could save this to PEDOMFILE for lastUpdate's day? // TODO: could save this to "wpedom.json" for lastUpdate's day?
stp_today = steps; stp_today = steps;
} }
if (stp_today === setting('goal') if (stp_today === settings.goal
&& !(require('Storage').readJSON('setting.json',1)||{}).quiet) { && !(require('Storage').readJSON('setting.json',1)||{}).quiet) {
let b = 3, buzz = () => { let b = 3, buzz = () => {
if (b--) Bangle.buzz().then(() => setTimeout(buzz, 100)) if (b--) Bangle.buzz().then(() => setTimeout(buzz, 100))
@ -51,29 +45,31 @@
}); });
// When unloading, save state // When unloading, save state
E.on('kill', () => { E.on('kill', () => {
if (!settings) { loadSettings() } require("Storage").writeJSON("wpedom.json",{
let d = {
lastUpdate : lastUpdate.valueOf(), lastUpdate : lastUpdate.valueOf(),
stepsToday : stp_today, stepsToday : stp_today,
settings : settings, settings : settings,
}; });
require("Storage").write(PEDOMFILE,d);
}); });
// add your widget // add your widget
WIDGETS["wpedom"]={area:"tl",width:26, WIDGETS["wpedom"]={area:"tl",width:0,
redraw:function() { // work out the width, and queue a full redraw if needed getWidth:function() {
let stps = stp_today.toString();
let newWidth = 24; let newWidth = 24;
if (setting('hide')) if (settings.hide)
newWidth = 0; newWidth = 0;
else { else {
if (setting('large')) { if (settings.large) {
newWidth = 12 * stps.length + 3; newWidth = 12 * stps.length + 3;
if (setting('progress')) if (settings.progress)
newWidth += 24; newWidth += 24;
} }
} }
return newWidth;
},
redraw:function() { // work out the width, and queue a full redraw if needed
let stps = stp_today.toString();
let newWidth = this.getWidth();
if (newWidth!=this.width) { if (newWidth!=this.width) {
// width has changed, re-layout all widgets // width has changed, re-layout all widgets
this.width = newWidth; this.width = newWidth;
@ -84,14 +80,14 @@
} }
}, },
draw:function() { draw:function() {
if (setting('hide')) return; if (settings.hide) return;
if (stp_today > 99999) if (stp_today > 99999)
stp_today = stp_today % 100000; // cap to five digits + comma = 6 characters stp_today = stp_today % 100000; // cap to five digits + comma = 6 characters
let stps = stp_today.toString(); let stps = stp_today.toString();
g.reset().clearRect(this.x, this.y, this.x + this.width, this.y + 23); // erase background g.reset().clearRect(this.x, this.y, this.x + this.width, this.y + 23); // erase background
if (setting('progress')) { if (settings.progress) {
const width = 23, half = 11; const width = 23, half = 11;
const goal = setting('goal'), left = Math.max(goal-stps,0); const goal = settings.goal, left = Math.max(goal-stps,0);
// blue or dark green // blue or dark green
g.setColor(left ? "#08f" : "#080").fillCircle(this.x + half, this.y + half, half); g.setColor(left ? "#08f" : "#080").fillCircle(this.x + half, this.y + half, half);
if (left) { if (left) {
@ -113,10 +109,10 @@
} }
g.reset(); g.reset();
} }
if (setting('large')) { if (settings.large) {
g.setFont("6x8",2); g.setFont("6x8",2);
g.setFontAlign(-1, 0); g.setFontAlign(-1, 0);
g.drawString(stps, this.x + (setting('progress')?28:4), this.y + 12); g.drawString(stps, this.x + (settings.progress?28:4), this.y + 12);
} else { } else {
let w = 24; let w = 24;
if (stps.length > 3){ if (stps.length > 3){
@ -137,11 +133,12 @@
getSteps:()=>stp_today getSteps:()=>stp_today
}; };
// Load data at startup // Load data at startup
let pedomData = require("Storage").readJSON(PEDOMFILE,1); let pedomData = loadSettings();
if (pedomData) { if (pedomData) {
if (pedomData.lastUpdate) if (pedomData.lastUpdate)
lastUpdate = new Date(pedomData.lastUpdate); lastUpdate = new Date(pedomData.lastUpdate);
stp_today = pedomData.stepsToday|0; stp_today = pedomData.stepsToday|0;
delete pedomData; delete pedomData;
} }
WIDGETS["wpedom"].width = WIDGETS["wpedom"].getWidth();
})() })()