Skip to content

Commit

Permalink
Update key listener and fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
tillh-stripe committed Sep 28, 2022
1 parent 0a6646a commit 2eb349c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -212,16 +212,22 @@ open class StripeEditText @JvmOverloads constructor(

// This method works for hard keyboards and older phones.
setOnKeyListener { _, keyCode, event ->
isLastKeyDelete = isDeleteKey(keyCode, event)
if (isLastKeyDelete && length() == 0) {
deleteEmptyListener?.onDeleteEmpty()
if (event.action == KeyEvent.ACTION_DOWN) {
// We only care about ACTION_DOWN and will ignore ACTION_UP
val isDelete = isDeleteKey(keyCode)
isLastKeyDelete = isDelete

if (isLastKeyDelete && length() == 0) {
deleteEmptyListener?.onDeleteEmpty()
}
}

false
}
}

private fun isDeleteKey(keyCode: Int, event: KeyEvent): Boolean {
return keyCode == KeyEvent.KEYCODE_DEL && event.action == KeyEvent.ACTION_DOWN
private fun isDeleteKey(keyCode: Int): Boolean {
return keyCode == KeyEvent.KEYCODE_DEL
}

fun interface DeleteEmptyListener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,4 +252,6 @@ internal class StripeEditTextTest {

private fun EditText.enterBackspace() {
dispatchKeyEvent(KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL))
setText(text.toString().dropLast(1))
dispatchKeyEvent(KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL))
}

0 comments on commit 2eb349c

Please sign in to comment.