Set identity server: do not show the error in the EditText when user want to use the default identity server

This commit is contained in:
Benoit Marty 2020-05-18 14:54:06 +02:00
parent f4108ae0eb
commit 92c719a803
3 changed files with 29 additions and 12 deletions

View File

@ -104,7 +104,7 @@ class SetIdentityServerFragment @Inject constructor(
viewModel.observeViewEvents {
when (it) {
is SetIdentityServerViewEvents.Loading -> showLoading(it.message)
is SetIdentityServerViewEvents.Failure -> identityServerSetDefaultAlternativeTil.error = getString(it.errorMessageId)
is SetIdentityServerViewEvents.Failure -> handleFailure(it)
is SetIdentityServerViewEvents.OtherFailure -> showFailure(it.failure)
is SetIdentityServerViewEvents.NoTerms -> {
AlertDialog.Builder(requireActivity())
@ -129,6 +129,21 @@ class SetIdentityServerFragment @Inject constructor(
}
}
private fun handleFailure(failure: SetIdentityServerViewEvents.Failure) {
val message = getString(failure.errorMessageId)
if (failure.forDefault) {
// Display the error in a dialog
AlertDialog.Builder(requireActivity())
.setTitle(R.string.dialog_title_error)
.setMessage(message)
.setPositiveButton(R.string.ok, null)
.show()
} else {
// Display the error inlined
identityServerSetDefaultAlternativeTil.error = message
}
}
override fun onResume() {
super.onResume()
(activity as? VectorBaseActivity)?.supportActionBar?.setTitle(R.string.identity_server)

View File

@ -21,7 +21,7 @@ import im.vector.riotx.core.platform.VectorViewEvents
sealed class SetIdentityServerViewEvents : VectorViewEvents {
data class Loading(val message: CharSequence? = null) : SetIdentityServerViewEvents()
data class Failure(@StringRes val errorMessageId: Int) : SetIdentityServerViewEvents()
data class Failure(@StringRes val errorMessageId: Int, val forDefault: Boolean) : SetIdentityServerViewEvents()
data class OtherFailure(val failure: Throwable) : SetIdentityServerViewEvents()
data class ShowTerms(val identityServerUrl: String) : SetIdentityServerViewEvents()

View File

@ -34,6 +34,7 @@ import im.vector.riotx.core.platform.VectorViewModel
import im.vector.riotx.core.resources.StringProvider
import im.vector.riotx.core.utils.ensureProtocol
import kotlinx.coroutines.launch
import java.net.UnknownHostException
class SetIdentityServerViewModel @AssistedInject constructor(
@Assisted initialState: SetIdentityServerState,
@ -77,16 +78,16 @@ class SetIdentityServerViewModel @AssistedInject constructor(
}
private fun useDefault() = withState { state ->
state.defaultIdentityServerUrl?.let { doChangeIdentityServerUrl(it) }
state.defaultIdentityServerUrl?.let { doChangeIdentityServerUrl(it, true) }
}
private fun usedCustomIdentityServerUrl(action: SetIdentityServerAction.UseCustomIdentityServer) {
doChangeIdentityServerUrl(action.url)
doChangeIdentityServerUrl(action.url, false)
}
private fun doChangeIdentityServerUrl(url: String) {
private fun doChangeIdentityServerUrl(url: String, isDefault: Boolean) {
if (url.isEmpty()) {
_viewEvents.post(SetIdentityServerViewEvents.Failure(R.string.settings_discovery_please_enter_server))
_viewEvents.post(SetIdentityServerViewEvents.Failure(R.string.settings_discovery_please_enter_server, isDefault))
return
}
val baseUrl = url.ensureProtocol().also { currentWantedUrl = it }
@ -102,10 +103,12 @@ class SetIdentityServerViewModel @AssistedInject constructor(
// Ok, next step
checkTerms(baseUrl)
} catch (failure: Throwable) {
if (failure is IdentityServiceError.OutdatedIdentityServer) {
_viewEvents.post(SetIdentityServerViewEvents.Failure(R.string.identity_server_error_outdated_identity_server))
} else {
_viewEvents.post(SetIdentityServerViewEvents.Failure(R.string.settings_discovery_bad_identity_server))
when {
failure is IdentityServiceError.OutdatedIdentityServer ->
_viewEvents.post(SetIdentityServerViewEvents.Failure(R.string.identity_server_error_outdated_identity_server, isDefault))
failure is Failure.NetworkConnection && failure.ioException is UnknownHostException ->
_viewEvents.post(SetIdentityServerViewEvents.Failure(R.string.settings_discovery_bad_identity_server, isDefault))
else ->
_viewEvents.post(SetIdentityServerViewEvents.OtherFailure(failure))
}
}
@ -137,7 +140,6 @@ class SetIdentityServerViewModel @AssistedInject constructor(
// 404: Same as NoTerms
_viewEvents.post(SetIdentityServerViewEvents.NoTerms)
} else {
_viewEvents.post(SetIdentityServerViewEvents.Failure(R.string.settings_discovery_bad_identity_server))
_viewEvents.post(SetIdentityServerViewEvents.OtherFailure(failure))
}
}