Ensure the textWatcher is not added several times

This commit is contained in:
Benoit Marty 2021-05-31 16:29:21 +02:00
parent 3a1f3c052c
commit 316e685690
6 changed files with 23 additions and 11 deletions

View file

@ -16,7 +16,9 @@
package im.vector.app.core.epoxy
import android.text.TextWatcher
import android.widget.CompoundButton
import android.widget.TextView
import com.google.android.material.switchmaterial.SwitchMaterial
import com.google.android.material.textfield.TextInputEditText
@ -39,3 +41,9 @@ fun VectorEpoxyHolder.setValueOnce(switchView: SwitchMaterial, switchChecked: Bo
switchView.setOnCheckedChangeListener(listener)
}
}
fun TextView.addTextChangedListenerOnce(textWatcher: TextWatcher) {
// Ensure the watcher is not added multiple times
removeTextChangedListener(textWatcher)
addTextChangedListener(textWatcher)
}

View file

@ -28,6 +28,7 @@ import com.google.android.material.textfield.TextInputLayout
import im.vector.app.R
import im.vector.app.core.epoxy.VectorEpoxyHolder
import im.vector.app.core.epoxy.VectorEpoxyModel
import im.vector.app.core.epoxy.addTextChangedListenerOnce
import im.vector.app.core.epoxy.setValueOnce
import im.vector.app.core.platform.SimpleTextWatcher
@ -61,11 +62,10 @@ abstract class FormEditTextItem : VectorEpoxyModel<FormEditTextItem.Holder>() {
@EpoxyAttribute
var endIconMode: Int? = null
// FIXME restore EpoxyAttribute.Option.DoNotHash and fix that properly
@EpoxyAttribute
@EpoxyAttribute(EpoxyAttribute.Option.DoNotHash)
var onTextChange: ((String) -> Unit)? = null
@EpoxyAttribute
@EpoxyAttribute(EpoxyAttribute.Option.DoNotHash)
var editorActionListener: TextView.OnEditorActionListener? = null
private val onTextChangeListener = object : SimpleTextWatcher() {
@ -88,7 +88,7 @@ abstract class FormEditTextItem : VectorEpoxyModel<FormEditTextItem.Holder>() {
holder.textInputEditText.isSingleLine = singleLine
holder.textInputEditText.imeOptions = imeOptions ?: EditorInfo.IME_ACTION_NONE
holder.textInputEditText.addTextChangedListener(onTextChangeListener)
holder.textInputEditText.addTextChangedListenerOnce(onTextChangeListener)
holder.textInputEditText.setOnEditorActionListener(editorActionListener)
holder.bottomSeparator.isVisible = showBottomSeparator
}

View file

@ -26,6 +26,7 @@ import im.vector.app.R
import im.vector.app.core.epoxy.ClickListener
import im.vector.app.core.epoxy.VectorEpoxyHolder
import im.vector.app.core.epoxy.VectorEpoxyModel
import im.vector.app.core.epoxy.addTextChangedListenerOnce
import im.vector.app.core.epoxy.onClick
import im.vector.app.core.epoxy.setValueOnce
import im.vector.app.core.platform.SimpleTextWatcher
@ -45,7 +46,7 @@ abstract class FormEditTextWithButtonItem : VectorEpoxyModel<FormEditTextWithBut
@EpoxyAttribute
var buttonText: String? = null
@EpoxyAttribute
@EpoxyAttribute(EpoxyAttribute.Option.DoNotHash)
var onTextChange: ((String) -> Unit)? = null
@EpoxyAttribute(EpoxyAttribute.Option.DoNotHash)
@ -66,7 +67,7 @@ abstract class FormEditTextWithButtonItem : VectorEpoxyModel<FormEditTextWithBut
holder.textInputEditText.isEnabled = enabled
holder.textInputEditText.addTextChangedListener(onTextChangeListener)
holder.textInputEditText.addTextChangedListenerOnce(onTextChangeListener)
holder.textInputButton.text = buttonText
holder.textInputButton.onClick(onButtonClicked)

View file

@ -27,6 +27,7 @@ import com.google.android.material.textfield.TextInputLayout
import im.vector.app.R
import im.vector.app.core.epoxy.VectorEpoxyHolder
import im.vector.app.core.epoxy.VectorEpoxyModel
import im.vector.app.core.epoxy.addTextChangedListenerOnce
import im.vector.app.core.epoxy.setValueOnce
import im.vector.app.core.platform.SimpleTextWatcher
@ -80,7 +81,7 @@ abstract class FormMultiLineEditTextItem : VectorEpoxyModel<FormMultiLineEditTex
holder.textInputEditText.isEnabled = enabled
holder.textInputEditText.addTextChangedListener(onTextChangeListener)
holder.textInputEditText.addTextChangedListenerOnce(onTextChangeListener)
holder.bottomSeparator.isVisible = showBottomSeparator
}

View file

@ -27,6 +27,7 @@ import com.google.android.material.textfield.TextInputLayout
import im.vector.app.R
import im.vector.app.core.epoxy.VectorEpoxyHolder
import im.vector.app.core.epoxy.VectorEpoxyModel
import im.vector.app.core.epoxy.addTextChangedListenerOnce
import im.vector.app.core.epoxy.setValueOnce
import im.vector.app.core.platform.SimpleTextWatcher
@ -48,7 +49,7 @@ abstract class RoomAliasEditItem : VectorEpoxyModel<RoomAliasEditItem.Holder>()
@EpoxyAttribute
var enabled: Boolean = true
@EpoxyAttribute
@EpoxyAttribute(EpoxyAttribute.Option.DoNotHash)
var onTextChange: ((String) -> Unit)? = null
private val onTextChangeListener = object : SimpleTextWatcher() {
@ -64,7 +65,7 @@ abstract class RoomAliasEditItem : VectorEpoxyModel<RoomAliasEditItem.Holder>()
holder.setValueOnce(holder.textInputEditText, value)
holder.textInputEditText.isEnabled = enabled
holder.textInputEditText.addTextChangedListener(onTextChangeListener)
holder.textInputEditText.addTextChangedListenerOnce(onTextChangeListener)
holder.homeServerText.text = homeServer
holder.bottomSeparator.isVisible = showBottomSeparator
}

View file

@ -27,6 +27,7 @@ import com.google.android.material.textfield.TextInputLayout
import im.vector.app.R
import im.vector.app.core.epoxy.VectorEpoxyHolder
import im.vector.app.core.epoxy.VectorEpoxyModel
import im.vector.app.core.epoxy.addTextChangedListenerOnce
import im.vector.app.core.extensions.showPassword
import im.vector.app.core.platform.SimpleTextWatcher
import im.vector.app.core.resources.StringProvider
@ -42,7 +43,7 @@ abstract class LoginPasswordFormItem : VectorEpoxyModel<LoginPasswordFormItem.Ho
@EpoxyAttribute var passwordRevealClickListener: (() -> Unit)? = null
@EpoxyAttribute var forgetPasswordClickListener: (() -> Unit)? = null
@EpoxyAttribute var submitClickListener: ((String) -> Unit)? = null
@EpoxyAttribute var onPasswordEdited: ((String) -> Unit)? = null
@EpoxyAttribute(EpoxyAttribute.Option.DoNotHash) var onPasswordEdited: ((String) -> Unit)? = null
private val textChangeListener = object : SimpleTextWatcher() {
override fun afterTextChanged(s: Editable) {
@ -60,7 +61,7 @@ abstract class LoginPasswordFormItem : VectorEpoxyModel<LoginPasswordFormItem.Ho
holder.forgetPassword.setOnClickListener { forgetPasswordClickListener?.invoke() }
holder.submit.isEnabled = submitEnabled
holder.submit.setOnClickListener { submitClickListener?.invoke(holder.passwordField.text.toString()) }
holder.passwordField.addTextChangedListener(textChangeListener)
holder.passwordField.addTextChangedListenerOnce(textChangeListener)
}
override fun unbind(holder: Holder) {