mirror of
https://github.com/element-hq/element-android
synced 2024-11-28 05:31:21 +03:00
Cleanup and format DebouncedClickListener
This commit is contained in:
parent
575ebdc3e8
commit
0c7b9f4690
1 changed files with 11 additions and 7 deletions
|
@ -15,6 +15,7 @@
|
||||||
*/
|
*/
|
||||||
package im.vector.app.core.utils
|
package im.vector.app.core.utils
|
||||||
|
|
||||||
|
import android.os.SystemClock
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import java.util.WeakHashMap
|
import java.util.WeakHashMap
|
||||||
|
|
||||||
|
@ -22,17 +23,20 @@ import java.util.WeakHashMap
|
||||||
* Simple Debounced OnClickListener
|
* Simple Debounced OnClickListener
|
||||||
* Safe to use in different views
|
* Safe to use in different views
|
||||||
*/
|
*/
|
||||||
class DebouncedClickListener(val original: View.OnClickListener, private val minimumInterval: Long = 400) : View.OnClickListener {
|
class DebouncedClickListener(
|
||||||
|
val original: View.OnClickListener,
|
||||||
|
private val minimumInterval: Long = 400
|
||||||
|
) : View.OnClickListener {
|
||||||
private val lastClickMap = WeakHashMap<View, Long>()
|
private val lastClickMap = WeakHashMap<View, Long>()
|
||||||
|
|
||||||
override fun onClick(clickedView: View) {
|
override fun onClick(v: View) {
|
||||||
val previousClickTimestamp = lastClickMap[clickedView]
|
val previousClickTimestamp = lastClickMap[v]
|
||||||
val currentTimestamp = System.currentTimeMillis()
|
val currentTimestamp = SystemClock.elapsedRealtime()
|
||||||
|
|
||||||
lastClickMap[clickedView] = currentTimestamp
|
lastClickMap[v] = currentTimestamp
|
||||||
|
|
||||||
if (previousClickTimestamp == null || currentTimestamp - previousClickTimestamp.toLong() > minimumInterval) {
|
if (previousClickTimestamp == null || currentTimestamp - previousClickTimestamp > minimumInterval) {
|
||||||
original.onClick(clickedView)
|
original.onClick(v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue