Created
May 6, 2016 15:07
-
-
Save pardom-zz/a403b7aae00f74ee023bfd5beaecae3e to your computer and use it in GitHub Desktop.
Service Factories
This file contains hidden or 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
class DaggerService(private val rootComponent: Any) : ServicesFactory() { | |
override fun bindServices(services: Binder) { | |
val key = services.getKey<Any>() | |
if (key !is WithComponent) { | |
return | |
} | |
val parent = services.getService<Any>(NAME) ?: rootComponent | |
val component = key.createComponent(parent) | |
services.bind(NAME, component) | |
} | |
interface WithComponent { | |
fun createComponent(parent: Any): Any | |
} | |
companion object { | |
const val NAME = "DaggerService" | |
fun <T> get(context: Context) = Flow.getService<T>(NAME, context) | |
} | |
} |
This file contains hidden or 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
class MortarService(private val rootScope: MortarScope) : ServicesFactory() { | |
override fun bindServices(services: Binder) { | |
val key = services.getKey<Any>() | |
if (key !is WithScope) { | |
return | |
} | |
val scopeName = key.javaClass.name | |
val parentScope = services.getService<MortarScope>(NAME) ?: rootScope | |
val childScope = parentScope.findChild(scopeName) ?: key.createScope(parentScope).build(scopeName) | |
services.bind(NAME, childScope) | |
} | |
override fun tearDownServices(services: Services) { | |
services.getService<MortarScope>(NAME)?.destroy() | |
super.tearDownServices(services) | |
} | |
interface WithScope { | |
fun createScope(parentScope: MortarScope): MortarScope.Builder | |
} | |
companion object { | |
const val NAME = "MortarService" | |
fun <T> get(context: Context) = Flow.getService<T>(NAME, context) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment