Skip to content

Commit

Permalink
Merge pull request #97 from Trendyol/debounced-click-toolbar-card-inp…
Browse files Browse the repository at this point in the history
…ut-view

Add setDebouncedClickListener extension function to :toolbar and :car…
  • Loading branch information
turkergoksu authored Jul 27, 2022
2 parents 2230a88 + 2748d00 commit 91cb08b
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 12 deletions.
4 changes: 2 additions & 2 deletions buildSrc/src/main/kotlin/ComponentVersions.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
object ComponentVersions {

const val toolbarVersion = "2.0.6"
const val toolbarVersion = "2.0.7"
const val suggestionInputViewVersion = "1.3.0"
const val ratingBarVersion = "1.0.2"
const val imageSliderVersion = "1.0.8"
const val phoneNumberVersion = "1.0.2"
const val dialogsVersion = "1.4.0"
const val cardInputViewVersion = "1.2.1"
const val cardInputViewVersion = "1.2.2"
const val quantityPickerViewVersion = "1.2.5"
const val timelineViewVersion = "1.0.0"
const val touchDelegatorVersion = "1.0.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,10 @@ class CardInputView : ConstraintLayout {
addTextChangedListener(CvvTextWatcher())
}

textViewCardExpiryMonth.setOnClickListener {
textViewCardExpiryMonth.setDebouncedOnClickListener {
openMonthSelectionListener?.invoke()
}
textViewCardExpiryYear.setOnClickListener {
textViewCardExpiryYear.setDebouncedOnClickListener {
openYearSelectionListener?.invoke()
}
textViewCvvInfo.setOnClickListener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,19 @@ internal fun Editable.updateText(currentText: String) {
replace(0, length, currentText, 0, currentText.length)
filters = currentFilters
}

/**
* Set debounce time(millis) to onClickListener for prevent multiple clicks.
* @param debounceMillis: Millis time that gives delay between clicks.
* @param onClickListener: Lambda function that invokes click listener.
*/
fun View.setDebouncedOnClickListener(debounceMillis: Long = 500L, onClickListener: View.OnClickListener) {
var lastClickTime: Long = 0
setOnClickListener { view ->
val shouldAcceptClick = (System.currentTimeMillis() - lastClickTime) > debounceMillis
if (shouldAcceptClick) {
lastClickTime = System.currentTimeMillis()
onClickListener.onClick(view)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,19 @@ internal fun View.setEndMargin(endMargin: Int?) {
it.marginEnd = endMargin
}
}

/**
* Set debounce time(millis) to onClickListener for prevent multiple clicks.
* @param debounceMillis: Millis time that gives delay between clicks.
* @param onClickListener: Lambda function that invokes click listener.
*/
fun View.setDebouncedOnClickListener(debounceMillis: Long = 500L, onClickListener: View.OnClickListener) {
var lastClickTime: Long = 0
setOnClickListener { view ->
val shouldAcceptClick = (System.currentTimeMillis() - lastClickTime) > debounceMillis
if (shouldAcceptClick) {
lastClickTime = System.currentTimeMillis()
onClickListener.onClick(view)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ class Toolbar : ConstraintLayout {
init {
inflate(context, R.layout.view_toolbar, this)

imageLeft.setOnClickListener { leftImageClickListener?.invoke() }
imageMiddle.setOnClickListener { middleImageClickListener?.invoke() }
imageRight.setOnClickListener { rightImageClickListener?.invoke() }
textLeftUp.setOnClickListener { upperLeftTextClickListener?.invoke() }
textLeftDown.setOnClickListener { lowerLeftTextClickListener?.invoke() }
textMiddle.setOnClickListener { middleTextClickListener?.invoke() }
textRightUp.setOnClickListener { upperRightTextClickListener?.invoke() }
textRightDown.setOnClickListener { lowerRightTextClickListener?.invoke() }
imageLeft.setDebouncedOnClickListener { leftImageClickListener?.invoke() }
imageMiddle.setDebouncedOnClickListener { middleImageClickListener?.invoke() }
imageRight.setDebouncedOnClickListener { rightImageClickListener?.invoke() }
textLeftUp.setDebouncedOnClickListener { upperLeftTextClickListener?.invoke() }
textLeftDown.setDebouncedOnClickListener { lowerLeftTextClickListener?.invoke() }
textMiddle.setDebouncedOnClickListener { middleTextClickListener?.invoke() }
textRightUp.setDebouncedOnClickListener { upperRightTextClickListener?.invoke() }
textRightDown.setDebouncedOnClickListener { lowerRightTextClickListener?.invoke() }
}

private fun readFromAttributes(attrs: AttributeSet?, defStyleAttr: Int = 0) {
Expand Down

0 comments on commit 91cb08b

Please sign in to comment.