mirror of
https://github.com/element-hq/element-android
synced 2024-11-25 02:45:37 +03:00
Rename Debouncer to FirstThrottler
This commit is contained in:
parent
21357a1ec7
commit
535b41d818
3 changed files with 9 additions and 8 deletions
|
@ -19,7 +19,7 @@ package im.vector.riotx.core.extensions
|
|||
import androidx.lifecycle.LifecycleOwner
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.Observer
|
||||
import im.vector.riotx.core.utils.Debouncer
|
||||
import im.vector.riotx.core.utils.FirstThrottler
|
||||
import im.vector.riotx.core.utils.EventObserver
|
||||
import im.vector.riotx.core.utils.LiveEvent
|
||||
|
||||
|
@ -35,11 +35,11 @@ inline fun <T> LiveData<LiveEvent<T>>.observeEvent(owner: LifecycleOwner, crossi
|
|||
this.observe(owner, EventObserver { it.run(observer) })
|
||||
}
|
||||
|
||||
inline fun <T> LiveData<LiveEvent<T>>.observeEventDebounced(owner: LifecycleOwner, minimumInterval: Long, crossinline observer: (T) -> Unit) {
|
||||
val debouncer = Debouncer(minimumInterval)
|
||||
inline fun <T> LiveData<LiveEvent<T>>.observeEventFirstThrottle(owner: LifecycleOwner, minimumInterval: Long, crossinline observer: (T) -> Unit) {
|
||||
val firstThrottler = FirstThrottler(minimumInterval)
|
||||
|
||||
this.observe(owner, EventObserver {
|
||||
if (debouncer.canHandle()) {
|
||||
if (firstThrottler.canHandle()) {
|
||||
it.run(observer)
|
||||
}
|
||||
})
|
||||
|
|
|
@ -17,9 +17,10 @@ package im.vector.riotx.core.utils
|
|||
|
||||
|
||||
/**
|
||||
* Simple Debouncer
|
||||
* Simple ThrottleFirst
|
||||
* See https://raw.githubusercontent.com/wiki/ReactiveX/RxJava/images/rx-operators/throttleFirst.png
|
||||
*/
|
||||
class Debouncer(private val minimumInterval: Long = 800) {
|
||||
class FirstThrottler(private val minimumInterval: Long = 800) {
|
||||
private var lastDate = 0L
|
||||
|
||||
fun canHandle(): Boolean {
|
|
@ -33,7 +33,7 @@ import im.vector.riotx.core.di.ScreenComponent
|
|||
import im.vector.riotx.core.epoxy.LayoutManagerStateRestorer
|
||||
import im.vector.riotx.core.error.ErrorFormatter
|
||||
import im.vector.riotx.core.extensions.observeEvent
|
||||
import im.vector.riotx.core.extensions.observeEventDebounced
|
||||
import im.vector.riotx.core.extensions.observeEventFirstThrottle
|
||||
import im.vector.riotx.core.platform.OnBackPressed
|
||||
import im.vector.riotx.core.platform.StateView
|
||||
import im.vector.riotx.core.platform.VectorBaseFragment
|
||||
|
@ -81,7 +81,7 @@ class RoomListFragment : VectorBaseFragment(), RoomSummaryController.Listener, O
|
|||
setupCreateRoomButton()
|
||||
setupRecyclerView()
|
||||
roomListViewModel.subscribe { renderState(it) }
|
||||
roomListViewModel.openRoomLiveData.observeEventDebounced(this, 800L) {
|
||||
roomListViewModel.openRoomLiveData.observeEventFirstThrottle(this, 800L) {
|
||||
navigator.openRoom(requireActivity(), it)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue