Inline wrong token error

This commit is contained in:
Benoit Marty 2020-08-31 23:11:58 +02:00
parent 5594489b69
commit 1d53b48c8a
3 changed files with 41 additions and 7 deletions

View file

@ -25,6 +25,7 @@ import com.airbnb.mvrx.Success
import im.vector.app.R import im.vector.app.R
import im.vector.app.core.epoxy.loadingItem import im.vector.app.core.epoxy.loadingItem
import im.vector.app.core.epoxy.noResultItem import im.vector.app.core.epoxy.noResultItem
import im.vector.app.core.error.ErrorFormatter
import im.vector.app.core.extensions.exhaustive import im.vector.app.core.extensions.exhaustive
import im.vector.app.core.extensions.getFormattedValue import im.vector.app.core.extensions.getFormattedValue
import im.vector.app.core.resources.ColorProvider import im.vector.app.core.resources.ColorProvider
@ -37,12 +38,15 @@ import im.vector.app.features.discovery.settingsEditTextItem
import im.vector.app.features.discovery.settingsInfoItem import im.vector.app.features.discovery.settingsInfoItem
import im.vector.app.features.discovery.settingsInformationItem import im.vector.app.features.discovery.settingsInformationItem
import im.vector.app.features.discovery.settingsSectionTitleItem import im.vector.app.features.discovery.settingsSectionTitleItem
import org.matrix.android.sdk.api.failure.Failure
import org.matrix.android.sdk.api.failure.MatrixError
import org.matrix.android.sdk.api.session.identity.ThreePid import org.matrix.android.sdk.api.session.identity.ThreePid
import javax.inject.Inject import javax.inject.Inject
class ThreePidsSettingsController @Inject constructor( class ThreePidsSettingsController @Inject constructor(
private val stringProvider: StringProvider, private val stringProvider: StringProvider,
private val colorProvider: ColorProvider private val colorProvider: ColorProvider,
private val errorFormatter: ErrorFormatter
) : TypedEpoxyController<ThreePidsSettingsViewState>() { ) : TypedEpoxyController<ThreePidsSettingsViewState>() {
interface InteractionListener { interface InteractionListener {
@ -116,7 +120,7 @@ class ThreePidsSettingsController @Inject constructor(
} }
} }
pendingList.forEach { buildPendingThreePid("p_email ", it) } pendingList.forEach { buildPendingThreePid(data, "p_email ", it) }
} }
when (data.state) { when (data.state) {
@ -171,7 +175,7 @@ class ThreePidsSettingsController @Inject constructor(
} }
} }
pendingList.forEach { buildPendingThreePid("p_msisdn ", it) } pendingList.forEach { buildPendingThreePid(data, "p_msisdn ", it) }
} }
when (data.state) { when (data.state) {
@ -222,7 +226,7 @@ class ThreePidsSettingsController @Inject constructor(
} }
} }
private fun buildPendingThreePid(idPrefix: String, threePid: ThreePid) { private fun buildPendingThreePid(data: ThreePidsSettingsViewState, idPrefix: String, threePid: ThreePid) {
threePidItem { threePidItem {
id(idPrefix + threePid.value) id(idPrefix + threePid.value)
// TODO Add an icon for emails // TODO Add an icon for emails
@ -253,6 +257,7 @@ class ThreePidsSettingsController @Inject constructor(
id("msisdnVerification${threePid.value}") id("msisdnVerification${threePid.value}")
inputType(InputType.TYPE_CLASS_NUMBER) inputType(InputType.TYPE_CLASS_NUMBER)
hint(stringProvider.getString(R.string.settings_text_message_sent_hint)) hint(stringProvider.getString(R.string.settings_text_message_sent_hint))
errorText(getCodeError(data, threePid))
interactionListener(object : SettingsEditTextItem.Listener { interactionListener(object : SettingsEditTextItem.Listener {
override fun onValidate() { override fun onValidate() {
interactionListener?.submitCode(threePid, currentCodes[threePid] ?: "") interactionListener?.submitCode(threePid, currentCodes[threePid] ?: "")
@ -271,4 +276,17 @@ class ThreePidsSettingsController @Inject constructor(
} }
} }
} }
private fun getCodeError(data: ThreePidsSettingsViewState, threePid: ThreePid.Msisdn): String? {
val failure = (data.msisdnValidationRequests[threePid.value] as? Fail)?.error ?: return null
// Wrong code?
// See https://github.com/matrix-org/synapse/issues/8218
return if (failure is Failure.ServerError
&& failure.httpCode == 400
&& failure.error.code == MatrixError.M_UNKNOWN) {
stringProvider.getString(R.string.settings_text_message_sent_wrong_code)
} else {
errorFormatter.toHumanReadable(failure)
}
}
} }

View file

@ -18,7 +18,9 @@ package im.vector.app.features.settings.threepids
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import com.airbnb.mvrx.ActivityViewModelContext import com.airbnb.mvrx.ActivityViewModelContext
import com.airbnb.mvrx.Fail
import com.airbnb.mvrx.FragmentViewModelContext import com.airbnb.mvrx.FragmentViewModelContext
import com.airbnb.mvrx.Loading
import com.airbnb.mvrx.MvRxViewModelFactory import com.airbnb.mvrx.MvRxViewModelFactory
import com.airbnb.mvrx.ViewModelContext import com.airbnb.mvrx.ViewModelContext
import com.squareup.inject.assisted.Assisted import com.squareup.inject.assisted.Assisted
@ -141,6 +143,14 @@ class ThreePidsSettingsViewModel @AssistedInject constructor(
private fun handleSubmitCode(action: ThreePidsSettingsAction.SubmitCode) { private fun handleSubmitCode(action: ThreePidsSettingsAction.SubmitCode) {
isLoading(true) isLoading(true)
setState {
copy(
msisdnValidationRequests = msisdnValidationRequests.toMutableMap().apply {
put(action.threePid.value, Loading())
}
)
}
viewModelScope.launch { viewModelScope.launch {
// First submit the code // First submit the code
session.submitSmsCode(action.threePid, action.code, object : MatrixCallback<Unit> { session.submitSmsCode(action.threePid, action.code, object : MatrixCallback<Unit> {
@ -151,9 +161,14 @@ class ThreePidsSettingsViewModel @AssistedInject constructor(
} }
override fun onFailure(failure: Throwable) { override fun onFailure(failure: Throwable) {
// Wrong code?
isLoading(false) isLoading(false)
_viewEvents.post(ThreePidsSettingsViewEvents.Failure(failure)) setState {
copy(
msisdnValidationRequests = msisdnValidationRequests.toMutableMap().apply {
put(action.threePid.value, Fail(failure))
}
)
}
} }
}) })
} }

View file

@ -25,5 +25,6 @@ data class ThreePidsSettingsViewState(
val state: ThreePidsSettingsState = ThreePidsSettingsState.Idle, val state: ThreePidsSettingsState = ThreePidsSettingsState.Idle,
val isLoading: Boolean = false, val isLoading: Boolean = false,
val threePids: Async<List<ThreePid>> = Uninitialized, val threePids: Async<List<ThreePid>> = Uninitialized,
val pendingThreePids: Async<List<ThreePid>> = Uninitialized val pendingThreePids: Async<List<ThreePid>> = Uninitialized,
val msisdnValidationRequests: Map<String, Async<Unit>> = emptyMap()
) : MvRxState ) : MvRxState