From 535b41d8180936dc14c56b04bf39f1eb3236d6e8 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Mon, 8 Jul 2019 10:49:32 +0200 Subject: [PATCH] Rename Debouncer to FirstThrottler --- .../main/java/im/vector/riotx/core/extensions/LiveData.kt | 8 ++++---- .../riotx/core/utils/{Debouncer.kt => FirstThrottler.kt} | 5 +++-- .../riotx/features/home/room/list/RoomListFragment.kt | 4 ++-- 3 files changed, 9 insertions(+), 8 deletions(-) rename vector/src/main/java/im/vector/riotx/core/utils/{Debouncer.kt => FirstThrottler.kt} (82%) diff --git a/vector/src/main/java/im/vector/riotx/core/extensions/LiveData.kt b/vector/src/main/java/im/vector/riotx/core/extensions/LiveData.kt index a6ab57fee8..a278eab087 100644 --- a/vector/src/main/java/im/vector/riotx/core/extensions/LiveData.kt +++ b/vector/src/main/java/im/vector/riotx/core/extensions/LiveData.kt @@ -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 LiveData>.observeEvent(owner: LifecycleOwner, crossi this.observe(owner, EventObserver { it.run(observer) }) } -inline fun LiveData>.observeEventDebounced(owner: LifecycleOwner, minimumInterval: Long, crossinline observer: (T) -> Unit) { - val debouncer = Debouncer(minimumInterval) +inline fun LiveData>.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) } }) diff --git a/vector/src/main/java/im/vector/riotx/core/utils/Debouncer.kt b/vector/src/main/java/im/vector/riotx/core/utils/FirstThrottler.kt similarity index 82% rename from vector/src/main/java/im/vector/riotx/core/utils/Debouncer.kt rename to vector/src/main/java/im/vector/riotx/core/utils/FirstThrottler.kt index 60e51ad2de..eba0d0d6f1 100644 --- a/vector/src/main/java/im/vector/riotx/core/utils/Debouncer.kt +++ b/vector/src/main/java/im/vector/riotx/core/utils/FirstThrottler.kt @@ -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 { diff --git a/vector/src/main/java/im/vector/riotx/features/home/room/list/RoomListFragment.kt b/vector/src/main/java/im/vector/riotx/features/home/room/list/RoomListFragment.kt index 07304ae30d..b6e2e24a0e 100644 --- a/vector/src/main/java/im/vector/riotx/features/home/room/list/RoomListFragment.kt +++ b/vector/src/main/java/im/vector/riotx/features/home/room/list/RoomListFragment.kt @@ -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) }