https://kotlinlang.org logo
#dagger
Title
# dagger
l

Lucien Guimaraes

03/09/2021, 12:16 PM
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

Colton Idle

03/09/2021, 4:25 PM
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

Lucien Guimaraes

03/09/2021, 4:52 PM
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

allan.conda

03/10/2021, 7:20 AM
it can be scoped if you use compose-navigation I think
a

Ahmed Ibrahim

03/10/2021, 9:11 AM
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

Lucien Guimaraes

03/11/2021, 2:36 PM
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

Ahmed Ibrahim

03/11/2021, 3:21 PM
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

Lucien Guimaraes

03/11/2021, 3:43 PM
This seems like a nice solution, thanks!
👍 1