Skip to content

Commit 3463519

Browse files
committed
feat: fling through notes and discard edits
The editor now scrolls inside a container, so a flick coasts instead of crawling a few lines, and it still covers the viewport so tapping below a short note opens the keyboard at its end. Leaving edit mode without keeping the changes restores the note, which is confirmed first because typing is saved continuously.
1 parent 33a07bc commit 3463519

3 files changed

Lines changed: 241 additions & 128 deletions

File tree

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

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,58 @@ class AppLaunchTest {
236236
composeRule.waitForTag("note-list")
237237
}
238238

239+
/**
240+
* Editing writes continuously, so leaving without keeping the changes has to restore the note
241+
* rather than merely drop what has not been written yet.
242+
*/
243+
@Test
244+
fun cancellingEditingRestoresTheNoteAfterConfirmation() {
245+
importAccount("alice", "Existing note", "etag-1", 10, "# Existing note\n\nOriginal body")
246+
composeRule.onNodeWithText("Existing note").performClick()
247+
composeRule.enterEditMode()
248+
onView(withId(R.id.markdown_editor)).perform(
249+
click(),
250+
replaceText("# Existing note\n\nAbandoned body")
251+
)
252+
awaitEditorText("Abandoned body")
253+
254+
composeRule.onNodeWithTag("cancel-editing").performClick()
255+
composeRule.onNodeWithText("This note was modified", substring = true).assertIsDisplayed()
256+
composeRule.onNodeWithTag("confirm-discard-changes").performClick()
257+
258+
composeRule.waitForTag("markdown-view")
259+
composeRule.waitUntil(timeoutMillis = 10_000) {
260+
runBlocking {
261+
application.component.noteRepository
262+
.observeNotes(testAccount("alice").localAccountId())
263+
.first()
264+
.any { it.content.contains("Original body") }
265+
}
266+
}
267+
assertTrue(
268+
runBlocking {
269+
application.component.noteRepository
270+
.observeNotes(testAccount("alice").localAccountId())
271+
.first()
272+
.none { it.content.contains("Abandoned body") }
273+
}
274+
)
275+
}
276+
277+
/** Leaving an unchanged note must not interrupt the writer with a question. */
278+
@Test
279+
fun cancellingWithoutChangesLeavesEditingImmediately() {
280+
importAccount("alice", "Existing note", "etag-1", 10)
281+
composeRule.onNodeWithText("Existing note").performClick()
282+
composeRule.enterEditMode()
283+
284+
composeRule.onNodeWithTag("cancel-editing").performClick()
285+
286+
composeRule.waitForTag("markdown-view")
287+
composeRule.onNodeWithTag("confirm-discard-changes").assertDoesNotExist()
288+
composeRule.onNodeWithTag("edit-note").assertIsDisplayed()
289+
}
290+
239291
/**
240292
* Regression test for the editor that could be displayed but never typed into. Espresso's
241293
* `typeText` taps the view and then injects key events into whichever view holds input focus,
@@ -350,11 +402,13 @@ class AppLaunchTest {
350402
assertTrue("expected selection after the start of the note", selection > 0)
351403
assertTrue("expected selection before the end of the note", selection < content.length)
352404

353-
val before = scrollYOf(R.id.markdown_editor)
405+
// The editor is taller than its scrolling container, so dragging the rail moves the
406+
// editor itself upwards rather than scrolling text inside a fixed view.
407+
val before = screenTopOf(R.id.markdown_editor)
354408
composeRule.onNodeWithTag("editor-fast-scroll").assertIsDisplayed()
355409
.performTouchInput { swipeDown() }
356410
composeRule.waitUntil(timeoutMillis = 10_000) {
357-
scrollYOf(R.id.markdown_editor) > before
411+
screenTopOf(R.id.markdown_editor) < before
358412
}
359413
}
360414

@@ -640,12 +694,6 @@ class AppLaunchTest {
640694
return top
641695
}
642696

643-
private fun scrollYOf(viewId: Int): Int {
644-
var scrollY = 0
645-
onView(withId(viewId)).check { view, _ -> scrollY = view.scrollY }
646-
return scrollY
647-
}
648-
649697
private fun textSizeOf(viewId: Int): Float {
650698
var size = 0f
651699
onView(withId(viewId)).check { view, _ -> size = (view as TextView).textSize }
Lines changed: 62 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
package org.qownnotes.mobile
22

33
import androidx.compose.foundation.layout.Box
4-
import androidx.compose.foundation.layout.fillMaxSize
4+
import androidx.compose.foundation.layout.Column
5+
import androidx.compose.foundation.layout.fillMaxWidth
56
import androidx.compose.foundation.layout.size
7+
import androidx.compose.foundation.rememberScrollState
8+
import androidx.compose.foundation.verticalScroll
69
import androidx.compose.material3.MaterialTheme
7-
import androidx.compose.runtime.mutableIntStateOf
810
import androidx.compose.runtime.mutableStateOf
911
import androidx.compose.ui.Alignment
1012
import androidx.compose.ui.Modifier
13+
import androidx.compose.ui.platform.testTag
1114
import androidx.compose.ui.test.junit4.createComposeRule
1215
import androidx.compose.ui.test.onNodeWithTag
1316
import androidx.compose.ui.test.performTouchInput
1417
import androidx.compose.ui.test.swipeDown
18+
import androidx.compose.ui.test.swipeUp
1519
import androidx.compose.ui.unit.dp
1620
import androidx.compose.ui.viewinterop.AndroidView
1721
import org.junit.Assert.assertTrue
@@ -22,50 +26,73 @@ import org.qownnotes.mobile.markdown.MarkdownEditText
2226
class EditorFastScrollerTest {
2327
@get:Rule val composeRule = createComposeRule()
2428

29+
private val scrollState = mutableStateOf(0)
30+
private val maximumScroll = mutableStateOf(0)
31+
32+
/**
33+
* A flick with the thumb, which is what reading through a long note actually uses. An
34+
* `EditText` scrolls its own text without any fling, so this only travels far when the editor
35+
* is hosted in a scrolling container.
36+
*/
2537
@Test
26-
fun draggingTheRailMovesThroughMostOfALongEditor() {
27-
val editor = mutableStateOf<MarkdownEditText?>(null)
28-
val scrollY = mutableIntStateOf(0)
29-
val scrollRange = mutableIntStateOf(0)
30-
val viewportHeight = mutableIntStateOf(0)
38+
fun flingingTheEditorCoastsThroughTheNote() {
39+
showEditor()
40+
41+
composeRule.onNodeWithTag("editor-host").performTouchInput { swipeUp() }
42+
composeRule.waitForIdle()
43+
44+
// A drag of roughly one viewport that only moved the text by a few lines has no fling.
45+
assertTrue(
46+
"expected the flick to coast, but it stopped at ${scrollState.value}",
47+
scrollState.value > 1_000
48+
)
49+
}
50+
51+
@Test
52+
fun draggingTheRailMovesThroughMostOfTheNote() {
53+
showEditor()
54+
55+
composeRule.onNodeWithTag("editor-fast-scroll").performTouchInput { swipeDown() }
56+
composeRule.waitForIdle()
3157

58+
assertTrue(
59+
"expected the rail to move most of the note, but it reached ${scrollState.value}",
60+
scrollState.value > maximumScroll.value / 2
61+
)
62+
}
63+
64+
private fun showEditor() {
65+
val editor = mutableStateOf<MarkdownEditText?>(null)
3266
composeRule.setContent {
3367
MaterialTheme {
3468
Box(modifier = Modifier.size(width = 320.dp, height = 480.dp)) {
35-
AndroidView(
36-
factory = { context ->
37-
MarkdownEditText(context).also { view ->
38-
fun updateMetrics() {
39-
scrollY.intValue = view.scrollY
40-
scrollRange.intValue = editorMaximumScroll(view)
41-
viewportHeight.intValue = view.height
42-
}
43-
editor.value = view
44-
view.setText((1..300).joinToString("\n") { "Long editor line $it" })
45-
view.setOnScrollChangeListener { _, _, _, _, _ -> updateMetrics() }
46-
view.addOnLayoutChangeListener { _, _, _, _, _, _, _, _, _ ->
47-
updateMetrics()
69+
val editorScrollState = rememberScrollState()
70+
scrollState.value = editorScrollState.value
71+
maximumScroll.value = editorScrollState.maxValue
72+
Column(
73+
modifier = Modifier.size(width = 320.dp, height = 480.dp)
74+
.verticalScroll(editorScrollState)
75+
.testTag("editor-host")
76+
) {
77+
AndroidView(
78+
factory = { context ->
79+
MarkdownEditText(context).also { view ->
80+
editor.value = view
81+
view.setText(
82+
(1..300).joinToString("\n") { "Long editor line $it" }
83+
)
4884
}
49-
}
50-
},
51-
modifier = Modifier.fillMaxSize()
52-
)
85+
},
86+
modifier = Modifier.fillMaxWidth()
87+
)
88+
}
5389
EditorFastScroller(
54-
editor = editor.value,
55-
scrollY = scrollY.intValue,
56-
scrollRange = scrollRange.intValue,
57-
viewportHeight = viewportHeight.intValue,
90+
scrollState = editorScrollState,
5891
modifier = Modifier.align(Alignment.CenterEnd)
5992
)
6093
}
6194
}
6295
}
63-
composeRule.waitUntil(timeoutMillis = 10_000) { scrollRange.intValue > 2_000 }
64-
composeRule.onNodeWithTag("editor-fast-scroll").performTouchInput { swipeDown() }
65-
66-
composeRule.waitUntil(timeoutMillis = 10_000) {
67-
editor.value!!.scrollY > scrollRange.intValue / 2
68-
}
69-
assertTrue(editor.value!!.scrollY > scrollRange.intValue / 2)
96+
composeRule.waitUntil(timeoutMillis = 10_000) { maximumScroll.value > 2_000 }
7097
}
7198
}

0 commit comments

Comments
 (0)