appstore: host TV rail tabs as routes, not an index
- Commit
13d4371a80586894e4dbc79fdd3560cc7e094521- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-19 18:25 UTC (16 days ago)
- Parents
497c5f6
TvStoreRail swapped five full screens by a rememberSaveable selectedIndex inside one Main route - the implicit state machine class the Route rules exist to prevent (the file even carried a comment about the rail resetting behind detail pages). The five tabs are now sibling TvStoreRoutes, hosted by the route-backed TvAppFrame as on phone AppRoot and uniflix TV; rail selection derives from the back stack, cross-jumps use backStack.reset(Updates), pushed Browse/Detail stack above the tab route as before, and the Updates count badge is preserved. Verified: :appstore:testDevUnitTest (669 tests, 0 failures, incl. TvMainActivityTest rail drive and new route round-trips). D-pad focus across rapid tab switches still needs a real TV.
Diff
appstore/src/main/java/com/vayunmathur/appstore/ui/tv/TvMainActivity.kt
diff --git a/appstore/src/main/java/com/vayunmathur/appstore/ui/tv/TvMainActivity.kt b/appstore/src/main/java/com/vayunmathur/appstore/ui/tv/TvMainActivity.ktindex 0fa162831b..bb956539d2 100644--- a/appstore/src/main/java/com/vayunmathur/appstore/ui/tv/TvMainActivity.kt+++ b/appstore/src/main/java/com/vayunmathur/appstore/ui/tv/TvMainActivity.kt@@ -6,10 +6,7 @@ import com.vayunmathur.appstore.platform.TvStoreViewModelFactory import androidx.compose.runtime.Composable import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue-import androidx.compose.runtime.mutableIntStateOf import androidx.compose.runtime.remember-import androidx.compose.runtime.saveable.rememberSaveable-import androidx.compose.runtime.setValue import androidx.compose.ui.res.stringResource import androidx.lifecycle.viewmodel.compose.viewModel import com.vayunmathur.appstore.data.AppStoreDatabaseRepository@@ -18,7 +15,7 @@ import com.vayunmathur.appstore.data.UpdateCheckWorker import com.vayunmathur.library.tv.MaTvTheme import com.vayunmathur.library.tv.TvAccent import com.vayunmathur.library.tv.TvAppFrame-import com.vayunmathur.library.tv.TvDestination+import com.vayunmathur.library.tv.TvRouteDestination import com.vayunmathur.library.ui.IconDownload import com.vayunmathur.library.ui.IconHome import com.vayunmathur.library.ui.IconPackage@@ -28,28 +25,51 @@ import com.vayunmathur.library.ui.MaActivity import com.vayunmathur.library.util.MainNavigation import com.vayunmathur.library.util.NavBackStack import com.vayunmathur.library.util.NavKey+import com.vayunmathur.library.util.SiblingPage import com.vayunmathur.library.util.rememberNavBackStack import kotlinx.serialization.Serializable /**- * TV-only destinations: the rail tabs plus pushed browse and detail pages.+ * TV-only destinations: the five rail tabs as sibling routes, plus pushed+ * browse and detail pages. *- * A separate sealed interface from the phone `Route`, following youpipe's `TvRoute`, so- * a TV destination can never leak into the phone back stack or vice versa. Home, search,- * updates, library and sources are rail destinations — selecting one swaps the content,- * and each keeps its own screen so focus restores per destination.+ * A separate sealed interface from the phone `Route`, following uniflix's+ * `TvRoute`, so a TV destination can never leak into the phone back stack or+ * vice versa. Home, search, updates, library and sources are sibling rail+ * routes: selecting one resets the back stack to that route (see the+ * route-backed [TvAppFrame]), so the tab on screen is the back stack itself —+ * visible to BACK, deep links and process restore — and each tab keeps its+ * own entry so focus restores per destination. There is no `Main` wrapper:+ * [Home] is the start destination and the rail host is whichever tab entry+ * is on top. */ @Serializable sealed interface TvStoreRoute : NavKey {- /** The rail: home, search, updates, library, sources. D-pad only, no swipe. */+ /** Rail tab: store home (the start destination). */ @Serializable- data object Main : TvStoreRoute+ data object Home : TvStoreRoute - /** One home section's full catalogue, pushed over the rail. */+ /** Rail tab: search. */+ @Serializable+ data object Search : TvStoreRoute++ /** Rail tab: available updates. */+ @Serializable+ data object Updates : TvStoreRoute++ /** Rail tab: installed-app library. */+ @Serializable+ data object Library : TvStoreRoute++ /** Rail tab: store sources. */+ @Serializable+ data object Sources : TvStoreRoute++ /** One home section's full catalogue, pushed over the rail tab that opened it. */ @Serializable data class Browse(val sectionId: String) : TvStoreRoute - /** One app's page, pushed over the rail. */+ /** One app's page, pushed over the rail tab that opened it. */ @Serializable data class Detail(val packageName: String) : TvStoreRoute }@@ -93,26 +113,72 @@ class TvMainActivity : MaActivity() { /** * The TV navigation host: [rememberNavBackStack] + [MainNavigation] over [TvStoreRoute]. *- * The browse and detail destinations bind to their route arguments- * ([TvStoreRoute.Browse.sectionId] / [TvStoreRoute.Detail.packageName]);- * their entries load exactly the section/app they name, so BACK and a- * restored stack always land on the destination that opened them with no- * ViewModel selection behind it.+ * [TvStoreRoute.Home] is the start destination. The five rail tabs are+ * sibling entries (each [SiblingPage], so tab switches crossfade instead of+ * sliding like a push) hosted by the route-backed [TvAppFrame] in+ * [TvStoreRail]; browse and detail push over whichever tab opened them, so+ * BACK from them lands back on that tab. Their entries bind to their route+ * arguments ([TvStoreRoute.Browse.sectionId] /+ * [TvStoreRoute.Detail.packageName]) and load exactly the section/app they+ * name, so BACK and a restored stack always land on the destination that+ * opened them with no ViewModel selection behind it. */ @Composable private fun TvStoreNavigation(viewModel: TvStoreViewModel) {- val backStack = rememberNavBackStack<TvStoreRoute>(TvStoreRoute.Main)+ val backStack = rememberNavBackStack<TvStoreRoute>(TvStoreRoute.Home)++ fun openDetail(app: UnifiedApp) {+ if (backStack.last() != TvStoreRoute.Detail(app.packageName)) {+ backStack.add(TvStoreRoute.Detail(app.packageName))+ }+ }+ MainNavigation(backStack) {- entry<TvStoreRoute.Main> {- TvStoreRail(backStack = backStack, viewModel = viewModel)+ entry<TvStoreRoute.Home>(metadata = SiblingPage()) {+ TvStoreRail(backStack = backStack, viewModel = viewModel) {+ TvStoreHome(+ viewModel = viewModel,+ onAppClick = ::openDetail,+ // Cross-tab jump: a sibling switch, not a push — the+ // phone's AppRoot does the same via backStack.reset.+ onOpenUpdates = { backStack.reset(TvStoreRoute.Updates) },+ onOpenBrowse = { section ->+ backStack.add(TvStoreRoute.Browse(section.id))+ },+ )+ }+ }+ entry<TvStoreRoute.Search>(metadata = SiblingPage()) {+ TvStoreRail(backStack = backStack, viewModel = viewModel) {+ TvStoreSearch(+ viewModel = viewModel,+ onAppClick = ::openDetail,+ // The keyboard opens only on an explicit CENTER press on the field;+ // arriving via the rail never starts editing.+ startEditing = false,+ )+ }+ }+ entry<TvStoreRoute.Updates>(metadata = SiblingPage()) {+ TvStoreRail(backStack = backStack, viewModel = viewModel) {+ TvStoreUpdates(viewModel = viewModel, onAppClick = ::openDetail)+ }+ }+ entry<TvStoreRoute.Library>(metadata = SiblingPage()) {+ TvStoreRail(backStack = backStack, viewModel = viewModel) {+ TvStoreLibrary(viewModel = viewModel, onAppClick = ::openDetail)+ }+ }+ entry<TvStoreRoute.Sources>(metadata = SiblingPage()) {+ TvStoreRail(backStack = backStack, viewModel = viewModel) {+ TvStoreSources(viewModel = viewModel)+ } } entry<TvStoreRoute.Browse> { route -> TvStoreBrowse( viewModel = viewModel, sectionId = route.sectionId,- onAppClick = { app ->- backStack.add(TvStoreRoute.Detail(app.packageName))- },+ onAppClick = ::openDetail, onBack = { backStack.pop() }, ) }@@ -126,22 +192,23 @@ private fun TvStoreNavigation(viewModel: TvStoreViewModel) { } } -/** Rail destination indices, matching the destinations list order below. */-private const val TV_DEST_HOME = 0-private const val TV_DEST_SEARCH = 1-private const val TV_DEST_UPDATES = 2-private const val TV_DEST_LIBRARY = 3-private const val TV_DEST_SOURCES = 4- /**- * The store shell: a [TvAppFrame] rail (Home, Search, Updates with count, Library,- * Sources — the phone's bottom bar turned sideways) and the selected destination's- * screen beside it.+ * One rail tab's frame: the route-backed [TvAppFrame] rail (Home, Search,+ * Updates with count, Library, Sources — the phone's bottom bar turned+ * sideways) around [content]. Only composed while a rail tab is on top —+ * pushed browse/detail pages get no rail.+ *+ * Rail selection derives from the back stack's top route and focusing or+ * clicking a rail item resets the stack to that tab (sibling semantics, in+ * the library frame); the frame's `focusRestorer` per selected route is also+ * the library's. BACK on a root tab returns to the rail; BACK from the rail+ * exits. */ @Composable private fun TvStoreRail( backStack: NavBackStack<TvStoreRoute>, viewModel: TvStoreViewModel,+ content: @Composable () -> Unit, ) { val updates by viewModel.updatesUi.collectAsState() val updateCount = updates.updates.size@@ -155,50 +222,21 @@ private fun TvStoreRail( } val libraryLabel = stringResource(R.string.nav_library) val sourcesLabel = stringResource(R.string.nav_sources)- val destinations = remember(+ val destinations: List<TvRouteDestination<TvStoreRoute>> = remember( homeLabel, searchLabel, updatesLabel, libraryLabel, sourcesLabel ) { listOf(- TvDestination(homeLabel) { IconHome() },- TvDestination(searchLabel) { IconSearch() },- TvDestination(updatesLabel) { IconDownload() },- TvDestination(libraryLabel) { IconPackage() },- TvDestination(sourcesLabel) { IconProvider() },+ TvRouteDestination(TvStoreRoute.Home, homeLabel) { IconHome() },+ TvRouteDestination(TvStoreRoute.Search, searchLabel) { IconSearch() },+ TvRouteDestination(TvStoreRoute.Updates, updatesLabel) { IconDownload() },+ TvRouteDestination(TvStoreRoute.Library, libraryLabel) { IconPackage() },+ TvRouteDestination(TvStoreRoute.Sources, sourcesLabel) { IconProvider() }, ) }- // Survives returning from a pushed route: without saveable the rail reset to- // Home behind every detail page, so BACK landed on the wrong destination.- var selectedIndex by rememberSaveable { mutableIntStateOf(0) }-- fun openDetail(app: UnifiedApp) {- backStack.add(TvStoreRoute.Detail(app.packageName))- } TvAppFrame(+ backStack = backStack, destinations = destinations,- selected = selectedIndex,- onSelect = { selectedIndex = it }, backReturnsToRail = backStack.backStack.size <= 1,- ) {- when (selectedIndex) {- TV_DEST_SEARCH -> TvStoreSearch(- viewModel = viewModel,- onAppClick = ::openDetail,- // The keyboard opens only on an explicit CENTER press on the field;- // arriving via the rail never starts editing.- startEditing = false,- )- TV_DEST_UPDATES -> TvStoreUpdates(viewModel = viewModel, onAppClick = ::openDetail)- TV_DEST_LIBRARY -> TvStoreLibrary(viewModel = viewModel, onAppClick = ::openDetail)- TV_DEST_SOURCES -> TvStoreSources(viewModel = viewModel)- else -> TvStoreHome(- viewModel = viewModel,- onAppClick = ::openDetail,- onOpenUpdates = { selectedIndex = TV_DEST_UPDATES },- onOpenBrowse = { section ->- backStack.add(TvStoreRoute.Browse(section.id))- },- )- }- }+ ) { content() } }
appstore/src/test/java/com/vayunmathur/appstore/RouteSerializerTest.kt
diff --git a/appstore/src/test/java/com/vayunmathur/appstore/RouteSerializerTest.kt b/appstore/src/test/java/com/vayunmathur/appstore/RouteSerializerTest.ktindex b361cf7e56..2c6e0e0ef9 100644--- a/appstore/src/test/java/com/vayunmathur/appstore/RouteSerializerTest.kt+++ b/appstore/src/test/java/com/vayunmathur/appstore/RouteSerializerTest.kt@@ -71,12 +71,15 @@ class RouteSerializerTest { json.encodeToString(TvStoreRoute.serializer(), browse), ), )- assertEquals(- TvStoreRoute.Main,- json.decodeFromString(+ for (route in listOf<TvStoreRoute>(+ TvStoreRoute.Home, TvStoreRoute.Search, TvStoreRoute.Updates,+ TvStoreRoute.Library, TvStoreRoute.Sources,+ )) {+ val restored = json.decodeFromString( TvStoreRoute.serializer(),- json.encodeToString(TvStoreRoute.serializer(), TvStoreRoute.Main),- ),- )+ json.encodeToString(TvStoreRoute.serializer(), route),+ )+ assertEquals(route, restored)+ } } }