Modern Apps git

← Modern-Apps.git

notes: push ink editor as Route.InkEditor(noteId, blockId) (#nav)

Commit
4978b0d3f5ecf85205f5518aa7d77b01c8622046
Author
Vayun Mathur <[email protected]>
When
2026-11-16 20:06 UTC (19 days ago)
Parents
9a55252
NotePage held the full-screen ink editor in a remember'd
editingInk state with an early-return render, invisible to the
back stack. Add Route.InkEditor(noteId, blockId) (blockId is a
String: NoteBlock.id is a UUID) with FullscreenPage metadata,
following the photos EditRoute graph pattern, and push it from
Route.Note whenever a drawing is edited or added.

A new InkEditorPage binder loads the note by id and reuses the
existing InkEditor composable: confirming replaces the block's
strokes in place (or inserts a fresh drawing block) and pops,
cancelling pops unsaved. NotePage removes editingInk and its
back-handler early return, and resyncs its block list from the
repository so a body saved by the ink route is not overwritten
by stale local state.

Caveat: a new drawing inserted from the ink route lands at the
end of the note, since the route carries only (noteId, blockId)
and not the previous insert position; cancel still inserts
nothing, as before.

Tests: IntentsAndRouteTest pins InkEditor values and its
serializer round-trip; NotePageBlocksCoverageTest asserts Add
drawing pushes Route.InkEditor and popping returns to Note(id).

Verified: ./gradlew :notes:compileDevKotlin :notes:testDevUnitTest

Diff

notes/src/main/java/com/vayunmathur/notes/Navigation.kt
diff --git a/notes/src/main/java/com/vayunmathur/notes/Navigation.kt b/notes/src/main/java/com/vayunmathur/notes/Navigation.ktindex 3e7c48944e..a7a1d3e9e8 100644--- a/notes/src/main/java/com/vayunmathur/notes/Navigation.kt+++ b/notes/src/main/java/com/vayunmathur/notes/Navigation.kt@@ -4,6 +4,7 @@ import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.lifecycle.compose.collectAsStateWithLifecycle+import com.vayunmathur.library.util.FullscreenPage import com.vayunmathur.library.util.ListDetailPage import com.vayunmathur.library.util.ListPage import com.vayunmathur.library.util.MainNavigation@@ -11,6 +12,7 @@ import com.vayunmathur.library.util.MorphPage import com.vayunmathur.library.util.rememberNavBackStack import com.vayunmathur.notes.platform.NotesViewModel import com.vayunmathur.notes.ui.ExternalNoteScreen+import com.vayunmathur.notes.ui.InkEditorPage import com.vayunmathur.notes.ui.NotePage import com.vayunmathur.notes.ui.NotesListPage @@ -34,5 +36,8 @@ fun Navigation(notesViewModel: NotesViewModel) {         entry<Route.ExternalNote>(metadata = ListDetailPage()) {             ExternalNoteScreen(backStack, notesViewModel, it.uri)         }+        entry<Route.InkEditor>(metadata = FullscreenPage()) {+            InkEditorPage(backStack, notesViewModel, it.noteId, it.blockId)+        }     } }
notes/src/main/java/com/vayunmathur/notes/Route.kt
diff --git a/notes/src/main/java/com/vayunmathur/notes/Route.kt b/notes/src/main/java/com/vayunmathur/notes/Route.ktindex f9bade15b5..ffa453e114 100644--- a/notes/src/main/java/com/vayunmathur/notes/Route.kt+++ b/notes/src/main/java/com/vayunmathur/notes/Route.kt@@ -11,4 +11,11 @@ sealed interface Route: NavKey {     data class Note(val id: Long): Route     @Serializable     data class ExternalNote(val uri: String): Route+    /**+     * Full-screen ink editor for one ink block. Block ids are the stable+     * [com.vayunmathur.notes.data.NoteBlock.id] values (UUID strings), so+     * [blockId] is a String, not a Long.+     */+    @Serializable+    data class InkEditor(val noteId: Long, val blockId: String): Route }
notes/src/main/java/com/vayunmathur/notes/ui/InkEditorPage.kt
diff --git a/notes/src/main/java/com/vayunmathur/notes/ui/InkEditorPage.kt b/notes/src/main/java/com/vayunmathur/notes/ui/InkEditorPage.ktindex 3cddf5f6e4..57372c155d 100644--- a/notes/src/main/java/com/vayunmathur/notes/ui/InkEditorPage.kt+++ b/notes/src/main/java/com/vayunmathur/notes/ui/InkEditorPage.kt@@ -48,7 +48,16 @@ import com.vayunmathur.library.ink.InkCanvasView import com.vayunmathur.library.ink.SerializedStroke import com.vayunmathur.library.ink.deserialize import com.vayunmathur.library.ink.serialize+import com.vayunmathur.library.util.NavBackStack import com.vayunmathur.notes.R+import com.vayunmathur.notes.Route+import com.vayunmathur.notes.data.Note+import com.vayunmathur.notes.data.NoteBlock+import com.vayunmathur.notes.data.NoteBody+import com.vayunmathur.notes.data.body+import com.vayunmathur.notes.data.randomBlockId+import com.vayunmathur.notes.data.withBody+import com.vayunmathur.notes.platform.NotesViewModel  private enum class InkTool { Pen, Highlighter, Eraser } @@ -64,6 +73,56 @@ private val inkColors = listOf(     Color(0xFFFDD835), Color(0xFF43A047), Color(0xFF1E88E5), Color(0xFF8E24AA), ) +/**+ * Route binder for [Route.InkEditor]: loads the note with [noteId] through the+ * ViewModel, hands the strokes of the ink block with [blockId] to [InkEditor],+ * and saves back through the same editable-note binding [NotePage] uses.+ *+ * A [blockId] that is not in the note yet is a new drawing (see+ * `NotePage`'s addDrawing): confirming inserts it at the end of the note with+ * a trailing text block; cancelling inserts nothing. Confirming an existing+ * block replaces its strokes in place. Either way, confirming or cancelling+ * (including the scaffold close / system back, which pops the route) returns+ * to the note without saving on cancel, as the in-page editor did before.+ */+@Composable+fun InkEditorPage(+    backStack: NavBackStack<Route>,+    notesViewModel: NotesViewModel,+    noteId: Long,+    blockId: String,+) {+    var note by notesViewModel.editableNote(noteId) { Note(0, "", "") }++    // Wait for the stored note (mirrors NotePage): rendering InkEditor with+    // the default note first would seed its strokes from emptyList() and the+    // later database value would never be adopted by its remember.+    if (noteId != 0L && note.id == 0L) return++    val ink = note.body().blocks.firstOrNull { it.id == blockId } as? NoteBlock.Ink++    InkEditor(+        initialStrokes = ink?.strokes ?: emptyList(),+        onDone = { newStrokes ->+            val blocks = note.body().blocks.toMutableList()+            val i = blocks.indexOfFirst { it.id == blockId }+            if (i >= 0) {+                val existing = blocks[i] as? NoteBlock.Ink+                blocks[i] = existing?.copy(strokes = newStrokes)+                    ?: NoteBlock.Ink(newStrokes, id = blockId)+            } else {+                blocks.add(NoteBlock.Ink(newStrokes, id = blockId))+                // Keep a text block after inserted media so the user can keep+                // typing below it (same rule as NotePage.insertBlock).+                blocks.add(NoteBlock.Text("", randomBlockId()))+            }+            note = note.withBody(NoteBody(blocks))+            backStack.pop()+        },+        onCancel = { backStack.pop() },+    )+}+ /**  * Full-screen handwriting editor for a single ink block. Supports pen /  * highlighter / eraser, color and stroke-width selection, and undo / redo.
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 7caf5c730f..f4732cc62a 100644--- a/notes/src/main/java/com/vayunmathur/notes/ui/NotePage.kt+++ b/notes/src/main/java/com/vayunmathur/notes/ui/NotePage.kt@@ -3,7 +3,6 @@ package com.vayunmathur.notes.ui import android.content.ClipData import android.content.Intent import android.net.Uri-import androidx.activity.compose.BackHandler import androidx.activity.compose.rememberLauncherForActivityResult import androidx.activity.result.PickVisualMediaRequest import androidx.activity.result.contract.ActivityResultContracts@@ -71,9 +70,21 @@ fun NotePage(     val blocks = remember(noteID) { mutableStateListOf<NoteBlock>().apply { addAll(note.body().blocks) } }     fun commit() { note = note.withBody(NoteBody(blocks.toList())) } -    var editingInk by remember { mutableStateOf<NoteBlock.Ink?>(null) }-    // Where inserted media lands: the text block that had focus when the picker or the-    // drawing editor was opened. Kept here because the insert completes after a trip out+    // The ink editor is a separate route (Route.InkEditor) and saves through its+    // own editableNote binding, so adopt a body written by that screen (or any+    // other writer) instead of overwriting it with this page's stale block list+    // on the next local edit. Local commits encode exactly [blocks], so this is+    // a no-op for this page's own edits.+    LaunchedEffect(note.blocks) {+        val parsed = note.body().blocks+        if (parsed != blocks.toList()) {+            blocks.clear()+            blocks.addAll(parsed)+        }+    }++    // Where inserted media lands: the text block that had focus when the picker+    // was opened. Kept here because the insert completes after a trip out     // of the app, by which time focus is gone.     var insertAfterId by remember { mutableStateOf<String?>(null) } @@ -116,27 +127,6 @@ fun NotePage(         }     } -    // The drawing editor takes over the whole screen while open.-    val ink = editingInk-    if (ink != null) {-        BackHandler { editingInk = null }-        InkEditor(-            initialStrokes = ink.strokes,-            onDone = { newStrokes ->-                val i = blocks.indexOfFirst { it.id == ink.id }-                if (i >= 0) {-                    blocks[i] = ink.copy(strokes = newStrokes)-                    commit()-                } else {-                    insertBlock(ink.copy(strokes = newStrokes))-                }-                editingInk = null-            },-            onCancel = { editingInk = null },-        )-        return-    }-     NoteScreen(         state = NoteUiState(title = note.title, blocks = blocks.toList(), tags = note.tags, knownTags = knownTags),         sharedTextKey = "note-title-$noteID",@@ -168,11 +158,17 @@ fun NotePage(             }              override fun addDrawing(afterBlockId: String?) {-                insertAfterId = afterBlockId-                editingInk = NoteBlock.Ink(emptyList(), id = randomBlockId())+                // A fresh block id that is not in the note yet: InkEditorPage+                // inserts the block under this id when the drawing is confirmed+                // (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()))             } -            override fun editDrawing(block: NoteBlock.Ink) { editingInk = block }+            override fun editDrawing(block: NoteBlock.Ink) {+                backStack.add(Route.InkEditor(noteID, block.id))+            }              override fun resizeImage(id: String, widthFraction: Float) {                 val i = blocks.indexOfFirst { it.id == id }
notes/src/test/java/com/vayunmathur/notes/IntentsAndRouteTest.kt
diff --git a/notes/src/test/java/com/vayunmathur/notes/IntentsAndRouteTest.kt b/notes/src/test/java/com/vayunmathur/notes/IntentsAndRouteTest.ktindex 5d538823c1..c0f37f8dfc 100644--- a/notes/src/test/java/com/vayunmathur/notes/IntentsAndRouteTest.kt+++ b/notes/src/test/java/com/vayunmathur/notes/IntentsAndRouteTest.kt@@ -89,6 +89,12 @@ class IntentsAndRouteTest {         assertEquals(Route.Note(42), Route.Note(42))         assertEquals("content://x/a.md", Route.ExternalNote("content://x/a.md").uri)         assertNotNull(Route.Note.serializer())+        // Ink blocks are identified by their stable String block id.+        val ink = Route.InkEditor(noteId = 42L, blockId = "ink-block-1")+        assertEquals(42L, ink.noteId)+        assertEquals("ink-block-1", ink.blockId)+        assertEquals(Route.InkEditor(42L, "ink-block-1"), ink)+        assertNotNull(Route.InkEditor.serializer())     }      @Test fun backupAgentProperties() {@@ -122,5 +128,7 @@ class IntentsAndRouteTest {         assertEquals(Route.Note(77), note)         val ext = Json.decodeFromString(Route.serializer(), json.encodeToString(Route.serializer(), Route.ExternalNote("content://a/b.md")))         assertEquals(Route.ExternalNote("content://a/b.md"), ext)+        val ink = Json.decodeFromString(Route.serializer(), json.encodeToString(Route.serializer(), Route.InkEditor(77L, "block-9")))+        assertEquals(Route.InkEditor(77L, "block-9"), ink)     } }
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 b14a35f2c5..b0a18e5f47 100644--- a/notes/src/test/java/com/vayunmathur/notes/ui/NotePageBlocksCoverageTest.kt+++ b/notes/src/test/java/com/vayunmathur/notes/ui/NotePageBlocksCoverageTest.kt@@ -88,6 +88,30 @@ class NotePageBlocksCoverageTest {         assertEquals(0, rule.onAllNodesWithContentDescription("Down").fetchSemanticsNodes().size)     } +    @Test fun notePageAddDrawingPushesInkEditorRoute() {+        val id = seedBlockNote()+        val stack = NavBackStack<Route>(arrayOf(Route.NotesList, Route.Note(id)))+        val vm = vm()+        rule.setContent { NotePage(stack, vm, id) }+        rule.waitUntil(timeoutMillis = 8000) {+            runCatching { rule.onNodeWithText("Block Note").assertIsDisplayed(); true }.getOrDefault(false)+        }+        rule.onNodeWithContentDescription("Add drawing").performClick()+        idle()+        // The ink editor is a route, not in-page state: the note stays under+        // a new InkEditor entry carrying this note's id and a fresh block id.+        val top = stack.last()+        assertTrue(top is Route.InkEditor, "expected InkEditor route, got $top")+        top as Route.InkEditor+        assertEquals(id, top.noteId)+        assertTrue(top.blockId.isNotBlank(), "new ink block id must not be blank")+        assertEquals(Route.Note(id), stack.backStack[stack.backStack.size - 2])+        // Popping the editor route (InkEditorPage onDone/onCancel) returns here.+        rule.runOnUiThread { stack.pop() }+        idle()+        assertEquals(Route.Note(id), stack.last())+    }+     @Test fun listPageTagRenameAndDeleteForwardToRepository() {         runBlocking {             repo.upsert(Note(title = "Home Note", content = "h", tags = listOf("Home"), syncId = "lp1"))