Modern Apps git

← Modern-Apps.git

flashcards: delete review logs when regeneration removes cards

Commit
029360a23131e5a55819067e681284b65bfd3660
Author
Vayun Mathur <[email protected]>
When
2026-11-07 20:14 UTC (29 days ago)
Parents
353119d
Symptom: editing a note so a template no longer generates a card
(e.g. blanking the Front of a reversed note) or editing a note type
deleted the card but left its ReviewLog rows behind. Those orphaned
logs kept counting in deck statistics and were grouped by cardId
and replayed as phantom cards by the FSRS optimizer.

Root cause: CardGenerator.regenerate (via regenerateCardsImpl) and
deleteNoteTypeImpl deleted cards without touching ReviewLog, unlike
the note/deck deletion paths which delete logs with their cards.

Fix: regenerateCardsImpl diffs the note's card ids before/after
regeneration and deletes logs for removed cards; deleteNoteTypeImpl
likewise deletes logs for the cards it removes.

Verification: new FlashcardsViewModelTest
.regeneratingCardsDeletesRemovedCardsReviewLogs logs both cards of
a reversed note, blanks the Front, and asserts only the surviving
card's log remains; clean rerun passes.

Diff

flashcards/src/main/java/com/vayunmathur/flashcards/data/FlashcardsRepositoryCardOps.kt
diff --git a/flashcards/src/main/java/com/vayunmathur/flashcards/data/FlashcardsRepositoryCardOps.kt b/flashcards/src/main/java/com/vayunmathur/flashcards/data/FlashcardsRepositoryCardOps.ktindex 86c3c352ad..3039be5397 100644--- a/flashcards/src/main/java/com/vayunmathur/flashcards/data/FlashcardsRepositoryCardOps.kt+++ b/flashcards/src/main/java/com/vayunmathur/flashcards/data/FlashcardsRepositoryCardOps.kt@@ -38,7 +38,17 @@ internal suspend fun FlashcardsRepository.regenerateCardsImpl(     noteType: NoteType,     templates: List<CardTemplate>,     fields: List<NoteTypeField>,-) = CardGenerator.regenerate(note, noteType, templates, fields, cardDao)+) {+    val beforeIds = cardDao.getByNote(note.id).map { it.id }+    CardGenerator.regenerate(note, noteType, templates, fields, cardDao)+    // Cards removed by regeneration must take their review history with+    // them (the note-deletion paths already delete logs with cards);+    // orphaned logs would keep counting in deck stats and be replayed+    // as phantom cards by the FSRS optimizer.+    val afterIds = cardDao.getByNote(note.id).map { it.id }.toSet()+    val removedIds = beforeIds.filter { it !in afterIds }+    if (removedIds.isNotEmpty()) reviewLogDao.deleteByCards(removedIds)+}  internal suspend fun FlashcardsRepository.getReviewLogsByDeckOrderedImpl(deckId: Long): List<ReviewLog> =     reviewLogDao.getByDeckOrdered(deckId)
flashcards/src/main/java/com/vayunmathur/flashcards/util/FlashcardsNoteOps.kt
diff --git a/flashcards/src/main/java/com/vayunmathur/flashcards/util/FlashcardsNoteOps.kt b/flashcards/src/main/java/com/vayunmathur/flashcards/util/FlashcardsNoteOps.ktindex 42ee029d17..10160dfe87 100644--- a/flashcards/src/main/java/com/vayunmathur/flashcards/util/FlashcardsNoteOps.kt+++ b/flashcards/src/main/java/com/vayunmathur/flashcards/util/FlashcardsNoteOps.kt@@ -253,7 +253,9 @@ internal fun FlashcardsViewModel.saveNoteTypeImpl( internal fun FlashcardsViewModel.deleteNoteTypeImpl(id: Long) = launchIoImpl {     if (id in FlashcardsViewModel.BUILT_IN_NOTE_TYPE_IDS) return@launchIoImpl     val notes = repository.getNotesByNoteTypeImpl(id)+    val cardIds = notes.flatMap { repository.getCardsByNoteImpl(it.id) }.map { it.id }     notes.forEach { repository.deleteCardsByNoteImpl(it.id) }+    if (cardIds.isNotEmpty()) repository.deleteReviewLogsByCardsImpl(cardIds)     notes.forEach { repository.deleteNoteImpl(it) }     repository.deleteFieldsByNoteTypeImpl(id)     repository.deleteTemplatesByNoteTypeImpl(id)
flashcards/src/test/java/com/vayunmathur/flashcards/util/FlashcardsViewModelTest.kt
diff --git a/flashcards/src/test/java/com/vayunmathur/flashcards/util/FlashcardsViewModelTest.kt b/flashcards/src/test/java/com/vayunmathur/flashcards/util/FlashcardsViewModelTest.ktindex c83e2cb9fa..d5b119c648 100644--- a/flashcards/src/test/java/com/vayunmathur/flashcards/util/FlashcardsViewModelTest.kt+++ b/flashcards/src/test/java/com/vayunmathur/flashcards/util/FlashcardsViewModelTest.kt@@ -351,6 +351,34 @@ class FlashcardsViewModelTest {         }     } +    @Test+    fun regeneratingCardsDeletesRemovedCardsReviewLogs() {+        val deck = uniqueDeck("RegenLogsDeck")+        vm.saveNoteImpl(0, 2L, deck.id, listOf("RegenFront", "RegenBack"), "")+        await("reversed note saved") {+            runBlocking { repo.getAllNotesImpl().any { it.sortField == "RegenFront" } }+        }+        val note = runBlocking { repo.getAllNotesImpl().first { it.sortField == "RegenFront" } }+        val cards = runBlocking { repo.getCardsByNoteImpl(note.id) }+        assertEquals(2, cards.size)+        runBlocking {+            cards.forEach {+                repo.insertReviewLogImpl(+                    ReviewLog(cardId = it.id, deckId = deck.id, reviewedAt = 1L, grade = 3,+                        elapsedDays = 1.0, scheduledDays = 1.0, state = 2),+                )+            }+        }+        // Blanking the Front removes Card 1 (its front renders blank).+        vm.saveNoteImpl(note.id, 2L, deck.id, listOf("", "RegenBack"), "")+        await("card 1 regenerated away with its log") {+            val remaining = runBlocking { repo.getCardsByNoteImpl(note.id) }+            remaining.size == 1 &&+                runBlocking { repo.getReviewLogsByDeckOrderedImpl(deck.id) }+                    .all { log -> remaining.any { it.id == log.cardId } }+        }+    }+     @Test     fun cramSessionDoesNotPersist() {         val deck = uniqueDeck("CramDeck")