|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Eric Li <[email protected]> |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify it under |
| 5 | + * the terms of the GNU General Public License as published by the Free Software |
| 6 | + * Foundation; either version 3 of the License, or (at your option) any later |
| 7 | + * version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, but WITHOUT ANY |
| 10 | + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 11 | + * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 12 | + * |
| 13 | + * You should have received a copy of the GNU General Public License along with |
| 14 | + * this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.ichi2.anki.reviewreminders |
| 18 | + |
| 19 | +import android.content.Context |
| 20 | +import androidx.core.content.edit |
| 21 | +import com.ichi2.anki.preferences.sharedPrefs |
| 22 | +import com.ichi2.libanki.DeckId |
| 23 | +import kotlinx.serialization.json.Json |
| 24 | + |
| 25 | +/** |
| 26 | + * Manages the storage and retrieval of [ReviewReminder]s in SharedPreferences. |
| 27 | + * |
| 28 | + * [ReviewReminder]s can either be tied to a specific deck and trigger based on the number of cards |
| 29 | + * due in that deck, or they can be app-wide reminders that trigger based on the total number |
| 30 | + * of cards due across all decks. |
| 31 | + */ |
| 32 | +class ReviewRemindersDatabase( |
| 33 | + val context: Context, |
| 34 | +) { |
| 35 | + companion object { |
| 36 | + /** |
| 37 | + * Key in SharedPreferences for retrieving deck-specific reminders. |
| 38 | + * Should have deck ID appended to its end, ex. "review_reminders_deck_12345". |
| 39 | + * Its value is a MutableList<[ReviewReminder]> serialized as a JSON String. |
| 40 | + */ |
| 41 | + private const val DECK_SPECIFIC_KEY = "review_reminders_deck_" |
| 42 | + |
| 43 | + /** |
| 44 | + * Key in SharedPreferences for retrieving app-wide reminders. |
| 45 | + * Its value is a MutableList<[ReviewReminder]> serialized as a JSON String. |
| 46 | + */ |
| 47 | + private const val APP_WIDE_KEY = "review_reminders_app_wide" |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Get the [ReviewReminder]s for a specific key. |
| 52 | + */ |
| 53 | + private fun getRemindersForKey(key: String): MutableList<ReviewReminder> { |
| 54 | + val jsonString = context.sharedPrefs().getString(key, null) ?: return mutableListOf() |
| 55 | + return Json.decodeFromString<MutableList<ReviewReminder>>(jsonString) |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Get the [ReviewReminder]s for a specific deck. |
| 60 | + */ |
| 61 | + fun getRemindersForDeck(did: DeckId): MutableList<ReviewReminder> = getRemindersForKey(DECK_SPECIFIC_KEY + did) |
| 62 | + |
| 63 | + /** |
| 64 | + * Get the app-wide [ReviewReminder]s. |
| 65 | + */ |
| 66 | + fun getAllAppWideReminders(): MutableList<ReviewReminder> = getRemindersForKey(APP_WIDE_KEY) |
| 67 | + |
| 68 | + /** |
| 69 | + * Get all [ReviewReminder]s that are associated with a specific deck, all in a single mutable list. |
| 70 | + */ |
| 71 | + fun getAllDeckSpecificReminders(): MutableList<ReviewReminder> { |
| 72 | + val allReminders = mutableListOf<ReviewReminder>() |
| 73 | + context.sharedPrefs().all.forEach { (key, value) -> |
| 74 | + if (key.startsWith(DECK_SPECIFIC_KEY)) { |
| 75 | + val reminders = Json.decodeFromString<MutableList<ReviewReminder>>(value.toString()) |
| 76 | + allReminders.addAll(reminders) |
| 77 | + } |
| 78 | + } |
| 79 | + return allReminders |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Edit the [ReviewReminder]s for a specific key. |
| 84 | + * @param key |
| 85 | + * @param reminderEditor A lambda that takes the current list and returns the updated list. |
| 86 | + */ |
| 87 | + private fun editRemindersForKey( |
| 88 | + key: String, |
| 89 | + reminderEditor: (MutableList<ReviewReminder>) -> MutableList<ReviewReminder>, |
| 90 | + ) { |
| 91 | + val reminders = getRemindersForKey(key) |
| 92 | + val updatedReminders = reminderEditor(reminders) |
| 93 | + context.sharedPrefs().edit { |
| 94 | + putString(key, Json.encodeToString(updatedReminders)) |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Edit the [ReviewReminder]s for a specific deck. |
| 100 | + * @param did |
| 101 | + * @param reminderEditor A lambda that takes the current list and returns the updated list. |
| 102 | + */ |
| 103 | + fun editRemindersForDeck( |
| 104 | + did: DeckId, |
| 105 | + reminderEditor: (MutableList<ReviewReminder>) -> MutableList<ReviewReminder>, |
| 106 | + ) = editRemindersForKey(DECK_SPECIFIC_KEY + did, reminderEditor) |
| 107 | + |
| 108 | + /** |
| 109 | + * Edit the app-wide [ReviewReminder]s. |
| 110 | + * @param reminderEditor A lambda that takes the current list and returns the updated list. |
| 111 | + */ |
| 112 | + fun editAllAppWideReminders(reminderEditor: (MutableList<ReviewReminder>) -> MutableList<ReviewReminder>) = |
| 113 | + editRemindersForKey(APP_WIDE_KEY, reminderEditor) |
| 114 | +} |
0 commit comments