music: tabs are sibling routes (#nav)
- Commit
80d1ecd66988271318b7c062d6dc78e40de673ca- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-16 20:00 UTC (19 days ago)
- Parents
dc3a7f8
Convert the four pager tabs (Songs/Albums/Artists/Playlists) from rememberPagerState inside single Route.Home to sibling Route.Home/Albums/Artists/Playlists destinations registered with ListPage()+SiblingPage(). MusicTabsScreen now uses the route-backed TabbedPagerScaffold (RouteTab) whose selection does backStack.reset(route); the deliberate 'NOT in the nav backstack' design is removed. Detail/dialog routes keep Morph/ListDetail/Dialog metadata and push over the current tab. The library refresh/SyncWorker kickoff is hoisted to Navigation so it does not refire on tab switch; now-playing leadingBottomBar and song-morph ownership are preserved. Deep links restore to Route.Home. Verified: ./gradlew :music:testDevUnitTest :music:compileDevKotlin green.
Diff
music/src/main/java/com/vayunmathur/music/Navigation.kt
diff --git a/music/src/main/java/com/vayunmathur/music/Navigation.kt b/music/src/main/java/com/vayunmathur/music/Navigation.ktindex 49ae3dbd4e..4fe026c2b9 100644--- a/music/src/main/java/com/vayunmathur/music/Navigation.kt+++ b/music/src/main/java/com/vayunmathur/music/Navigation.kt@@ -1,13 +1,17 @@ package com.vayunmathur.music import androidx.compose.runtime.Composable+import androidx.compose.runtime.LaunchedEffect+import androidx.compose.ui.platform.LocalContext import com.vayunmathur.library.util.DialogPage import com.vayunmathur.library.util.ListDetailPage import com.vayunmathur.library.util.ListPage import com.vayunmathur.library.util.MainNavigation import com.vayunmathur.library.util.MorphPage+import com.vayunmathur.library.util.SiblingPage import com.vayunmathur.library.util.rememberNavBackStack import com.vayunmathur.music.platform.MusicViewModel+import com.vayunmathur.music.platform.SyncWorker import com.vayunmathur.music.ui.AlbumDetailScreen import com.vayunmathur.music.ui.ArtistDetailScreen import com.vayunmathur.music.ui.MusicTabsScreen@@ -17,10 +21,37 @@ import com.vayunmathur.music.ui.dialogs.AddToPlaylistDialog @Composable fun Navigation(musicViewModel: MusicViewModel) {+ val context = LocalContext.current++ // Single kickoff for all four tabs, hoisted out of MusicTabsScreen now+ // that each tab is its own entry (doing it per-tab would fire it on+ // every tab switch).+ //+ // The refresh runs here rather than being left to WorkManager because this is the first point+ // at which the audio permission is definitely granted - PermissionsChecker gates the whole+ // navigation graph - and because going through the scheduler would put the list behind a job+ // dispatch for no reason. `enqueue` only registers the content-URI triggers that notice later+ // changes to the library.+ LaunchedEffect(Unit) {+ musicViewModel.refreshLibrary()+ SyncWorker.enqueue(context)+ }++ // Start destination is Route.Home (the Songs tab): deep links and+ // process restore land there unless the retained stack says otherwise. val backStack = rememberNavBackStack<Route>(Route.Home) MainNavigation(backStack) {- entry<Route.Home>(metadata = ListPage()) {- MusicTabsScreen(backStack, musicViewModel)+ entry<Route.Home>(metadata = ListPage() + SiblingPage()) {+ MusicTabsScreen(backStack, musicViewModel, Route.Home)+ }+ entry<Route.Albums>(metadata = ListPage() + SiblingPage()) {+ MusicTabsScreen(backStack, musicViewModel, Route.Albums)+ }+ entry<Route.Artists>(metadata = ListPage() + SiblingPage()) {+ MusicTabsScreen(backStack, musicViewModel, Route.Artists)+ }+ entry<Route.Playlists>(metadata = ListPage() + SiblingPage()) {+ MusicTabsScreen(backStack, musicViewModel, Route.Playlists) } entry<Route.Song>(metadata = MorphPage()) { SongScreen(backStack, musicViewModel)
music/src/main/java/com/vayunmathur/music/Route.kt
diff --git a/music/src/main/java/com/vayunmathur/music/Route.kt b/music/src/main/java/com/vayunmathur/music/Route.ktindex 6077401720..402ca979aa 100644--- a/music/src/main/java/com/vayunmathur/music/Route.kt+++ b/music/src/main/java/com/vayunmathur/music/Route.kt@@ -10,6 +10,18 @@ sealed interface Route : NavKey { @Serializable data object Home : Route + /** Albums tab: sibling of [Home], switched with `backStack.reset`. */+ @Serializable+ data object Albums : Route++ /** Artists tab: sibling of [Home], switched with `backStack.reset`. */+ @Serializable+ data object Artists : Route++ /** Playlists tab: sibling of [Home], switched with `backStack.reset`. */+ @Serializable+ data object Playlists : Route+ /** Singleton holding song state and operations. */ @Serializable data object Song : Route
music/src/main/java/com/vayunmathur/music/ui/MusicTabsScreen.kt
diff --git a/music/src/main/java/com/vayunmathur/music/ui/MusicTabsScreen.kt b/music/src/main/java/com/vayunmathur/music/ui/MusicTabsScreen.ktindex 3bfd19471c..c0e281caa6 100644--- a/music/src/main/java/com/vayunmathur/music/ui/MusicTabsScreen.kt+++ b/music/src/main/java/com/vayunmathur/music/ui/MusicTabsScreen.kt@@ -1,65 +1,50 @@ package com.vayunmathur.music.ui -import androidx.compose.foundation.layout.Column-import androidx.compose.foundation.layout.WindowInsets-import androidx.compose.foundation.layout.consumeWindowInsets-import androidx.compose.foundation.layout.fillMaxSize-import androidx.compose.foundation.layout.navigationBars-import androidx.compose.foundation.pager.HorizontalPager-import androidx.compose.foundation.pager.rememberPagerState import androidx.compose.runtime.Composable-import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember-import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.setValue-import androidx.compose.ui.Modifier-import androidx.compose.ui.platform.LocalContext+import androidx.compose.ui.res.stringResource+import com.vayunmathur.library.ui.IconAlbum+import com.vayunmathur.library.ui.IconLibraryMusic+import com.vayunmathur.library.ui.IconPerson+import com.vayunmathur.library.ui.R as UiR+import com.vayunmathur.library.ui.RouteTab+import com.vayunmathur.library.ui.TabbedPagerScaffold import com.vayunmathur.library.util.NavBackStack import com.vayunmathur.library.util.isNavLeaving-import com.vayunmathur.library.util.sinksBelow+import com.vayunmathur.music.R import com.vayunmathur.music.Route import com.vayunmathur.music.platform.MusicViewModel-import com.vayunmathur.music.platform.SyncWorker-import com.vayunmathur.music.ui.components.MusicTabsBar import com.vayunmathur.music.ui.components.PlayingBottomBar-import kotlinx.coroutines.launch /**- * Hosts the four main tabs (Songs / Albums / Artists / Playlists) in a swipeable- * pager, with the now-playing controls and the tab bar pinned across all four.+ * Hosts one of the four main tabs (Songs / Albums / Artists / Playlists) with+ * the now-playing controls and the tab bar / rail pinned across all four. *- * A plain [Column] rather than a scaffold: each tab page brings its own, and- * `MainNavigation` already owns the outer one, so a third would only nest.+ * Tabs are sibling [Route]s, not pager pages: each tab is registered as its+ * own entry in `Navigation` with `SiblingPage()` metadata, and the+ * route-backed [TabbedPagerScaffold] switches between them with+ * `backStack.reset(tab.route)` on tab select — so back, deep links (the+ * start destination is [Route.Home]) and process restore see the real+ * screen, and a detail pushed over a tab (tap an album → AlbumDetail →+ * back) returns to that tab. [currentTab] is the route this instance was+ * registered for; it, not the (possibly detail) top of the stack, decides+ * which tab's content renders, so a list pane beside a detail still shows+ * its tab. Per-tab `rememberSaveable` state (search, scroll) lives in the+ * tab contents themselves and survives via their entry's saved state. *- * Tab selection lives in the pager's own state, NOT in the nav backstack - so deep- * navigation (tap an album → AlbumDetail → back) returns the user to whatever tab- * they were on, scroll position intact.+ * The library refresh kickoff lives in `Navigation`, not here: there is+ * one instance of this host per tab entry, and doing it here would fire+ * it on every tab switch. */ @Composable fun MusicTabsScreen( backStack: NavBackStack<Route>, musicViewModel: MusicViewModel,+ currentTab: Route = Route.Home, ) {- val context = LocalContext.current-- // Single kickoff for all four tabs (the pager composes them lazily, so doing this per-tab- // would fire it four times).- //- // The refresh runs here rather than being left to WorkManager because this is the first point- // at which the audio permission is definitely granted - PermissionsChecker gates the whole- // navigation graph - and because going through the scheduler would put the list behind a job- // dispatch for no reason. `enqueue` only registers the content-URI triggers that notice later- // changes to the library.- LaunchedEffect(Unit) {- musicViewModel.refreshLibrary()- SyncWorker.enqueue(context)- }-- val pagerState = rememberPagerState(pageCount = { 4 })- val scope = rememberCoroutineScope()- // Which element the song morph should use as its counterpart. // // The graph loops here: a row tap carries the song up to the player, but coming back the song@@ -72,43 +57,35 @@ fun MusicTabsScreen( val leaving = isNavLeaving() val barOwnsSongKeys = !leaving || !tappedSongRow - Column(Modifier.fillMaxSize()) {- HorizontalPager(- state = pagerState,- // The bars below already cover the navigation bar, so a page's own- // scaffold must not inset for it again and leave a gap.- modifier = Modifier- .weight(1f)- .consumeWindowInsets(WindowInsets.navigationBars),- ) { page ->- when (page) {- 0 -> HomeTabContent(- backStack,- musicViewModel,- rowOwnsSongKeys = leaving && tappedSongRow,- onSongTapped = { tappedSongRow = true },- )- 1 -> AlbumsTabContent(backStack, musicViewModel)- 2 -> ArtistsTabContent(backStack, musicViewModel)- else -> PlaylistsTabContent(backStack, musicViewModel)- }+ TabbedPagerScaffold(+ backStack = backStack,+ tabs = listOf(+ RouteTab(Route.Home, stringResource(UiR.string.home)) { IconLibraryMusic() },+ RouteTab(Route.Albums, stringResource(R.string.label_albums)) { IconAlbum() },+ RouteTab(Route.Artists, stringResource(R.string.nav_artists)) { IconPerson() },+ RouteTab(Route.Playlists, stringResource(R.string.nav_playlists)) { IconLibraryMusic() },+ ),+ leadingBottomBar = {+ PlayingBottomBar(+ musicViewModel,+ backStack,+ owsSharedKeys = barOwnsSongKeys,+ onOpen = { tappedSongRow = false; backStack.add(Route.Song) },+ )+ },+ ) {+ // Ignore the scaffold's top-of-stack route: on a detail destination+ // this host is the list pane underneath, and must keep showing its tab.+ when (currentTab) {+ Route.Albums -> AlbumsTabContent(backStack, musicViewModel)+ Route.Artists -> ArtistsTabContent(backStack, musicViewModel)+ Route.Playlists -> PlaylistsTabContent(backStack, musicViewModel)+ else -> HomeTabContent(+ backStack,+ musicViewModel,+ rowOwnsSongKeys = leaving && tappedSongRow,+ onSongTapped = { tappedSongRow = true },+ ) }- PlayingBottomBar(- musicViewModel,- backStack,- owsSharedKeys = barOwnsSongKeys,- onOpen = { tappedSongRow = false; backStack.add(Route.Song) },- )- MusicTabsBar(- selectedTab = pagerState.currentPage,- onSelectTab = { index ->- if (pagerState.currentPage != index) {- scope.launch { pagerState.animateScrollToPage(index) }- }- },- // The bar belongs to Home, not to the detail pages Home pushes, so it leaves the way it- // would be dismissed rather than dissolving where it sits.- modifier = Modifier.sinksBelow(),- ) } }
music/src/test/java/com/vayunmathur/music/NavigationRouteTest.kt
diff --git a/music/src/test/java/com/vayunmathur/music/NavigationRouteTest.kt b/music/src/test/java/com/vayunmathur/music/NavigationRouteTest.ktindex 1ed449c578..261d508ce9 100644--- a/music/src/test/java/com/vayunmathur/music/NavigationRouteTest.kt+++ b/music/src/test/java/com/vayunmathur/music/NavigationRouteTest.kt@@ -36,7 +36,7 @@ class NavigationRouteTest { @Test fun routesSerialiseAndRoundTrip() { val routes: List<Route> = listOf(- Route.Home, Route.Song, Route.AlbumDetail(7L), Route.ArtistDetail(11L),+ Route.Home, Route.Albums, Route.Artists, Route.Playlists, Route.Song, Route.AlbumDetail(7L), Route.ArtistDetail(11L), Route.PlaylistDetail(3L), Route.AddToPlaylistDialog(42L)) for (route in routes) { val encoded = Json.encodeToString(Route.serializer(), route)