Modern Apps git

← Modern-Apps.git

notes: search title and content only, not toString

Commit
79f963c26ddbd326fbefffe07c62301652afc843
Author
Vayun Mathur <[email protected]>
When
2026-11-07 20:09 UTC (29 days ago)
Parents
b779366
Symptom: list search matched notes the user could not explain:
querying "image", "deleted", a file name from the blocks JSON, or
digits from a timestamp or position matched nearly every note,
while the result list gave no hint why.

Root cause: NotesListScreen filtered with
it.toString().contains(query, true). The data-class rendering embeds
id, position, the raw blocks JSON, syncId, updatedAt and the deleted
flag alongside title and content.

Fix: extract the predicate as noteMatchesSearch() matching only
title and content (content is kept in sync with the text blocks by
withBody, so body text stays searchable), and call it from the
screen.

Verification: NoteSearchFilterTest now calls the real predicate
instead of mirroring the old one, and a new case asserts internal
fields never match while visible text still does. Full notes
unit-test suite run at the end of this stack.

Diff

notes/src/main/java/com/vayunmathur/notes/ui/NotesListScreen.kt
diff --git a/notes/src/main/java/com/vayunmathur/notes/ui/NotesListScreen.kt b/notes/src/main/java/com/vayunmathur/notes/ui/NotesListScreen.ktindex 42981598bc..297046f689 100644--- a/notes/src/main/java/com/vayunmathur/notes/ui/NotesListScreen.kt+++ b/notes/src/main/java/com/vayunmathur/notes/ui/NotesListScreen.kt@@ -52,6 +52,18 @@ import com.vayunmathur.notes.data.Note import com.vayunmathur.notes.platform.NotesListActions import com.vayunmathur.notes.platform.NotesListUiState +/**+ * List search predicate: a note matches when the query appears in its title+ * or its plain-text content (which [com.vayunmathur.notes.data.withBody]+ * keeps in sync with the text blocks). Deliberately NOT the data-class+ * `toString()`: that rendering also embeds the id, position, blocks JSON,+ * syncId, updatedAt and deleted flag, so queries like "image", "deleted"+ * or a digit from a timestamp matched nearly every note.+ */+internal fun noteMatchesSearch(note: Note, query: String): Boolean =+    note.title.contains(query, ignoreCase = true) ||+        note.content.contains(query, ignoreCase = true)+ /**  * The notes list, with no dependency on the ViewModel or the back stack so it can be  * rendered from a `@Preview` — see `src/screenshotTest`, which is where the store listing@@ -73,7 +85,7 @@ fun NotesListScreen(     var searchQuery by remember { mutableStateOf(initialSearchQuery) }     val filtered = remember(state.notes, searchQuery) {         if (searchQuery.isBlank()) state.notes-        else state.notes.filter { it.toString().contains(searchQuery, true) }+        else state.notes.filter { noteMatchesSearch(it, searchQuery) }     }      BackHandler(enabled = searchQuery.isNotEmpty()) {
notes/src/test/java/com/vayunmathur/notes/data/NoteSearchFilterTest.kt
diff --git a/notes/src/test/java/com/vayunmathur/notes/data/NoteSearchFilterTest.kt b/notes/src/test/java/com/vayunmathur/notes/data/NoteSearchFilterTest.ktindex 41f39eeb0d..41bc8ef278 100644--- a/notes/src/test/java/com/vayunmathur/notes/data/NoteSearchFilterTest.kt+++ b/notes/src/test/java/com/vayunmathur/notes/data/NoteSearchFilterTest.kt@@ -1,24 +1,22 @@ package com.vayunmathur.notes.data +import com.vayunmathur.notes.ui.noteMatchesSearch import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertTrue  /**- * Pins the list search predicate from `NotesListScreen`: a blank query keeps- * every row, otherwise a row matches when its `toString` contains the query,- * case-insensitively.- *- * The predicate lives inline in a composable, so it is mirrored here rather- * than called. The mirror is exact: blank check, then `contains(..., true)`.- * `toString` is the data-class rendering, which embeds title and content, so- * both fields stay searchable.+ * Pins the list search predicate from `NotesListScreen` ([noteMatchesSearch]):+ * a blank query keeps every row, otherwise a row matches when the query+ * appears in its title or plain-text content, case-insensitively. Internal+ * fields (id, position, blocks JSON, syncId, updatedAt, deleted) must never+ * make a note match.  */ class NoteSearchFilterTest {      private fun filter(notes: List<Note>, query: String): List<Note> {         if (query.isBlank()) return notes-        return notes.filter { it.toString().contains(query, true) }+        return notes.filter { noteMatchesSearch(it, query) }     }      private fun note(id: Long, title: String, content: String) =@@ -79,4 +77,21 @@ class NoteSearchFilterTest {         assertTrue(filter(notes, "line two").isNotEmpty())         assertTrue(filter(notes, "LINE ONE").isNotEmpty())     }++    @Test+    fun `internal fields never make a note match`() {+        val withImage = Note(id = 7, title = "plain", content = "")+            .withBody(NoteBody(listOf(NoteBlock.Text("words", "t1"), NoteBlock.Image("deadbeef.jpg", id = "i1"))))+            .copy(syncId = "sync-abc", updatedAt = 1_700_000_000_000, position = 42.0)+        val notes = listOf(withImage)+        // Each of these appears in the old data-class toString() rendering+        // (blocks JSON, syncId, timestamps, position, field names) but in+        // neither the title nor the content.+        for (query in listOf("image", "deadbeef", "sync-abc", "1700000000000", "42", "deleted", "markdown")) {+            assertEquals(emptyList(), filter(notes, query), "query=$query must not match")+        }+        // The visible text still matches.+        assertEquals(listOf(7L), filter(notes, "words").map { it.id })+        assertEquals(listOf(7L), filter(notes, "plain").map { it.id })+    } }