Last active
June 25, 2018 15:58
-
-
Save Jeevuz/0af9979fb517d303832ba909038b61b8 to your computer and use it in GitHub Desktop.
Helpers for Koin (0.9.3)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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