Coverage Summary for Class: DebouncingClickListener (vit.khudenko.android.safe_click)

Class Class, % Method, % Block, % Line, %
DebouncingClickListener 100% (1/1) 100% (3/3) 100% (3/3) 100% (11/11)


1 package vit.khudenko.android.safe_click 2  3 import android.os.SystemClock 4 import android.view.View 5  6 /** 7  * Base class for building click listeners that use click event debouncing. Child classes should implement [onClickAction] method. 8  * 9  * @param debounceTimeoutMillis debounce timeout in milliseconds. 10  * 11  * @throws IllegalArgumentException if [debounceTimeoutMillis] parameter is negative 12  */ 13 abstract class DebouncingClickListener( 14  private val debounceTimeoutMillis: Long 15 ) : View.OnClickListener { 16  17  init { 18  require(debounceTimeoutMillis >= 0) { 19  "debounceTimeoutMillis parameter can not be negative" 20  } 21  } 22  23  private var lastClickTimestamp: Long = 0 24  25  override fun onClick(v: View) { 26  val now = SystemClock.elapsedRealtime() 27  if (now - lastClickTimestamp < debounceTimeoutMillis) { 28  return 29  } 30  lastClickTimestamp = now 31  onClickAction(v) 32  } 33  34  abstract fun onClickAction(v: View) 35  36  fun reset() { 37  lastClickTimestamp = 0 38  } 39 }