Skip to content

Instantly share code, notes, and snippets.

@Jeevuz
Last active June 25, 2018 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jeevuz/0af9979fb517d303832ba909038b61b8 to your computer and use it in GitHub Desktop.
Save Jeevuz/0af9979fb517d303832ba909038b61b8 to your computer and use it in GitHub Desktop.
Helpers for Koin (0.9.3)
package ru.mobileup.midhub.extension
import org.koin.android.ext.koin.androidApplication
import org.koin.core.bean.BeanDefinition
import org.koin.core.bean.Definition
import org.koin.dsl.context.Context
import org.koin.dsl.module.Module
import org.koin.dsl.module.applicationContext
/**
* Extensions to make Koin usage more convenient for us.
*
* Some of them is to prepare for the lib's next release (single, module) and can be removed later.
*
*/
fun module(init: Context.() -> Unit): Module = applicationContext(init)
fun Context.scope(name: String, init: Context.() -> Unit): Context = context(name, init)
inline fun <reified T : Any> Context.single(
name: String = "",
noinline definition: Definition<T>
): BeanDefinition<T> {
return bean(name, definition)
}
val Context.context get() = androidApplication()
package ru.mobileup.midhub
import org.koin.KoinContext
import org.koin.standalone.StandAloneContext
import ru.mobileup.midhub.data.gateway.ApplicationGateway
import ru.mobileup.midhub.data.gateway.LicenseGateway
import ru.mobileup.midhub.data.gateway.YellowFormGateway
import ru.mobileup.midhub.domain.wallet.InitWalletInteractor
import ru.mobileup.midhub.domain.whitelicense.WhiteLicenseObtainingInteractor
import ru.mobileup.midhub.domain.yellowlicense.*
/**
* Helps to use **Koin** in more convenient way.
*
* Use instance of this class if you need test open/close scope and mock dependencies.
*/
class KoinHelper {
companion object {
private var scopeToPropertyNames = mutableMapOf<String, Set<String>>()
val koinContext = StandAloneContext.koinContext as KoinContext
inline fun <reified T> get(name: String = ""): T = koinContext.get(name)
inline fun <reified T> getWithProperties(
vararg properties: Pair<String, Any>,
name: String = ""
): T {
properties.forEach { (propertyName, property) ->
koinContext.setProperty(propertyName, property)
}
val result = koinContext.get<T>(name)
koinContext.releaseProperties(*properties.toMap().keys.toTypedArray())
return result
}
}
fun openScope(name: String, vararg properties: Pair<String, Any>) {
koinContext.releaseContext(name)
properties.forEach { (propertyName, property) ->
koinContext.setProperty(propertyName, property)
}
scopeToPropertyNames[name] = properties.toMap().keys
}
fun closeScope(name: String = "") {
koinContext.releaseContext(name)
scopeToPropertyNames[name]?.let {
koinContext.releaseProperties(*it.toTypedArray())
}
}
// We can not create generic get function that is mockable.
val someClass get() = koinContext.get<SomeClass>() // Some class that must be provided via get() and be mocked in tests.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment