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.02: Adjust for touch events outside of screen g dimensions
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",
"name": "Stopwatch Touch",
"version": "0.03",
"version": "0.04",
"description": "A touch based stop watch for Bangle JS 2",
"icon": "stopwatch.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 h = g.getHeight();
let tTotal = Date.now();
let tStart = tTotal;
let tCurrent = tTotal;
let running = false;
let tTotal = config.state.total;
let tStart = config.state.start;
let tCurrent = config.state.current;
let running = config.state.running;
let timeY = 2*h/5;
let displayInterval;
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 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) {
//console.log(o);
}
@ -106,6 +126,7 @@ function stopStart() {
} else {
draw();
}
saveState();
}
function setButtonImages() {
@ -130,6 +151,7 @@ function lapReset() {
g.clearRect(0,24,w,h);
draw();
}
saveState();
}
// simple on screen button class
@ -226,5 +248,10 @@ g.fillRect(0,0,w,h);
Bangle.loadWidgets();
Bangle.drawWidgets();
draw();
setButtonImages();
if (running) {
startTimer();
} else {
draw();
}
setWatch(() => load(), BTN, { repeat: false, edge: "falling" });