Skip to content

Commit 9eeb031

Browse files
authored
Merge pull request #4311 from ticalc-travis/sched-update-alarms
Fix snoozing alarm near midnight (ref #4241)
2 parents ae5b8c1 + 82856e1 commit 9eeb031

7 files changed

Lines changed: 46 additions & 28 deletions

File tree

apps/sched/ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,4 @@
4141
0.38: Fix timer going off immediately if it was long enough it went to the next day (#4220)
4242
0.39: Allow a press of the button to stop/snooze the alarm when it's gone off
4343
0.40: Prevent time-of-day alarms set via setAlarm() that have a time earlier than the current time triggering immediately after #4220 fix; schedule them for tomorrow by default to match original behavior
44+
0.41: Fix issue with snoozing alarms and timers beyond midnight causing the alarm/timer to go off immediately instead of snoozing

apps/sched/lib.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,27 @@ exports.getActiveAlarms = function (alarms, time) {
3434
// Set up a modified alarm/timer so it's ready to insert into the list of alarms. For timers, they fire the set time from now, and alarms are set to fire at the right time on the right day
3535
exports.updateAlarm = function(alarm) {
3636
var time = new Date(), currentTime = timeToMillis(time);
37-
if (alarm.timer) { // if it's a timer, set the start time as a time from *now*
38-
alarm.t = (currentTime + alarm.timer) % 86400000; // alarm time in day
37+
if (alarm.timer) {
38+
if (alarm.ot!==undefined) {
39+
// if `ot` exists, the timer is currently snoozed. In that case,
40+
// `t` is expected to be the time of day the snoozed alarm
41+
// should trigger again. If `t` < current time of day, set
42+
// `date` to tomorrow to avoid an immediate false trigger.
43+
// (#4241)
44+
if (alarm.t < currentTime) {
45+
let tomorrow = new Date();
46+
tomorrow.setDate(tomorrow.getDate() + 1);
47+
alarm.date = tomorrow.toLocalISOString().substr(0, 10);
48+
}
49+
} else {
50+
// if it's a new timer, set the start time as a time from *now*
51+
alarm.t = (currentTime + alarm.timer) % 86400000; // alarm time in day
52+
if (alarm.t < currentTime || alarm.timer>86400000/*24h*/)
53+
alarm.date = new Date(time.getTime() + alarm.timer).toLocalISOString().substr(0, 10);
54+
else delete alarm.date;
55+
}
3956
alarm.last = 0; // don't need to specify a last day for alarms
4057
// if timer would have gone on until a later day, set a date (fix #4220)
41-
if (alarm.t < currentTime || alarm.timer>86400000/*24h*/)
42-
alarm.date = new Date(time.getTime() + alarm.timer).toLocalISOString().substr(0, 10);
43-
else delete alarm.date;
4458
} else { // If it's an alarm, default to triggering tomorrow if time < current time of day (#4232)
4559
if (alarm.last===undefined) {
4660
var time = new Date();

apps/sched/metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "sched",
33
"name": "Scheduler",
4-
"version": "0.40",
4+
"version": "0.41",
55
"author": "gfwilliams",
66
"description": "Scheduling library for alarms and timers",
77
"icon": "app.png",

apps/sched/sched.js

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,39 @@ function formatMS(ms) {
1616
}
1717
}
1818

19-
function showSnoozeMenu(alarm){
20-
21-
Bangle.buzz(40);
22-
23-
function onSnooze(snoozeTime) {
19+
function snoozeAlarm(alarm, snoozeTime) {
2420
if (alarm.ot === undefined) {
2521
alarm.ot = alarm.t;
2622
}
2723
let time = new Date();
2824
let currentTime = (time.getHours()*3600000)+(time.getMinutes()*60000)+(time.getSeconds()*1000);
2925
alarm.t = currentTime + snoozeTime;
3026
alarm.t %= 86400000;
27+
28+
// This makes updateAlarm() recompute the last alarm date so
29+
// that it works correctly if we're snoozing beyond midnight
30+
delete alarm.last;
31+
32+
require("sched").updateAlarm(alarm);
3133
Bangle.emit("alarmSnooze", alarm);
3234

3335
// The updated alarm is still a member of 'alarms'
3436
// so writing to array writes changes back directly
3537
require("sched").setAlarms(alarms);
38+
}
39+
40+
function showSnoozeMenu(alarm){
41+
42+
Bangle.buzz(40);
43+
44+
function onSnooze(snoozeTime) {
45+
snoozeAlarm(alarm, snoozeTime);
3646
load();
3747
}
3848

3949
if(alarm.timer){
4050

41-
let timerLength=alarm.timer
51+
let timerLength=alarm.timer;
4252
let buttons={ "15s": 15, "30s":30,"1m":60 ,"2m":120,"5m":360};
4353
let formattedLength = formatMS(timerLength)+"*";
4454
buttons[formattedLength] = Math.round(timerLength/1000);
@@ -67,7 +77,7 @@ function showAlarm(alarm) {
6777
message = (alarm.timer
6878
? atob("ACQswgD//33vRcGHIQAAABVVVAAAAAAAABVVVAAAAAAAABVVVAAAAAAAABVVVAAAAAAAABVVVAAAAAAAABVVVAAAAAAAAAP/wAAAAAAAAAP/wAAAAAAAAAqqoAPAAAAAAqqqqoP8AAAAKqqqqqv/AAACqqqqqqq/wAAKqqqlWqqvwAAqqqqlVaqrAACqqqqlVVqqAAKqqqqlVVaqgAKqaqqlVVWqgAqpWqqlVVVqoAqlWqqlVVVaoCqlV6qlVVVaqCqVVfqlVVVWqCqVVf6lVVVWqKpVVX/lVVVVqqpVVV/+VVVVqqpVVV//lVVVqqpVVVfr1VVVqqpVVVfr1VVVqqpVVVb/lVVVqqpVVVW+VVVVqqpVVVVVVVVVqiqVVVVVVVVWqCqVVVVVVVVWqCqlVVVVVVVaqAqlVVVVVVVaoAqpVVVVVVVqoAKqVVVVVVWqgAKqlVVVVVaqgACqpVVVVVqqAAAqqlVVVaqoAAAKqqVVWqqgAAACqqqqqqqAAAAAKqqqqqgAAAAAAqqqqoAAAAAAAAqqoAAAAA==")
6979
: atob("AC0swgF97///RcEpMlVVVVVVf9VVVVVVVVX/9VVf9VVf/1VVV///1Vf9VX///VVX///VWqqlV///1Vf//9aqqqqpf//9V///2qqqqqqn///V///6qqqqqqr///X//+qqoAAKqqv//3//6qoAAAAKqr//3//qqAAAAAAqq//3/+qoAADwAAKqv/3/+qgAADwAACqv/3/aqAAADwAAAqp/19qoAAADwAAAKqfV1qgAAADwAAACqXVWqgAAADwAAACqlVWqAAAADwAAAAqlVWqAAAADwAAAAqlVWqAAAADwAAAAqlVaoAAAADwAAAAKpVaoAAAADwAAAAKpVaoAAAADwAAAAKpVaoAAAAOsAAAAKpVaoAAAAOsAAAAKpVaoAAAAL/AAAAKpVaoAAAAgPwAAAKpVaoAAACAD8AAAKpVWqAAAIAA/AAAqlVWqAAAgAAPwAAqlVWqAACAAADwAAqlVWqgAIAAAAAACqlVVqgAgAAAAAACqVVVqoAAAAAAAAKqVVVaqAAAAAAAAqpVVVWqgAAAAAACqlVVVWqoAAAAAAKqlVVVVqqAAAAAAqqVVVVVaqoAAAAKqpVVVVVeqqoAAKqqtVVVVV/6qqqqqqr/VVVVX/2qqqqqqn/1VVVf/VaqqqqpV/9VVVf9VVWqqlVVf9VVVf1VVVVVVVVX9VQ==")
70-
) + " " + message
80+
) + " " + message;
7181
}
7282

7383
Bangle.loadWidgets();
@@ -83,14 +93,7 @@ function showAlarm(alarm) {
8393
return;
8494
}
8595
if (sleep==1) {
86-
if (alarm.ot === undefined) {
87-
alarm.ot = alarm.t;
88-
}
89-
let time = new Date();
90-
let currentTime = (time.getHours()*3600000)+(time.getMinutes()*60000)+(time.getSeconds()*1000);
91-
alarm.t = currentTime + settings.defaultSnoozeMillis;
92-
alarm.t %= 86400000;
93-
Bangle.emit("alarmSnooze", alarm);
96+
snoozeAlarm(alarm, settings.defaultSnoozeMillis);
9497
} else { // sleep=2, stop the alarm
9598
let del = alarm.del === undefined ? settings.defaultDeleteExpiredTimers : alarm.del;
9699
if (del) {
@@ -110,11 +113,12 @@ function showAlarm(alarm) {
110113
}
111114
}
112115
Bangle.emit("alarmDismiss", alarm);
116+
117+
// The updated alarm is still a member of 'alarms'
118+
// so writing to array writes changes back directly
119+
require("sched").setAlarms(alarms);
113120
}
114121

115-
// The updated alarm is still a member of 'alarms'
116-
// so writing to array writes changes back directly
117-
require("sched").setAlarms(alarms);
118122
load();
119123
}
120124

apps/tevtimer/ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
0.01: Official release
22
0.02: Fix misbehavior if multiple alarms become active at once
3+
0.03: Fix issues with snoozing timers past midnight (#4241)

apps/tevtimer/alarm.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ function showAlarm(alarm) {
8484
let currentTime = (time.getHours()*3600000)+(time.getMinutes()*60000)+(time.getSeconds()*1000);
8585
alarm.t = currentTime + settings.defaultSnoozeMillis;
8686
alarm.t %= 86400000;
87+
require("sched").updateAlarm(alarm);
8788
require("sched").setAlarms(alarms);
8889

8990
Bangle.emit("alarmSnooze", alarm);
@@ -105,12 +106,9 @@ function showAlarm(alarm) {
105106
chainTimer.pause();
106107
}
107108
tt.update_system_alarms();
108-
alarms = require("sched").getAlarms();
109109

110110
Bangle.emit("alarmDismiss", alarm);
111111

112-
require("sched").setAlarms(alarms);
113-
114112
if (action === 'halt' || tt.SETTINGS.alarm_return) {
115113
load('tevtimer.app.js');
116114
} else {

apps/tevtimer/metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"name": "tev's timer",
44
"shortName":"tev's timer",
55
"icon": "app.png",
6-
"version": "0.02",
6+
"version": "0.03",
77
"author": "ticalc-travis",
88
"description": "A countdown timer app with interval and repeat features",
99
"screenshots": [ {"url": "screenshot.png" } ],

0 commit comments

Comments
 (0)