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'); const tt = require('tevtimer');
function showAlarm(alarm) { function showAlarm(alarm) {
const alarmIndex = alarms.indexOf(alarm);
const settings = require("sched").getSettings(); 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; let message = timer.display_name() + '\n' + alarm.msg;
// If there's a timer chained from this one, start it // If there's a timer chained from this one, start it
@ -20,8 +23,6 @@ function showAlarm(alarm) {
chainTimer.reset(); chainTimer.reset();
chainTimer.start(); chainTimer.start();
tt.set_last_viewed_timer(chainTimer); tt.set_last_viewed_timer(chainTimer);
tt.save_timers();
tt.update_system_alarms(); // FIXME: This might break snoozing
} else { } else {
console.warn("tevtimer: unable to find chained timer with ID " + timer.chain_id); console.warn("tevtimer: unable to find chained timer with ID " + timer.chain_id);
} }
@ -58,10 +59,10 @@ function showAlarm(alarm) {
alarm.t %= 86400000; alarm.t %= 86400000;
Bangle.emit("alarmSnooze", alarm); Bangle.emit("alarmSnooze", alarm);
} else { } else {
let del = alarm.del === undefined ? settings.defaultDeleteExpiredTimers : alarm.del; // Don't do timer deletions here; this is handled by the
if (del) { // tevtimer library code (and it may rearrange the alarm indeces
alarms.splice(alarmIndex, 1); // in the process)
} else {
if (alarm.date && alarm.rp) { if (alarm.date && alarm.rp) {
setNextRepeatDate(alarm); setNextRepeatDate(alarm);
} else if (!alarm.timer) { } else if (!alarm.timer) {
@ -76,11 +77,14 @@ function showAlarm(alarm) {
} }
} }
Bangle.emit("alarmDismiss", alarm); Bangle.emit("alarmDismiss", alarm);
}
// The updated alarm is still a member of 'alarms' // The updated alarm is still a member of 'alarms'
// so writing to array writes changes back directly // so writing to array writes changes back directly
require("sched").setAlarms(alarms); require("sched").setAlarms(alarms);
// Update system alarms for any changed timers just before we finish
tt.update_system_alarms();
load(); load();
}); });

View File

@ -297,7 +297,7 @@ class TimerView {
// Set up timeout to render timer again when needed // Set up timeout to render timer again when needed
if (update_interval !== Infinity) { 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 = setTimeout(
() => { () => {
this.listeners.timer_render_timeout = null; this.listeners.timer_render_timeout = null;

View File

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