Skip to content

Instantly share code, notes, and snippets.

@humblehacker
Last active March 17, 2022 13:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save humblehacker/5b2c73b8cb4874503b33d9050609224d to your computer and use it in GitHub Desktop.
Save humblehacker/5b2c73b8cb4874503b33d9050609224d to your computer and use it in GitHub Desktop.
Injecting view models into Composable functions with kotlin-inject

According to the docs, the non-DI way to associate an view model with an @Composable is via the viewModel() function from androidx.lifecycle.viewmodel.compose.viewModel, like this:

@Composable
fun ProfileScreen(val viewModel: ProfileViewModel = viewModel() {
  ...

But if your view model requires dependencies, you have to pass a subclass of ViewModelProvider.Factory that holds the dependency and knows how to construct the view model for you. Like this:

class ProfileViewModelFactory(private val userRepository: UserRepository) : ViewModelProvider.Factory {
    override fun <T : ViewModel?> create(modelClass: Class<T>): T {
        return modelClass
            .getConstructor(UserRepository::class.java)
            .newInstance(userRepository)
    }
}

Then you'd have to pass the factory to your @Composable instead of the view model. Like this:

@Composable
fun ProfileScreen(vmFactory: ProfileViewModelFactory) {
    val viewModel: ProfileViewModel = viewModel(factory = vmFactory)
    ...

So injection becomes a simple matter of annotating the above function and ProfileViewModelFactory:

@Inject
@Composable
fun ProfileScreen(vmFactory: ProfileViewModelFactory) {
    val viewModel: ProfileViewModel = viewModel(factory = vmFactory)
    ...

@Inject
class ProfileViewModelFactory(private val userRepository: UserRepository) : ViewModelProvider.Factory {
    ...

… and configuring it in your component:

@Component
abstract class AppComponent() {
    abstract val ProfileScreen: ProfileScreen
@humblehacker
Copy link
Author

This write-up is the result of this conversation on the kotlinlang #kotlin-inject Slack channel.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment