Modern Apps git

← Modern-Apps.git

education: broken streaks display as zero instead of haunting the home screens

Commit
d7f1fcea4dc9a2fbd8fb8a154d3a855e7746657e
Author
Vayun Mathur <[email protected]>
When
2026-11-30 22:59 UTC (5 days ago)
Parents
bc93782
streakCount was only written at commit time and read raw by
five display sites, so after a missed day the home screens
rendered e.g. '4 days' (or 4 suns for the kids' views) forever
โ€” even though the app's own commit rule (nextStreak) considers
that run ended. Expiry existed only as a side effect of the
next completed activity.

effectiveStreak is now derived at read: the run stands only if
the last activity was today or yesterday (future dates count,
for clock rollback); all five sites route through currentStreak.
Nothing is persisted on read โ€” the stored row is untouched,
since nextStreak already restarts at 1 on the next commit.
Quiz scoring, progress math, offline packs and deadlines were
audited and cleared. Verified: :education:testDevUnitTest 76
tests pass; lint errors are pre-existing on untouched lines.

Diff

education/src/main/java/com/vayunmathur/education/platform/EducationViewModel.kt
diff --git a/education/src/main/java/com/vayunmathur/education/platform/EducationViewModel.kt b/education/src/main/java/com/vayunmathur/education/platform/EducationViewModel.ktindex eca81726ed..d8e63cfc5d 100644--- a/education/src/main/java/com/vayunmathur/education/platform/EducationViewModel.kt+++ b/education/src/main/java/com/vayunmathur/education/platform/EducationViewModel.kt@@ -156,11 +156,27 @@ class EducationViewModel(         )     } +    /** Today's local epoch day, the unit every streak/deadline calculation uses. */+    private fun todayEpochDay(): Long =+        Clock.System.now().toLocalDateTime(TimeZone.currentSystemDefault()).date.toEpochDays()++    /**+     * The streak as it stands today. The stored [Learner.streakCount] only+     * changes when a result is committed, so after a missed day it still+     * holds the old run; read through [effectiveStreak] it correctly shows+     * as broken (0) until the next activity starts a new one. The stored+     * value is left untouched โ€” [nextStreak] already restarts at 1 then.+     */+    fun currentStreak(learner: Learner?): Int {+        if (learner == null) return 0+        return effectiveStreak(learner.streakCount, learner.lastActivityEpochDay, todayEpochDay())+    }+     /** Persists a graded [result]: best-of star per skill, streak, and total stars. */     fun commitResult(result: QuizResult) {         viewModelScope.launch(Dispatchers.IO) {             writeMutex.withLock {-            val today = Clock.System.now().toLocalDateTime(TimeZone.currentSystemDefault()).date.toEpochDays()+            val today = todayEpochDay()             for ((skillId, stars) in result.perSkillStars) {                 val existing = repository.getSkillProgress(skillId)                 repository.upsertSkillProgress(@@ -230,6 +246,15 @@ class EducationViewModel(             else -> 1         } +        /**+         * The streak still alive on [today]: a run whose last activity was+         * today or yesterday can still be extended, so it stands; once a+         * whole day has been missed the run is over and reads as 0. A+         * future [lastEpochDay] (clock moved backwards) has missed nothing.+         */+        fun effectiveStreak(current: Int, lastEpochDay: Long, today: Long): Int =+            if (current > 0 && lastEpochDay >= today - 1) current else 0+         /** Maps a correct/total ratio to a 0-3 star (lite mastery) rating. */         fun starsFor(correct: Int, total: Int): Int {             if (total == 0) return 0
education/src/main/java/com/vayunmathur/education/ui/ExplorerHomePage.kt
diff --git a/education/src/main/java/com/vayunmathur/education/ui/ExplorerHomePage.kt b/education/src/main/java/com/vayunmathur/education/ui/ExplorerHomePage.ktindex 19a80ede1a..794385f3b2 100644--- a/education/src/main/java/com/vayunmathur/education/ui/ExplorerHomePage.kt+++ b/education/src/main/java/com/vayunmathur/education/ui/ExplorerHomePage.kt@@ -87,7 +87,7 @@ fun ExplorerHomePage(backStack: NavBackStack<Route>, viewModel: EducationViewMod                 Modifier.fillMaxWidth(),                 horizontalArrangement = Arrangement.spacedBy(8.dp),             ) {-                StreakChip(l.streakCount)+                StreakChip(viewModel.currentStreak(l))                 StarsChip(l.totalStars)             }         }
education/src/main/java/com/vayunmathur/education/ui/HomePage.kt
diff --git a/education/src/main/java/com/vayunmathur/education/ui/HomePage.kt b/education/src/main/java/com/vayunmathur/education/ui/HomePage.ktindex 3259503cde..ef067f1497 100644--- a/education/src/main/java/com/vayunmathur/education/ui/HomePage.kt+++ b/education/src/main/java/com/vayunmathur/education/ui/HomePage.kt@@ -51,7 +51,7 @@ fun ScholarHomePage(backStack: NavBackStack<Route>, viewModel: EducationViewMode     ScholarHomeScreen(         state = HomeUiState(             learnerName = l.name,-            streakCount = l.streakCount,+            streakCount = viewModel.currentStreak(l),             totalStars = l.totalStars,             deadlines = deadlines.map { d ->                 val type = runCatching { ModuleType.valueOf(d.moduleType) }.getOrNull()
education/src/main/java/com/vayunmathur/education/ui/K2HomePage.kt
diff --git a/education/src/main/java/com/vayunmathur/education/ui/K2HomePage.kt b/education/src/main/java/com/vayunmathur/education/ui/K2HomePage.ktindex 761bbdf508..dad0794e85 100644--- a/education/src/main/java/com/vayunmathur/education/ui/K2HomePage.kt+++ b/education/src/main/java/com/vayunmathur/education/ui/K2HomePage.kt@@ -63,7 +63,7 @@ fun K2HomePage(backStack: NavBackStack<Route>, viewModel: EducationViewModel) {     }      AppScaffold(-        title = { SunStreak(l.streakCount) },+        title = { SunStreak(viewModel.currentStreak(l)) },         actions = {             IconButton(onClick = { backStack.add(Route.Badges) }) {                 Text("\uD83C\uDF1F", fontSize = 24.sp)
education/src/main/java/com/vayunmathur/education/ui/ParentPage.kt
diff --git a/education/src/main/java/com/vayunmathur/education/ui/ParentPage.kt b/education/src/main/java/com/vayunmathur/education/ui/ParentPage.ktindex d76aeec352..8e948aded1 100644--- a/education/src/main/java/com/vayunmathur/education/ui/ParentPage.kt+++ b/education/src/main/java/com/vayunmathur/education/ui/ParentPage.kt@@ -113,7 +113,7 @@ fun ParentPage(backStack: NavBackStack<Route>, viewModel: EducationViewModel) {             // --- Progress ---             SectionHeader(stringResource(R.string.progress))             Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {-                StreakChip(l.streakCount)+                StreakChip(viewModel.currentStreak(l))                 StarsChip(l.totalStars)             }             val practiced = progress.values.count { it.stars > 0 }
education/src/main/java/com/vayunmathur/education/ui/ResultsPage.kt
diff --git a/education/src/main/java/com/vayunmathur/education/ui/ResultsPage.kt b/education/src/main/java/com/vayunmathur/education/ui/ResultsPage.ktindex 7b0aac7c22..9484ad74e4 100644--- a/education/src/main/java/com/vayunmathur/education/ui/ResultsPage.kt+++ b/education/src/main/java/com/vayunmathur/education/ui/ResultsPage.kt@@ -61,7 +61,7 @@ fun ResultsPage(                 textAlign = TextAlign.Center,                 color = MaterialTheme.colorScheme.primary,             )-            learner?.let { StreakChip(it.streakCount) }+            learner?.let { StreakChip(viewModel.currentStreak(it)) }              Button(                 onClick = { backStack.pop() },
education/src/test/java/com/vayunmathur/education/util/EducationViewModelTest.kt
diff --git a/education/src/test/java/com/vayunmathur/education/util/EducationViewModelTest.kt b/education/src/test/java/com/vayunmathur/education/util/EducationViewModelTest.ktindex 059e375582..f6b8a2a0d3 100644--- a/education/src/test/java/com/vayunmathur/education/util/EducationViewModelTest.kt+++ b/education/src/test/java/com/vayunmathur/education/util/EducationViewModelTest.kt@@ -188,6 +188,29 @@ class EducationViewModelTest {         assertEquals(1, EducationViewModel.nextStreak(3, 1, 5))     } +    @Test fun effectiveStreakExpiresAfterAMissedDay() {+        // Alive: activity today, or yesterday (today can still extend it).+        assertEquals(3, EducationViewModel.effectiveStreak(3, 5, 5))+        assertEquals(3, EducationViewModel.effectiveStreak(3, 4, 5))+        // Regression: a whole missed day ends the run, so it reads as 0+        // instead of the stored count (which only changes on the next commit).+        assertEquals(0, EducationViewModel.effectiveStreak(3, 3, 5))+        assertEquals(0, EducationViewModel.effectiveStreak(3, 1, 5))+        // No activity ever (sentinel 0) is not a streak on any real today.+        assertEquals(0, EducationViewModel.effectiveStreak(3, 0, 5))+        assertEquals(0, EducationViewModel.effectiveStreak(0, 5, 5))+        // Clock moved backwards: the last activity is in the future, nothing missed.+        assertEquals(3, EducationViewModel.effectiveStreak(3, 6, 5))+    }++    @Test fun currentStreakReadsExpiredLearnerAsZero() {+        // Epoch day 100 is far in the past for any real "today", so a stored+        // streak from then has certainly missed a day since.+        assertEquals(0, vm.currentStreak(Learner(streakCount = 4, lastActivityEpochDay = 100)))+        assertEquals(0, vm.currentStreak(Learner()))+        assertEquals(0, vm.currentStreak(null))+    }+     // --- VM persistence flows --------------------------------------------      @Test fun learnerEditsPersistAndPinVerifies() = runBlocking<Unit> {