wordmaker: keep a new swipe when the last word is rejected (#ux-audit)
- Commit
9dfb18de5c3a7746b8e4d1eae9e25a83fd1bb1d4- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-13 01:14 UTC (23 days ago)
- Parents
269b073
Symptom: after a rejected word, starting the next swipe straight away
lost that swipe's first letters and its trail. The same happened with
tap-to-spell: letters tapped while the rejected word shook were
appended to it, then wiped.
Root cause: the drag-end handler (and the tap-to-spell submit button)
launched a coroutine that awaited the submit handler - which, for a
rejected word, runs the 240-360 ms shake - and only then cleared the
selection and the drag position. A swipe started during the shake
wrote its letters into the same selection, and the late clear erased
them mid-gesture.
Fix: LetterChooser snapshots the submitted word into its own
"judged word" state and clears the selection and drag position
immediately, before the verdict. The word box shows the live selection
while one exists and the judged word otherwise, and the shake
translation applies only to the judged word. A serial number makes sure
a stale verdict never clears a newer judged word. A cancelled drag now
also clears its selection instead of leaving a stuck trail.
User-visible effect: you can start the next word while the previous
one is still shaking; the new swipe (or tapped word) is kept in full.
The rejected word still visibly shakes in the word box.
Verification: new Robolectric LetterChooserInputTest drives real touch
streams - a rejected swipe followed by a swipe that is in progress
when the shake ends keeps all of its letters ("TSC"), and the
tap-to-spell equivalent keeps the newly tapped word.Diff
games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/ui/components/LetterChooser.kt
diff --git a/games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/ui/components/LetterChooser.kt b/games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/ui/components/LetterChooser.ktindex a6db49d2e6..58dbfc6903 100644--- a/games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/ui/components/LetterChooser.kt+++ b/games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/ui/components/LetterChooser.kt@@ -23,6 +23,7 @@ import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.runtime.key+import androidx.compose.runtime.mutableIntStateOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope@@ -63,7 +64,7 @@ fun LetterChooser( onWordSubmitted: suspend CoroutineScope.(String, List<Int>) -> Unit, onWordBoxPositioned: (Offset) -> Unit, onLetterPositioned: (id: Int, offset: Offset) -> Unit,- wordShakeTranslation: Float+ wordShakeTranslation: Float = 0f ) { val coroutineScope = rememberCoroutineScope() @@ -78,6 +79,32 @@ fun LetterChooser( var dragStartOffset by remember(letters) { mutableStateOf(Offset.Zero) } var currentDragPosition by remember(letters) { mutableStateOf<Offset?>(null) } + // The word box keeps showing a submitted word while it is judged (a rejected word shakes+ // for 240-360 ms), but the selection itself is cleared the moment the word is submitted.+ // Clearing it only after the verdict wiped the first letters and the trail of a swipe the+ // player had already started in the meantime.+ var judgedWord by remember(letters) { mutableStateOf<String?>(null) }+ var submitSerial by remember(letters) { mutableIntStateOf(0) }++ fun submitSelection() {+ if (selectedLettersIndices.isEmpty()) return+ val word = selectedLettersIndices.map { letters[it].char }.joinToString("")+ val ids = selectedLettersIndices.map { letters[it].id }+ selectedLettersIndices = emptyList()+ currentDragPosition = null+ val serial = submitSerial + 1+ submitSerial = serial+ judgedWord = word+ coroutineScope.launch {+ try {+ submitWord(word, ids)+ } finally {+ // A newer submission owns the word box now; leave it alone.+ if (submitSerial == serial) judgedWord = null+ }+ }+ }+ val density = LocalDensity.current val letterCircleRadius = with(density) { 35.dp.toPx() } val boxSize = wheelSpacing.boxSize@@ -115,16 +142,21 @@ fun LetterChooser( return null } + // While a swipe is in progress the box shows it; otherwise it shows the word being judged.+ val isSpelling = selectedLettersIndices.isNotEmpty()+ val shownWord = if (isSpelling) formedWord else judgedWord.orEmpty()+ Column(horizontalAlignment = Alignment.CenterHorizontally) { SurfaceText( Modifier .padding(bottom = 20.dp) .graphicsLayer(- alpha = if (selectedLettersIndices.isNotEmpty()) 1f else 0f,- translationX = wordShakeTranslation+ alpha = if (shownWord.isNotEmpty()) 1f else 0f,+ // The shake belongs to the judged word, never to a new swipe.+ translationX = if (isSpelling) 0f else wordShakeTranslation ), RoundedCornerShape(8.dp), MaterialTheme.colorScheme.primaryContainer,- formedWord.ifEmpty { " " },+ shownWord.ifEmpty { " " }, Modifier .padding(horizontal = 16.dp, vertical = 12.dp) .onGloballyPositioned { onWordBoxPositioned(it.localToRoot(Offset.Zero)) },@@ -145,16 +177,7 @@ fun LetterChooser( Icon(painterResource(R.drawable.backspace_24px), contentDescription = stringResource(R.string.cd_backspace)) } Spacer(modifier = Modifier.height(8.dp))- FilledIconButton(onClick = {- if (selectedLettersIndices.isNotEmpty()) {- val word = selectedLettersIndices.map { letters[it].char }.joinToString("")- val ids = selectedLettersIndices.map { letters[it].id }- coroutineScope.launch {- submitWord(word, ids)- selectedLettersIndices = emptyList()- }- }- }) {+ FilledIconButton(onClick = { submitSelection() }) { Icon(painterResource(R.drawable.keyboard_return_24px), contentDescription = stringResource(R.string.cd_submit)) } }@@ -187,16 +210,12 @@ fun LetterChooser( } }, onDragEnd = {- coroutineScope.launch {- if (selectedLettersIndices.isNotEmpty()) {- submitWord(- selectedLettersIndices.map { letters[it].char }.joinToString(""),- selectedLettersIndices.map { letters[it].id }- )- }- selectedLettersIndices = emptyList()- currentDragPosition = null- }+ currentDragPosition = null+ submitSelection()+ },+ onDragCancel = {+ currentDragPosition = null+ selectedLettersIndices = emptyList() } ) },
games/wordmaker/src/test/java/com/vayunmathur/games/wordmaker/ui/components/LetterChooserInputTest.kt
diff --git a/games/wordmaker/src/test/java/com/vayunmathur/games/wordmaker/ui/components/LetterChooserInputTest.kt b/games/wordmaker/src/test/java/com/vayunmathur/games/wordmaker/ui/components/LetterChooserInputTest.ktnew file mode 100644index 0000000000..e1f8d9eb3b--- /dev/null+++ b/games/wordmaker/src/test/java/com/vayunmathur/games/wordmaker/ui/components/LetterChooserInputTest.kt@@ -0,0 +1,100 @@+package com.vayunmathur.games.wordmaker.ui.components++import androidx.compose.runtime.CompositionLocalProvider+import androidx.compose.ui.geometry.Offset+import androidx.compose.ui.platform.LocalLayoutDirection+import androidx.compose.ui.test.junit4.createComposeRule+import androidx.compose.ui.test.onNodeWithContentDescription+import androidx.compose.ui.test.onNodeWithText+import androidx.compose.ui.test.onRoot+import androidx.compose.ui.test.performClick+import androidx.compose.ui.test.performTouchInput+import androidx.compose.ui.unit.LayoutDirection+import com.vayunmathur.games.wordmaker.data.WheelSpacing+import com.vayunmathur.games.wordmaker.ui.ChooserLetter+import com.vayunmathur.library.ui.DynamicTheme+import kotlinx.coroutines.CompletableDeferred+import org.junit.Rule+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import org.robolectric.annotation.Config+import kotlin.test.Test+import kotlin.test.assertEquals++/**+ * Gesture-level tests for the letter wheel: real touch streams through the wheel's+ * pointer handling, aimed at where the letters are actually drawn.+ */+@RunWith(RobolectricTestRunner::class)+@Config(qualifiers = "w411dp-h891dp")+class LetterChooserInputTest {+ @get:Rule val rule = createComposeRule()++ // Four letters: C at the top, A on the right, T at the bottom, S on the left (in LTR).+ private val letters = listOf(+ ChooserLetter(0, 'C'), ChooserLetter(1, 'A'), ChooserLetter(2, 'T'), ChooserLetter(3, 'S'),+ )++ private fun setChooser(+ tapToSpell: Boolean = false,+ layoutDirection: LayoutDirection = LayoutDirection.Ltr,+ spacing: WheelSpacing = WheelSpacing.DEFAULT,+ onSubmit: suspend (String) -> Unit,+ ) {+ rule.setContent {+ DynamicTheme {+ CompositionLocalProvider(LocalLayoutDirection provides layoutDirection) {+ LetterChooser(+ letters = letters, tapToSpell = tapToSpell, wheelSpacing = spacing,+ onShuffle = {}, onWordSubmitted = { w, _ -> onSubmit(w) },+ onWordBoxPositioned = {}, onLetterPositioned = { _, _ -> },+ )+ }+ }+ }+ }++ /** Where [c] is drawn, in root coordinates. Read before any word box shows letters. */+ private fun centerOf(c: Char): Offset =+ rule.onNodeWithText(c.toString()).fetchSemanticsNode().boundsInRoot.center++ @Test fun rejectedSwipeThenImmediateSwipeKeepsTheNewSwipe() {+ val verdict = CompletableDeferred<Unit>()+ val submitted = mutableListOf<String>()+ // The first word is "being judged" (shaking) until the test releases it.+ setChooser { w -> submitted += w; if (submitted.size == 1) verdict.await() }+ val c = centerOf('C'); val a = centerOf('A'); val t = centerOf('T'); val s = centerOf('S')++ rule.onRoot().performTouchInput { down(c); moveTo(c + Offset(0f, 12f)); moveTo(a); up() }+ rule.waitForIdle()+ assertEquals(listOf("CA"), submitted)++ // Start the next swipe while the first word is still shaking, then let the shake end+ // mid-swipe: the new swipe must keep its first letters.+ rule.onRoot().performTouchInput { down(t); moveTo(t + Offset(0f, -12f)); moveTo(s) }+ verdict.complete(Unit)+ rule.waitForIdle()+ rule.onRoot().performTouchInput { moveTo(c); up() }+ rule.waitForIdle()+ assertEquals(listOf("CA", "TSC"), submitted)+ }++ @Test fun rejectedTapWordThenImmediateTapsKeepTheNewWord() {+ val verdict = CompletableDeferred<Unit>()+ val submitted = mutableListOf<String>()+ setChooser(tapToSpell = true) { w -> submitted += w; if (submitted.size == 1) verdict.await() }++ rule.onNodeWithText("C").performClick()+ rule.onNodeWithText("A").performClick()+ rule.onNodeWithContentDescription("Submit").performClick()+ rule.waitForIdle()+ // Tapping on while "CA" shakes starts a fresh word rather than extending "CA"...+ rule.onNodeWithText("T").performClick()+ verdict.complete(Unit)+ rule.waitForIdle()+ // ...and the end of the shake does not wipe it.+ rule.onNodeWithContentDescription("Submit").performClick()+ rule.waitForIdle()+ assertEquals(listOf("CA", "T"), submitted)+ }+}