Commit 723f7cc3 authored by Valere's avatar Valere
Browse files

VectorEpoxyHolder Form Ext

parent 80366ee9
/*
* 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)
}
}
...@@ -23,9 +23,7 @@ import android.view.View ...@@ -23,9 +23,7 @@ import android.view.View
import android.view.inputmethod.EditorInfo import android.view.inputmethod.EditorInfo
import android.widget.EditText import android.widget.EditText
import androidx.annotation.DrawableRes import androidx.annotation.DrawableRes
import com.google.android.material.textfield.TextInputEditText
import im.vector.app.R import im.vector.app.R
import im.vector.app.core.epoxy.VectorEpoxyHolder
import im.vector.app.core.platform.SimpleTextWatcher import im.vector.app.core.platform.SimpleTextWatcher
fun EditText.setupAsSearch(@DrawableRes searchIconRes: Int = R.drawable.ic_search, fun EditText.setupAsSearch(@DrawableRes searchIconRes: Int = R.drawable.ic_search,
...@@ -59,17 +57,3 @@ fun EditText.setupAsSearch(@DrawableRes searchIconRes: Int = R.drawable.ic_searc ...@@ -59,17 +57,3 @@ fun EditText.setupAsSearch(@DrawableRes searchIconRes: Int = R.drawable.ic_searc
return@OnTouchListener false 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)
}
}
...@@ -27,7 +27,7 @@ import com.google.android.material.textfield.TextInputLayout ...@@ -27,7 +27,7 @@ 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.setValueOnce import im.vector.app.core.epoxy.setValueOnce
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)
...@@ -76,7 +76,7 @@ abstract class FormEditTextItem : VectorEpoxyModel<FormEditTextItem.Holder>() { ...@@ -76,7 +76,7 @@ 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
holder.textInputEditText.setValueOnce(value, holder) holder.setValueOnce(holder.textInputEditText, value)
holder.textInputEditText.isEnabled = enabled holder.textInputEditText.isEnabled = enabled
inputType?.let { holder.textInputEditText.inputType = it } inputType?.let { holder.textInputEditText.inputType = it }
......
...@@ -26,7 +26,7 @@ import com.google.android.material.textfield.TextInputLayout ...@@ -26,7 +26,7 @@ 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.setValueOnce import im.vector.app.core.epoxy.setValueOnce
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,7 +61,7 @@ abstract class FormEditTextWithButtonItem : VectorEpoxyModel<FormEditTextWithBut ...@@ -61,7 +61,7 @@ abstract class FormEditTextWithButtonItem : VectorEpoxyModel<FormEditTextWithBut
holder.textInputLayout.isEnabled = enabled holder.textInputLayout.isEnabled = enabled
holder.textInputLayout.hint = hint holder.textInputLayout.hint = hint
holder.textInputEditText.setValueOnce(value, holder) holder.setValueOnce(holder.textInputEditText, value)
holder.textInputEditText.isEnabled = enabled holder.textInputEditText.isEnabled = enabled
......
...@@ -27,7 +27,7 @@ import com.google.android.material.textfield.TextInputLayout ...@@ -27,7 +27,7 @@ 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.setValueOnce import im.vector.app.core.epoxy.setValueOnce
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)
...@@ -76,7 +76,7 @@ abstract class FormMultiLineEditTextItem : VectorEpoxyModel<FormMultiLineEditTex ...@@ -76,7 +76,7 @@ abstract class FormMultiLineEditTextItem : VectorEpoxyModel<FormMultiLineEditTex
holder.textInputEditText.textSize = textSizeSp?.toFloat() ?: 14f holder.textInputEditText.textSize = textSizeSp?.toFloat() ?: 14f
holder.textInputEditText.minLines = minLines holder.textInputEditText.minLines = minLines
holder.textInputEditText.setValueOnce(value, holder) holder.setValueOnce(holder.textInputEditText, value)
holder.textInputEditText.isEnabled = enabled holder.textInputEditText.isEnabled = enabled
......
...@@ -25,6 +25,7 @@ import com.google.android.material.switchmaterial.SwitchMaterial ...@@ -25,6 +25,7 @@ import com.google.android.material.switchmaterial.SwitchMaterial
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.epoxy.setValueOnce
import im.vector.app.core.extensions.setTextOrHide import im.vector.app.core.extensions.setTextOrHide
@EpoxyModelClass(layout = R.layout.item_form_switch) @EpoxyModelClass(layout = R.layout.item_form_switch)
...@@ -61,16 +62,10 @@ abstract class FormSwitchItem : VectorEpoxyModel<FormSwitchItem.Holder>() { ...@@ -61,16 +62,10 @@ abstract class FormSwitchItem : VectorEpoxyModel<FormSwitchItem.Holder>() {
holder.switchView.isEnabled = enabled holder.switchView.isEnabled = enabled
if (holder.view.isAttachedToWindow) { holder.setValueOnce(holder.switchView, switchChecked) { _, isChecked ->
// the view is attached to the window listener?.invoke(isChecked)
// 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 holder.divider.isVisible = showDivider
} }
......
...@@ -27,7 +27,7 @@ import com.google.android.material.textfield.TextInputLayout ...@@ -27,7 +27,7 @@ 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.setValueOnce import im.vector.app.core.epoxy.setValueOnce
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,7 +62,7 @@ abstract class RoomAliasEditItem : VectorEpoxyModel<RoomAliasEditItem.Holder>() ...@@ -62,7 +62,7 @@ abstract class RoomAliasEditItem : VectorEpoxyModel<RoomAliasEditItem.Holder>()
holder.textInputLayout.isEnabled = enabled holder.textInputLayout.isEnabled = enabled
holder.textInputLayout.error = errorMessage holder.textInputLayout.error = errorMessage
holder.textInputEditText.setValueOnce(value, holder) holder.setValueOnce(holder.textInputEditText, 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
......
...@@ -70,24 +70,24 @@ data class RoomSettingsViewState( ...@@ -70,24 +70,24 @@ data class RoomSettingsViewState(
) { ) {
fun hasChanged() = newJoinRules != null || newGuestAccess != null fun hasChanged() = newJoinRules != null || newGuestAccess != null
} }
}
fun RoomSettingsViewState.getJoinRuleWording(stringProvider: StringProvider): String { fun getJoinRuleWording(stringProvider: StringProvider): String {
return when (val joinRule = newRoomJoinRules.newJoinRules ?: currentRoomJoinRules) { return when (val joinRule = newRoomJoinRules.newJoinRules ?: currentRoomJoinRules) {
RoomJoinRules.INVITE -> { RoomJoinRules.INVITE -> {
stringProvider.getString(R.string.room_settings_room_access_private_title) stringProvider.getString(R.string.room_settings_room_access_private_title)
} }
RoomJoinRules.PUBLIC -> { RoomJoinRules.PUBLIC -> {
stringProvider.getString(R.string.room_settings_room_access_public_title) stringProvider.getString(R.string.room_settings_room_access_public_title)
} }
RoomJoinRules.KNOCK -> { RoomJoinRules.KNOCK -> {
stringProvider.getString(R.string.room_settings_room_access_entry_knock) stringProvider.getString(R.string.room_settings_room_access_entry_knock)
} }
RoomJoinRules.RESTRICTED -> { RoomJoinRules.RESTRICTED -> {
stringProvider.getString(R.string.room_settings_room_access_restricted_title) stringProvider.getString(R.string.room_settings_room_access_restricted_title)
} }
else -> { else -> {
stringProvider.getString(R.string.room_settings_room_access_entry_unknown, joinRule.value) stringProvider.getString(R.string.room_settings_room_access_entry_unknown, joinRule.value)
}
} }
} }
} }
...@@ -28,7 +28,6 @@ import im.vector.app.features.form.formMultiLineEditTextItem ...@@ -28,7 +28,6 @@ import im.vector.app.features.form.formMultiLineEditTextItem
import im.vector.app.features.form.formSwitchItem import im.vector.app.features.form.formSwitchItem
import im.vector.app.features.home.AvatarRenderer import im.vector.app.features.home.AvatarRenderer
import im.vector.app.features.roomprofile.settings.RoomSettingsViewState import im.vector.app.features.roomprofile.settings.RoomSettingsViewState
import im.vector.app.features.roomprofile.settings.getJoinRuleWording
import im.vector.app.features.settings.VectorPreferences import im.vector.app.features.settings.VectorPreferences
import org.matrix.android.sdk.api.session.room.model.RoomJoinRules import org.matrix.android.sdk.api.session.room.model.RoomJoinRules
import org.matrix.android.sdk.api.util.toMatrixItem import org.matrix.android.sdk.api.util.toMatrixItem
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment