Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent double taps on multi select actions #6186

Merged
merged 2 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.odk.collect.androidshared.ui.multiclicksafe

import android.content.Context
import android.util.AttributeSet
import com.google.android.material.button.MaterialButton

class DoubleClickSafeMaterialButton @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : MaterialButton(context, attrs, defStyleAttr) {

override fun performClick(): Boolean {
return allowClick() && super.performClick()
}

/**
* Use [MultiClickGuard] with a scope unique to this object (class name + hash).
*/
private fun allowClick(): Boolean {
val scope = javaClass.name + hashCode()
return MultiClickGuard.allowClick(scope)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ object MultiClickGuard {
}

/**
* Debounce multiple clicks within the same screen
* Debounce multiple clicks within the same scope
*
* @param screenName The name of the screen. If not provided, the Java class name of the element
* @param scope If not provided, the Java class name of the element
* is used. However, this approach is imperfect, as elements on the same screen might belong to
* different classes. Consequently, clicks on these elements are treated as interactions occurring
* on two distinct screens, not protecting from rapid clicking.
*/
@JvmStatic
@JvmOverloads
fun allowClick(screenName: String = javaClass.name, clickDebounceMs: Long = 1000): Boolean {
fun allowClick(scope: String = javaClass.name, clickDebounceMs: Long = 1000): Boolean {
if (test) {
return true
}
val elapsedRealtime = SystemClock.elapsedRealtime()
val isSameClass = screenName == lastClickName
val isSameClass = scope == lastClickName
val isBeyondThreshold = elapsedRealtime - lastClickTime > clickDebounceMs
val isBeyondTestThreshold =
lastClickTime == 0L || lastClickTime == elapsedRealtime // just for tests
Expand All @@ -38,7 +38,7 @@ object MultiClickGuard {

if (allowClick) {
lastClickTime = elapsedRealtime
lastClickName = screenName
lastClickName = scope
}
return allowClick
}
Expand Down
2 changes: 1 addition & 1 deletion lists/src/main/res/layout/multi_select_controls_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
android:layout_weight="1"
android:text="@string/select_all" />

<com.google.android.material.button.MaterialButton
<org.odk.collect.androidshared.ui.multiclicksafe.DoubleClickSafeMaterialButton
android:id="@+id/action"
style="?materialButtonStyle"
android:layout_width="0dp"
Expand Down