Koin support

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 Koin support for your dependency injection needs.

For a starter project that already has Koin set up for use with RainbowCake, check out Blank-Koin.

See the Dependency Injection page for more details about dependency injection in RainbowCake in general.

Setup

To get started with RainbowCake and Koin, include the rainbow-cake-koin artifact, as described in detail on the Dependencies page.

You’ll also have to include Koin in your dependencies. The library comes in several different packages, here are some recommended ones to start with:

def koin_version = '2.1.6'
implementation "org.koin:koin-core:$koin_version"
implementation "org.koin:koin-android:$koin_version"
implementation "org.koin:koin-android-viewmodel:$koin_version"

For the latest version of Koin, see its releases page.

Code differences

Instead of declaring a ViewModelModule class like in the Dagger setup, you can collect all of your ViewModels and Presenters in Koin modules, such as this:

val UIModule = module {
    factory { UserPresenter(get()) }
    factory { UserViewModel(get()) }
}

When using Koin, you won’t need to create a component, and there is also no need to extend your application from a special base class. Instead, you will have to start up Koin within your application, declaring any modules that will supply your dependencies:

class MyApplication : Application() {
    override fun onCreate() {
        super.onCreate()

        startKoin {
            androidContext(this@MyApplication)
            modules(UIModule)
        }
    }
}

Since they aren’t being injected by Dagger, your classes no longer need @Inject constructors. A simple ViewModel may look like this:

class UserViewModel(
    private val userPresenter: UserPresenter
): RainbowCakeViewModel<UserViewState>(Loading) {
    // ...
}

Finally, fetching a ViewModel in a Fragment happens the same way as with the Dagger setup, using the getViewModelFromFactory method, except it comes from a different package when using the Koin artifact:

import co.zsmb.rainbowcake.koin.getViewModelFromFactory

class UserFragment : RainbowCakeFragment<UserViewState, UserViewModel>() {
    override fun provideViewModel() = getViewModelFromFactory()
    // ...
}

That’s it, you should be up and running with DI! For details on how to use Koin, take a look at its official documentation.