Add a trick to reset EditText field when recycled.

This commit is contained in:
Benoit Marty 2020-09-01 10:05:39 +02:00
parent 1d53b48c8a
commit 56261bd741
5 changed files with 74 additions and 3 deletions

View file

@ -0,0 +1,45 @@
/*
* Copyright (c) 2020 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.utils
import java.util.concurrent.atomic.AtomicBoolean
/**
* Use this container to read a value only once
*/
class ReadOnce<T>(
private val value: T
) {
private val valueHasBeenRead = AtomicBoolean(false)
fun get(): T? {
return if (valueHasBeenRead.getAndSet(true)) {
null
} else {
value
}
}
}
/**
* Only the first call to isTrue() will return true
*/
class ReadOnceTrue {
private val readOnce = ReadOnce(true)
fun isTrue() = readOnce.get() == true
}

View file

@ -32,6 +32,7 @@ import im.vector.app.core.extensions.setTextOrHide
abstract class SettingsEditTextItem : EpoxyModelWithHolder<SettingsEditTextItem.Holder>() {
@EpoxyAttribute var hint: String? = null
@EpoxyAttribute var value: String? = null
@EpoxyAttribute var descriptionText: String? = null
@EpoxyAttribute var errorText: String? = null
@EpoxyAttribute var inProgress: Boolean = false
@ -70,6 +71,9 @@ abstract class SettingsEditTextItem : EpoxyModelWithHolder<SettingsEditTextItem.
holder.editText.doOnTextChanged(textChangeListener)
holder.editText.setOnEditorActionListener(editorActionListener)
if (value != null) {
holder.editText.setText(value)
}
}
class Holder : VectorEpoxyHolder() {

View file

@ -136,6 +136,9 @@ class ThreePidsSettingsController @Inject constructor(
id("addingEmail")
inputType(InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS)
hint(stringProvider.getString(R.string.medium_email))
if (data.editTextReinitiator?.isTrue() == true) {
value("")
}
errorText(data.state.error)
interactionListener(object : SettingsEditTextItem.Listener {
override fun onValidate() {
@ -196,6 +199,9 @@ class ThreePidsSettingsController @Inject constructor(
id("addingMsisdn")
inputType(InputType.TYPE_CLASS_PHONE)
hint(stringProvider.getString(R.string.medium_phone_number))
if (data.editTextReinitiator?.isTrue() == true) {
value("")
}
errorText(data.state.error)
interactionListener(object : SettingsEditTextItem.Listener {
override fun onValidate() {
@ -257,6 +263,9 @@ class ThreePidsSettingsController @Inject constructor(
id("msisdnVerification${threePid.value}")
inputType(InputType.TYPE_CLASS_NUMBER)
hint(stringProvider.getString(R.string.settings_text_message_sent_hint))
if (data.msisdnValidationReinitiator[threePid]?.isTrue() == true) {
value("")
}
errorText(getCodeError(data, threePid))
interactionListener(object : SettingsEditTextItem.Listener {
override fun onValidate() {

View file

@ -29,6 +29,7 @@ import im.vector.app.R
import im.vector.app.core.extensions.exhaustive
import im.vector.app.core.platform.VectorViewModel
import im.vector.app.core.resources.StringProvider
import im.vector.app.core.utils.ReadOnceTrue
import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.MatrixCallback
import org.matrix.android.sdk.api.auth.data.LoginFlowTypes
@ -124,7 +125,15 @@ class ThreePidsSettingsViewModel @AssistedInject constructor(
.livePendingThreePIds()
.execute {
copy(
pendingThreePids = it
pendingThreePids = it,
// Ensure the editText for code will be reset
msisdnValidationReinitiator = msisdnValidationReinitiator.toMutableMap().apply {
it.invoke()
?.filterIsInstance(ThreePid.Msisdn::class.java)
?.forEach { threePid ->
getOrPut(threePid) { ReadOnceTrue() }
}
}
)
}
}
@ -177,7 +186,8 @@ class ThreePidsSettingsViewModel @AssistedInject constructor(
private fun handleChangeState(action: ThreePidsSettingsAction.ChangeState) {
setState {
copy(
state = action.newState
state = action.newState,
editTextReinitiator = ReadOnceTrue()
)
}
}

View file

@ -19,6 +19,7 @@ package im.vector.app.features.settings.threepids
import com.airbnb.mvrx.Async
import com.airbnb.mvrx.MvRxState
import com.airbnb.mvrx.Uninitialized
import im.vector.app.core.utils.ReadOnceTrue
import org.matrix.android.sdk.api.session.identity.ThreePid
data class ThreePidsSettingsViewState(
@ -26,5 +27,7 @@ data class ThreePidsSettingsViewState(
val isLoading: Boolean = false,
val threePids: Async<List<ThreePid>> = Uninitialized,
val pendingThreePids: Async<List<ThreePid>> = Uninitialized,
val msisdnValidationRequests: Map<String, Async<Unit>> = emptyMap()
val msisdnValidationRequests: Map<String, Async<Unit>> = emptyMap(),
val editTextReinitiator: ReadOnceTrue? = null,
val msisdnValidationReinitiator: Map<ThreePid, ReadOnceTrue> = emptyMap()
) : MvRxState