Epoxy Form fixes

This commit is contained in:
Valere 2021-05-10 09:07:38 +02:00
parent e6d4f9a1dc
commit 5be3faf914
8 changed files with 38 additions and 32 deletions

View File

@ -57,15 +57,3 @@ fun EditText.setupAsSearch(@DrawableRes searchIconRes: Int = R.drawable.ic_searc
return@OnTouchListener false return@OnTouchListener false
}) })
} }
/**
* Update the edit text value, only if necessary and move the cursor to the end of the text
*/
fun EditText.setTextSafe(value: String?) {
if (value != null && text.toString() != value) {
setText(value)
// To fix jumping cursor to the start https://github.com/airbnb/epoxy/issues/426
// Note: there is still a known bug if deleting char in the middle of the text, by long pressing on the backspace button.
setSelection(value.length)
}
}

View File

@ -27,7 +27,6 @@ import com.google.android.material.textfield.TextInputLayout
import im.vector.app.R import im.vector.app.R
import im.vector.app.core.epoxy.VectorEpoxyHolder import im.vector.app.core.epoxy.VectorEpoxyHolder
import im.vector.app.core.epoxy.VectorEpoxyModel import im.vector.app.core.epoxy.VectorEpoxyModel
import im.vector.app.core.extensions.setTextSafe
import im.vector.app.core.platform.SimpleTextWatcher import im.vector.app.core.platform.SimpleTextWatcher
@EpoxyModelClass(layout = R.layout.item_form_text_input) @EpoxyModelClass(layout = R.layout.item_form_text_input)
@ -60,7 +59,7 @@ abstract class FormEditTextItem : VectorEpoxyModel<FormEditTextItem.Holder>() {
@EpoxyAttribute @EpoxyAttribute
var endIconMode: Int? = null var endIconMode: Int? = null
@EpoxyAttribute @EpoxyAttribute(EpoxyAttribute.Option.DoNotHash)
var onTextChange: ((String) -> Unit)? = null var onTextChange: ((String) -> Unit)? = null
private val onTextChangeListener = object : SimpleTextWatcher() { private val onTextChangeListener = object : SimpleTextWatcher() {
@ -76,8 +75,13 @@ abstract class FormEditTextItem : VectorEpoxyModel<FormEditTextItem.Holder>() {
holder.textInputLayout.error = errorMessage holder.textInputLayout.error = errorMessage
holder.textInputLayout.endIconMode = endIconMode ?: TextInputLayout.END_ICON_NONE holder.textInputLayout.endIconMode = endIconMode ?: TextInputLayout.END_ICON_NONE
// Update only if text is different and value is not null if (holder.view.isAttachedToWindow) {
holder.textInputEditText.setTextSafe(value) // the view is attached to the window
// So it is a rebind of new data and you could ignore it assuming this is text that was already inputted into the view.
// Downside is if you ever wanted to programmatically change the content of the edit text while it is on screen you would not be able to
} else {
holder.textInputEditText.setText(value)
}
holder.textInputEditText.isEnabled = enabled holder.textInputEditText.isEnabled = enabled
inputType?.let { holder.textInputEditText.inputType = it } inputType?.let { holder.textInputEditText.inputType = it }
holder.textInputEditText.isSingleLine = singleLine ?: false holder.textInputEditText.isSingleLine = singleLine ?: false

View File

@ -26,7 +26,6 @@ import com.google.android.material.textfield.TextInputLayout
import im.vector.app.R import im.vector.app.R
import im.vector.app.core.epoxy.VectorEpoxyHolder import im.vector.app.core.epoxy.VectorEpoxyHolder
import im.vector.app.core.epoxy.VectorEpoxyModel import im.vector.app.core.epoxy.VectorEpoxyModel
import im.vector.app.core.extensions.setTextSafe
import im.vector.app.core.platform.SimpleTextWatcher import im.vector.app.core.platform.SimpleTextWatcher
@EpoxyModelClass(layout = R.layout.item_form_text_input_with_button) @EpoxyModelClass(layout = R.layout.item_form_text_input_with_button)
@ -61,8 +60,12 @@ abstract class FormEditTextWithButtonItem : VectorEpoxyModel<FormEditTextWithBut
holder.textInputLayout.isEnabled = enabled holder.textInputLayout.isEnabled = enabled
holder.textInputLayout.hint = hint holder.textInputLayout.hint = hint
// Update only if text is different if (holder.view.isAttachedToWindow) {
holder.textInputEditText.setTextSafe(value) // the view is attached to the window
// So it is a rebind of new data and you could ignore it assuming this is text that was already inputted into the view.
} else {
holder.textInputEditText.setText(value)
}
holder.textInputEditText.isEnabled = enabled holder.textInputEditText.isEnabled = enabled
holder.textInputEditText.addTextChangedListener(onTextChangeListener) holder.textInputEditText.addTextChangedListener(onTextChangeListener)

View File

@ -83,7 +83,6 @@ abstract class FormEditableSquareAvatarItem : EpoxyModelWithHolder<FormEditableS
override fun unbind(holder: Holder) { override fun unbind(holder: Holder) {
avatarRenderer?.clear(holder.image) avatarRenderer?.clear(holder.image)
GlideApp.with(holder.image).clear(holder.image)
super.unbind(holder) super.unbind(holder)
} }

View File

@ -27,7 +27,6 @@ import com.google.android.material.textfield.TextInputLayout
import im.vector.app.R import im.vector.app.R
import im.vector.app.core.epoxy.VectorEpoxyHolder import im.vector.app.core.epoxy.VectorEpoxyHolder
import im.vector.app.core.epoxy.VectorEpoxyModel import im.vector.app.core.epoxy.VectorEpoxyModel
import im.vector.app.core.extensions.setTextSafe
import im.vector.app.core.platform.SimpleTextWatcher import im.vector.app.core.platform.SimpleTextWatcher
@EpoxyModelClass(layout = R.layout.item_form_multiline_text_input) @EpoxyModelClass(layout = R.layout.item_form_multiline_text_input)
@ -57,7 +56,7 @@ abstract class FormMultiLineEditTextItem : VectorEpoxyModel<FormMultiLineEditTex
@EpoxyAttribute @EpoxyAttribute
var typeFace: Typeface = Typeface.DEFAULT var typeFace: Typeface = Typeface.DEFAULT
@EpoxyAttribute @EpoxyAttribute(EpoxyAttribute.Option.DoNotHash)
var onTextChange: ((String) -> Unit)? = null var onTextChange: ((String) -> Unit)? = null
private val onTextChangeListener = object : SimpleTextWatcher() { private val onTextChangeListener = object : SimpleTextWatcher() {
@ -77,7 +76,12 @@ abstract class FormMultiLineEditTextItem : VectorEpoxyModel<FormMultiLineEditTex
holder.textInputEditText.minLines = minLines holder.textInputEditText.minLines = minLines
// Update only if text is different and value is not null // Update only if text is different and value is not null
holder.textInputEditText.setTextSafe(value) if (holder.view.isAttachedToWindow) {
// the view is attached to the window
// So it is a rebind of new data and you could ignore it assuming this is text that was already inputted into the view.
} else {
holder.textInputEditText.setText(value)
}
holder.textInputEditText.isEnabled = enabled holder.textInputEditText.isEnabled = enabled
holder.textInputEditText.addTextChangedListener(onTextChangeListener) holder.textInputEditText.addTextChangedListener(onTextChangeListener)

View File

@ -40,7 +40,7 @@ abstract class FormSwitchItem : VectorEpoxyModel<FormSwitchItem.Holder>() {
var switchChecked: Boolean = false var switchChecked: Boolean = false
@EpoxyAttribute @EpoxyAttribute
var title: String? = null var title: CharSequence? = null
@EpoxyAttribute @EpoxyAttribute
var summary: String? = null var summary: String? = null
@ -61,11 +61,16 @@ abstract class FormSwitchItem : VectorEpoxyModel<FormSwitchItem.Holder>() {
holder.switchView.isEnabled = enabled holder.switchView.isEnabled = enabled
if (holder.view.isAttachedToWindow) {
// the view is attached to the window
// So it is a rebind of new data and you could ignore it assuming this is value that was already inputted into the view.
} else {
holder.switchView.setOnCheckedChangeListener(null) holder.switchView.setOnCheckedChangeListener(null)
holder.switchView.isChecked = switchChecked holder.switchView.isChecked = switchChecked
holder.switchView.setOnCheckedChangeListener { _, isChecked -> holder.switchView.setOnCheckedChangeListener { _, isChecked ->
listener?.invoke(isChecked) listener?.invoke(isChecked)
} }
}
holder.divider.isVisible = showDivider holder.divider.isVisible = showDivider
} }

View File

@ -27,7 +27,6 @@ import com.google.android.material.textfield.TextInputLayout
import im.vector.app.R import im.vector.app.R
import im.vector.app.core.epoxy.VectorEpoxyHolder import im.vector.app.core.epoxy.VectorEpoxyHolder
import im.vector.app.core.epoxy.VectorEpoxyModel import im.vector.app.core.epoxy.VectorEpoxyModel
import im.vector.app.core.extensions.setTextSafe
import im.vector.app.core.platform.SimpleTextWatcher import im.vector.app.core.platform.SimpleTextWatcher
@EpoxyModelClass(layout = R.layout.item_room_alias_text_input) @EpoxyModelClass(layout = R.layout.item_room_alias_text_input)
@ -62,8 +61,12 @@ abstract class RoomAliasEditItem : VectorEpoxyModel<RoomAliasEditItem.Holder>()
holder.textInputLayout.isEnabled = enabled holder.textInputLayout.isEnabled = enabled
holder.textInputLayout.error = errorMessage holder.textInputLayout.error = errorMessage
// Update only if text is different and value is not null if (holder.view.isAttachedToWindow) {
holder.textInputEditText.setTextSafe(value) // the view is attached to the window
// So it is a rebind of new data and you could ignore it assuming this is text that was already inputted into the view.
} else {
holder.textInputEditText.setText(value)
}
holder.textInputEditText.isEnabled = enabled holder.textInputEditText.isEnabled = enabled
holder.textInputEditText.addTextChangedListener(onTextChangeListener) holder.textInputEditText.addTextChangedListener(onTextChangeListener)
holder.homeServerText.text = homeServer holder.homeServerText.text = homeServer

View File

@ -50,7 +50,7 @@
android:id="@+id/formSwitchDivider" android:id="@+id/formSwitchDivider"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="1dp" android:layout_height="1dp"
android:background="?riotx_header_panel_border_mobile" android:background="?vctr_list_divider_color"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" /> app:layout_constraintStart_toStartOf="parent" />