I'm trying to for example change the title of the appBar or whether the bottomNavBar should be shown in different composables. Since I don't want to pass that viewModel to every composable, I've created a viewModel called AppViewModel and inject it with Hilt to change these values, The mentioned viewModel is like this:
@HiltViewModel
class AppViewModel @Inject constructor(): ViewModel() {
var showBottomNavBar = mutableStateOf(true)
}
In my main activity
@AndroidEntryPoint
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
val appViewModel = hiltViewModel<AppViewModel>()
val showBottomNavbar by remember {appViewModel.showBottomNavBar}
AppTheme {
val navController = rememberNavController()
Scaffold(
bottomBar = {
if (showBottomNavbar)
BottomNavigation1(navController = navController)
}
) {
Box(Modifier.padding(it)) {
NavigationGraph(navController = navController)
}
}
}
}
}
}
And in one of my destination composables like the one below If I change the showBottomNavbar
it doesn't change anything.
@Composable
fun Product(
navController: NavController,
appViewModel: AppViewModel = hiltViewModel()
) {
LaunchedEffect(Unit) {
appViewModel.showBottomNavBar.value = false
}
}
What am I doing wrong and how can I achieve the what I'm looking for? Thanks in advance.