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 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() { let saveStopWatch = function() {
require("Storage").writeJSON("stopwatch.json", stopWatch); require("Storage").writeJSON("mystopwatch.json", stopWatch);
} }
let stopWatch1Timer = null; let stopWatchTimer = null;
let myMessage = ""; let myMessage = "";
let temperature = ""; let temperature = "";
@@ -26,6 +26,38 @@
let subMenu = null; let subMenu = null;
let menuCommand = ""; 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() { let paintFace = function() {
if (watchState != STATE_IDLE) return; if (watchState != STATE_IDLE) return;
@@ -64,6 +96,8 @@
if (!stopWatch.start1 && !stopWatch.elapsed1) { if (!stopWatch.start1 && !stopWatch.elapsed1) {
g.setFontAlign(0, 0).setFont("Vector", 26).drawString(myMessage, x, y-45); g.setFontAlign(0, 0).setFont("Vector", 26).drawString(myMessage, x, y-45);
} }
drawStopWatches();
}; };
// Actually draw the watch face // Actually draw the watch face
@@ -106,36 +140,6 @@
return text; 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) { let handleSwipe = function(dir1, dir2) {
var direction; var direction;
@@ -192,9 +196,9 @@
} }
saveStopWatch(); saveStopWatch();
if (!stopWatch1Timer) { if (!stopWatchTimer) {
drawStopWatches(); drawStopWatches();
stopWatch1Timer = setInterval(drawStopWatches, 100); stopWatchTimer = setInterval(drawStopWatches, 100);
} }
} }
@@ -206,9 +210,9 @@
} }
saveStopWatch(); saveStopWatch();
if (!stopWatch1Timer) { if (!stopWatchTimer) {
drawStopWatches(); drawStopWatches();
stopWatch1Timer = setInterval(drawStopWatches, 100); stopWatchTimer = setInterval(drawStopWatches, 100);
} }
} }
@@ -219,9 +223,9 @@
saveStopWatch(); saveStopWatch();
if (!stopWatch.start2) { if (!stopWatch.start2) {
if (stopWatch1Timer) { if (stopWatchTimer) {
clearInterval(stopWatch1Timer); clearInterval(stopWatchTimer);
stopWatch1Timer = null; stopWatchTimer = null;
} }
} }
}; };
@@ -232,9 +236,9 @@
saveStopWatch(); saveStopWatch();
if (!stopWatch.start2) { if (!stopWatch.start2) {
if (stopWatch1Timer) { if (stopWatchTimer) {
clearInterval(stopWatch1Timer); clearInterval(stopWatchTimer);
stopWatch1Timer = null; stopWatchTimer = null;
} }
} }
}; };
@@ -246,9 +250,9 @@
saveStopWatch(); saveStopWatch();
if (!stopWatch.start1) { if (!stopWatch.start1) {
if (stopWatch1Timer) { if (stopWatchTimer) {
clearInterval(stopWatch1Timer); clearInterval(stopWatchTimer);
stopWatch1Timer = null; stopWatchTimer = null;
} }
} }
}; };
@@ -259,9 +263,9 @@
saveStopWatch(); saveStopWatch();
if (!stopWatch.start1) { if (!stopWatch.start1) {
if (stopWatch1Timer) { if (stopWatchTimer) {
clearInterval(stopWatch1Timer); clearInterval(stopWatchTimer);
stopWatch1Timer = null; stopWatchTimer = null;
} }
} }
}; };
@@ -357,10 +361,7 @@
} }
myMessage = key; myMessage = key;
if (paintFace) { if (paintFace) paintFace();
paintFace();
drawStopWatches();
}
} }
}; };
@@ -402,6 +403,8 @@
Bangle.removeListener('twist', handleTwist); Bangle.removeListener('twist', handleTwist);
if (drawTimer) clearTimeout(drawTimer); if (drawTimer) clearTimeout(drawTimer);
drawTimer = undefined; drawTimer = undefined;
if (stopWatchTimer) clearTimeout(stopWatchTimer);
stopWatchTimer = undefined;
paintFace = undefined; // http request may resolve after font's been unloaded, so unset paintFace = undefined; // http request may resolve after font's been unloaded, so unset
}}); }});
@@ -410,8 +413,8 @@
paintFace(); paintFace();
drawStopWatches(); drawStopWatches();
if (stopWatch.start1 || stopWatch.start2) { if (stopWatch.start1 || stopWatch.start2) {
if (!stopWatch1Timer) { if (!stopWatchTimer) {
stopWatch1Timer = setInterval(drawStopWatches, 100); stopWatchTimer = setInterval(drawStopWatches, 100);
} }
} }
draw(); draw();