I'm working on a new application, so we decided to work with compose and navigation with compose, but the deal is, we forced to use Anvil for DI,
so my question is: is there a better way to inject ViewModels within Composable context, other than using ViewModelProvider.Factory directly to retrieve ViewModel, with Anvil, please review the code example below:
fun NavGraphBuilder.ProfileDestination(
route: String,
profileViewModelFactory: ProfileViewModelFactory, // e.g. passed from MainActivity
) {
composable(route) {
// this were my question is about.
val profileViewModel: ProfileViewModel = ViewModelProvider(
it.viewModelStore,
profileViewModelFactory
)[ProfileViewModel::class.java]
ProfileScreen(profileViewModel = profileViewModel)
}
}
Note: please note that, we will be working with pure compose with compose navigation (no fragments) just one MainActivity, so it would be a miss to just use this Activity as an EntryPoint for any VM factory we need to inject to use it to retrieve VM within composable context.