To clarify the title: I've got a multi-module app that has 5 feature-modules, I've set up navigation etc. but currently for my bottom navigation bar everything is hard-coded, the paths to graphs + icons + titles, I'm trying to find a way to create a class / object of some sort that would let me to just do something like:
val nestedGraphsRoutes = listOf(
ProductModuleInfo,
CategoriesModuleInfo,
SearchModuleInfo,
ProfileModuleInfo,
CartModuleInfo
)
BottomNavigation() {
nestedGraphsRoutes.forEach { graph ->
BottomNavigationItem(
label = {Text(graph.title)},
icon = {Icon(painterResource(graph.icon, null)},
selected = {...},
onClick = {navController.navigate(graph.routes.nestedGraphRoute)} // I want the "routes" to contain other strings as well
}
}
however I currently cannot think what wound fit well here, I'm currently using simple objects and that's... not pleasant to work with
Thanks in advance, cheers
Holding module representation is like holding data: why not a "Data class"?
Data classes are a concise way of creating classes used mainly for holding data. They implicitly declare component functions,
equals()
,hashCode()
,toString()
, and others based on the properties declared in the primary constructor.For example, in the context of your use case:
You can then create your instances like so:
And then you can use these instances in your
BottomNavigation()
call:That way, the
ModuleInfo
instances are effectively replacing the hard-coded data in yourBottomNavigation()
call. Each module is represented by an instance ofModuleInfo
and that instance carries all the necessary information (title, icon, routes) for theBottomNavigationItem
.That should give you a more flexible and clean way to organize your module information.