tvlauncher: single-path reorder state machine, fixed hint slot
- Commit
87752c0450925837561817a5bfe51e194df814fd- Author
- vayun-mathur <[email protected]>
- When
- 2026-10-20 04:49 UTC (1 month ago)
- Parents
73fc1a9
Diff
tvlauncher/src/main/java/com/vayunmathur/tvlauncher/platform/TvHomeViewModel.kt
diff --git a/tvlauncher/src/main/java/com/vayunmathur/tvlauncher/platform/TvHomeViewModel.kt b/tvlauncher/src/main/java/com/vayunmathur/tvlauncher/platform/TvHomeViewModel.ktindex 16e1e83946..e74ef4bbb4 100644--- a/tvlauncher/src/main/java/com/vayunmathur/tvlauncher/platform/TvHomeViewModel.kt+++ b/tvlauncher/src/main/java/com/vayunmathur/tvlauncher/platform/TvHomeViewModel.kt@@ -32,6 +32,12 @@ class TvHomeViewModel(app: Application) : AndroidViewModel(app) { private val artCache = TvArtCache(app) /** Serializes rebuilds: package broadcasts burst, and last-writer must win. */ private val refreshMutex = Mutex()+ /**+ * The last order known persisted in DataStore. Moves only persist on drop, so while a+ * tile is held (or after arming) the screen order is newer; cancel/disarm restores from+ * here quietly instead of paying for a full reload flash.+ */+ private var lastPersistedOrder: List<String> = emptyList() private val _home = MutableStateFlow(TvHomeState()) val home: StateFlow<TvHomeState> = _home@@ -87,6 +93,7 @@ class TvHomeViewModel(app: Application) : AndroidViewModel(app) { val inputs = withContext(Dispatchers.IO) { TvInputs.list(context, TvRowOrder.maxHdmiSlots(hasStore)) }+ lastPersistedOrder = row _home.value = _home.value.copy( rowApps = tiles, showAppStore = hasStore,@@ -155,24 +162,41 @@ class TvHomeViewModel(app: Application) : AndroidViewModel(app) { } fun startMove(pkg: String) {- _home.value = _home.value.copy(editingKey = null, movingKey = pkg, reorderArmed = false)+ // Picking up keeps reorder mode armed: dropping returns to ARMED so several apps can+ // be arranged without re-arming each time. Only the toggle or Back disarms.+ _home.value = _home.value.copy(editingKey = null, movingKey = pkg, reorderArmed = true) } /** Arms reorder mode: OK on a main-row tile picks it up, arrows move it. */ fun toggleReorder() { val armed = !_home.value.reorderArmed- _home.value = _home.value.copy(- reorderArmed = armed,- editingKey = null,- // Disarming drops any held tile without persisting (same as Back).- movingKey = if (armed) _home.value.movingKey else null,- )- if (!armed) refreshApps()+ if (!armed && _home.value.movingKey != null) {+ // Disarming mid-hold drops the hold: restore the last persisted order quietly.+ _home.value = _home.value.copy(+ reorderArmed = false,+ editingKey = null,+ movingKey = null,+ rowApps = reorderTiles(_home.value.rowApps, lastPersistedOrder),+ )+ } else {+ _home.value = _home.value.copy(reorderArmed = armed, editingKey = null)+ }+ // No refreshApps(): the on-screen order is already correct, and a reload would flash+ // loading + steal focus.+ }++ /** Disarms reorder mode (Back while armed but not holding). Never reloads. */+ fun disarmReorder() {+ if (_home.value.movingKey != null) {+ cancelMove()+ return+ }+ _home.value = _home.value.copy(reorderArmed = false, editingKey = null) } /** * The main-row OK press: in reorder mode it picks up / drops the tile; otherwise it- * opens the app. Long-OK still opens the edit menu (handled by the tile key path).+ * opens the app. This is the single pick-up/drop path — there is no long-press. */ fun pressTile(pkg: String) { val state = _home.value@@ -184,21 +208,27 @@ class TvHomeViewModel(app: Application) : AndroidViewModel(app) { } } + /** Drops the held tile: persists the on-screen order, stays armed for the next move. */ fun finishMove() { viewModelScope.launch { refreshMutex.withLock {- withContext(Dispatchers.IO) {- prefs.saveOrder(_home.value.rowApps.map { it.packageName })- }- _home.value = _home.value.copy(movingKey = null, reorderArmed = false)+ val order = _home.value.rowApps.map { it.packageName }+ withContext(Dispatchers.IO) { prefs.saveOrder(order) }+ lastPersistedOrder = order+ _home.value = _home.value.copy(movingKey = null) } } } - /** Exits move mode without persisting (Back while moving). */+ /**+ * Exits move mode without persisting (Back while moving): restores the last persisted+ * order in memory, stays armed, no loading flash.+ */ fun cancelMove() {- _home.value = _home.value.copy(movingKey = null)- refreshApps()+ _home.value = _home.value.copy(+ movingKey = null,+ rowApps = reorderTiles(_home.value.rowApps, lastPersistedOrder),+ ) } fun moveKey(pkg: String, step: Int) {@@ -305,6 +335,8 @@ class TvHomeViewModel(app: Application) : AndroidViewModel(app) { private fun reorderTiles(tiles: List<TvTile>, order: List<String>): List<TvTile> { val byPkg = tiles.associateBy { it.packageName }- return order.mapNotNull { byPkg[it] }+ // Packages installed since the snapshot (absent from order) keep their relative+ // position at the end rather than vanishing.+ return order.mapNotNull { byPkg[it] } + tiles.filter { it.packageName !in order } } }
tvlauncher/src/main/java/com/vayunmathur/tvlauncher/platform/TvUiContract.kt
diff --git a/tvlauncher/src/main/java/com/vayunmathur/tvlauncher/platform/TvUiContract.kt b/tvlauncher/src/main/java/com/vayunmathur/tvlauncher/platform/TvUiContract.ktindex cad68ee109..79f98abac8 100644--- a/tvlauncher/src/main/java/com/vayunmathur/tvlauncher/platform/TvUiContract.kt+++ b/tvlauncher/src/main/java/com/vayunmathur/tvlauncher/platform/TvUiContract.kt@@ -17,9 +17,12 @@ data class TvHomeState( /** True when the MA App Store is installed (bottom-row "App Store" tile). */ val showAppStore: Boolean = false, val hdmiInputs: List<TvHdmiInput> = emptyList(),- /** The package being moved with the arrows, or null when not in move mode. */+ /** The package being moved with the arrows, or null when not holding a tile. */ val movingKey: String? = null,- /** True while reorder mode is armed: OK on a main-row tile picks it up. */+ /**+ * True while reorder mode is armed. Armed persists across drops — only the Reorder+ * toggle or Back disarms. While armed, short-OK picks a tile up instead of opening it.+ */ val reorderArmed: Boolean = false, /** Packages offered by the add menu (not on the row, by label). */ val addCandidates: List<TvAppEntry> = emptyList(),
tvlauncher/src/main/java/com/vayunmathur/tvlauncher/ui/TvAppGrid.kt
diff --git a/tvlauncher/src/main/java/com/vayunmathur/tvlauncher/ui/TvAppGrid.kt b/tvlauncher/src/main/java/com/vayunmathur/tvlauncher/ui/TvAppGrid.ktindex 8f8cfcb2b3..ce5a361bb1 100644--- a/tvlauncher/src/main/java/com/vayunmathur/tvlauncher/ui/TvAppGrid.kt+++ b/tvlauncher/src/main/java/com/vayunmathur/tvlauncher/ui/TvAppGrid.kt@@ -46,9 +46,7 @@ fun TvAppGrid( state: TvHomeState, onPressTile: (String) -> Unit, onOpenEditMenu: (String) -> Unit,- onPickUp: (String) -> Unit, onMoveKey: (String, Int) -> Unit,- onFinishMove: () -> Unit, ) { // Stable per-package focus owners: a reorder changes positions, not identities, so the // held tile keeps its requester and focus can chase it. Populated outside composition@@ -112,15 +110,10 @@ fun TvAppGrid( // While one tile is held, every other tile dims: the held // one is the only bright thing on screen. dimmed = movingKey != null && !held,- reorderArmed = state.reorderArmed,- // Pure read: populated by the LaunchedEffect above. Falls- // back to the cold-start requester before first populate. focusRequester = requesters[tile.packageName] ?: firstFocus, onPress = { onPressTile(tile.packageName) }, onEdit = { onOpenEditMenu(tile.packageName) },- onPickUp = { onPickUp(tile.packageName) }, onMove = { step -> onMoveKey(tile.packageName, step) },- onFinishMove = onFinishMove, ) } }
tvlauncher/src/main/java/com/vayunmathur/tvlauncher/ui/TvHomePage.kt
diff --git a/tvlauncher/src/main/java/com/vayunmathur/tvlauncher/ui/TvHomePage.kt b/tvlauncher/src/main/java/com/vayunmathur/tvlauncher/ui/TvHomePage.ktindex 3373dfc931..b48883bafb 100644--- a/tvlauncher/src/main/java/com/vayunmathur/tvlauncher/ui/TvHomePage.kt+++ b/tvlauncher/src/main/java/com/vayunmathur/tvlauncher/ui/TvHomePage.kt@@ -29,9 +29,9 @@ fun TvHomePage(viewModel: TvHomeViewModel) { onOpenEditMenu = viewModel::openEditMenu, onCloseEditMenu = viewModel::closeEditMenu, onStartMove = viewModel::startMove,- onFinishMove = viewModel::finishMove, onCancelMove = viewModel::cancelMove, onToggleReorder = viewModel::toggleReorder,+ onDisarmReorder = viewModel::disarmReorder, onMoveKey = viewModel::moveKey, onRemoveTile = viewModel::removeTile, onOpenAddMenu = viewModel::openAddMenu,
tvlauncher/src/main/java/com/vayunmathur/tvlauncher/ui/TvHomeScreen.kt
diff --git a/tvlauncher/src/main/java/com/vayunmathur/tvlauncher/ui/TvHomeScreen.kt b/tvlauncher/src/main/java/com/vayunmathur/tvlauncher/ui/TvHomeScreen.ktindex 3116679a3d..a77b5a3e4b 100644--- a/tvlauncher/src/main/java/com/vayunmathur/tvlauncher/ui/TvHomeScreen.kt+++ b/tvlauncher/src/main/java/com/vayunmathur/tvlauncher/ui/TvHomeScreen.kt@@ -16,6 +16,7 @@ import androidx.activity.compose.BackHandler import com.vayunmathur.library.ui.MaterialTheme import com.vayunmathur.library.ui.Text import com.vayunmathur.library.ui.TextButton+import com.vayunmathur.tvlauncher.ui.components.TvReorderHint // RAW SCAFFOLD EXCEPTION: TV launcher fullscreen grid, no app bar. @@ -40,9 +41,9 @@ fun TvHomeScreen( onOpenEditMenu: (String) -> Unit, onCloseEditMenu: () -> Unit, onStartMove: (String) -> Unit,- onFinishMove: () -> Unit, onCancelMove: () -> Unit, onToggleReorder: () -> Unit,+ onDisarmReorder: () -> Unit, onMoveKey: (String, Int) -> Unit, onRemoveTile: (String) -> Unit, onOpenAddMenu: (Int) -> Unit,@@ -56,12 +57,18 @@ fun TvHomeScreen( .fillMaxSize() .background(TvHomeBackground), ) {- // A home screen has nothing to go back to: Back closes overlays, or exits move mode- // without persisting, and is otherwise a no-op (the tv-detox precedent).+ // A home screen has nothing to go back to: Back closes overlays, cancels a hold, or+ // disarms reorder — and is otherwise a no-op (the tv-detox precedent). Order matters:+ // most specific first. BackHandler(enabled = state.editingKey != null) { onCloseEditMenu() } BackHandler(enabled = state.editingKey == null && state.movingKey != null) { onCancelMove() }+ BackHandler(+ enabled = state.editingKey == null && state.movingKey == null && state.reorderArmed,+ ) {+ onDisarmReorder()+ } Column( modifier = Modifier .fillMaxSize()@@ -71,29 +78,17 @@ fun TvHomeScreen( TvClockHeader( onOpenNetworkSettings = onOpenNetworkSettings, )- if (state.movingKey != null) {- Text(- text = "Arrows move it — OK drops it, Back cancels",- style = MaterialTheme.typography.labelLarge,- color = Color(0xA8FFFFFF),- modifier = Modifier.fillMaxWidth(),- )- } else if (state.reorderArmed) {- Text(- text = "OK on an app picks it up — Reorder again when done",- style = MaterialTheme.typography.labelLarge,- color = Color(0xA8FFFFFF),- modifier = Modifier.fillMaxWidth(),- )- }+ // Always composed at fixed height: hint swaps never move the grid.+ TvReorderHint(+ moving = state.movingKey != null,+ armed = state.reorderArmed,+ ) Box(modifier = Modifier.weight(1f).fillMaxWidth()) { TvAppGrid( state = state, onPressTile = onPressTile, onOpenEditMenu = onOpenEditMenu,- onPickUp = onStartMove, onMoveKey = onMoveKey,- onFinishMove = onFinishMove, ) } if (!state.loading) {
tvlauncher/src/main/java/com/vayunmathur/tvlauncher/ui/components/TvBannerTile.kt
diff --git a/tvlauncher/src/main/java/com/vayunmathur/tvlauncher/ui/components/TvBannerTile.kt b/tvlauncher/src/main/java/com/vayunmathur/tvlauncher/ui/components/TvBannerTile.ktindex 6db46d1790..d744cb5e22 100644--- a/tvlauncher/src/main/java/com/vayunmathur/tvlauncher/ui/components/TvBannerTile.kt+++ b/tvlauncher/src/main/java/com/vayunmathur/tvlauncher/ui/components/TvBannerTile.kt@@ -53,24 +53,25 @@ import kotlinx.coroutines.withContext /** Space under each tile reserved for the focus caption. */ val TvCaptionReserve = 28.dp -private val MoveFinishKeys = setOf(Key.Enter, Key.NumPadEnter, Key.DirectionCenter)- /** * One 16:9 banner tile. * * Three states, each unmistakable from a sofa: * - idle: art only, no ring, no caption; * - focused: black outer outline + white inner ring at the tile's own corner radius,- * caption underneath, slight lift. The black outline is what reads on white banners;- * the white ring is what reads on the dark background;- * - held (being moved): like focused, plus a persistent check badge and full caption —- * and every *other* tile dims, so the held one is the only bright thing on screen.+ * caption underneath. The black outline is what reads on white banners; the white ring+ * is what reads on the dark background;+ * - held (being moved): like focused but the ring is the theme primary, plus a MOVING+ * badge — and every *other* tile dims, so the held one is the only bright thing on+ * screen.+ *+ * Geometry never changes between states: the ring and badge are overlays and the caption+ * slot is always reserved, so focusing or picking up a tile cannot shift the list. *- * Geometry never changes between states: the ring is drawn inside a fixed outline reserve- * and the caption slot is always reserved, so focusing or picking up a tile cannot shift- * the list vertically. OK behaviour depends on [reorderArmed]: disarmed it presses through- * [onPress]; armed a long-OK picks the tile up ([onPickUp]) and a later OK drops it- * ([onFinishMove]).+ * OK handling is deliberately single-path: [clickable] → [onPress] → ViewModel `pressTile`+ * (pick up / drop / open depending on mode). There is no long-press: reorder mode already+ * disambiguates pick-up from open, and a hold would pick up on KeyDown then instantly drop+ * on the KeyUp click. The preview handler owns arrows, Back-passthrough and Menu only. */ @Composable fun TvBannerTile(@@ -78,14 +79,11 @@ fun TvBannerTile( tileWidth: Dp, artHeight: Dp, moving: Boolean,- reorderArmed: Boolean, dimmed: Boolean, focusRequester: FocusRequester?, onPress: () -> Unit, onEdit: () -> Unit,- onPickUp: () -> Unit, onMove: (Int) -> Unit,- onFinishMove: () -> Unit, ) { var focused by remember { mutableStateOf(false) } // No scale on focus: scaling changes painted bounds and pushes neighbours' captions.@@ -116,19 +114,6 @@ fun TvBannerTile( // pushes on a D-pad (first press focuses, second clicks). .clickable(onClick = onPress) .onPreviewKeyEvent { event ->- // Long-OK (held center) picks the tile up directly when reorder mode is- // armed; otherwise it opens the edit menu. One edge only: handling both- // edges double-fires.- if (reorderArmed && !moving) {- if (event.type != KeyEventType.KeyDown) return@onPreviewKeyEvent false- val held = event.key == Key.DirectionCenter || event.key == Key.Enter ||- event.key == Key.NumPadEnter- if (held && event.nativeKeyEvent.isLongPress) {- onPickUp()- return@onPreviewKeyEvent true- }- return@onPreviewKeyEvent false- } if (moving) { if (event.type != KeyEventType.KeyDown) return@onPreviewKeyEvent false when (event.key) {@@ -148,14 +133,12 @@ fun TvBannerTile( onMove(TvRowOrder.PER_ROW) true }- in MoveFinishKeys -> {- onFinishMove()- true- } // Back cancels the move; the screen's BackHandler owns it, so let // it through rather than swallowing it here. Key.Back -> false // Swallow the rest while moving so stray keys cannot open menus.+ // OK (Enter/DirectionCenter) is deliberately NOT handled here: the+ // click above is the single drop path. Key.Menu -> true else -> false }
tvlauncher/src/main/java/com/vayunmathur/tvlauncher/ui/components/TvReorderHint.kt
diff --git a/tvlauncher/src/main/java/com/vayunmathur/tvlauncher/ui/components/TvReorderHint.kt b/tvlauncher/src/main/java/com/vayunmathur/tvlauncher/ui/components/TvReorderHint.ktnew file mode 100644index 0000000000..f956ccc78a--- /dev/null+++ b/tvlauncher/src/main/java/com/vayunmathur/tvlauncher/ui/components/TvReorderHint.kt@@ -0,0 +1,48 @@+package com.vayunmathur.tvlauncher.ui.components++import androidx.compose.foundation.layout.Box+import androidx.compose.foundation.layout.fillMaxWidth+import androidx.compose.foundation.layout.height+import androidx.compose.runtime.Composable+import androidx.compose.ui.Alignment+import androidx.compose.ui.Modifier+import androidx.compose.ui.draw.alpha+import androidx.compose.ui.graphics.Color+import androidx.compose.ui.unit.dp+import com.vayunmathur.library.ui.MaterialTheme+import com.vayunmathur.library.ui.Text+import com.vayunmathur.library.ui.animatedFloat++/** Fixed slot for the reorder hint. Always composed at the same height. */+val TvHintSlot = 28.dp++/**+ * Contextual hint under the header. Always composed at [TvHintSlot] so showing or hiding+ * the text never moves the grid — the label cross-fades instead. Idle shows nothing+ * (alpha 0) rather than being removed from the tree.+ */+@Composable+fun TvReorderHint(+ moving: Boolean,+ armed: Boolean,+) {+ Box(+ modifier = Modifier+ .fillMaxWidth()+ .height(TvHintSlot),+ contentAlignment = Alignment.CenterStart,+ ) {+ val text = when {+ moving -> "Arrows move • OK drops • Back cancels"+ armed -> "OK picks up • Back done"+ else -> ""+ }+ Text(+ text = text,+ style = MaterialTheme.typography.labelLarge,+ color = Color(0xA8FFFFFF),+ maxLines = 1,+ modifier = Modifier.alpha(animatedFloat(if (text.isEmpty()) 0f else 1f)),+ )+ }+}
tvlauncher/src/test/java/com/vayunmathur/tvlauncher/domain/TvRowOrderTest.kt
diff --git a/tvlauncher/src/test/java/com/vayunmathur/tvlauncher/domain/TvRowOrderTest.kt b/tvlauncher/src/test/java/com/vayunmathur/tvlauncher/domain/TvRowOrderTest.ktindex 37b5449fd7..16e3f1da05 100644--- a/tvlauncher/src/test/java/com/vayunmathur/tvlauncher/domain/TvRowOrderTest.kt+++ b/tvlauncher/src/test/java/com/vayunmathur/tvlauncher/domain/TvRowOrderTest.kt@@ -53,6 +53,45 @@ class TvRowOrderTest { assertEquals(order, TvRowOrder.move(order, 9, 0)) } + @Test+ fun chainedMovesTrackHeldTile() {+ // Pick up "b" at 1, move right twice, back once: the held tile is found by value+ // each step (the ViewModel moveKey contract), ending where it belongs.+ var order = listOf("a", "b", "c", "d", "e", "f", "g")+ var held = "b"+ fun step(delta: Int) {+ val from = order.indexOf(held)+ val to = (from + delta).coerceIn(0, order.size - 1)+ order = TvRowOrder.move(order, from, to)+ }+ step(+1)+ assertEquals(listOf("a", "c", "b", "d", "e", "f", "g"), order)+ step(+1)+ assertEquals(listOf("a", "c", "d", "b", "e", "f", "g"), order)+ step(-1)+ assertEquals(listOf("a", "c", "b", "d", "e", "f", "g"), order)+ }++ @Test+ fun crossRowStepsMoveByFullRow() {+ val order = listOf("a", "b", "c", "d", "e", "f", "g")+ // Down from index 1 lands on index 1 + PER_ROW, clamped into range.+ val from = 1+ val to = (from + TvRowOrder.PER_ROW).coerceIn(0, order.size - 1)+ assertEquals(listOf("a", "c", "d", "e", "f", "g", "b"), TvRowOrder.move(order, from, to))+ // Up past the start clamps to 0.+ assertEquals(listOf("b", "a", "c"), TvRowOrder.move(listOf("a", "b", "c"), 1, -5))+ }++ @Test+ fun clampBeyondRangeIsNoOp() {+ val order = listOf("a", "b", "c")+ // Clamped to the end: a real move, landing last.+ assertEquals(listOf("b", "c", "a"), TvRowOrder.move(order, 0, 99))+ // Out-of-range source: untouched.+ assertEquals(order, TvRowOrder.move(order, -1, 2))+ }+ @Test fun replaceSwapsWithoutDuplicating() { val order = listOf("a", "b", "c")