Member-only story

Simplify ViewModelProvider.Factory() Implementation with Kotlin Lambda and Object Expressions

Learn how to streamline the implementation of ViewModelProvider.Factory() using Kotlin Lambdas and Object Expressions with examples.

--

If you are like me and still have not gotten used to the Dependency Injection framework (don’t you think it complicates things?), you probably have been implementing the ViewModelProvider.Factory() interface for your view models that has custom constructor parameters.

Multiple ViewModel Factory Classes

class AViewModelFactory(private val repository: ARepository) :
ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
return AViewModel(repository) as T
}
}

For example, AViewModelFactory is the view model factory class that creates ViewModel which takes in ARepository as a constructor parameter. So what if you have another ViewModel to create? Then, you need to implement another factory class (i.e. BViewModelFactory)

class BViewModelFactory(private val repository: BRepository) :
ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
return BViewModel(repository) as T
}
}

Well, this seems redundant and can be simplified.

Kotlin Lambda and Object Expressions

You can simply the implementation without explicitly having these factory classes using Kotlin Lambda Functions and Object Expressions as you can see in the following createViewModelFactory() helper function.

fun <VM : ViewModel> createViewModelFactory(createViewModel: () -> VM)
: ViewModelProvider.Factory {

return object : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
return createViewModel() as T
}
}
}

The createViewModel is the lambda where the callers define how they want to create the ViewModel. The return of this function is the implementation of ViewModelProvider.Factory() interface for the ViewModel that you want to create.

--

--

No responses yet