Hello :wave: Is there a Dagger way to provide a V...
# dagger
l
Hello 👋 Is there a Dagger way to provide a ViewModel to a Composable Screen ? I'm not using Hilt so I don't have the
@HiltViewModel
annotation. How would you achieve it only with Dagger ?
c
Not going to be too helpful here. Sorry. But since hilt is built on top of dagger... technically anything you can do in hilt you'll be able to "backport" onto dagger. In my opinion it seems like Hilt is the future going forward and I would say that now that hilt is in beta (and therefore api stable) you should consider moving to hilt.
l
Migrating to Hilt will be quite an effort to do with the current project I'm working on. That's why I'm looking for a Dagger solution until I have the time to do the migration
👍 1
a
it can be scoped if you use compose-navigation I think
a
You can provide your VM through this method by providing a factory that will provide your vanilla Dagger ViewModel. But watch out, without using Compose Navigation your VM will be scoped to the Activity/Fragment.
l
I'm aware of this solution, but it's not easy to provide a factory for example in feature module gradle. Where you don't have access to your graph and so inject your factory to your VM
a
So in your feature module you can't use
@Inject
? or can you? If you can, I think this solution might help you. The key is to inject a
Provider
to your own ViewModel, and wrap it around a
ViewModelProvider.Factory
https://kotlinlang.slack.com/archives/C5FT9Q36G/p1603961693063400?thread_ts=1603860503.061400&cid=C5FT9Q36G
l
This seems like a nice solution, thanks!
👍 1