Skip to content

Commit f2c9dde

Browse files
committed
feat(reminders): set alarms from ScheduleReminders
GSoC 2025: Review Reminders - Modifies ScheduleReminders so that it sets alarms for review reminders via AlarmManagerService.
1 parent 48eb1e4 commit f2c9dde

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

AnkiDroid/src/main/java/com/ichi2/anki/reviewreminders/ScheduleReminders.kt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import com.ichi2.anki.dialogs.DeckSelectionDialog
3838
import com.ichi2.anki.launchCatchingTask
3939
import com.ichi2.anki.libanki.DeckId
4040
import com.ichi2.anki.model.SelectableDeck
41+
import com.ichi2.anki.services.AlarmManagerService
4142
import com.ichi2.anki.showError
4243
import com.ichi2.anki.snackbar.BaseSnackbarBuilderProvider
4344
import com.ichi2.anki.snackbar.SnackbarBuilder
@@ -182,6 +183,7 @@ class ScheduleReminders :
182183
Timber.d("Handling add/edit dialog result: mode=%s reminder=%s", modeOfFinishedDialog, newOrModifiedReminder)
183184
updateDatabaseForAddEditDialog(newOrModifiedReminder, modeOfFinishedDialog)
184185
updateUIForAddEditDialog(newOrModifiedReminder, modeOfFinishedDialog)
186+
updateAlarmsForAddEditDialog(newOrModifiedReminder, modeOfFinishedDialog)
185187
// Feedback
186188
showSnackbar(
187189
when (modeOfFinishedDialog) {
@@ -278,6 +280,28 @@ class ScheduleReminders :
278280
triggerUIUpdate()
279281
}
280282

283+
/**
284+
* Update the AlarmManager notifications for the new or modified reminder.
285+
* @see handleAddEditDialogResult
286+
*/
287+
private fun updateAlarmsForAddEditDialog(
288+
newOrModifiedReminder: ReviewReminder?,
289+
modeOfFinishedDialog: AddEditReminderDialog.DialogMode,
290+
) {
291+
if (modeOfFinishedDialog is AddEditReminderDialog.DialogMode.Edit) {
292+
AlarmManagerService.unscheduleReviewReminderNotifications(
293+
requireContext(),
294+
modeOfFinishedDialog.reminderToBeEdited,
295+
)
296+
}
297+
newOrModifiedReminder?.let {
298+
AlarmManagerService.scheduleReviewReminderNotification(
299+
requireContext(),
300+
it,
301+
)
302+
}
303+
}
304+
281305
/**
282306
* Sets a TextView's text based on a [ReviewReminderScope].
283307
* The text is either the scope's associated deck's name, or "All Decks" if the scope is global.
@@ -330,6 +354,12 @@ class ScheduleReminders :
330354
// Update UI
331355
reminder.enabled = newState
332356
triggerUIUpdate()
357+
358+
// Update scheduled AlarmManager notifications
359+
when (newState) {
360+
true -> AlarmManagerService.scheduleReviewReminderNotification(requireContext(), reminder)
361+
false -> AlarmManagerService.unscheduleReviewReminderNotifications(requireContext(), reminder)
362+
}
333363
}
334364

335365
/**

0 commit comments

Comments
 (0)