Rework timer alarm handling

Avoid conflicts with alarm snoozing, etc.
master
Travis Evans 2025-04-25 15:56:10 -05:00
parent f14046aff6
commit fe04af7cc2
3 changed files with 37 additions and 28 deletions

View File

@ -8,9 +8,12 @@ if (Bangle.SCHED) {
const tt = require('tevtimer');
function showAlarm(alarm) {
const alarmIndex = alarms.indexOf(alarm);
const settings = require("sched").getSettings();
const timer = tt.TIMERS[alarm.data.idx];
const timer = tt.TIMERS[tt.find_timer_by_id(alarm.id)];
if (timer === undefined) {
console.error("tevtimer: unable to find timer with ID " + alarm.id);
return;
}
let message = timer.display_name() + '\n' + alarm.msg;
// If there's a timer chained from this one, start it
@ -20,8 +23,6 @@ function showAlarm(alarm) {
chainTimer.reset();
chainTimer.start();
tt.set_last_viewed_timer(chainTimer);
tt.save_timers();
tt.update_system_alarms(); // FIXME: This might break snoozing
} else {
console.warn("tevtimer: unable to find chained timer with ID " + timer.chain_id);
}
@ -58,29 +59,32 @@ function showAlarm(alarm) {
alarm.t %= 86400000;
Bangle.emit("alarmSnooze", alarm);
} else {
let del = alarm.del === undefined ? settings.defaultDeleteExpiredTimers : alarm.del;
if (del) {
alarms.splice(alarmIndex, 1);
} else {
if (alarm.date && alarm.rp) {
setNextRepeatDate(alarm);
} else if (!alarm.timer) {
alarm.last = new Date().getDate();
}
if (alarm.ot !== undefined) {
alarm.t = alarm.ot;
delete alarm.ot;
}
if (!alarm.rp) {
alarm.on = false;
}
// Don't do timer deletions here; this is handled by the
// tevtimer library code (and it may rearrange the alarm indeces
// in the process)
if (alarm.date && alarm.rp) {
setNextRepeatDate(alarm);
} else if (!alarm.timer) {
alarm.last = new Date().getDate();
}
if (alarm.ot !== undefined) {
alarm.t = alarm.ot;
delete alarm.ot;
}
if (!alarm.rp) {
alarm.on = false;
}
Bangle.emit("alarmDismiss", alarm);
}
Bangle.emit("alarmDismiss", alarm);
// The updated alarm is still a member of 'alarms'
// so writing to array writes changes back directly
require("sched").setAlarms(alarms);
// Update system alarms for any changed timers just before we finish
tt.update_system_alarms();
load();
});

View File

@ -297,7 +297,7 @@ class TimerView {
// Set up timeout to render timer again when needed
if (update_interval !== Infinity) {
console.debug('Next render update scheduled in ' + update_interval);
console.debug('Next render update scheduled in ' + update_interval + ' ms');
this.listeners.timer_render_timeout = setTimeout(
() => {
this.listeners.timer_render_timeout = null;

View File

@ -305,8 +305,13 @@ function set_settings_dirty() {
function delete_system_alarms() {
var alarms = Sched.getAlarms().filter(a => a.appid == 'tevtimer');
for (let alarm of alarms) {
console.debug('delete sched alarm ' + alarm.id);
Sched.setAlarm(alarm.id, undefined);
if (alarm.ot === undefined) {
console.debug('delete_system_alarms: delete sched alarm ' + alarm.id);
Sched.setAlarm(alarm.id, undefined);
} else {
// Avoid deleting timers awaiting snoozing
console.debug('delete_system_alarms: skipping snoozed alarm ' + alarm.id);
}
}
Sched.reload();
}
@ -314,15 +319,15 @@ function delete_system_alarms() {
function set_system_alarms() {
for (let idx = 0; idx < TIMERS.length; idx++) {
let timer = TIMERS[idx];
let time_to_next_alarm = timer.get() / Math.abs(timer.rate);
let time_to_next_alarm = timer.to_msec();
if (timer.is_running() && time_to_next_alarm > 0) {
console.debug('set sched alarm ' + idx + ' (' + time_to_next_alarm + ')');
Sched.setAlarm(idx.toString(), {
console.debug('set_system_alarms: set sched alarm ' + timer.id
+ ' (' + time_to_next_alarm + ' ms)');
Sched.setAlarm(timer.id, {
appid: 'tevtimer',
timer: time_to_next_alarm,
msg: '',
js: "load('tevtimer.alarm.js');",
data: { idx: idx },
});
}
}