RainbowCake’s primary dependency injection mechanism remains Dagger 2, as shown by other pages of this documentation that discuss DI. However, it also ships with an artifact that gives you simple Hilt support for your DI needs.
For a starter project that already has Hilt set up for use with RainbowCake, check out Blank-Hilt.
See the Dependency Injection page for more details about dependency injection in RainbowCake in general.
To get started with RainbowCake and Hilt, include the rainbow-cake-hilt
artifact, as described in detail on the Dependencies page.
You’ll also have to set up Hilt in your project, following the steps in the official Android guide.
This includes:
@HiltAndroidApp
@AndroidEntryPoint
Add the @HiltViewModel
annotation to your RainbowCake ViewModels:
@HiltViewModel
class UserViewModel @Inject constructor(
private val userPresenter: UserPresenter
) : RainbowCakeViewModel<UserViewState>(Loading) {
// ...
}
Annotate each individual screen with @AndroidEntryPoint
, and then use the getViewModelFromFactory
method from the hilt
package:
import co.zsmb.rainbowcake.hilt.getViewModelFromFactory
@AndroidEntryPoint
class UserFragment : RainbowCakeFragment<UserViewState, UserViewModel>() {
override fun provideViewModel() = getViewModelFromFactory()
}
That’s it! Now your RainbowCake app is powered by Hilt.