Skip to content

Commit bce2e55

Browse files
committed
[multitimer] handle timers going into next day
1 parent e43ce34 commit bce2e55

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

apps/multitimer/app.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Bangle.loadWidgets();
33
Bangle.drawWidgets();
44

55
const R = Bangle.appRect;
6+
const MS_PER_DAY = 86400000;
67
let layer;
78
let drag;
89
let timerInt1 = [];
@@ -17,6 +18,20 @@ function getCurrentTime() {
1718
);
1819
}
1920

21+
function getTimeRemaining(alarm) {
22+
let rem = require('sched').getTimeToAlarm(alarm);
23+
if (rem === undefined) {
24+
// fallback: compute time difference
25+
const now = getCurrentTime();
26+
if (alarm.t > now) {
27+
rem = alarm.t - now;
28+
} else {
29+
rem = MS_PER_DAY - (now - alarm.t);
30+
}
31+
}
32+
return rem;
33+
}
34+
2035
function decodeTime(t) {
2136
let hrs = 0 | Math.floor(t / 3600000);
2237
let mins = 0 | Math.floor(t / 60000 % 60);
@@ -100,7 +115,7 @@ function drawTimers() {
100115
}
101116
else if (idx > 0 && idx < timers.length+1) {
102117
if (timers[idx-1].on == true) {
103-
drawMenuItem(formatTime(timers[idx-1].t-getCurrentTime()));
118+
drawMenuItem(formatTime(getTimeRemaining(timers[idx-1])));
104119
updateTimers(idx-1);
105120
}
106121
else drawMenuItem(formatTime(timers[idx-1].timer));
@@ -156,7 +171,7 @@ function timerMenu(idx) {
156171
let msg = "";
157172
if (a.msg) msg = "\n"+(a.msg.length > 10 ? a.msg.substring(0, 10)+"..." : a.msg);
158173
if (a.on == true) {
159-
drawMenuItem(formatTime(a.t-getCurrentTime())+msg);
174+
drawMenuItem(formatTime(getTimeRemaining(a))+msg);
160175
updateTimer();
161176
}
162177
else {
@@ -184,7 +199,7 @@ function timerMenu(idx) {
184199
if (i == 1) {
185200
if (a.on == true) {
186201
clearInt();
187-
a.timer = a.t-getCurrentTime();
202+
a.timer = getTimeRemaining(a);
188203
a.on = false;
189204
timers[timerIdx[idx]] = a;
190205
saveAndReload();
@@ -694,8 +709,7 @@ function setUI() {
694709
const origRemove = Bangle.uiRemove;
695710
Bangle.uiRemove = () => {
696711
Bangle.removeListener("drag", onDrag);
697-
Object.values(timerInt1).forEach(clearTimeout);
698-
Object.values(timerInt2).forEach(clearTimeout);
712+
clearInt();
699713
if (origRemove) origRemove();
700714
};
701715
}

0 commit comments

Comments
 (0)