Created
February 27, 2020 12:31
-
-
Save seoft/0761805ae4cf3f4c891c1e062e83f919 to your computer and use it in GitHub Desktop.
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
import androidx.lifecycle.Lifecycle | |
import androidx.lifecycle.LifecycleObserver | |
import androidx.lifecycle.OnLifecycleEvent | |
import io.reactivex.disposables.CompositeDisposable | |
import io.reactivex.subjects.BehaviorSubject | |
import java.util.concurrent.TimeUnit | |
class ClickUtil(lifecycle: Lifecycle, private val delay: Long = 1000L) : LifecycleObserver { | |
private lateinit var compositeDisposable: CompositeDisposable | |
private lateinit var clickSubject: BehaviorSubject<(() -> Unit)> | |
init { | |
lifecycle.addObserver(this) | |
} | |
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE) | |
private fun create() { | |
compositeDisposable = CompositeDisposable() | |
clickSubject = BehaviorSubject.create<(() -> Unit)>() | |
compositeDisposable.add( | |
clickSubject.throttleFirst(delay, TimeUnit.MILLISECONDS) | |
.subscribe { | |
it.invoke() | |
}) | |
} | |
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) | |
private fun destroy() { | |
compositeDisposable.dispose() | |
} | |
fun run(hof: () -> Unit) { | |
clickSubject.onNext(hof) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment