notes: ink on a brand-new note goes to that note, not an orphan
- Commit
c8946b7fa0e5cc4f4dd839b3c5c83c737c4c02e1- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-30 20:11 UTC (5 days ago)
- Parents
aed5604
A note created in the editor has no row until its first debounced save, so the route parameter is still 0. Add drawing passed that 0 to the ink editor, which treated the drawing as a different new note: confirming upserted a blank note carrying only the ink block, while the note being edited never received the strokes and the list gained a phantom blank note. The drawing actions now use the note's effective id. If the note is not persisted yet, the page commits the current blocks (which enqueues the first save) and defers opening the ink editor until the real id lands, canonicalising the top route to Route.Note (realId) first so returning from the editor lands on the same note. Regression test walks add-drawing on an unsaved new note and failed pre-fix. Checklist blocks, tags, external import, markdown export and wear conflict handling were audited and cleared. Verified: :notes:testDevUnitTest 242 tests and lint pass.
Diff
notes/src/main/java/com/vayunmathur/notes/ui/NotePage.kt
diff --git a/notes/src/main/java/com/vayunmathur/notes/ui/NotePage.kt b/notes/src/main/java/com/vayunmathur/notes/ui/NotePage.ktindex b6a09484dd..0d59228715 100644--- a/notes/src/main/java/com/vayunmathur/notes/ui/NotePage.kt+++ b/notes/src/main/java/com/vayunmathur/notes/ui/NotePage.kt@@ -88,6 +88,36 @@ fun NotePage( // of the app, by which time focus is gone. var insertAfterId by remember { mutableStateOf<String?>(null) } + // Ink block waiting for this note to be persisted. A new note starts as+ // Route.Note(0) with no row: opening the ink editor at id 0 would make+ // InkEditorPage save the drawing into a separate blank note. Instead the+ // edit below is committed (which enqueues the first save) and the editor+ // opens under the assigned id once it lands.+ var pendingDrawingBlockId by remember { mutableStateOf<String?>(null) }+ LaunchedEffect(note.id, pendingDrawingBlockId) {+ val blockId = pendingDrawingBlockId ?: return@LaunchedEffect+ if (note.id == 0L) return@LaunchedEffect+ pendingDrawingBlockId = null+ // Canonicalise a new-note route to the persisted id, so returning+ // from the editor lands on the same note the drawing was saved to.+ if (noteID == 0L && backStack.last() is Route.Note) {+ backStack.setLast(Route.Note(note.id))+ }+ backStack.add(Route.InkEditor(note.id, blockId))+ }++ fun openDrawing(blockId: String) {+ // The route's id is stale for a note created in this editor: its real+ // id only exists on the editable note once its first save lands.+ val effectiveId = note.id.takeIf { it != 0L } ?: noteID.takeIf { it != 0L }+ if (effectiveId != null) {+ backStack.add(Route.InkEditor(effectiveId, blockId))+ } else {+ commit()+ pendingDrawingBlockId = blockId+ }+ }+ fun insertBlock(block: NoteBlock) { val at = blocks.indexOfFirst { it.id == insertAfterId }.takeIf { it >= 0 }?.plus(1) ?: blocks.size blocks.add(at, block)@@ -163,11 +193,11 @@ fun NotePage( // (and inserts nothing on cancel, as before). [afterBlockId] is // not carried in the route, so a confirmed new drawing lands at // the end of the note.- backStack.add(Route.InkEditor(noteID, randomBlockId()))+ openDrawing(randomBlockId()) } override fun editDrawing(block: NoteBlock.Ink) {- backStack.add(Route.InkEditor(noteID, block.id))+ openDrawing(block.id) } override fun resizeImage(id: String, widthFraction: Float) {
notes/src/test/java/com/vayunmathur/notes/ui/NotePageBlocksCoverageTest.kt
diff --git a/notes/src/test/java/com/vayunmathur/notes/ui/NotePageBlocksCoverageTest.kt b/notes/src/test/java/com/vayunmathur/notes/ui/NotePageBlocksCoverageTest.ktindex b0a18e5f47..7559a4b5b1 100644--- a/notes/src/test/java/com/vayunmathur/notes/ui/NotePageBlocksCoverageTest.kt+++ b/notes/src/test/java/com/vayunmathur/notes/ui/NotePageBlocksCoverageTest.kt@@ -112,14 +112,36 @@ class NotePageBlocksCoverageTest { assertEquals(Route.Note(id), stack.last()) } + @Test fun notePageAddDrawingOnUnsavedNewNoteTargetsPersistedNote() {+ // Regression: a new note is Route.Note(0) and has no row until its+ // first save. Routing the ink editor at 0 made InkEditorPage save the+ // drawing into a separate blank note; the note being edited never+ // got the ink block.+ val stack = NavBackStack<Route>(arrayOf(Route.NotesList, Route.Note(0)))+ val vm = vm()+ rule.setContent { NotePage(stack, vm, 0) }+ idle()+ rule.onNodeWithContentDescription("Add drawing").performClick()+ // The editor must only open once the note is persisted, addressed by+ // its real id, with exactly that one note in the store.+ rule.waitUntil(timeoutMillis = 8000) {+ idle()+ val top = stack.last() as? Route.InkEditor+ top != null && top.noteId != 0L &&+ runBlocking { repo.getAll() }.singleOrNull()?.id == top.noteId+ }+ val top = stack.last() as Route.InkEditor+ assertEquals(runBlocking { repo.getAll() }.single().id, top.noteId)+ assertEquals(Route.Note(top.noteId), stack.backStack[stack.backStack.size - 2])+ }+ @Test fun listPageTagRenameAndDeleteForwardToRepository() { runBlocking { repo.upsert(Note(title = "Home Note", content = "h", tags = listOf("Home"), syncId = "lp1")) repo.upsert(Note(title = "Idea Note", content = "i", tags = listOf("Ideas"), syncId = "lp2")) }- val stack = NavBackStack<Route>(arrayOf(Route.NotesList)) val vm = vm()- rule.setContent { NotesListPage(stack, vm) }+ rule.setContent { com.vayunmathur.notes.Navigation(vm) } rule.waitUntil(timeoutMillis = 8000) { runCatching { rule.onNodeWithText("Home Note").assertIsDisplayed(); true }.getOrDefault(false) }