Code review: use isInvalidPassword() extension everywhere. Also be robust if the Fragment is destroyed

This commit is contained in:
Benoit Marty 2020-04-15 15:36:30 +02:00
parent dbabe0232f
commit 85493b7532
4 changed files with 12 additions and 7 deletions

View File

@ -34,5 +34,6 @@ fun Throwable.shouldBeRetried(): Boolean {
fun Throwable.isInvalidPassword(): Boolean { fun Throwable.isInvalidPassword(): Boolean {
return this is Failure.ServerError return this is Failure.ServerError
&& error.code == MatrixError.M_FORBIDDEN
&& error.message == "Invalid password" && error.message == "Invalid password"
} }

View File

@ -18,6 +18,7 @@ package im.vector.riotx.core.error
import im.vector.matrix.android.api.failure.Failure import im.vector.matrix.android.api.failure.Failure
import im.vector.matrix.android.api.failure.MatrixError import im.vector.matrix.android.api.failure.MatrixError
import im.vector.matrix.android.api.failure.isInvalidPassword
import im.vector.riotx.R import im.vector.riotx.R
import im.vector.riotx.core.resources.StringProvider import im.vector.riotx.core.resources.StringProvider
import java.net.HttpURLConnection import java.net.HttpURLConnection
@ -54,8 +55,7 @@ class DefaultErrorFormatter @Inject constructor(
// Special case for terms and conditions // Special case for terms and conditions
stringProvider.getString(R.string.error_terms_not_accepted) stringProvider.getString(R.string.error_terms_not_accepted)
} }
throwable.error.code == MatrixError.M_FORBIDDEN throwable.isInvalidPassword() -> {
&& throwable.error.message == "Invalid password" -> {
stringProvider.getString(R.string.auth_invalid_login_param) stringProvider.getString(R.string.auth_invalid_login_param)
} }
throwable.error.code == MatrixError.M_USER_IN_USE -> { throwable.error.code == MatrixError.M_USER_IN_USE -> {

View File

@ -28,6 +28,7 @@ import com.airbnb.mvrx.Success
import com.jakewharton.rxbinding3.widget.textChanges import com.jakewharton.rxbinding3.widget.textChanges
import im.vector.matrix.android.api.failure.Failure import im.vector.matrix.android.api.failure.Failure
import im.vector.matrix.android.api.failure.MatrixError import im.vector.matrix.android.api.failure.MatrixError
import im.vector.matrix.android.api.failure.isInvalidPassword
import im.vector.riotx.R import im.vector.riotx.R
import im.vector.riotx.core.extensions.hideKeyboard import im.vector.riotx.core.extensions.hideKeyboard
import im.vector.riotx.core.extensions.showPassword import im.vector.riotx.core.extensions.showPassword
@ -209,10 +210,7 @@ class LoginFragment @Inject constructor() : AbstractLoginFragment() {
} else { } else {
// Trick to display the error without text. // Trick to display the error without text.
loginFieldTil.error = " " loginFieldTil.error = " "
if (error is Failure.ServerError if (error.isInvalidPassword() && spaceInPassword()) {
&& error.error.code == MatrixError.M_FORBIDDEN
&& error.error.message == "Invalid password"
&& spaceInPassword()) {
passwordFieldTil.error = getString(R.string.auth_invalid_login_param_space_in_password) passwordFieldTil.error = getString(R.string.auth_invalid_login_param_space_in_password)
} else { } else {
passwordFieldTil.error = errorFormatter.toHumanReadable(error) passwordFieldTil.error = errorFormatter.toHumanReadable(error)

View File

@ -775,12 +775,18 @@ class VectorSettingsGeneralFragment : VectorSettingsBaseFragment() {
showPasswordLoadingView(true) showPasswordLoadingView(true)
session.changePassword(oldPwd, newPwd, object : MatrixCallback<Unit> { session.changePassword(oldPwd, newPwd, object : MatrixCallback<Unit> {
override fun onSuccess(data: Unit) { override fun onSuccess(data: Unit) {
if (!isAdded) {
return
}
showPasswordLoadingView(false) showPasswordLoadingView(false)
dialog.dismiss() dialog.dismiss()
activity.toast(R.string.settings_password_updated) activity.toast(R.string.settings_password_updated)
} }
override fun onFailure(failure: Throwable) { override fun onFailure(failure: Throwable) {
if (!isAdded) {
return
}
showPasswordLoadingView(false) showPasswordLoadingView(false)
if (failure.isInvalidPassword()) { if (failure.isInvalidPassword()) {
activity.toast(R.string.settings_fail_to_update_password_invalid_current_password) activity.toast(R.string.settings_fail_to_update_password_invalid_current_password)