library: shorten the achievement toast and make it tap-to-dismiss (#ux-audit)
- Commit
70b99ab4fc863cd026dc71d30057292bf24935fd- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-13 01:21 UTC (23 days ago)
- Parents
1bdc295
Symptom: the "Achievement Unlocked!" card stayed on screen for eight seconds over the top of the game. Tapping it did nothing - only an upward swipe, which nothing hinted at, removed it early - and because the card is a hit target, those taps were simply swallowed, so the toast also blocked whatever control sat under it for the whole time. Root cause: a hard-coded delay(8000) and a card whose only gesture handler was a vertical-drag detector; taps on it were absorbed by the card surface with no action attached. Fix: - Display time is now 4 s (DISPLAY_MILLIS), passed through AccessibilityManager.calculateRecommendedTimeoutMillis so users who asked Android for longer timed messages still get them. - The card is clickable (onClickLabel "Dismiss") and a tap plays the same exit animation as the timeout; swipe-up still works. Only the card takes touches - the area around it never had a pointer handler and still passes taps straight through to the game. - Each achievement gets a fresh toast via key(achievement), and the dismiss callback fires from a snapshotFlow once the exit transition is idle, with the state starting hidden-with-visible-target. This removes the old reliance on two LaunchedEffects running in order and makes the FIFO unlock queue advance cleanly in place: the next unlock slides in from the start instead of inheriting the previous toast's finished transition. User-visible effect: unlock toasts are half as intrusive, go away on a tap, and queued unlocks follow one another. Verification: new AchievementNotificationTest (virtual clock) - still up at 3 s, gone by 5.5 s; a tap on the card dismisses it within a second without reaching the screen below; a tap outside the card reaches a clickable under it and leaves the toast up; two queued unlocks each get a full display in order. Existing MiscUiSlice3Test toast test still passes.
Diff
library/ui/src/main/java/com/vayunmathur/library/ui/AchievementNotification.kt
diff --git a/library/ui/src/main/java/com/vayunmathur/library/ui/AchievementNotification.kt b/library/ui/src/main/java/com/vayunmathur/library/ui/AchievementNotification.ktindex 46180a7a66..7758286b9c 100644--- a/library/ui/src/main/java/com/vayunmathur/library/ui/AchievementNotification.kt+++ b/library/ui/src/main/java/com/vayunmathur/library/ui/AchievementNotification.kt@@ -6,6 +6,7 @@ import androidx.compose.animation.fadeIn import androidx.compose.animation.fadeOut import androidx.compose.animation.slideInVertically import androidx.compose.animation.slideOutVertically+import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Column import androidx.compose.foundation.gestures.detectVerticalDragGestures import androidx.compose.foundation.layout.Row@@ -19,37 +20,66 @@ import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect+import androidx.compose.runtime.getValue+import androidx.compose.runtime.key import androidx.compose.runtime.remember+import androidx.compose.runtime.rememberUpdatedState+import androidx.compose.runtime.snapshotFlow import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.input.pointer.pointerInput+import androidx.compose.ui.platform.LocalAccessibilityManager import androidx.compose.ui.res.painterResource import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.unit.dp-import androidx.compose.ui.unit.sp import androidx.compose.ui.res.stringResource import com.vayunmathur.library.ui.R import com.vayunmathur.library.util.Achievement import kotlinx.coroutines.delay+import kotlinx.coroutines.flow.first +/**+ * The toast announcing an unlocked achievement, sliding in from the top of the screen.+ *+ * It stays about four seconds — longer when the user has asked Android to give timed messages more+ * time — and goes early on a tap or an upward swipe. Only the card itself takes touches: the rest of+ * the screen keeps working under and around it.+ *+ * [onDismiss] runs once the exit animation has finished, which is when the caller should move on to the+ * next queued unlock. Each [achievement] gets a fresh toast, so a queue that advances in place shows+ * the next one from the start.+ */ @Composable fun AchievementNotification( achievement: Achievement,- onDismiss: () -> Unit+ onDismiss: () -> Unit, ) {- val visibleState = remember { MutableTransitionState(initialState = false) }+ key(achievement) { AchievementToast(achievement, onDismiss) }+}++@Composable+private fun AchievementToast(achievement: Achievement, onDismiss: () -> Unit) {+ // Starts hidden with a visible target, so it slides in on first composition.+ val visibleState = remember { MutableTransitionState(false).apply { targetState = true } }+ val currentOnDismiss by rememberUpdatedState(onDismiss)+ val accessibilityManager = LocalAccessibilityManager.current+ val hide = { visibleState.targetState = false } - LaunchedEffect(achievement) {- visibleState.targetState = true- delay(8000)- visibleState.targetState = false+ LaunchedEffect(Unit) {+ val shownFor = accessibilityManager?.calculateRecommendedTimeoutMillis(+ DISPLAY_MILLIS,+ containsIcons = true,+ containsText = true,+ containsControls = true,+ ) ?: DISPLAY_MILLIS+ delay(shownFor)+ hide() }- // Notify the caller only once the exit transition has fully played out.- LaunchedEffect(visibleState.isIdle) {- if (visibleState.isIdle && !visibleState.currentState) {- onDismiss()- }+ // Tell the caller only once the exit transition has fully played out.+ LaunchedEffect(Unit) {+ snapshotFlow { visibleState.isIdle && !visibleState.currentState }.first { it }+ currentOnDismiss() } AnimatedVisibility(@@ -62,10 +92,11 @@ fun AchievementNotification( modifier = Modifier .fillMaxWidth() .padding(top = 24.dp)- // Swipe up on the card to dismiss it early. Dragging up past the- // threshold plays the same exit animation as the auto-dismiss.+ // Tap to dismiss; swipe up past the threshold to dismiss too. Both play the same exit+ // animation as the timeout.+ .clickable(onClickLabel = stringResource(R.string.dismiss), onClick = hide) .pointerInput(Unit) {- val threshold = 48.dp.toPx()+ val threshold = SWIPE_THRESHOLD.toPx() var totalDrag = 0f detectVerticalDragGestures( onDragEnd = { totalDrag = 0f },@@ -73,9 +104,7 @@ fun AchievementNotification( ) { change, dragAmount -> change.consume() totalDrag += dragAmount- if (totalDrag <= -threshold) {- visibleState.targetState = false- }+ if (totalDrag <= -threshold) hide() } }, shape = MaterialTheme.shapes.largeIncreased,@@ -119,3 +148,8 @@ fun AchievementNotification( } } }++/** Long enough to read a name and a one-line description; the old eight seconds overstayed. */+private const val DISPLAY_MILLIS = 4_000L++private val SWIPE_THRESHOLD = 48.dp
library/ui/src/main/res/values/strings.xml
diff --git a/library/ui/src/main/res/values/strings.xml b/library/ui/src/main/res/values/strings.xmlindex 70f5e2d8ea..e1cbecc191 100644--- a/library/ui/src/main/res/values/strings.xml+++ b/library/ui/src/main/res/values/strings.xml@@ -47,7 +47,7 @@ <string name="link_action_open">Open</string> <!-- Shared library: UI – MarkdownEditor: Toolbar heading dropdown item. %1$s = level number 1-3. Shows in heading selector --> <string name="heading">Heading <xliff:g id="level" example="2">%1$s</xliff:g></string>- <!-- Shared library: UI – AchievementNotification: Banner title shown when achievement unlocked, auto-dismiss 8s swipeable card above achievement name -->+ <!-- Shared library: UI – AchievementNotification: Banner title shown when achievement unlocked, auto-dismiss ~4s, tap or swipe up to dismiss; label above achievement name --> <string name="achievement_unlocked">Achievement Unlocked!</string> <!-- Shared library: UI – EditorFormatter: Accessibility content description for link toolbar icon button, not visible text. Active when cursor on link --> <string name="cd_link">Link</string>
library/ui/src/test/java/com/vayunmathur/library/ui/AchievementNotificationTest.kt
diff --git a/library/ui/src/test/java/com/vayunmathur/library/ui/AchievementNotificationTest.kt b/library/ui/src/test/java/com/vayunmathur/library/ui/AchievementNotificationTest.ktnew file mode 100644index 0000000000..e32ddecacf--- /dev/null+++ b/library/ui/src/test/java/com/vayunmathur/library/ui/AchievementNotificationTest.kt@@ -0,0 +1,111 @@+package com.vayunmathur.library.ui++import androidx.compose.foundation.clickable+import androidx.compose.foundation.layout.Box+import androidx.compose.foundation.layout.fillMaxSize+import androidx.compose.runtime.mutableStateListOf+import androidx.compose.ui.Modifier+import androidx.compose.ui.geometry.Offset+import androidx.compose.ui.platform.testTag+import androidx.compose.ui.test.assertIsDisplayed+import androidx.compose.ui.test.click+import androidx.compose.ui.test.junit4.createComposeRule+import androidx.compose.ui.test.onNodeWithTag+import androidx.compose.ui.test.onNodeWithText+import androidx.compose.ui.test.performClick+import androidx.compose.ui.test.performTouchInput+import com.vayunmathur.library.util.Achievement+import org.junit.Rule+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import kotlin.test.Test+import kotlin.test.assertEquals++/** The unlock toast: how long it stays, what dismisses it, and what it lets through. */+@RunWith(RobolectricTestRunner::class)+class AchievementNotificationTest {++ @get:Rule+ val rule = createComposeRule()++ private val first = Achievement(+ id = "a1", name = "First win", description = "Win once", targetProgress = 1, isSecret = false,+ )++ private var dismissed = 0+ private var behindTaps = 0++ private fun setToastOverScreen(achievement: Achievement = first) {+ rule.mainClock.autoAdvance = false+ rule.setContent {+ Box(Modifier.fillMaxSize().testTag(ROOT)) {+ Box(Modifier.fillMaxSize().clickable { behindTaps++ })+ AchievementNotification(achievement = achievement, onDismiss = { dismissed++ })+ }+ }+ rule.mainClock.advanceTimeBy(ENTER_SETTLED)+ }++ @Test+ fun `the toast stays about four seconds, not eight`() {+ setToastOverScreen()+ rule.mainClock.advanceTimeBy(3_000 - ENTER_SETTLED)+ assertEquals(0, dismissed, "still up at three seconds")+ rule.mainClock.advanceTimeBy(2_500)+ rule.waitForIdle()+ assertEquals(1, dismissed, "gone, exit animation included, by five and a half seconds")+ }++ @Test+ fun `a tap on the card dismisses it early`() {+ setToastOverScreen()+ rule.onNodeWithText("First win").performClick()+ rule.mainClock.advanceTimeBy(1_000)+ rule.waitForIdle()+ assertEquals(1, dismissed)+ assertEquals(0, behindTaps, "the tap belonged to the card")+ }++ @Test+ fun `a tap outside the card passes through to the screen`() {+ setToastOverScreen()+ rule.onNodeWithTag(ROOT).performTouchInput { click(Offset(centerX, height - 4f)) }+ rule.mainClock.advanceTimeBy(100)+ rule.waitForIdle()+ assertEquals(1, behindTaps)+ assertEquals(0, dismissed)+ }++ @Test+ fun `queued unlocks each get a full toast, in order`() {+ val second = first.copy(id = "a2", name = "Ten wins", description = "Win ten times")+ val queue = mutableStateListOf(first, second)+ rule.mainClock.autoAdvance = false+ rule.setContent {+ queue.firstOrNull()?.let { AchievementNotification(achievement = it, onDismiss = { queue.removeAt(0) }) }+ }+ rule.mainClock.advanceTimeBy(ENTER_SETTLED)+ rule.onNodeWithText("First win").assertIsDisplayed()+ rule.onNodeWithText("Ten wins").assertDoesNotExist()++ rule.mainClock.advanceTimeBy(5_500)+ rule.waitForIdle()+ rule.onNodeWithText("First win").assertDoesNotExist()+ rule.onNodeWithText("Ten wins").assertIsDisplayed()+ // The second only started once the first had timed out (t >= 4 s), so it is still up at 7.5 s:+ // it got its own full display rather than inheriting the first one's exit.+ rule.mainClock.advanceTimeBy(1_000)+ rule.onNodeWithText("Ten wins").assertIsDisplayed()++ rule.mainClock.advanceTimeBy(4_000)+ rule.waitForIdle()+ assertEquals(0, queue.size)+ }++ private companion object {+ const val ROOT = "root"++ /** Past the slide-in, so the card is in place and hit-testable. */+ const val ENTER_SETTLED = 1_000L+ }+}