Make an Observable Lifecycle aware - disposing itself when the lifecycle stops
/** @author Aidan Follestad (afollestad) */
class LifecycleAwareDisposable(
private val disposable: Disposable
) : LifecycleObserver {
@OnLifecycleEvent(ON_DESTROY)
fun dispose() = disposable.dispose()
}
/** @author Aidan Follestad (afollestad) */
fun LifecycleOwner.ownRx(disposable: Disposable) {
this.lifecycle.addObserver(LifecycleAwareDisposable(disposable))
}
/** @author Aidan Follestad (afollestad) */
fun Disposable.attachLifecycle(lifecycleOwner: LifecycleOwner) {
lifecycleOwner.ownRx(this)
}