Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AAverin/22da3fbd75a84a797bc60b65763f39a4 to your computer and use it in GitHub Desktop.
Save AAverin/22da3fbd75a84a797bc60b65763f39a4 to your computer and use it in GitHub Desktop.
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()
}
}
class GlobalBusSubscriber @Inject constructor(
val bus: GlobalBus
) : BusSubscriber() {
init {
bus(bus)
}
}
@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