From 0c7b9f4690bfe47e20afcbe21559df972424f3af Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Mon, 31 May 2021 11:58:04 +0200 Subject: [PATCH] Cleanup and format DebouncedClickListener --- .../app/core/utils/DebouncedClickListener.kt | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/vector/src/main/java/im/vector/app/core/utils/DebouncedClickListener.kt b/vector/src/main/java/im/vector/app/core/utils/DebouncedClickListener.kt index 8f96a286d9..d5164371bb 100644 --- a/vector/src/main/java/im/vector/app/core/utils/DebouncedClickListener.kt +++ b/vector/src/main/java/im/vector/app/core/utils/DebouncedClickListener.kt @@ -15,6 +15,7 @@ */ package im.vector.app.core.utils +import android.os.SystemClock import android.view.View import java.util.WeakHashMap @@ -22,17 +23,20 @@ import java.util.WeakHashMap * Simple Debounced OnClickListener * 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() - override fun onClick(clickedView: View) { - val previousClickTimestamp = lastClickMap[clickedView] - val currentTimestamp = System.currentTimeMillis() + override fun onClick(v: View) { + val previousClickTimestamp = lastClickMap[v] + val currentTimestamp = SystemClock.elapsedRealtime() - lastClickMap[clickedView] = currentTimestamp + lastClickMap[v] = currentTimestamp - if (previousClickTimestamp == null || currentTimestamp - previousClickTimestamp.toLong() > minimumInterval) { - original.onClick(clickedView) + if (previousClickTimestamp == null || currentTimestamp - previousClickTimestamp > minimumInterval) { + original.onClick(v) } } }