mirror of
https://github.com/msfjarvis/compose-lobsters
synced 2025-08-17 23:47:02 +05:30
ui: fix backstack behavior
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
parent
2b2acd1311
commit
39d36eece6
2 changed files with 16 additions and 11 deletions
|
@ -54,17 +54,16 @@ class MainActivity : AppCompatActivity() {
|
||||||
fun LobstersApp() {
|
fun LobstersApp() {
|
||||||
val viewModel: LobstersViewModel = viewModel()
|
val viewModel: LobstersViewModel = viewModel()
|
||||||
val navController = rememberNavController()
|
val navController = rememberNavController()
|
||||||
val destinations = arrayOf(Destination.Hottest, Destination.Saved)
|
|
||||||
val hottestPosts by viewModel.posts.collectAsState()
|
val hottestPosts by viewModel.posts.collectAsState()
|
||||||
val savedPosts by viewModel.savedPosts.collectAsState()
|
val savedPosts by viewModel.savedPosts.collectAsState()
|
||||||
|
|
||||||
Scaffold(
|
Scaffold(
|
||||||
bottomBar = {
|
bottomBar = {
|
||||||
LobstersBottomNav(navController, destinations)
|
LobstersBottomNav(navController)
|
||||||
},
|
},
|
||||||
) { innerPadding ->
|
) { innerPadding ->
|
||||||
val hottestPostsListState = rememberLazyListState()
|
val hottestPostsListState = rememberLazyListState()
|
||||||
NavHost(navController, startDestination = Destination.Hottest.route) {
|
NavHost(navController, startDestination = Destination.startDestination.route) {
|
||||||
composable(Destination.Hottest.route) {
|
composable(Destination.Hottest.route) {
|
||||||
HottestPosts(
|
HottestPosts(
|
||||||
posts = hottestPosts,
|
posts = hottestPosts,
|
||||||
|
@ -88,20 +87,21 @@ fun LobstersApp() {
|
||||||
@Composable
|
@Composable
|
||||||
fun LobstersBottomNav(
|
fun LobstersBottomNav(
|
||||||
navController: NavHostController,
|
navController: NavHostController,
|
||||||
destinations: Array<Destination>,
|
|
||||||
) {
|
) {
|
||||||
BottomNavigation {
|
BottomNavigation {
|
||||||
val navBackStackEntry by navController.currentBackStackEntryAsState()
|
val navBackStackEntry by navController.currentBackStackEntryAsState()
|
||||||
val currentRoute = navBackStackEntry?.arguments?.getString(KEY_ROUTE)
|
val currentRoute =
|
||||||
destinations.forEach { screen ->
|
navBackStackEntry?.arguments?.getString(KEY_ROUTE) ?: Destination.startDestination.route
|
||||||
|
Destination.values().forEach { screen ->
|
||||||
BottomNavigationItem(
|
BottomNavigationItem(
|
||||||
icon = { IconResource(resourceId = screen.badgeRes) },
|
icon = { IconResource(resourceId = screen.badgeRes) },
|
||||||
label = { Text(stringResource(id = screen.labelRes)) },
|
label = { Text(stringResource(id = screen.labelRes)) },
|
||||||
selected = currentRoute == screen.route,
|
selected = currentRoute == screen.route,
|
||||||
alwaysShowLabels = false,
|
alwaysShowLabels = false,
|
||||||
onClick = {
|
onClick = {
|
||||||
if (currentRoute != screen.route) {
|
if (currentRoute == screen.route) return@BottomNavigationItem
|
||||||
navController.popBackStack(navController.graph.startDestination, false)
|
navController.popBackStack(navController.graph.startDestination, false)
|
||||||
|
if (screen.route != Destination.startDestination.route) {
|
||||||
navController.navigate(screen.route)
|
navController.navigate(screen.route)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,11 +7,16 @@ import dev.msfjarvis.lobsters.R
|
||||||
/**
|
/**
|
||||||
* Destinations for navigation within the app.
|
* Destinations for navigation within the app.
|
||||||
*/
|
*/
|
||||||
sealed class Destination(
|
enum class Destination(
|
||||||
val route: String,
|
val route: String,
|
||||||
@StringRes val labelRes: Int,
|
@StringRes val labelRes: Int,
|
||||||
@DrawableRes val badgeRes: Int,
|
@DrawableRes val badgeRes: Int,
|
||||||
) {
|
) {
|
||||||
object Hottest : Destination("hottest", R.string.hottest_posts, R.drawable.ic_whatshot_24px)
|
Hottest("hottest", R.string.hottest_posts, R.drawable.ic_whatshot_24px),
|
||||||
object Saved : Destination("saved", R.string.saved_posts, R.drawable.ic_favorite_24px)
|
Saved("saved", R.string.saved_posts, R.drawable.ic_favorite_24px),
|
||||||
|
;
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
val startDestination = Hottest
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue