Last active
June 26, 2016 10:49
-
-
Save AAverin/22da3fbd75a84a797bc60b65763f39a4 to your computer and use it in GitHub Desktop.
gist for CleanAndroidCode article series https://medium.com/@anton.averin.dev/keep-your-droid-clean-e9c093140eb6
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
interface BusSubscriberContract { | |
fun bus(bus: RxBus) | |
fun <T : Any> subscribe(clazz: KClass<T>, callback: (T) -> Unit) | |
fun unsubscribe() | |
} | |
open class BusSubscriber @Inject constructor() : BusSubscriberContract { | |
val subscriptions = mutableListOf<Subscription>() | |
private lateinit var bus: RxBus | |
override fun bus(bus: RxBus) { | |
this.bus = bus | |
} | |
override fun <T : Any> subscribe(clazz: KClass<T>, callback: (T) -> Unit) { | |
subscriptions.add(bus.events(clazz.java).subscribe(callback)) | |
} | |
override fun unsubscribe() { | |
subscriptions.forEach { it.unsubscribe() } | |
subscriptions.clear() | |
} | |
} |
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 GlobalBusSubscriber @Inject constructor( | |
val bus: GlobalBus | |
) : BusSubscriber() { | |
init { | |
bus(bus) | |
} | |
} |
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
@ActivityScope | |
class ActivityBusSubscriber @Inject constructor( | |
val bus: ActivityBus | |
) : BusSubscriber() { | |
init { | |
bus(bus) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment