main, fix rescheduling by making each day's alarm ID unique so that it does not cancel the alarm currently going off.

This commit is contained in:
me committed 2026-11-11 08:14:47 -07:00
1 parent 8adaecbf7d
commit f0b723e158
1 file changed
+27 -5
+27 -5
View File
@@ -88,7 +88,12 @@ class _HomeScreenState extends State<HomeScreen> {
String? _minuteErrorText; String? _minuteErrorText;
bool _validTime = false; bool _validTime = false;
static const _alarmId = 1001; static int _alarmId = 20260000;
int dateAsId(DateTime d) => d.year * 10000 + d.month * 100 + d.day;
int todayAsId() {
return dateAsId(DateTime.now());
}
@override @override
void initState() { void initState() {
@@ -99,7 +104,10 @@ class _HomeScreenState extends State<HomeScreen> {
// Reschedule alarm any time one goes off. // Reschedule alarm any time one goes off.
Alarm.ringing.listen((AlarmSet alarmSet) { Alarm.ringing.listen((AlarmSet alarmSet) {
for (final alarm in alarmSet.alarms) { for (final alarm in alarmSet.alarms) {
if (alarm.id == _alarmId) _scheduleAlarm(); if (alarm.id == todayAsId()) {
_scheduleAlarm();
break;
}
} }
}); });
} }
@@ -249,7 +257,9 @@ class _HomeScreenState extends State<HomeScreen> {
alarmTime = alarmTime.add(const Duration(days: 1)); alarmTime = alarmTime.add(const Duration(days: 1));
} }
_debug("_scheduleAlarm, alarmTime=" + alarmTime.toIso8601String()); _debug("_scheduleAlarm, alarmTime=${alarmTime.toIso8601String()}");
_alarmId = dateAsId(alarmTime);
_debug("_alarmId=$_alarmId");
if (Platform.isLinux) { if (Platform.isLinux) {
// Linux: use a Timer (app must stay open) // Linux: use a Timer (app must stay open)
@@ -338,6 +348,16 @@ class _HomeScreenState extends State<HomeScreen> {
icon: 'ic_notification', icon: 'ic_notification',
), ),
); );
// Skip if tomorrow's alarm already exists.
final existing = await Alarm.getAlarm(_alarmId);
if (existing == settings) {
_debug(
"Alarm already exists, skipping schedule. ${settings.toString()}",
);
return;
}
await Alarm.set(alarmSettings: settings); await Alarm.set(alarmSettings: settings);
} }
// End Alarm Method ***/ // End Alarm Method ***/
@@ -363,11 +383,13 @@ class _HomeScreenState extends State<HomeScreen> {
} else if (Platform.isAndroid) { } else if (Platform.isAndroid) {
// Notification Method // Notification Method
if (ANDROID_USE_NOTIF) { if (ANDROID_USE_NOTIF) {
await _notifications.cancel(id: _alarmId); //await _notifications.cancel(id: _alarmId);
await _notifications.cancelAll();
} }
// Alarm Method // Alarm Method
if (ANDROID_USE_ALARM) { if (ANDROID_USE_ALARM) {
await Alarm.stop(_alarmId); //await Alarm.stop(_alarmId);
await Alarm.stopAll();
} }
} }
_showSnack('Alarm Cancelled'); _showSnack('Alarm Cancelled');