From 5be3faf914f0c77533fc7cbeac0604fd8d3df751 Mon Sep 17 00:00:00 2001 From: Valere Date: Mon, 10 May 2021 09:07:38 +0200 Subject: [PATCH] Epoxy Form fixes --- .../im/vector/app/core/extensions/EditText.kt | 12 ------------ .../vector/app/features/form/FormEditTextItem.kt | 12 ++++++++---- .../features/form/FormEditTextWithButtonItem.kt | 9 ++++++--- .../features/form/FormEditableSquareAvatarItem.kt | 1 - .../features/form/FormMultiLineEditTextItem.kt | 10 +++++++--- .../im/vector/app/features/form/FormSwitchItem.kt | 15 ++++++++++----- .../roomdirectory/createroom/RoomAliasEditItem.kt | 9 ++++++--- vector/src/main/res/layout/item_form_switch.xml | 2 +- 8 files changed, 38 insertions(+), 32 deletions(-) diff --git a/vector/src/main/java/im/vector/app/core/extensions/EditText.kt b/vector/src/main/java/im/vector/app/core/extensions/EditText.kt index 33e7199334..05b70def3d 100644 --- a/vector/src/main/java/im/vector/app/core/extensions/EditText.kt +++ b/vector/src/main/java/im/vector/app/core/extensions/EditText.kt @@ -57,15 +57,3 @@ fun EditText.setupAsSearch(@DrawableRes searchIconRes: Int = R.drawable.ic_searc 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) - } -} diff --git a/vector/src/main/java/im/vector/app/features/form/FormEditTextItem.kt b/vector/src/main/java/im/vector/app/features/form/FormEditTextItem.kt index fdac8afaed..74f088d739 100644 --- a/vector/src/main/java/im/vector/app/features/form/FormEditTextItem.kt +++ b/vector/src/main/java/im/vector/app/features/form/FormEditTextItem.kt @@ -27,7 +27,6 @@ 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.extensions.setTextSafe import im.vector.app.core.platform.SimpleTextWatcher @EpoxyModelClass(layout = R.layout.item_form_text_input) @@ -60,7 +59,7 @@ abstract class FormEditTextItem : VectorEpoxyModel() { @EpoxyAttribute var endIconMode: Int? = null - @EpoxyAttribute + @EpoxyAttribute(EpoxyAttribute.Option.DoNotHash) var onTextChange: ((String) -> Unit)? = null private val onTextChangeListener = object : SimpleTextWatcher() { @@ -76,8 +75,13 @@ abstract class FormEditTextItem : VectorEpoxyModel() { holder.textInputLayout.error = errorMessage holder.textInputLayout.endIconMode = endIconMode ?: TextInputLayout.END_ICON_NONE - // 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. + // 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 inputType?.let { holder.textInputEditText.inputType = it } holder.textInputEditText.isSingleLine = singleLine ?: false diff --git a/vector/src/main/java/im/vector/app/features/form/FormEditTextWithButtonItem.kt b/vector/src/main/java/im/vector/app/features/form/FormEditTextWithButtonItem.kt index 08fc435e11..227e93d2d4 100644 --- a/vector/src/main/java/im/vector/app/features/form/FormEditTextWithButtonItem.kt +++ b/vector/src/main/java/im/vector/app/features/form/FormEditTextWithButtonItem.kt @@ -26,7 +26,6 @@ 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.extensions.setTextSafe import im.vector.app.core.platform.SimpleTextWatcher @EpoxyModelClass(layout = R.layout.item_form_text_input_with_button) @@ -61,8 +60,12 @@ abstract class FormEditTextWithButtonItem : VectorEpoxyModel Unit)? = null private val onTextChangeListener = object : SimpleTextWatcher() { @@ -77,7 +76,12 @@ abstract class FormMultiLineEditTextItem : VectorEpoxyModel() { var switchChecked: Boolean = false @EpoxyAttribute - var title: String? = null + var title: CharSequence? = null @EpoxyAttribute var summary: String? = null @@ -61,10 +61,15 @@ abstract class FormSwitchItem : VectorEpoxyModel() { holder.switchView.isEnabled = enabled - holder.switchView.setOnCheckedChangeListener(null) - holder.switchView.isChecked = switchChecked - holder.switchView.setOnCheckedChangeListener { _, isChecked -> - listener?.invoke(isChecked) + 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.isChecked = switchChecked + holder.switchView.setOnCheckedChangeListener { _, isChecked -> + listener?.invoke(isChecked) + } } holder.divider.isVisible = showDivider } diff --git a/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/RoomAliasEditItem.kt b/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/RoomAliasEditItem.kt index 2a30545a47..11f75ddfa7 100644 --- a/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/RoomAliasEditItem.kt +++ b/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/RoomAliasEditItem.kt @@ -27,7 +27,6 @@ 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.extensions.setTextSafe import im.vector.app.core.platform.SimpleTextWatcher @EpoxyModelClass(layout = R.layout.item_room_alias_text_input) @@ -62,8 +61,12 @@ abstract class RoomAliasEditItem : VectorEpoxyModel() holder.textInputLayout.isEnabled = enabled holder.textInputLayout.error = errorMessage - // 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.addTextChangedListener(onTextChangeListener) holder.homeServerText.text = homeServer diff --git a/vector/src/main/res/layout/item_form_switch.xml b/vector/src/main/res/layout/item_form_switch.xml index cc662680bb..54eabd703d 100644 --- a/vector/src/main/res/layout/item_form_switch.xml +++ b/vector/src/main/res/layout/item_form_switch.xml @@ -50,7 +50,7 @@ android:id="@+id/formSwitchDivider" android:layout_width="0dp" android:layout_height="1dp" - android:background="?riotx_header_panel_border_mobile" + android:background="?vctr_list_divider_color" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" />