Fixed comments
https://github.com/TouchInstinct/RoboSwag/pull/121#discussion_r408021807 – prevention of click again coefficient extracted into a value https://github.com/TouchInstinct/RoboSwag/pull/121#discussion_r408025993 – empty line removed https://github.com/TouchInstinct/RoboSwag/pull/121#discussion_r408027482 – "MS" posfix was added to delay constants
This commit is contained in:
parent
2df5e2e3c3
commit
8b2c061528
|
|
@ -4,7 +4,7 @@ import android.os.Build
|
|||
import android.view.View
|
||||
import ru.touchin.utils.ActionThrottler
|
||||
|
||||
const val RIPPLE_EFFECT_DELAY = 150L
|
||||
const val RIPPLE_EFFECT_DELAY_MS = 150L
|
||||
|
||||
/**
|
||||
* Sets click listener to view. On click it will call something after delay.
|
||||
|
|
@ -15,7 +15,7 @@ fun View.setOnRippleClickListener(listener: () -> Unit) {
|
|||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||
setOnClickListener {
|
||||
ActionThrottler.throttleAction {
|
||||
postDelayed({ if (hasWindowFocus()) listener() }, RIPPLE_EFFECT_DELAY)
|
||||
postDelayed({ if (hasWindowFocus()) listener() }, RIPPLE_EFFECT_DELAY_MS)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -1,20 +1,21 @@
|
|||
package ru.touchin.utils
|
||||
|
||||
import android.os.SystemClock
|
||||
import ru.touchin.extensions.RIPPLE_EFFECT_DELAY
|
||||
import ru.touchin.extensions.RIPPLE_EFFECT_DELAY_MS
|
||||
|
||||
object ActionThrottler {
|
||||
|
||||
// Multiplied by 2 because in interval after ripple effect finish and before
|
||||
// It is necessary because in interval after ripple effect finish and before
|
||||
// action invoking start user may be in time to click and launch action again
|
||||
private const val DELAY = 2 * RIPPLE_EFFECT_DELAY
|
||||
private const val PREVENTION_OF_CLICK_AGAIN_COEFFICIENT = 2
|
||||
private const val DELAY_MS = PREVENTION_OF_CLICK_AGAIN_COEFFICIENT * RIPPLE_EFFECT_DELAY_MS
|
||||
private var lastActionTime = 0L
|
||||
|
||||
fun throttleAction(action: () -> Unit): Boolean {
|
||||
val currentTime = SystemClock.elapsedRealtime()
|
||||
val diff = currentTime - lastActionTime
|
||||
|
||||
return if (diff >= DELAY) {
|
||||
return if (diff >= DELAY_MS) {
|
||||
lastActionTime = currentTime
|
||||
action.invoke()
|
||||
true
|
||||
|
|
@ -24,5 +25,3 @@ object ActionThrottler {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue