Merge pull request #235 from vector-im/feature/debounce

Debounce click on room
This commit is contained in:
Benoit Marty 2019-06-28 10:24:45 +02:00 committed by GitHub
commit 2c9280dca6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 4 deletions

View File

@ -19,8 +19,9 @@ package im.vector.riotredesign.core.extensions
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
import im.vector.riotredesign.core.utils.LiveEvent
import im.vector.riotredesign.core.utils.Debouncer
import im.vector.riotredesign.core.utils.EventObserver
import im.vector.riotredesign.core.utils.LiveEvent
inline fun <T> LiveData<T>.observeK(owner: LifecycleOwner, crossinline observer: (T?) -> Unit) {
this.observe(owner, Observer { observer(it) })
@ -32,4 +33,14 @@ inline fun <T> LiveData<T>.observeNotNull(owner: LifecycleOwner, crossinline obs
inline fun <T> LiveData<LiveEvent<T>>.observeEvent(owner: LifecycleOwner, crossinline observer: (T) -> Unit) {
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)
this.observe(owner, EventObserver {
if (debouncer.canHandle()) {
it.run(observer)
}
})
}

View File

@ -0,0 +1,35 @@
/*
* Copyright 2019 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.riotredesign.core.utils
/**
* Simple Debouncer
*/
class Debouncer(private val minimumInterval: Long = 800) {
private var lastDate = 0L
fun canHandle(): Boolean {
val now = System.currentTimeMillis()
if (now > lastDate + minimumInterval) {
lastDate = now
return true
}
// Too soon
return false
}
}

View File

@ -30,7 +30,7 @@ import im.vector.matrix.android.api.session.room.model.RoomSummary
import im.vector.riotredesign.R
import im.vector.riotredesign.core.di.ScreenComponent
import im.vector.riotredesign.core.epoxy.LayoutManagerStateRestorer
import im.vector.riotredesign.core.extensions.observeEvent
import im.vector.riotredesign.core.extensions.observeEventDebounced
import im.vector.riotredesign.core.platform.OnBackPressed
import im.vector.riotredesign.core.platform.StateView
import im.vector.riotredesign.core.platform.VectorBaseFragment
@ -77,7 +77,7 @@ class RoomListFragment : VectorBaseFragment(), RoomSummaryController.Callback, O
setupCreateRoomButton()
setupRecyclerView()
roomListViewModel.subscribe { renderState(it) }
roomListViewModel.openRoomLiveData.observeEvent(this) {
roomListViewModel.openRoomLiveData.observeEventDebounced(this, 800L) {
navigator.openRoom(requireActivity(), it)
}