Skip to content

Commit

Permalink
Add check if text watcher is already running
Browse files Browse the repository at this point in the history
  • Loading branch information
tillh-stripe committed Sep 14, 2022
1 parent e8420f7 commit ea0630b
Showing 1 changed file with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ open class StripeEditText @JvmOverloads constructor(
val internalFocusChangeListeners = mutableListOf<OnFocusChangeListener>()
private var externalFocusChangeListener: OnFocusChangeListener? = null

private val isLastKeyDeleteTextWatcher = object : StripeTextWatcher() {
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
isLastKeyDelete = count == 0
}
}

protected open val accessibilityText: String? = null

override fun setTextColor(colors: ColorStateList?) {
Expand Down Expand Up @@ -199,14 +205,10 @@ open class StripeEditText @JvmOverloads constructor(
// On some devices, the OnKeyListener isn't invoked for software keyboards. It is invoked on
// other devices such as my Pixel. To fix the issue for all devices, we're adding an
// additional text watcher to keep isLastKeyDelete in the correct state.
val textWatcher = object : StripeTextWatcher() {
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
isLastKeyDelete = count == 0
}
if (isLastKeyDeleteTextWatcher !in textWatchers.orEmpty()) {
addTextChangedListener(isLastKeyDeleteTextWatcher)
}

addTextChangedListener(textWatcher)

// This method works for hard keyboards and older phones.
setOnKeyListener { _, keyCode, event ->
isLastKeyDelete = isDeleteKey(keyCode, event)
Expand Down

0 comments on commit ea0630b

Please sign in to comment.