Rename Debouncer to FirstThrottler

This commit is contained in:
Benoit Marty 2019-07-08 10:49:32 +02:00
parent 21357a1ec7
commit 535b41d818
3 changed files with 9 additions and 8 deletions

View file

@ -19,7 +19,7 @@ package im.vector.riotx.core.extensions
import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer 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.EventObserver
import im.vector.riotx.core.utils.LiveEvent 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) }) this.observe(owner, EventObserver { it.run(observer) })
} }
inline fun <T> LiveData<LiveEvent<T>>.observeEventDebounced(owner: LifecycleOwner, minimumInterval: Long, crossinline observer: (T) -> Unit) { inline fun <T> LiveData<LiveEvent<T>>.observeEventFirstThrottle(owner: LifecycleOwner, minimumInterval: Long, crossinline observer: (T) -> Unit) {
val debouncer = Debouncer(minimumInterval) val firstThrottler = FirstThrottler(minimumInterval)
this.observe(owner, EventObserver { this.observe(owner, EventObserver {
if (debouncer.canHandle()) { if (firstThrottler.canHandle()) {
it.run(observer) it.run(observer)
} }
}) })

View file

@ -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 private var lastDate = 0L
fun canHandle(): Boolean { fun canHandle(): Boolean {

View file

@ -33,7 +33,7 @@ import im.vector.riotx.core.di.ScreenComponent
import im.vector.riotx.core.epoxy.LayoutManagerStateRestorer import im.vector.riotx.core.epoxy.LayoutManagerStateRestorer
import im.vector.riotx.core.error.ErrorFormatter import im.vector.riotx.core.error.ErrorFormatter
import im.vector.riotx.core.extensions.observeEvent 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.OnBackPressed
import im.vector.riotx.core.platform.StateView import im.vector.riotx.core.platform.StateView
import im.vector.riotx.core.platform.VectorBaseFragment import im.vector.riotx.core.platform.VectorBaseFragment
@ -81,7 +81,7 @@ class RoomListFragment : VectorBaseFragment(), RoomSummaryController.Listener, O
setupCreateRoomButton() setupCreateRoomButton()
setupRecyclerView() setupRecyclerView()
roomListViewModel.subscribe { renderState(it) } roomListViewModel.subscribe { renderState(it) }
roomListViewModel.openRoomLiveData.observeEventDebounced(this, 800L) { roomListViewModel.openRoomLiveData.observeEventFirstThrottle(this, 800L) {
navigator.openRoom(requireActivity(), it) navigator.openRoom(requireActivity(), it)
} }