Coverage Summary for Class: NavigationItem (debug.com.greybox.projectmesh.navigation)
| Class |
Class, %
|
Method, %
|
Branch, %
|
Line, %
|
Instruction, %
|
| NavigationItem |
0%
(0/1)
|
0%
(0/1)
|
|
0%
(0/4)
|
0%
(0/17)
|
package com.greybox.projectmesh.navigation
import androidx.compose.runtime.Composable
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.NavHostController
import androidx.compose.material3.*
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.sp
import androidx.navigation.compose.rememberNavController
import com.greybox.projectmesh.R
/**
* Represents a single item inside the bottom navigation bar.
*
* @param route The navigation route associated with this item.
* @param label The text label shown beneath the icon.
* @param icon The vector icon displayed for this item.
*/
data class NavigationItem(
val route: String,
val label: String,
val icon: ImageVector,
)
//Preview is to show the bottom navigation bar in the preview and notice what it looks like
/**
* Preview for displaying the bottom navigation bar inside the design tools.
*/
@Preview(showBackground = true)
@Composable
fun BottomNavigationBarPreview() {
val navController = rememberNavController()
BottomNavigationBar(navController = navController)
}
/**
* Displays the application's bottom navigation bar.
*
* Automatically highlights the currently selected destination and handles
* navigation state restoration and avoiding duplicate destinations.
*
* @param navController The controller used to perform navigation actions.
*/
@Composable
fun BottomNavigationBar(navController: NavHostController) {
val items = listOf(
NavigationItem(BottomNavItem.Home.route, stringResource(id = R.string.home), BottomNavItem.Home.icon),
NavigationItem(BottomNavItem.Network.route, stringResource(id = R.string.network), BottomNavItem.Network.icon),
NavigationItem(BottomNavItem.Send.route, stringResource(id = R.string.send), BottomNavItem.Send.icon),
NavigationItem(BottomNavItem.Receive.route, stringResource(id = R.string.receive), BottomNavItem.Receive.icon),
NavigationItem(BottomNavItem.Chat.route, stringResource(id=R.string.chat), BottomNavItem.Chat.icon),
NavigationItem(BottomNavItem.Log.route, stringResource(id = R.string.log), BottomNavItem.Log.icon),
NavigationItem(BottomNavItem.Settings.route, stringResource(id = R.string.settings), BottomNavItem.Settings.icon)
)
NavigationBar {
val currentRoute = navController.currentDestination?.route
items.forEach { item ->
NavigationBarItem(
icon = { Icon(item.icon, contentDescription = item.label) },
//Modify label Sizes to fit the screen of multiple devices
label = {
Text(
item.label,
fontSize = 7.5.sp,
maxLines = 1,
softWrap = false,
overflow = TextOverflow.Clip
)
},
selected = currentRoute == item.route,
onClick = {
navController.navigate(item.route) {
// Avoid multiple copies of the same destination when reselecting the same item
popUpTo(navController.graph.findStartDestination().id) {
saveState = true
}
// Restore state when reselecting a previously selected item
restoreState = true
// Avoid multiple copies of the same destination when reselecting the same item
launchSingleTop = true
}
},
)
}
}
}