Skip to content

Commit

Permalink
Simplify PinTextWatcher
Browse files Browse the repository at this point in the history
There is no needed to keep a reference to the EditText inside a
TextWatcher because Android passes that reference in all the interface
methods
  • Loading branch information
devinmorgan committed Jul 9, 2024
1 parent 974ff8e commit a4d0ae1
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ package com.joinforage.forage.android.core.ui.textwatcher

import android.text.Editable
import android.text.TextWatcher
import android.widget.EditText

internal class PinTextWatcher(
private val editText: EditText
) : TextWatcher {
internal class PinTextWatcher : TextWatcher {
private var onInputChangeEvent: ((Boolean, Boolean) -> Unit)? = null

fun onInputChangeEvent(callback: (Boolean, Boolean) -> Unit) {
Expand All @@ -22,8 +19,9 @@ internal class PinTextWatcher(
}

override fun afterTextChanged(editable: Editable) {
val isValidAndComplete = editText.length() == 4
val isEmpty = editText.length() == 0
val len = editable.toString().length
val isValidAndComplete = len == 4
val isEmpty = len == 0
onInputChangeEvent?.invoke(isValidAndComplete, isEmpty)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ internal class RosettaPinElement @JvmOverloads constructor(
}

private fun registerTextWatcher() {
val pinTextWatcher = PinTextWatcher(_editText)
val pinTextWatcher = PinTextWatcher()
pinTextWatcher.onInputChangeEvent { isComplete, isEmpty ->
inputState = inputState.handleChangeEvent(
isComplete = isComplete,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class PinTextWatcherTest {
@Test
fun `ensure afterTextChanged event is fired correctly`() {
val editText = EditText(ApplicationProvider.getApplicationContext())
val watcher = PinTextWatcher(editText)
val watcher = PinTextWatcher()
editText.addTextChangedListener(watcher)

// mutable state to help us test the callback
Expand Down

0 comments on commit a4d0ae1

Please sign in to comment.