Skip to content

Commit fb0f1fd

Browse files
committed
fix: snapshot reading position before reserving an edit
Fix the cursor race behind the app-1 CI device-test failure. Room's edit reservation can rerender the preview while beginEditing suspends, leaving its layout unavailable and moving the cursor to the end. Strengthen the regression test to check the actual reading-position offset. Verify with just check and the full API 36 device-test suite. CI failure: https://github.com/qownnotes/qownnotes-android/actions/runs/43240632072/job/131259729568
1 parent 1b92011 commit fb0f1fd

3 files changed

Lines changed: 39 additions & 5 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ zero-based increasing release counter within the month. See [the release guide](
1414
and card creation, note trash, version search and restoration, desktop-compatible tags, and
1515
password-protected sharing alongside Nextcloud and local-folder setup.
1616

17+
### Fixed
18+
19+
- Entering the editor from a scrolled note no longer occasionally puts the cursor at the end of
20+
the note when reserving an edit triggers a preview rerender. This also fixes the reading-position
21+
device-test failure in CI.
22+
1723
## [26.11.1] - 2026-11-12
1824

1925
### Added

‎app/src/androidTest/kotlin/org/qownnotes/mobile/AppLaunchTest.kt‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3031,6 +3031,12 @@ class AppLaunchTest {
30313031
importAccount("alice", "Long note", "etag-1", 10, content)
30323032
composeRule.onNodeWithText("Long note").performClick()
30333033
composeRule.waitForTag("markdown-view")
3034+
composeRule.waitUntil(timeoutMillis = 10_000) {
3035+
composeRule.runOnIdle {
3036+
val view = composeRule.activity.findViewById<TextView>(R.id.markdown_view)
3037+
view?.text?.contains("Paragraph 80") == true && view.layout != null
3038+
}
3039+
}
30343040
val renderedTopBeforeScroll = screenTopOf(R.id.markdown_view)
30353041
composeRule.onNodeWithTag("markdown-view").performTouchInput {
30363042
swipeUp(durationMillis = 1_000)
@@ -3039,6 +3045,23 @@ class AppLaunchTest {
30393045
screenTopOf(R.id.markdown_view) < renderedTopBeforeScroll
30403046
}
30413047

3048+
// Remember the actual visible paragraph before Room reserves the edit and rerenders the
3049+
// preview. Merely checking a nonzero caret also accepts unrelated positions in the note.
3050+
var expectedSelection = 0
3051+
onView(withId(R.id.markdown_view)).check { view, _ ->
3052+
val text = view as TextView
3053+
val layout = checkNotNull(text.layout)
3054+
val scrollY = renderedTopBeforeScroll - IntArray(2).also(view::getLocationOnScreen)[1]
3055+
val line = layout.getLineForVertical(scrollY)
3056+
val renderedOffset = layout.getLineStart(line)
3057+
val sourceOffset = (renderedOffset.toLong() * content.length / text.length()).toInt()
3058+
expectedSelection = if (sourceOffset == 0) {
3059+
0
3060+
} else {
3061+
content.lastIndexOf('\n', sourceOffset - 1) + 1
3062+
}
3063+
}
3064+
30423065
composeRule.enterEditMode()
30433066

30443067
var selection = 0
@@ -3047,6 +3070,7 @@ class AppLaunchTest {
30473070
}
30483071
assertTrue("expected selection after the start of the note", selection > 0)
30493072
assertTrue("expected selection before the end of the note", selection < content.length)
3073+
assertEquals("expected selection at the reading position", expectedSelection, selection)
30503074

30513075
lateinit var editor: org.qownnotes.mobile.markdown.MarkdownEditText
30523076
onView(withId(R.id.markdown_editor)).check { view, _ ->

‎app/src/main/kotlin/org/qownnotes/mobile/MainActivity.kt‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3577,15 +3577,19 @@ private fun NoteDetailScreen(
35773577
(current.syncState != SyncState.FAILED || current.remoteId != null)
35783578
}
35793579
val startEditing = {
3580+
// Reserving the edit changes the Room revision. Its Flow can rerender the preview and
3581+
// invalidate TextView.layout while beginEditing suspends, so snapshot the reading position
3582+
// before starting database work rather than falling back to the end of the note afterward.
3583+
val readingOffset = sourceOffsetForReadingPosition(
3584+
renderedView,
3585+
scrollState.value,
3586+
note?.content.orEmpty()
3587+
)
35803588
scope.launch {
35813589
component.beginEditing(localId)?.let { editable ->
35823590
draft = editable.content
35833591
contentBeforeEditing = editable.content
3584-
selectionStart = sourceOffsetForReadingPosition(
3585-
renderedView,
3586-
scrollState.value,
3587-
editable.content
3588-
)
3592+
selectionStart = readingOffset.coerceIn(0, editable.content.length)
35893593
selectionEnd = selectionStart
35903594
editing = true
35913595
}

0 commit comments

Comments
 (0)