widpedom: Fix daily goal, don't store settings in separate file

master
Richard de Boer 2020-04-09 12:05:25 +02:00
parent 4f7cbbb1b5
commit a0fa104078
4 changed files with 35 additions and 32 deletions

View File

@ -786,7 +786,7 @@
{ "id": "widpedom", { "id": "widpedom",
"name": "Pedometer widget", "name": "Pedometer widget",
"icon": "widget.png", "icon": "widget.png",
"version":"0.09", "version":"0.10",
"description": "Daily pedometer widget", "description": "Daily pedometer widget",
"tags": "widget", "tags": "widget",
"type":"widget", "type":"widget",

View File

@ -6,3 +6,4 @@
0.07: Tweaks for variable size widget system 0.07: Tweaks for variable size widget system
0.08: Ensure redrawing works with variable size widget system 0.08: Ensure redrawing works with variable size widget system
0.09: Add daily goal 0.09: Add daily goal
0.10: Fix daily goal, don't store settings in separate file

View File

@ -1,5 +1,5 @@
(function(back) { (function(back) {
const SETTINGS_FILE = 'widpedom.settings.json' const PEDOMFILE = "wpedom.json";
// initialize with default settings... // initialize with default settings...
let s = { let s = {
@ -9,13 +9,15 @@
// ...and overwrite them with any saved values // ...and overwrite them with any saved values
// This way saved values are preserved if a new version adds more settings // This way saved values are preserved if a new version adds more settings
const storage = require('Storage') const storage = require('Storage')
const saved = storage.readJSON(SETTINGS_FILE, 1) || {} const d = storage.readJSON(PEDOMFILE, 1) || {}
const saved = d.settings || {}
for (const key in saved) { for (const key in saved) {
s[key] = saved[key] s[key] = saved[key]
} }
function save() { function save() {
storage.write(SETTINGS_FILE, s) d.settings = s
storage.write(PEDOMFILE, d)
WIDGETS['wpedom'].reload() WIDGETS['wpedom'].reload()
} }

View File

@ -1,6 +1,5 @@
(() => { (() => {
const PEDOMFILE = "wpedom.json" const PEDOMFILE = "wpedom.json"
const SETTINGS_FILE = "widpedom.settings.json"
const DEFAULTS = { const DEFAULTS = {
'goal': 10000, 'goal': 10000,
'progress': false, 'progress': false,
@ -16,7 +15,8 @@
let settings; let settings;
function loadSettings() { function loadSettings() {
settings = require('Storage').readJSON(SETTINGS_FILE, 1) || {}; const d = require('Storage').readJSON(PEDOMFILE, 1) || {};
settings = d.settings || {};
} }
function setting(key) { function setting(key) {
@ -25,31 +25,29 @@
} }
function drawProgress(stps) { function drawProgress(stps) {
if (setting('progress')) { const width = 24, half = width/2;
const width = 24, half = width/2; const goal = setting('goal'), left = Math.max(goal-stps,0);
const goal = setting('goal'), left = Math.max(goal-stps,0); const c = left ? COLORS.progress : COLORS.done;
const c = left ? COLORS.progress : COLORS.done; g.setColor(c).fillCircle(this.x + half, this.y + half, half);
g.setColor(c).fillCircle(this.x + half, this.y + half, half); if (left) {
if (left) { const f = left/goal; // fraction to blank out
const f = left/goal; // fraction to blank out let p = [];
let p = []; p.push(half,half);
p.push(half,half); p.push(half,0);
p.push(half,0); if(f>1/8) p.push(0,0);
if(f>1/8) p.push(0,0); if(f>2/8) p.push(0,half);
if(f>2/8) p.push(0,half); if(f>3/8) p.push(0,width);
if(f>3/8) p.push(0,width); if(f>4/8) p.push(half,width);
if(f>4/8) p.push(half,width); if(f>5/8) p.push(width,width);
if(f>5/8) p.push(width,width); if(f>6/8) p.push(width,half);
if(f>6/8) p.push(width,half); if(f>7/8) p.push(width,0);
if(f>7/8) p.push(width,0); p.push(half - Math.sin(f * TAU) * half);
p.push(half - Math.sin(f * TAU) * half); p.push(half - Math.cos(f * TAU) * half);
p.push(half - Math.cos(f * TAU) * half); for (let i = p.length; i; i -= 2) {
for (let i = p.length; i; i -= 2) { p[i - 2] += this.x;
p[i - 2] += this.x; p[i - 1] += this.y;
p[i - 1] += this.y;
}
g.setColor(0).fillPoly(p);
} }
g.setColor(0).fillPoly(p);
} }
} }
@ -62,7 +60,7 @@
let stps = stp_today.toString(); let stps = stp_today.toString();
g.reset(); g.reset();
g.clearRect(this.x, this.y, this.x + width, this.y + 23); // erase background g.clearRect(this.x, this.y, this.x + width, this.y + 23); // erase background
drawProgress(stps); if (setting('progress')){ drawProgress.call(this, stps); }
g.setColor(COLORS.white); g.setColor(COLORS.white);
if (stps.length > 3){ if (stps.length > 3){
stps = stps.slice(0,-3) + "," + stps.slice(-3); stps = stps.slice(0,-3) + "," + stps.slice(-3);
@ -104,9 +102,11 @@
}); });
// When unloading, save state // When unloading, save state
E.on('kill', () => { E.on('kill', () => {
if (!settings) { loadSettings() }
let d = { let d = {
lastUpdate : lastUpdate.toISOString(), lastUpdate : lastUpdate.toISOString(),
stepsToday : stp_today stepsToday : stp_today,
settings : settings,
}; };
require("Storage").write(PEDOMFILE,d); require("Storage").write(PEDOMFILE,d);
}); });