Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add back button in nested screen in demo #348

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
package ch.srgssr.pillarbox.demo.ui

import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.Scaffold
Expand All @@ -19,6 +22,7 @@ import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.core.content.res.ResourcesCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.repeatOnLifecycle
import androidx.lifecycle.viewmodel.compose.viewModel
Expand All @@ -37,7 +41,6 @@ import androidx.navigation.compose.rememberNavController
import androidx.navigation.navigation
import ch.srgssr.pillarbox.analytics.SRGAnalytics
import ch.srgssr.pillarbox.demo.DemoPageView
import ch.srgssr.pillarbox.demo.R
import ch.srgssr.pillarbox.demo.shared.data.DemoItem
import ch.srgssr.pillarbox.demo.shared.di.PlayerModule
import ch.srgssr.pillarbox.demo.shared.ui.HomeDestination
Expand All @@ -51,6 +54,8 @@ import ch.srgssr.pillarbox.demo.ui.player.SimplePlayerActivity
import ch.srgssr.pillarbox.demo.ui.showcases.showCasesNavGraph

private val bottomNavItems = listOf(HomeDestination.Examples, HomeDestination.ShowCases, HomeDestination.Lists, HomeDestination.Search)
private val topLevelRoutes =
listOf(HomeDestination.Examples.route, NavigationRoutes.showcaseList, NavigationRoutes.contentLists, HomeDestination.Search)

/**
* Main view with all the navigation
Expand All @@ -62,10 +67,27 @@ fun MainNavigation() {
val navController = rememberNavController()
val navBackStackEntry by navController.currentBackStackEntryAsState()
val currentDestination = navBackStackEntry?.destination

Scaffold(
topBar = {
TopAppBar(title = { Text(text = stringResource(id = currentDestination.getLabelResId())) })
TopAppBar(
title = {
currentDestination?.let {
Text(text = stringResource(id = currentDestination.getLabelResId()))
}
},
navigationIcon = {
currentDestination?.route?.let {
if (!topLevelRoutes.contains(it)) {
IconButton(onClick = { navController.navigateUp() }) {
Icon(
imageVector = Icons.Default.ArrowBack,
contentDescription = stringResource(androidx.appcompat.R.string.abc_action_bar_up_description)
)
}
}
}
}
)
},
bottomBar = {
DemoBottomNavigation(navController = navController, currentDestination = currentDestination)
Expand Down Expand Up @@ -139,16 +161,10 @@ private fun DemoBottomNavigation(navController: NavController, currentDestinatio
}
}

private fun NavDestination?.getLabelResId(): Int {
val navItem: HomeDestination? = this?.let {
for (item in bottomNavItems) {
if (hierarchy.any { it.route == item.route }) {
return@let item
}
}
null
}
return navItem?.labelResId ?: R.string.app_name
private fun NavDestination.getLabelResId(): Int {
val routes = hierarchy.map { it.route }
val navItem: HomeDestination? = bottomNavItems.firstOrNull { it.route in routes }
return navItem?.labelResId ?: ResourcesCompat.ID_NULL
}

/**
Expand Down