stopwatch: Save state to storage

This keeps the stopwatch running in the background.
pull/2486/head
Erik Andresen 2023-01-09 20:08:51 +01:00
parent cdbc331b17
commit 5494fcc0ec
3 changed files with 34 additions and 6 deletions

View File

@ -1,3 +1,4 @@
0.01: first release 0.01: first release
0.02: Adjust for touch events outside of screen g dimensions 0.02: Adjust for touch events outside of screen g dimensions
0.03: Do not register as watch, manually start clock on button 0.03: Do not register as watch, manually start clock on button
0.04: Keep running in background by saving state

View File

@ -1,7 +1,7 @@
{ {
"id": "stopwatch", "id": "stopwatch",
"name": "Stopwatch Touch", "name": "Stopwatch Touch",
"version": "0.03", "version": "0.04",
"description": "A touch based stop watch for Bangle JS 2", "description": "A touch based stop watch for Bangle JS 2",
"icon": "stopwatch.png", "icon": "stopwatch.png",
"screenshots": [{"url":"screenshot1.png"},{"url":"screenshot2.png"},{"url":"screenshot3.png"}], "screenshots": [{"url":"screenshot1.png"},{"url":"screenshot2.png"},{"url":"screenshot3.png"}],

View File

@ -1,9 +1,21 @@
const CONFIGFILE = "stopwatch.json";
const now = Date.now();
const config = Object.assign({
state: {
total: now,
start: now,
current: now,
running: false,
}
}, require("Storage").readJSON(CONFIGFILE,1) || {});
let w = g.getWidth(); let w = g.getWidth();
let h = g.getHeight(); let h = g.getHeight();
let tTotal = Date.now(); let tTotal = config.state.total;
let tStart = tTotal; let tStart = config.state.start;
let tCurrent = tTotal; let tCurrent = config.state.current;
let running = false; let running = config.state.running;
let timeY = 2*h/5; let timeY = 2*h/5;
let displayInterval; let displayInterval;
let redrawButtons = true; let redrawButtons = true;
@ -15,6 +27,14 @@ const pause_img = atob("GBiBAf////////////////wYP/wYP/wYP/wYP/wYP/wYP/wYP/wYP/wY
const play_img = atob("GBjBAP//AAAAAAAAAAAIAAAOAAAPgAAP4AAP+AAP/AAP/wAP/8AP//AP//gP//gP//AP/8AP/wAP/AAP+AAP4AAPgAAOAAAIAAAAAAAAAAA="); const play_img = atob("GBjBAP//AAAAAAAAAAAIAAAOAAAPgAAP4AAP+AAP/AAP/wAP/8AP//AP//gP//gP//AP/8AP/wAP/AAP+AAP4AAPgAAOAAAIAAAAAAAAAAA=");
const reset_img = atob("GBiBAf////////////AAD+AAB+f/5+f/5+f/5+cA5+cA5+cA5+cA5+cA5+cA5+cA5+cA5+f/5+f/5+f/5+AAB/AAD////////////w=="); const reset_img = atob("GBiBAf////////////AAD+AAB+f/5+f/5+f/5+cA5+cA5+cA5+cA5+cA5+cA5+cA5+cA5+f/5+f/5+f/5+AAB/AAD////////////w==");
function saveState() {
config.state.total = tTotal;
config.state.start = tStart;
config.state.current = tCurrent;
config.state.running = running;
require("Storage").writeJSON(CONFIGFILE, config);
}
function log_debug(o) { function log_debug(o) {
//console.log(o); //console.log(o);
} }
@ -106,6 +126,7 @@ function stopStart() {
} else { } else {
draw(); draw();
} }
saveState();
} }
function setButtonImages() { function setButtonImages() {
@ -130,6 +151,7 @@ function lapReset() {
g.clearRect(0,24,w,h); g.clearRect(0,24,w,h);
draw(); draw();
} }
saveState();
} }
// simple on screen button class // simple on screen button class
@ -226,5 +248,10 @@ g.fillRect(0,0,w,h);
Bangle.loadWidgets(); Bangle.loadWidgets();
Bangle.drawWidgets(); Bangle.drawWidgets();
setButtonImages();
if (running) {
startTimer();
} else {
draw(); draw();
}
setWatch(() => load(), BTN, { repeat: false, edge: "falling" }); setWatch(() => load(), BTN, { repeat: false, edge: "falling" });