reschedule method
Reschedule all notifications based on current preferences and state.
Implementation
Future<void> reschedule() async {
try {
final service = _ref.read(notificationServiceProvider);
if (service == null) return;
final ds = _ref.read(supabaseDatasourceProvider);
final prefsJson = await ds.getNotificationPreferences();
final prefs = NotificationPreferences.fromJson(prefsJson);
// Daily reminder
if (prefs.dailyReminderEnabled) {
final dueCount = await ds.getDueCardCount();
await service.scheduleDailyReminder(
hour: prefs.dailyReminderHour,
minute: prefs.dailyReminderMinute,
dueCardCount: dueCount,
);
} else {
await service.cancel(NotificationIds.dailyReminder);
}
// Streak alert
if (prefs.streakAlertEnabled) {
final dates = await ds.getSessionDates(limit: 90);
final streak = StreakCalculator.currentStreak(dates);
if (streak > 0) {
// Only schedule if user hasn't studied today
final today = DateTime.now();
final todayDate = DateTime(today.year, today.month, today.day);
final studiedToday = dates.isNotEmpty && dates.first == todayDate;
if (!studiedToday) {
await service.scheduleStreakAlert(currentStreak: streak);
} else {
await service.cancel(NotificationIds.streakAlert);
}
} else {
await service.cancel(NotificationIds.streakAlert);
}
} else {
await service.cancel(NotificationIds.streakAlert);
}
// Overdue warning
if (prefs.overdueAlertEnabled) {
final nextReview = await ds.getNextReviewDate();
if (nextReview != null) {
await service.scheduleOverdueWarning(
nextReviewDate: nextReview,
thresholdDays: prefs.overdueThresholdDays,
);
} else {
await service.cancel(NotificationIds.overdueWarning);
}
} else {
await service.cancel(NotificationIds.overdueWarning);
}
} catch (e) {
dev.log(
'Failed to reschedule notifications: $e',
name: 'NotificationScheduler',
);
}
}