I’m developing an application in Jetpack Compose with a main file App.kt
that handles navigation and contains a lower bar (BottomBar) that allows navigation and triggers other shared actions across the entire app. This bar is present at all times, even when navigating to different screens.
The issue is that App.kt
and the bottom navigation bar are always visible. When navigating to Screen A
, Screen B
, or Screen C
, the parent Scaffold
in App.kt
and its bottom bar with navigation buttons etc remain visible.
How to apply the MVVM pattern in this case?
Should the app have an AppViewModel
loaded for App.kt
, managing shared logic across screens (like navigation, dialogs, or shared logic that can be triggered from any screen)?
Additionally, is it okay for each screen (for example, ScreenAViewModel
, ScreenBViewModel
, etc.) to have its own ViewModel, and have them coexist with the AppViewModel
at the same time?
In summary, the app has a fixed navigation bar and other shared functionalities that are always visible on App.kt
, and I want to understand if it's correct for both viewmodels (App and Screen) to coexist at once.
I’m developing an application in Jetpack Compose with a main file App.kt
that handles navigation and contains a lower bar (BottomBar) that allows navigation and triggers other shared actions across the entire app. This bar is present at all times, even when navigating to different screens.
The issue is that App.kt
and the bottom navigation bar are always visible. When navigating to Screen A
, Screen B
, or Screen C
, the parent Scaffold
in App.kt
and its bottom bar with navigation buttons etc remain visible.
How to apply the MVVM pattern in this case?
Should the app have an AppViewModel
loaded for App.kt
, managing shared logic across screens (like navigation, dialogs, or shared logic that can be triggered from any screen)?
Additionally, is it okay for each screen (for example, ScreenAViewModel
, ScreenBViewModel
, etc.) to have its own ViewModel, and have them coexist with the AppViewModel
at the same time?
In summary, the app has a fixed navigation bar and other shared functionalities that are always visible on App.kt
, and I want to understand if it's correct for both viewmodels (App and Screen) to coexist at once.
How to apply the MVVM pattern in this case?
I would like to offer a different implementation angle.
IMO, you should have only one view model for every activity. You could, for example, have several top level Composables that each represent a screen in itself and then switching between them is as trivial as changing a state enum in your view model that says which screen you currently want to show.
But, if you insist on having several view models, i won't stop you... So:
instead of having the App.kt
to show the bottom bar at all times,
create a public global Composable and a BaseViewModel
class that contain the bottom bar functionality logic.
Extend BaseViewModel
with your view models and call your public Composable in your views.
If there's any shared state in the bottom bar you can keep it in your BaseViewModel companion object or another shared object.
I assume that the bottom bar composable interacts with the view model, so it can accept it in the parameters.