Skip to content

Commit

Permalink
Refactor Navigation related codes, Change TopBar title
Browse files Browse the repository at this point in the history
  • Loading branch information
jhg3410 committed Jul 6, 2023
1 parent 82c824d commit 1a45925
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.jik.movie.navigation

import androidx.compose.ui.graphics.vector.ImageVector
import androidx.navigation.NavDestination
import com.jik.core.designsystem.icon.MovieIcons
import com.jik.core.ui.R
import com.jik.feature.home.navigation.HomeNavigation
import com.jik.feature.popular.navigation.PopularNavigation
import com.jik.feature.home.R as homeR
Expand All @@ -13,22 +13,21 @@ enum class TopLevelDestination(
val route: String,
val selectedIcon: ImageVector,
val unselectedIcon: ImageVector,
val iconTextId: Int
val iconTextId: Int,
val titleTextId: Int,
) {
HOME(
route = HomeNavigation.route,
selectedIcon = MovieIcons.HomeRounded,
unselectedIcon = MovieIcons.HomeRounded,
iconTextId = homeR.string.home
iconTextId = homeR.string.home,
titleTextId = R.string.app_name
),
POPULAR(
route = PopularNavigation.route,
selectedIcon = MovieIcons.LocalFireDepartmentRounded,
unselectedIcon = MovieIcons.LocalFireDepartmentRounded,
iconTextId = popularR.string.popular
iconTextId = popularR.string.popular,
titleTextId = popularR.string.popular
)
}

fun TopLevelDestination.isCurrentDestination(navDestination: NavDestination?): Boolean {
return this.route == (navDestination?.route ?: false)
}
24 changes: 11 additions & 13 deletions app/src/main/java/com/jik/movie/ui/MovieApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.unit.dp
import androidx.navigation.NavDestination
import com.jik.core.designsystem.component.*
import com.jik.core.designsystem.theme.MovieTheme
import com.jik.core.ui.R
import com.jik.movie.navigation.MovieNavHost
import com.jik.movie.navigation.TopLevelDestination
import com.jik.movie.navigation.isCurrentDestination

@OptIn(ExperimentalMaterial3Api::class)
@Composable
Expand All @@ -22,23 +19,24 @@ fun MovieApp() {
) {
val appState = rememberMovieAppState()
val scrollBehavior = appState.topAppBarScrollBehavior
val destination = appState.currentTopLevelDestination

Scaffold(
modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection),
topBar = {
if (appState.isTopLevelDestination) {
if (destination != null) {
MovieTopAppBar(
titleRes = R.string.app_name,
titleRes = destination.titleTextId,
scrollBehavior = scrollBehavior
)
}
},
bottomBar = {
if (appState.isTopLevelDestination) {
if (destination != null) {
MovieBottomBar(
destination = appState.topLevelDestinations,
topLevelDestination = appState.topLevelDestinations,
currentDestination = destination,
onNavigateToDestination = appState::navigateToDestination,
currentDestination = appState.currentDestination
)
}
}
Expand All @@ -50,7 +48,7 @@ fun MovieApp() {
navController = appState.navController,
modifier = Modifier.padding(
top = topPadding,
bottom = if (appState.isTopLevelDestination && bottomPadding > 0.dp) bottomPadding - NavigationBarCornerSize
bottom = if (destination != null && bottomPadding > 0.dp) bottomPadding - NavigationBarCornerSize
else bottomPadding
),
ExpandTopBar = {
Expand All @@ -65,16 +63,16 @@ fun MovieApp() {

@Composable
fun MovieBottomBar(
destination: List<TopLevelDestination>,
currentDestination: NavDestination?,
topLevelDestination: List<TopLevelDestination>,
currentDestination: TopLevelDestination,
modifier: Modifier = Modifier,
onNavigateToDestination: (TopLevelDestination) -> Unit,
) {
MovieNavigationBar(
modifier = modifier,
content = {
destination.forEach { destination ->
val selected = destination.isCurrentDestination(currentDestination)
topLevelDestination.forEach { destination ->
val selected = destination.route == currentDestination.route
MovieNavigationBarItem(
selected = selected,
onClick = { onNavigateToDestination(destination) },
Expand Down
25 changes: 15 additions & 10 deletions app/src/main/java/com/jik/movie/ui/MovieAppState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ import androidx.navigation.NavHostController
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navOptions
import com.jik.feature.home.navigation.HomeNavigation
import com.jik.feature.home.navigation.HomeNavigation.navigateHome
import com.jik.feature.popular.navigation.PopularNavigation
import com.jik.feature.popular.navigation.PopularNavigation.navigatePopular
import com.jik.movie.navigation.TopLevelDestination
import com.jik.movie.navigation.TopLevelDestination.HOME
import com.jik.movie.navigation.TopLevelDestination.POPULAR

@Composable
fun rememberMovieAppState(
Expand All @@ -29,18 +33,19 @@ class MovieAppState(
val navController: NavHostController,
) {

val currentDestination: NavDestination?
@Composable get() = navController.currentBackStackEntryAsState().value?.destination

private val topLevelDestinationRoutes = TopLevelDestination.values().map { it.route }

@OptIn(ExperimentalMaterial3Api::class)
val topAppBarScrollBehavior
@Composable get() = TopAppBarDefaults.enterAlwaysScrollBehavior()

val isTopLevelDestination: Boolean
@Composable get() = navController
.currentBackStackEntryAsState().value?.destination?.route in topLevelDestinationRoutes
private val currentDestination: NavDestination?
@Composable get() = navController.currentBackStackEntryAsState().value?.destination

val currentTopLevelDestination: TopLevelDestination?
@Composable get() = when (currentDestination?.route) {
HomeNavigation.route -> HOME
PopularNavigation.route -> POPULAR
else -> null
}

val topLevelDestinations: List<TopLevelDestination> = TopLevelDestination.values().toList()

Expand All @@ -53,8 +58,8 @@ class MovieAppState(
restoreState = true
}
when (topLevelDestination) {
TopLevelDestination.HOME -> navController.navigateHome(navOptions)
TopLevelDestination.POPULAR -> navController.navigatePopular(navOptions)
HOME -> navController.navigateHome(navOptions)
POPULAR -> navController.navigatePopular(navOptions)
}
}
}

0 comments on commit 1a45925

Please sign in to comment.