diff --git a/vector/src/main/java/im/vector/app/core/utils/ReadOnce.kt b/vector/src/main/java/im/vector/app/core/utils/ReadOnce.kt new file mode 100644 index 0000000000..4283ecefab --- /dev/null +++ b/vector/src/main/java/im/vector/app/core/utils/ReadOnce.kt @@ -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( + 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 +} diff --git a/vector/src/main/java/im/vector/app/features/discovery/SettingsEditTextItem.kt b/vector/src/main/java/im/vector/app/features/discovery/SettingsEditTextItem.kt index 8d199e4eda..99303a8934 100644 --- a/vector/src/main/java/im/vector/app/features/discovery/SettingsEditTextItem.kt +++ b/vector/src/main/java/im/vector/app/features/discovery/SettingsEditTextItem.kt @@ -32,6 +32,7 @@ import im.vector.app.core.extensions.setTextOrHide abstract class SettingsEditTextItem : EpoxyModelWithHolder() { @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 + 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() ) } } diff --git a/vector/src/main/java/im/vector/app/features/settings/threepids/ThreePidsSettingsViewState.kt b/vector/src/main/java/im/vector/app/features/settings/threepids/ThreePidsSettingsViewState.kt index 4233ee1005..2b3bdf21ad 100644 --- a/vector/src/main/java/im/vector/app/features/settings/threepids/ThreePidsSettingsViewState.kt +++ b/vector/src/main/java/im/vector/app/features/settings/threepids/ThreePidsSettingsViewState.kt @@ -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> = Uninitialized, val pendingThreePids: Async> = Uninitialized, - val msisdnValidationRequests: Map> = emptyMap() + val msisdnValidationRequests: Map> = emptyMap(), + val editTextReinitiator: ReadOnceTrue? = null, + val msisdnValidationReinitiator: Map = emptyMap() ) : MvRxState