Modern Apps git

← Modern-Apps.git

flashcards: the review streak anchors at yesterday when today is still open

Commit
8181e3b7a010bfed40de7fc9d1fa45ef3ecaf492
Author
Vayun Mathur <[email protected]>
When
2026-12-01 09:42 UTC (5 days ago)
Parents
6075215
The streak loop anchored the consecutive-day run at today
unconditionally, so any day with no review yet — every
morning before the first review — showed streak 0 despite
four consecutive reviewed days ending yesterday. The run
had not broken: today is still in progress, which the same
screen's forecast already assumes (overdue cards lump into
today). The streak logic was the inconsistent one.

The computation is extracted to a pure currentStreakDays
and anchors the run at today when today has a review, else
at yesterday, else 0. Five tests pin: run ending today,
ending yesterday, ended earlier (0), gap yesterday with a
review today (1), and empty (0). FSRS scheduling, sessions,
deck state and import/export were audited and cleared.
Verified: :flashcards:testDevUnitTest 178 tests pass; lint
errors are pre-existing on untouched files.

Diff

flashcards/src/main/java/com/vayunmathur/flashcards/ui/StatsPage.kt
diff --git a/flashcards/src/main/java/com/vayunmathur/flashcards/ui/StatsPage.kt b/flashcards/src/main/java/com/vayunmathur/flashcards/ui/StatsPage.ktindex 7b638ed210..e6b77d562b 100644--- a/flashcards/src/main/java/com/vayunmathur/flashcards/ui/StatsPage.kt+++ b/flashcards/src/main/java/com/vayunmathur/flashcards/ui/StatsPage.kt@@ -68,8 +68,7 @@ fun StatsPage(backStack: NavBackStack<Route>, viewModel: FlashcardsViewModel) {     }      val today = LocalDate.now().toEpochDay()-    var streak = 0-    while (byDay.containsKey(today - streak)) streak+++    val streak = currentStreakDays(byDay.keys, today)      val deckCards = if (selectedDeckId == null) cards else cards.filter { it.deckId == selectedDeckId } @@ -121,3 +120,22 @@ fun StatsPage(backStack: NavBackStack<Route>, viewModel: FlashcardsViewModel) {         },     ) }++/**+ * Current review streak in days, given the set of local epoch-days that have+ * at least one review and [today]'s epoch-day. The streak is the run of+ * consecutive reviewed days ending today, or ending yesterday when today has+ * no review yet: the current day is still in progress, so a streak earned+ * through yesterday has not broken until a whole day passes with no review.+ * A run that ended the day before yesterday (or earlier) is over and counts 0.+ */+internal fun currentStreakDays(reviewDays: Set<Long>, today: Long): Int {+    val anchor = when {+        today in reviewDays -> today+        today - 1 in reviewDays -> today - 1+        else -> return 0+    }+    var streak = 0+    while ((anchor - streak) in reviewDays) streak+++    return streak+}
flashcards/src/test/java/com/vayunmathur/flashcards/util/StatsStreakTest.kt
diff --git a/flashcards/src/test/java/com/vayunmathur/flashcards/util/StatsStreakTest.kt b/flashcards/src/test/java/com/vayunmathur/flashcards/util/StatsStreakTest.ktnew file mode 100644index 0000000000..71287571b3--- /dev/null+++ b/flashcards/src/test/java/com/vayunmathur/flashcards/util/StatsStreakTest.kt@@ -0,0 +1,49 @@+package com.vayunmathur.flashcards.util++import com.vayunmathur.flashcards.ui.currentStreakDays+import java.time.LocalDate+import kotlin.test.Test+import kotlin.test.assertEquals++class StatsStreakTest {++    // Fixed calendar: "today" is Tuesday 2026-12-01; the four days before it+    // (Fri 2026-11-27 through Mon 2026-11-30) form a four-day review run.+    private val today = LocalDate.of(2026, 12, 1).toEpochDay()+    private val runThroughYesterday = setOf(today - 4, today - 3, today - 2, today - 1)++    @Test+    fun streakSurvivesUntilEndOfTodayWhenTodayHasNoReviewYet() {+        // Reviews on each of the last four days, none yet today (e.g. the user+        // opens Stats in the morning): the streak is still alive at 4, not 0.+        assertEquals(4, currentStreakDays(runThroughYesterday, today))+    }++    @Test+    fun streakCountsRunEndingToday() {+        val days = runThroughYesterday + today+        assertEquals(5, currentStreakDays(days, today))+    }++    @Test+    fun streakBreaksAtTheMostRecentGap() {+        // Reviewed today and four/five days ago, but not yesterday or the day+        // before: only today's isolated review counts.+        val days = setOf(today, today - 4, today - 5)+        assertEquals(1, currentStreakDays(days, today))+    }++    @Test+    fun streakIsZeroWhenYesterdayAndTodayAreBothMissed() {+        // The run ended the day before yesterday, so it is over even though+        // it was four days long.+        val days = setOf(today - 5, today - 4, today - 3, today - 2)+        assertEquals(0, currentStreakDays(days, today))+    }++    @Test+    fun streakIsZeroWithNoReviewsAndOneForTodayOnly() {+        assertEquals(0, currentStreakDays(emptySet(), today))+        assertEquals(1, currentStreakDays(setOf(today), today))+    }+}