diff --git a/vector/src/main/java/im/vector/app/core/epoxy/VectorEpoxyFormExt.kt b/vector/src/main/java/im/vector/app/core/epoxy/VectorEpoxyFormExt.kt new file mode 100644 index 0000000000..f93b07a98d --- /dev/null +++ b/vector/src/main/java/im/vector/app/core/epoxy/VectorEpoxyFormExt.kt @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2021 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.app.core.epoxy + +import android.widget.CompoundButton +import com.google.android.material.switchmaterial.SwitchMaterial +import com.google.android.material.textfield.TextInputEditText + +fun VectorEpoxyHolder.setValueOnce(textInputEditText: TextInputEditText, value: String?) { + if (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 { + textInputEditText.setText(value) + } +} + +fun VectorEpoxyHolder.setValueOnce(switchView: SwitchMaterial, switchChecked: Boolean, listener: CompoundButton.OnCheckedChangeListener) { + if (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 { + switchView.isChecked = switchChecked + switchView.setOnCheckedChangeListener(listener) + } +} 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 e65dc4d2c6..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 @@ -23,9 +23,7 @@ import android.view.View import android.view.inputmethod.EditorInfo import android.widget.EditText import androidx.annotation.DrawableRes -import com.google.android.material.textfield.TextInputEditText import im.vector.app.R -import im.vector.app.core.epoxy.VectorEpoxyHolder import im.vector.app.core.platform.SimpleTextWatcher fun EditText.setupAsSearch(@DrawableRes searchIconRes: Int = R.drawable.ic_search, @@ -59,17 +57,3 @@ fun EditText.setupAsSearch(@DrawableRes searchIconRes: Int = R.drawable.ic_searc return@OnTouchListener false }) } - -/** - * Set the initial value of the textEdit. - * Avoids issue with two way bindings, the value is only set the first time - */ -fun TextInputEditText.setValueOnce(value: String?, holder: VectorEpoxyHolder) { - 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 { - setText(value) - } -} 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 9ab939945c..f8bcbddd34 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,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.extensions.setValueOnce +import im.vector.app.core.epoxy.setValueOnce import im.vector.app.core.platform.SimpleTextWatcher @EpoxyModelClass(layout = R.layout.item_form_text_input) @@ -76,7 +76,7 @@ abstract class FormEditTextItem : VectorEpoxyModel() { holder.textInputLayout.error = errorMessage holder.textInputLayout.endIconMode = endIconMode ?: TextInputLayout.END_ICON_NONE - holder.textInputEditText.setValueOnce(value, holder) + holder.setValueOnce(holder.textInputEditText, value) holder.textInputEditText.isEnabled = enabled inputType?.let { holder.textInputEditText.inputType = it } 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 ef3ee2a371..6cf862b10b 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,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.extensions.setValueOnce +import im.vector.app.core.epoxy.setValueOnce import im.vector.app.core.platform.SimpleTextWatcher @EpoxyModelClass(layout = R.layout.item_form_text_input_with_button) @@ -61,7 +61,7 @@ abstract class FormEditTextWithButtonItem : VectorEpoxyModel() { 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.isChecked = switchChecked - holder.switchView.setOnCheckedChangeListener { _, isChecked -> - listener?.invoke(isChecked) - } + holder.setValueOnce(holder.switchView, switchChecked) { _, 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 aec2da22d7..800d7292d2 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,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.extensions.setValueOnce +import im.vector.app.core.epoxy.setValueOnce import im.vector.app.core.platform.SimpleTextWatcher @EpoxyModelClass(layout = R.layout.item_room_alias_text_input) @@ -62,7 +62,7 @@ abstract class RoomAliasEditItem : VectorEpoxyModel() holder.textInputLayout.isEnabled = enabled holder.textInputLayout.error = errorMessage - holder.textInputEditText.setValueOnce(value, holder) + holder.setValueOnce(holder.textInputEditText, value) holder.textInputEditText.isEnabled = enabled holder.textInputEditText.addTextChangedListener(onTextChangeListener) holder.homeServerText.text = homeServer diff --git a/vector/src/main/java/im/vector/app/features/roomprofile/settings/RoomSettingsViewState.kt b/vector/src/main/java/im/vector/app/features/roomprofile/settings/RoomSettingsViewState.kt index bb97acde61..e220687878 100644 --- a/vector/src/main/java/im/vector/app/features/roomprofile/settings/RoomSettingsViewState.kt +++ b/vector/src/main/java/im/vector/app/features/roomprofile/settings/RoomSettingsViewState.kt @@ -70,24 +70,24 @@ data class RoomSettingsViewState( ) { fun hasChanged() = newJoinRules != null || newGuestAccess != null } -} -fun RoomSettingsViewState.getJoinRuleWording(stringProvider: StringProvider): String { - return when (val joinRule = newRoomJoinRules.newJoinRules ?: currentRoomJoinRules) { - RoomJoinRules.INVITE -> { - stringProvider.getString(R.string.room_settings_room_access_private_title) - } - RoomJoinRules.PUBLIC -> { - stringProvider.getString(R.string.room_settings_room_access_public_title) - } - RoomJoinRules.KNOCK -> { - stringProvider.getString(R.string.room_settings_room_access_entry_knock) - } - RoomJoinRules.RESTRICTED -> { - stringProvider.getString(R.string.room_settings_room_access_restricted_title) - } - else -> { - stringProvider.getString(R.string.room_settings_room_access_entry_unknown, joinRule.value) + fun getJoinRuleWording(stringProvider: StringProvider): String { + return when (val joinRule = newRoomJoinRules.newJoinRules ?: currentRoomJoinRules) { + RoomJoinRules.INVITE -> { + stringProvider.getString(R.string.room_settings_room_access_private_title) + } + RoomJoinRules.PUBLIC -> { + stringProvider.getString(R.string.room_settings_room_access_public_title) + } + RoomJoinRules.KNOCK -> { + stringProvider.getString(R.string.room_settings_room_access_entry_knock) + } + RoomJoinRules.RESTRICTED -> { + stringProvider.getString(R.string.room_settings_room_access_restricted_title) + } + else -> { + stringProvider.getString(R.string.room_settings_room_access_entry_unknown, joinRule.value) + } } } } diff --git a/vector/src/main/java/im/vector/app/features/spaces/manage/SpaceSettingsController.kt b/vector/src/main/java/im/vector/app/features/spaces/manage/SpaceSettingsController.kt index 34263bcb32..de51805472 100644 --- a/vector/src/main/java/im/vector/app/features/spaces/manage/SpaceSettingsController.kt +++ b/vector/src/main/java/im/vector/app/features/spaces/manage/SpaceSettingsController.kt @@ -28,7 +28,6 @@ import im.vector.app.features.form.formMultiLineEditTextItem import im.vector.app.features.form.formSwitchItem import im.vector.app.features.home.AvatarRenderer import im.vector.app.features.roomprofile.settings.RoomSettingsViewState -import im.vector.app.features.roomprofile.settings.getJoinRuleWording import im.vector.app.features.settings.VectorPreferences import org.matrix.android.sdk.api.session.room.model.RoomJoinRules import org.matrix.android.sdk.api.util.toMatrixItem