minesweeper: buzz on long press only when a flag actually changes (#ux-audit)
- Commit
67e9ca9ef0d418b4df3f41bb8e471d563dfbe407- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-13 01:17 UTC (23 days ago)
- Parents
dfa2c65
Symptom: every long press vibrated, including presses that did nothing - on an already-open cell, on a field before the first dig, or (in Flag mode) a long-press dig. The buzz is meant to confirm "flag placed/removed", so it told the player a flag had gone in when none had. Root cause: MineCell performed the LongPress haptic unconditionally before calling onLongPress, with no way to know whether the action changed anything. Fix: MinesweeperActions.flagCell now returns Boolean - true only when a flag was placed or removed (default false, so previews/fakes need no change). MinesweeperViewModel.toggleFlag reports whether MinesweeperRules.toggleFlag produced a new state; the Flag-mode dig/chord branches return false. MineFieldGrid's onLongPress is (Int) -> Boolean and MineCell buzzes only on true, for both the touch long press and the TalkBack long-click action. User-visible effect: long-pressing a covered cell to flag/unflag still buzzes; long presses that change nothing are silent. Verification: MinesweeperBoardTest longPressBuzzesOnlyWhenAFlagChanges (fake LocalHapticFeedback: no buzz when the callback reports no change, one LongPress buzz when it does); MinesweeperViewModelTest asserts flagCell's return value before start, on place, on remove and on an open cell. :games:minesweeper:testDevUnitTest in the toolchain container.
Diff
games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/platform/MinesweeperUiContract.kt
diff --git a/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/platform/MinesweeperUiContract.kt b/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/platform/MinesweeperUiContract.ktindex 496625c830..19a2e3ac1c 100644--- a/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/platform/MinesweeperUiContract.kt+++ b/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/platform/MinesweeperUiContract.kt@@ -18,8 +18,13 @@ interface MinesweeperActions { /** Tap: digs or flags depending on the current [TapMode]; in Dig mode chords an open number. */ fun tapCell(index: Int) {} - /** Long press: the other action from whatever the mode is (in Flag mode, digs or chords). */- fun flagCell(index: Int) {}+ /**+ * Long press: the other action from whatever the mode is (in Flag mode, digs or chords).+ *+ * @return true only when a flag was actually placed or removed, so the board buzzes for a+ * real change and stays quiet for a press that did nothing (an open cell, an unstarted field).+ */+ fun flagCell(index: Int): Boolean = false fun setTapMode(mode: TapMode) {} fun restart() {}
games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/platform/MinesweeperViewModel.kt
diff --git a/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/platform/MinesweeperViewModel.kt b/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/platform/MinesweeperViewModel.ktindex 84b6341445..4056ef8707 100644--- a/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/platform/MinesweeperViewModel.kt+++ b/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/platform/MinesweeperViewModel.kt@@ -112,14 +112,14 @@ class MinesweeperViewModel(application: Application) : * In flag mode that is digging, so a long press on an open number chords — the opening gesture * stays available in flag mode, just never on the plain tap. */- override fun flagCell(index: Int) {+ override fun flagCell(index: Int): Boolean { val state = _uiState.value- val game = state.game ?: return- if (game.isOver) return- when {+ val game = state.game ?: return false+ if (game.isOver) return false+ return when { state.tapMode == TapMode.DIG -> toggleFlag(game, index)- game.revealed[index] -> chord(game, index)- else -> dig(state, game, index)+ game.revealed[index] -> { chord(game, index); false }+ else -> { dig(state, game, index); false } } } @@ -152,12 +152,15 @@ class MinesweeperViewModel(application: Application) : publish(chorded) } - private fun toggleFlag(game: MinesweeperGameState, index: Int) {+ /** Returns whether the flag actually changed; open cells and unstarted fields refuse. */+ private fun toggleFlag(game: MinesweeperGameState, index: Int): Boolean { // Flagging before the first dig would have nothing to flag: the mines are not laid yet.- if (!game.started) return+ if (!game.started) return false val next = MinesweeperRules.toggleFlag(game, index)+ if (next === game) return false if (next.flagsPlaced > game.flagsPlaced) placedAnyFlag = true publish(next)+ return true } /** A fresh field with the same settings. */
games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/ui/components/MineFieldGrid.kt
diff --git a/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/ui/components/MineFieldGrid.kt b/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/ui/components/MineFieldGrid.ktindex 2f8f2d706c..0d56d709c4 100644--- a/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/ui/components/MineFieldGrid.kt+++ b/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/ui/components/MineFieldGrid.kt@@ -42,12 +42,14 @@ import com.vayunmathur.library.ui.Text * than squeezing the cells. Tap and long press are handled per cell by [detectTapGestures] rather * than by `clickable` + `combinedClickable`, because the flag gesture needs to fire on press-and-hold * without also delivering the tap that would open the cell.+ *+ * @param onLongPress returns whether a flag actually changed; the press buzzes only then. */ @Composable fun MineFieldGrid( game: MinesweeperGameState, onTap: (Int) -> Unit,- onLongPress: (Int) -> Unit,+ onLongPress: (Int) -> Boolean, modifier: Modifier = Modifier, ) { Column(modifier) {@@ -76,7 +78,7 @@ private fun MineCell( game: MinesweeperGameState, index: Int, onTap: () -> Unit,- onLongPress: () -> Unit,+ onLongPress: () -> Boolean, modifier: Modifier = Modifier, ) { val scheme = MaterialTheme.colorScheme@@ -122,8 +124,9 @@ private fun MineCell( onTap = { onTap() }, onLongPress = { // Confirms the flag without the player having to look away from the field.- haptics.performHapticFeedback(HapticFeedbackType.LongPress)- onLongPress()+ // Only for a real change: buzzing for a press that did nothing (an open+ // cell, an unstarted field) told the player a flag went in when none did.+ if (onLongPress()) haptics.performHapticFeedback(HapticFeedbackType.LongPress) }, ) }@@ -134,8 +137,7 @@ private fun MineCell( // tap/long-press split that detectTapGestures above preserves. onClick { onTap(); true } onLongClick {- haptics.performHapticFeedback(HapticFeedbackType.LongPress)- onLongPress()+ if (onLongPress()) haptics.performHapticFeedback(HapticFeedbackType.LongPress) true } },
games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/platform/MinesweeperViewModelTest.kt
diff --git a/games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/platform/MinesweeperViewModelTest.kt b/games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/platform/MinesweeperViewModelTest.ktindex 46420b52ec..cdd3a38cd0 100644--- a/games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/platform/MinesweeperViewModelTest.kt+++ b/games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/platform/MinesweeperViewModelTest.kt@@ -120,15 +120,16 @@ class MinesweeperViewModelTest { fun flagBeforeStartDoesNothingModeToggleFlagsAfterStart() { val vm = vm() vm.newGame(GameConfig(BoardSize.SMALL, Difficulty.EASY))- vm.flagCell(5)+ assertFalse(vm.flagCell(5), "no flag before the mines are laid") assertEquals(0, vm.uiState.value.game!!.flagsPlaced) vm.tapCell(0) val started = vm.uiState.value.game!! val hidden = started.revealed.indices.first { !started.revealed[it] && !started.mines[it] }- vm.flagCell(hidden)+ assertTrue(vm.flagCell(hidden), "placing a flag reports a change") assertEquals(1, vm.uiState.value.game!!.flagsPlaced)- vm.flagCell(hidden)+ assertTrue(vm.flagCell(hidden), "removing it does too") assertEquals(0, vm.uiState.value.game!!.flagsPlaced)+ assertFalse(vm.flagCell(0), "an open cell cannot be flagged") // Flag-mode tap also flags (never digs). vm.setTapMode(TapMode.FLAG) vm.tapCell(hidden)
games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/ui/MinesweeperBoardTest.kt
diff --git a/games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/ui/MinesweeperBoardTest.kt b/games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/ui/MinesweeperBoardTest.ktindex 965cfa90fc..9801520d51 100644--- a/games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/ui/MinesweeperBoardTest.kt+++ b/games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/ui/MinesweeperBoardTest.kt@@ -10,6 +10,10 @@ import androidx.compose.ui.test.onNodeWithContentDescription import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.performClick import androidx.compose.ui.test.performSemanticsAction+import androidx.compose.runtime.CompositionLocalProvider+import androidx.compose.ui.hapticfeedback.HapticFeedback+import androidx.compose.ui.hapticfeedback.HapticFeedbackType+import androidx.compose.ui.platform.LocalHapticFeedback import androidx.compose.ui.semantics.SemanticsActions import androidx.test.core.app.ApplicationProvider import com.vayunmathur.games.minesweeper.R@@ -59,7 +63,7 @@ class MinesweeperBoardTest { fun gridCellsExposePositionAndStateDescriptions() { var tapped = -1 composeRule.setContent {- MineFieldGrid(field(), onTap = { tapped = it }, onLongPress = {})+ MineFieldGrid(field(), onTap = { tapped = it }, onLongPress = { false }) } // Centre mine stays described as covered during play. composeRule.onNodeWithContentDescription("Row 2, column 2, Covered", substring = true)@@ -74,7 +78,7 @@ class MinesweeperBoardTest { var tapped = -1 var longPressed = -1 composeRule.setContent {- MineFieldGrid(field(), onTap = { tapped = it }, onLongPress = { longPressed = it })+ MineFieldGrid(field(), onTap = { tapped = it }, onLongPress = { longPressed = it; true }) } // The description alone only lets TalkBack announce the cell; activation // needs the click actions too, or the board is heard but not playable.@@ -86,12 +90,34 @@ class MinesweeperBoardTest { assertEquals(0, longPressed) } + @Test+ fun longPressBuzzesOnlyWhenAFlagChanges() {+ val buzzes = mutableListOf<HapticFeedbackType>()+ val haptics = object : HapticFeedback {+ override fun performHapticFeedback(hapticFeedbackType: HapticFeedbackType) {+ buzzes += hapticFeedbackType+ }+ }+ var changes = false+ composeRule.setContent {+ CompositionLocalProvider(LocalHapticFeedback provides haptics) {+ MineFieldGrid(field(), onTap = {}, onLongPress = { changes })+ }+ }+ val cell = composeRule.onNodeWithContentDescription("Row 1, column 1, Covered", substring = true)+ cell.performSemanticsAction(SemanticsActions.OnLongClick)+ assertEquals(0, buzzes.size, "nothing changed, so no buzz")+ changes = true+ cell.performSemanticsAction(SemanticsActions.OnLongClick)+ assertEquals(listOf(HapticFeedbackType.LongPress), buzzes)+ }+ @Test fun gridRevealedNumberAndFlagDescriptions() { val revealed = List(9) { it == 0 } val flagged = List(9) { it == 4 } composeRule.setContent {- MineFieldGrid(field(revealed = revealed, flagged = flagged), onTap = {}, onLongPress = {})+ MineFieldGrid(field(revealed = revealed, flagged = flagged), onTap = {}, onLongPress = { false }) } // Index 0 touches the centre mine, so it reads as a count of 1. composeRule.onNodeWithContentDescription("Row 1, column 1", substring = true).assertIsDisplayed()