Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is it bad to inject two ViewModel objects into a composable function?

I’m using Hilt for injecting regular objects, as well as ViewModels. In a single part of my app, there is a composable function where I need to inject two ViewModel objects:

@Composable
fun AllProducts(
    allProductsViewModel: AllProductsViewModel = hiltViewModel(),
    authViewModel: AuthViewModel = hiltViewModel()
) {
    //Stuff
}

Is it bad to inject two ViewModel objects rather than a single, as I do in the other parts of my application? Are there any downsides?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This pulls the idea of having two models describing your ui state.

The only downside is how you later provide your @PreviewParameter in your composable previews.

Under assumption that there will be two models that describe your composable state, you will need to tinker with the previews, where you will be forced to introduce third class that will just be used for the preview parameters and nowhere else.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading