refactor: Improve stopwatch management and display

This commit is contained in:
2026-03-09 14:12:06 -06:00
committed by Tanner Collin (aider)
parent dc44ac9910
commit 0b4d8d84c4

View File

@@ -11,11 +11,11 @@
let watchState = STATE_IDLE;
let stopWatch = require("Storage").readJSON("stopwatch.json", {start1: null, elapsed1: null, start2: null, elapsed2: null});
let stopWatch = require("Storage").readJSON("mystopwatch.json", {start1: null, elapsed1: null, start2: null, elapsed2: null});
let saveStopWatch = function() {
require("Storage").writeJSON("stopwatch.json", stopWatch);
require("Storage").writeJSON("mystopwatch.json", stopWatch);
}
let stopWatch1Timer = null;
let stopWatchTimer = null;
let myMessage = "";
let temperature = "";
@@ -26,6 +26,38 @@
let subMenu = null;
let menuCommand = "";
let drawStopWatches = function () {
if (watchState != STATE_IDLE) return;
var w = g.getWidth();
var x = w / 2;
var y = g.getHeight() / 2;
if (stopWatch.start1 || stopWatch.elapsed1) {
let Tt1 = (stopWatch.elapsed1 || 0);
if (stopWatch.start1) {
Tt1 += Date.now() - stopWatch.start1;
}
let Ttxt1 = timeToText(Tt1);
g.clearRect(0, y-60, w, y-34);
g.setColor(g.theme.fg);
g.setFontAlign(0, 0).setFont("Vector", 26).drawString("S1: " + Ttxt1, x, y-45);
}
if (stopWatch.start2 || stopWatch.elapsed2) {
let Tt2 = (stopWatch.elapsed2 || 0);
if (stopWatch.start2) {
Tt2 += Date.now() - stopWatch.start2;
}
let Ttxt2 = timeToText(Tt2);
g.clearRect(0, y+61, w, y+88);
g.setColor(g.theme.fg);
g.setFontAlign(0, 0).setFont("Vector", 26).drawString("S2: " + Ttxt2, x, y+76);
}
}
let paintFace = function() {
if (watchState != STATE_IDLE) return;
@@ -64,6 +96,8 @@
if (!stopWatch.start1 && !stopWatch.elapsed1) {
g.setFontAlign(0, 0).setFont("Vector", 26).drawString(myMessage, x, y-45);
}
drawStopWatches();
};
// Actually draw the watch face
@@ -106,36 +140,6 @@
return text;
};
let drawStopWatches = function () {
if (watchState != STATE_IDLE) return;
var w = g.getWidth();
var x = w / 2;
var y = g.getHeight() / 2;
if (stopWatch.start1 || stopWatch.elapsed1) {
let Tt1 = (stopWatch.elapsed1 || 0);
if (stopWatch.start1) {
Tt1 += Date.now() - stopWatch.start1;
}
let Ttxt1 = timeToText(Tt1);
g.clearRect(0, y-60, w, y-34);
g.setFontAlign(0, 0).setFont("Vector", 26).drawString(Ttxt1, x, y-45);
}
if (stopWatch.start2 || stopWatch.elapsed2) {
let Tt2 = (stopWatch.elapsed2 || 0);
if (stopWatch.start2) {
Tt2 += Date.now() - stopWatch.start2;
}
let Ttxt2 = timeToText(Tt2);
g.clearRect(0, y+61, w, y+88);
g.setFontAlign(0, 0).setFont("Vector", 26).drawString(Ttxt2, x, y+76);
}
}
let handleSwipe = function(dir1, dir2) {
var direction;
@@ -192,9 +196,9 @@
}
saveStopWatch();
if (!stopWatch1Timer) {
if (!stopWatchTimer) {
drawStopWatches();
stopWatch1Timer = setInterval(drawStopWatches, 100);
stopWatchTimer = setInterval(drawStopWatches, 100);
}
}
@@ -206,9 +210,9 @@
}
saveStopWatch();
if (!stopWatch1Timer) {
if (!stopWatchTimer) {
drawStopWatches();
stopWatch1Timer = setInterval(drawStopWatches, 100);
stopWatchTimer = setInterval(drawStopWatches, 100);
}
}
@@ -219,9 +223,9 @@
saveStopWatch();
if (!stopWatch.start2) {
if (stopWatch1Timer) {
clearInterval(stopWatch1Timer);
stopWatch1Timer = null;
if (stopWatchTimer) {
clearInterval(stopWatchTimer);
stopWatchTimer = null;
}
}
};
@@ -232,9 +236,9 @@
saveStopWatch();
if (!stopWatch.start2) {
if (stopWatch1Timer) {
clearInterval(stopWatch1Timer);
stopWatch1Timer = null;
if (stopWatchTimer) {
clearInterval(stopWatchTimer);
stopWatchTimer = null;
}
}
};
@@ -246,9 +250,9 @@
saveStopWatch();
if (!stopWatch.start1) {
if (stopWatch1Timer) {
clearInterval(stopWatch1Timer);
stopWatch1Timer = null;
if (stopWatchTimer) {
clearInterval(stopWatchTimer);
stopWatchTimer = null;
}
}
};
@@ -259,9 +263,9 @@
saveStopWatch();
if (!stopWatch.start1) {
if (stopWatch1Timer) {
clearInterval(stopWatch1Timer);
stopWatch1Timer = null;
if (stopWatchTimer) {
clearInterval(stopWatchTimer);
stopWatchTimer = null;
}
}
};
@@ -357,10 +361,7 @@
}
myMessage = key;
if (paintFace) {
paintFace();
drawStopWatches();
}
if (paintFace) paintFace();
}
};
@@ -402,6 +403,8 @@
Bangle.removeListener('twist', handleTwist);
if (drawTimer) clearTimeout(drawTimer);
drawTimer = undefined;
if (stopWatchTimer) clearTimeout(stopWatchTimer);
stopWatchTimer = undefined;
paintFace = undefined; // http request may resolve after font's been unloaded, so unset
}});
@@ -410,8 +413,8 @@
paintFace();
drawStopWatches();
if (stopWatch.start1 || stopWatch.start2) {
if (!stopWatch1Timer) {
stopWatch1Timer = setInterval(drawStopWatches, 100);
if (!stopWatchTimer) {
stopWatchTimer = setInterval(drawStopWatches, 100);
}
}
draw();