Skip to content

Commit 54a3f7d

Browse files
committed
style: minor review reminder comments and reorganization
Some minor changes requested at ankidroid#20325 by David and Ashish.
1 parent 4f61155 commit 54a3f7d

4 files changed

Lines changed: 31 additions & 20 deletions

File tree

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ package com.ichi2.anki.reviewreminders
1919
import kotlinx.serialization.SerializationException
2020
import kotlinx.serialization.json.Json
2121

22+
/**
23+
* Convenience typealias for the mutation functions passed to editors of [ReviewReminderGroup].
24+
*/
25+
typealias ReviewReminderGroupEditor = (ReviewReminderGroup) -> ReviewReminderGroup
26+
2227
/**
2328
* A group of review reminders, all for the same [ReviewReminderScope],
2429
* persisted as a single JSON string in SharedPreferences.
@@ -27,6 +32,9 @@ import kotlinx.serialization.json.Json
2732
* explicitly defined to restrict what can be done with the interface and to eliminate the
2833
* need to verbosely type out "HashMap<ReviewReminderId, ReviewReminder>" everywhere.
2934
*
35+
* Edits to instances of this class are not automatically persisted to SharedPreferences;
36+
* that functionality is provided by [ReviewRemindersDatabase].
37+
*
3038
* A HashMap is used to allow for O(1) access to individual reminders by [ReviewReminderId].
3139
*/
3240
class ReviewReminderGroup(
@@ -39,9 +47,7 @@ class ReviewReminderGroup(
3947
/**
4048
* Manually construct a [ReviewReminderGroup] from key-value pairs.
4149
*/
42-
constructor(vararg pairs: Pair<ReviewReminderId, ReviewReminder>) : this(
43-
buildMap { pairs.forEach { put(it.first, it.second) } },
44-
)
50+
constructor(vararg pairs: Pair<ReviewReminderId, ReviewReminder>) : this(hashMapOf(*pairs))
4551

4652
/**
4753
* Merge multiple [ReviewReminderGroup]s into one.
@@ -112,8 +118,3 @@ class ReviewReminderGroup(
112118
* Convenience extension constructor for merging a list of [ReviewReminderGroup]s into one.
113119
*/
114120
fun List<ReviewReminderGroup>.mergeAll() = ReviewReminderGroup(*this.toTypedArray())
115-
116-
/**
117-
* Convenience typealias for the mutation functions passed to editors of [ReviewReminderGroup].
118-
*/
119-
typealias ReviewReminderGroupEditor = (ReviewReminderGroup) -> ReviewReminderGroup

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ data class ReviewReminderSchemaV2(
115115
/**
116116
* Schema migration settings for testing purposes.
117117
* Consult this as an example of how to save old schemas and define their [ReviewReminderSchema.migrate] methods.
118+
* Also see the unit tests for [ReviewRemindersDatabase], where these classes are exercised.
119+
* These classes cannot be moved directly to the test file because [ReviewReminderSchema] is a sealed interface,
120+
* meaning all implementations of it must be within the same module.
118121
*/
119122
object TestingReviewReminderMigrationSettings {
120123
/**

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

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import androidx.annotation.VisibleForTesting
2222
import androidx.core.content.edit
2323
import com.ichi2.anki.AnkiDroidApp
2424
import com.ichi2.anki.CrashReportService
25+
import com.ichi2.anki.common.utils.ellipsize
2526
import com.ichi2.anki.libanki.DeckId
2627
import com.ichi2.anki.settings.Prefs
2728
import com.ichi2.anki.showError
@@ -108,9 +109,9 @@ object ReviewRemindersDatabase {
108109
/**
109110
* Current [ReviewReminder] schema version. Whenever [ReviewReminder] is modified, this integer MUST be incremented.
110111
*
111-
* Version 1: 3 August 2025 - Initial version
112-
* Version 2: 25 January 2026 - Added [ReviewReminder.onlyNotifyIfNoReviews]
113-
* Version 3: 8 February 2026 - Added [ReviewReminder.latestNotifTime]
112+
* - Version 1: [ReviewReminderSchemaV1]: 3 August 2025 - Initial version
113+
* - Version 2: [ReviewReminderSchemaV2]: 25 January 2026 - Added [ReviewReminder.onlyNotifyIfNoReviews]
114+
* - Version 3: [ReviewReminder]: 8 February 2026 - Added [ReviewReminder.latestNotifTime]
114115
*
115116
* @see [oldReviewReminderSchemasForMigration]
116117
* @see [ReviewReminder]
@@ -275,6 +276,8 @@ object ReviewRemindersDatabase {
275276
* Get the [ReviewReminder]s for a specific key.
276277
* Deletes the stored review reminders and returns an empty [ReviewReminderGroup] if deserialization fails
277278
* and no valid schema migrations exist.
279+
*
280+
* @see decodeJson
278281
*/
279282
private fun getRemindersForKey(key: String): ReviewReminderGroup {
280283
val jsonString = remindersSharedPrefs.getString(key, null) ?: return ReviewReminderGroup()
@@ -320,6 +323,8 @@ object ReviewRemindersDatabase {
320323
* @param expectedScope The expected scope of all review reminders in the resulting map.
321324
*
322325
* @throws IllegalArgumentException If the result of applying the [editor] contains review reminders of a scope other than [expectedScope].
326+
*
327+
* @see getRemindersForKey
323328
*/
324329
private fun editRemindersForKey(
325330
key: String,
@@ -380,15 +385,15 @@ object ReviewRemindersDatabase {
380385
*/
381386
fun checkDeserializationErrors(context: Context) {
382387
Prefs.reviewReminderDeserializationErrors?.let { errorString ->
383-
if (errorString.isNotEmpty()) {
384-
context.showError(
385-
message =
386-
"An error occurred while loading your review reminders, corrupted reminders have been deleted. " +
387-
"Details:\n\n$errorString",
388-
crashReportData = null, // Crash reports are sent when the error is first encountered
389-
)
390-
Prefs.reviewReminderDeserializationErrors = ""
391-
}
388+
if (errorString.isEmpty()) return
389+
390+
context.showError(
391+
message =
392+
"An error occurred while loading your review reminders, corrupted reminders have been deleted. " +
393+
"Details:\n\n${errorString.ellipsize(1000)}",
394+
crashReportData = null, // Crash reports are sent when the error is first encountered
395+
)
396+
Prefs.reviewReminderDeserializationErrors = ""
392397
}
393398
}
394399
}

AnkiDroid/src/test/java/com/ichi2/anki/reviewreminders/ReviewRemindersDatabaseTest.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import org.hamcrest.Matchers.equalTo
3232
import org.hamcrest.Matchers.hasItem
3333
import org.hamcrest.Matchers.not
3434
import org.hamcrest.TypeSafeMatcher
35+
import org.intellij.lang.annotations.Language
3536
import org.junit.After
3637
import org.junit.Before
3738
import org.junit.Test
@@ -445,6 +446,7 @@ class ReviewRemindersDatabaseTest : RobolectricTest() {
445446
*/
446447
@Test
447448
fun `raw ReviewReminder string can be deserialized without throwing`() {
449+
@Language("JSON")
448450
val rawString =
449451
"""
450452
{

0 commit comments

Comments
 (0)