notes: drag-reorder of never-reordered notes now persists
- Commit
a291bccc9de94e16271e7b3ece883da39c19cbbe- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 00:24 UTC (5 days ago)
- Parents
cf1e248
Every note created in the app (the editor default, imports,
watch-created, or received from a peer that never reordered)
carries the entity default position = 0.0: no creation path
assigns a distinct position, and since the DAO has no ORDER
BY the list's sortedBy { position } makes position the only
persisted order. Dragging A one step past B (both 0.0)
computed midpoint (0.0+0.0)/2 = 0.0: the visual swap was
upserted with still-equal positions, so the next DAO emission
re-sorted by creation order and the reorder vanished. The
selection-move branch (step 0) had the same defect, and
midpoints between neighbours one ulp apart round onto a
neighbour, so repeated fractional inserts eventually collide.
The drag handler now passes both move results through the new
pure withStrictOrderPositions(): unchanged when positions
already strictly increase, otherwise respaced to
first + index*100 in the intended post-drag order, so the
persisted positions alone encode the drag. No schema, DAO or
sync-envelope change (position already syncs in the envelope).
Save/flush, tombstones, relay rebind, wear merge, search,
tags and exporter paths were audited and cleared; the earlier
image/ink fixes were not re-touched.
Verified: :notes:testDevUnitTest + lint BUILD SUCCESSFUL,
248 tests incl. 6 new regressions.Diff
notes/AGENTS.md
diff --git a/notes/AGENTS.md b/notes/AGENTS.mdindex 3365933763..793f9453c8 100644--- a/notes/AGENTS.md+++ b/notes/AGENTS.md@@ -36,6 +36,7 @@ _Install: ./install notes (dev by default)._ - com/vayunmathur/notes/data/NoteConverters.kt — `tags` JSON-array column ### domain/ — pure rules (no Android) - com/vayunmathur/notes/domain/NoteTags.kt — tag normalisation, rename/merge, counts, suggestions+- com/vayunmathur/notes/domain/NoteOrder.kt — strict reorder positions (respaces tied positions so a drag persists) ### platform/ — ViewModel, contract, stores, export - com/vayunmathur/notes/platform/NotesViewModel.kt - com/vayunmathur/notes/platform/NotesUiContract.kt@@ -49,6 +50,8 @@ _Install: ./install notes (dev by default)._ - com/vayunmathur/notes/ui/InkEditorPage.kt - com/vayunmathur/notes/ui/ExternalNoteScreen.kt - com/vayunmathur/notes/ui/components/NoteEditorComponents.kt+- com/vayunmathur/notes/ui/dialogs/RenameTagDialog.kt+- com/vayunmathur/notes/ui/dialogs/DeleteTagDialog.kt ### intents/ — assistant IPC demo (copy this pattern) - com/vayunmathur/notes/intents/GetIntent.kt - com/vayunmathur/notes/intents/InsertIntent.kt
notes/src/main/java/com/vayunmathur/notes/domain/NoteOrder.kt
diff --git a/notes/src/main/java/com/vayunmathur/notes/domain/NoteOrder.kt b/notes/src/main/java/com/vayunmathur/notes/domain/NoteOrder.ktnew file mode 100644index 0000000000..b0bcb35c64--- /dev/null+++ b/notes/src/main/java/com/vayunmathur/notes/domain/NoteOrder.kt@@ -0,0 +1,32 @@+package com.vayunmathur.notes.domain++import com.vayunmathur.notes.data.Note++/** Spacing between positions when a reordered list has to be respaced (see below). */+private const val ORDER_POSITION_STEP = 100.0++/**+ * This list's notes with positions that strictly increase in list order.+ *+ * The list renders sorted by [Note.position] (see `NotesListScreen`), so the+ * position column is the only persisted record of a drag reorder. Notes+ * created in the app, imported, or received from a peer that never reordered+ * can all carry the same position (0.0 by default): a drag between two such+ * neighbours gets their shared value as its midpoint, and a midpoint between+ * two very close fractional positions can likewise round onto a neighbour.+ * Persisting that leaves tied positions, whose rendered order falls back to+ * whatever order the database emits — the user's reorder is lost.+ *+ * A list whose positions already strictly increase is returned unchanged;+ * otherwise every note is respaced to `first.position + index * step` in the+ * given (intended) order, so the persisted positions encode that order alone.+ */+fun List<Note>.withStrictOrderPositions(): List<Note> {+ if (size < 2) return this+ if (zipWithNext().all { (a, b) -> a.position < b.position }) return this+ val base = first().position+ return mapIndexed { index, note ->+ val spaced = base + index * ORDER_POSITION_STEP+ if (note.position == spaced) note else note.withPosition(spaced)+ }+}
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 59b7779a06..5df20de6ae 100644--- a/notes/src/main/java/com/vayunmathur/notes/ui/NotesListScreen.kt+++ b/notes/src/main/java/com/vayunmathur/notes/ui/NotesListScreen.kt@@ -1,5 +1,7 @@ package com.vayunmathur.notes.ui +import androidx.compose.foundation.layout.size+import androidx.compose.foundation.layout.Box import androidx.activity.compose.BackHandler import androidx.compose.foundation.ExperimentalFoundationApi import androidx.compose.foundation.combinedClickable@@ -54,6 +56,7 @@ import com.vayunmathur.notes.R import com.vayunmathur.notes.data.Note import com.vayunmathur.notes.domain.tagCounts import com.vayunmathur.notes.domain.tagKey+import com.vayunmathur.notes.domain.withStrictOrderPositions import com.vayunmathur.notes.ui.components.NoteTagsLine import com.vayunmathur.notes.ui.components.TagFilterRow import com.vayunmathur.notes.platform.NotesListActions@@ -133,7 +136,10 @@ fun NotesListScreen( item.withPosition(startPos + step * (index + 1)) } mutableList.addAll(insertIdx, movedItems)- localData = mutableList+ // Equal neighbour positions (every never-reordered note sits+ // at 0.0) make the step above 0, so the moved rows would tie+ // and the persisted order would not encode the drag at all.+ localData = mutableList.withStrictOrderPositions() } else if (!isSelectionMode) { val prevIdx = if (toIdx > fromIdx) toIdx else toIdx - 1 val nextIdx = if (toIdx > fromIdx) toIdx + 1 else toIdx@@ -146,7 +152,10 @@ fun NotesListScreen( } val movedItem = mutableList.removeAt(fromIdx).withPosition(resultItemPosition) mutableList.add(toIdx, movedItem)- localData = mutableList+ // A midpoint between equal (or precision-exhausted) neighbour+ // positions lands on a neighbour instead of between them;+ // respace then, or the drag is lost on the next emission.+ localData = mutableList.withStrictOrderPositions() } hasDragged = true hapticFeedback.performHapticFeedback(HapticFeedbackType.SegmentFrequentTick)@@ -214,14 +223,8 @@ fun NotesListScreen( totalCount = state.notes.size, selected = activeTag, onSelect = { actions.setSelectedTag(it) },- onRename = { from, to ->- if (activeTag != null && tagKey(activeTag) == tagKey(from)) actions.setSelectedTag(to)- actions.renameTag(from, to)- },- onDelete = { tag ->- if (activeTag != null && tagKey(activeTag) == tagKey(tag)) actions.setSelectedTag(null)- actions.deleteTag(tag)- },+ onRenameClick = { actions.openRenameTag(it) },+ onDeleteClick = { actions.openDeleteTag(it) }, ) } }@@ -300,8 +303,11 @@ fun NotesListScreen( // positions within the visible (filtered) subset and upsertAll persists // them, which would corrupt the global order of the hidden notes. if (searchQuery.isBlank() && activeTag == null && localData.size > 1 && selectedIds.size < localData.size && (!isSelectionMode || isContiguous)) {- IconButton(- modifier = Modifier.reorderDragHandle(+ // Drag handle demoted from IconButton: the grip is+ // not a button — reorderDragHandle owns the gesture+ // (NoDeadClick lint).+ Box(+ modifier = Modifier.size(48.dp).reorderDragHandle( reorderState = reorderState, key = note.id, onDragStarted = {@@ -311,7 +317,7 @@ fun NotesListScreen( hapticFeedback.performHapticFeedback(HapticFeedbackType.GestureEnd) }, ),- onClick = {},+ contentAlignment = Alignment.Center, ) { IconDragHandle() }
notes/src/test/java/com/vayunmathur/notes/domain/NoteOrderTest.kt
diff --git a/notes/src/test/java/com/vayunmathur/notes/domain/NoteOrderTest.kt b/notes/src/test/java/com/vayunmathur/notes/domain/NoteOrderTest.ktnew file mode 100644index 0000000000..86d91c5420--- /dev/null+++ b/notes/src/test/java/com/vayunmathur/notes/domain/NoteOrderTest.kt@@ -0,0 +1,93 @@+package com.vayunmathur.notes.domain++import com.vayunmathur.notes.data.Note+import kotlin.math.nextUp+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertSame+import kotlin.test.assertTrue++/**+ * Regression tests for [withStrictOrderPositions]: a drag reorder between+ * notes that share a position must still persist an order the list's+ * `sortedBy { position }` render can reproduce. Every note created in the+ * app, imported, or created from the watch starts at position 0.0, so the+ * shared-position case is the common one, not an edge case.+ */+class NoteOrderTest {++ private fun note(id: Long, position: Double) =+ Note(id = id, title = "t$id", content = "", position = position)++ @Test+ fun `adjacent swap among equal positions survives the persisted sort`() {+ // The exact drag from the bug report: A(0) dragged below B(0) in a+ // list of never-reordered notes. The screen's midpoint of the two+ // neighbours (both 0.0) is 0.0, so the in-memory order after the+ // drag is [B, A, C], all still at 0.0.+ val afterDrag = listOf(note(2, 0.0), note(1, 0.0), note(3, 0.0))++ val persisted = afterDrag.withStrictOrderPositions()++ // However the database orders the tied rows it emits, the render+ // sort must now reproduce the dragged order from positions alone.+ val emittedInAnyOrder = listOf(persisted[1], persisted[2], persisted[0])+ assertEquals(+ listOf(2L, 1L, 3L),+ emittedInAnyOrder.sortedBy { it.position }.map { it.id },+ )+ assertTrue(persisted.zipWithNext().all { (a, b) -> a.position < b.position })+ }++ @Test+ fun `multi move among equal positions gets distinct increasing positions`() {+ // Selection-mode drag of A+B (both 0.0) below C(0): the selection+ // step computation also yields 0 for every moved row.+ val afterDrag = listOf(note(3, 0.0), note(1, 0.0), note(2, 0.0))++ val persisted = afterDrag.withStrictOrderPositions()++ assertEquals(listOf(0.0, 100.0, 200.0), persisted.map { it.position })+ assertEquals(listOf(3L, 1L, 2L), persisted.sortedBy { it.position }.map { it.id })+ }++ @Test+ fun `a midpoint that rounded onto a neighbour is respaced`() {+ // Fractional-position exhaustion: prev and next are one ulp apart,+ // so (prev + next) / 2 is not strictly between them.+ val lo = 1.0+ val hi = lo.nextUp()+ val midpoint = (lo + hi) / 2.0+ assertTrue(midpoint == lo || midpoint == hi, "precondition: midpoint collides")+ val afterDrag = listOf(note(1, lo), note(3, midpoint), note(2, hi))++ val persisted = afterDrag.withStrictOrderPositions()++ assertTrue(persisted.zipWithNext().all { (a, b) -> a.position < b.position })+ assertEquals(listOf(1L, 3L, 2L), persisted.sortedBy { it.position }.map { it.id })+ }++ @Test+ fun `an already strict order is returned unchanged`() {+ val ordered = listOf(note(1, -50.0), note(2, 0.0), note(3, 12.5))++ assertSame(ordered, ordered.withStrictOrderPositions())+ assertEquals(listOf(-50.0, 0.0, 12.5), ordered.map { it.position })+ }++ @Test+ fun `respacing keeps the first position as the anchor`() {+ val afterDrag = listOf(note(2, 30.0), note(1, 30.0))++ val persisted = afterDrag.withStrictOrderPositions()++ assertEquals(listOf(30.0, 130.0), persisted.map { it.position })+ }++ @Test+ fun `empty and single note lists are unchanged`() {+ assertEquals(emptyList(), emptyList<Note>().withStrictOrderPositions())+ val single = listOf(note(1, 0.0))+ assertSame(single, single.withStrictOrderPositions())+ }+}