Skip to content

Commit

Permalink
refactor: Create a sub compose function for navigation icon and label
Browse files Browse the repository at this point in the history
  • Loading branch information
sirambd committed Jul 25, 2024
1 parent 5e85ba0 commit f0dcc40
Showing 1 changed file with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,14 @@ private fun MainScaffold(
navController: NavHostController,
content: @Composable () -> Unit,
) {
val isNavigationBar = navType == NavigationSuiteType.NavigationBar

NavigationSuiteScaffold(
navigationSuiteItems = {
NavigationItem.entries.forEach { navigationItem ->
item(
icon = {
val contentDescription = when (navType) {
NavigationSuiteType.NavigationRail -> stringResource(navigationItem.label)
else -> ""
}
Icon(navigationItem.icon, contentDescription)
},
label = {
if (navType == NavigationSuiteType.NavigationBar) {
Text(stringResource(navigationItem.label))
}
},
icon = { NavigationIcon(isNavigationBar, navigationItem) },
label = { NavigationLabel(isNavigationBar, navigationItem) },
selected = currentDestination == navigationItem.destination,
onClick = { navController.navigateToSelectedItem(navigationItem.destination) }
)
Expand All @@ -92,6 +84,19 @@ private fun MainScaffold(
)
}

@Composable
private fun NavigationIcon(isNavigationBar: Boolean, navigationItem: NavigationItem) {
val contentDescription = if (isNavigationBar) null else stringResource(navigationItem.label)
Icon(navigationItem.icon, contentDescription)
}

@Composable
private fun NavigationLabel(isNavigationBar: Boolean, navigationItem: NavigationItem) {
if (isNavigationBar) {
Text(stringResource(navigationItem.label))
}
}

private fun NavHostController.navigateToSelectedItem(destination: MainNavigation) {
navigate(destination) {
// Pop up to the start destination of the graph to avoid building up a large stack of destinations
Expand Down

0 comments on commit f0dcc40

Please sign in to comment.