From 85493b7532aa58a75ce4ab73dede679663406920 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Wed, 15 Apr 2020 15:36:30 +0200 Subject: [PATCH] Code review: use isInvalidPassword() extension everywhere. Also be robust if the Fragment is destroyed --- .../java/im/vector/matrix/android/api/failure/Extensions.kt | 1 + .../main/java/im/vector/riotx/core/error/ErrorFormatter.kt | 6 +++--- .../java/im/vector/riotx/features/login/LoginFragment.kt | 6 ++---- .../features/settings/VectorSettingsGeneralFragment.kt | 6 ++++++ 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/failure/Extensions.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/failure/Extensions.kt index 260b3bb8a4..3a068af076 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/failure/Extensions.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/failure/Extensions.kt @@ -34,5 +34,6 @@ fun Throwable.shouldBeRetried(): Boolean { fun Throwable.isInvalidPassword(): Boolean { return this is Failure.ServerError + && error.code == MatrixError.M_FORBIDDEN && error.message == "Invalid password" } diff --git a/vector/src/main/java/im/vector/riotx/core/error/ErrorFormatter.kt b/vector/src/main/java/im/vector/riotx/core/error/ErrorFormatter.kt index 88b6ef5463..557bc93bb1 100644 --- a/vector/src/main/java/im/vector/riotx/core/error/ErrorFormatter.kt +++ b/vector/src/main/java/im/vector/riotx/core/error/ErrorFormatter.kt @@ -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.MatrixError +import im.vector.matrix.android.api.failure.isInvalidPassword import im.vector.riotx.R import im.vector.riotx.core.resources.StringProvider import java.net.HttpURLConnection @@ -54,8 +55,7 @@ class DefaultErrorFormatter @Inject constructor( // Special case for terms and conditions stringProvider.getString(R.string.error_terms_not_accepted) } - throwable.error.code == MatrixError.M_FORBIDDEN - && throwable.error.message == "Invalid password" -> { + throwable.isInvalidPassword() -> { stringProvider.getString(R.string.auth_invalid_login_param) } throwable.error.code == MatrixError.M_USER_IN_USE -> { @@ -67,7 +67,7 @@ class DefaultErrorFormatter @Inject constructor( throwable.error.code == MatrixError.M_NOT_JSON -> { stringProvider.getString(R.string.login_error_not_json) } - throwable.error.code == MatrixError.M_THREEPID_DENIED -> { + throwable.error.code == MatrixError.M_THREEPID_DENIED -> { stringProvider.getString(R.string.login_error_threepid_denied) } throwable.error.code == MatrixError.M_LIMIT_EXCEEDED -> { diff --git a/vector/src/main/java/im/vector/riotx/features/login/LoginFragment.kt b/vector/src/main/java/im/vector/riotx/features/login/LoginFragment.kt index 6fb746e095..57f914118b 100644 --- a/vector/src/main/java/im/vector/riotx/features/login/LoginFragment.kt +++ b/vector/src/main/java/im/vector/riotx/features/login/LoginFragment.kt @@ -28,6 +28,7 @@ import com.airbnb.mvrx.Success import com.jakewharton.rxbinding3.widget.textChanges import im.vector.matrix.android.api.failure.Failure 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.core.extensions.hideKeyboard import im.vector.riotx.core.extensions.showPassword @@ -209,10 +210,7 @@ class LoginFragment @Inject constructor() : AbstractLoginFragment() { } else { // Trick to display the error without text. loginFieldTil.error = " " - if (error is Failure.ServerError - && error.error.code == MatrixError.M_FORBIDDEN - && error.error.message == "Invalid password" - && spaceInPassword()) { + if (error.isInvalidPassword() && spaceInPassword()) { passwordFieldTil.error = getString(R.string.auth_invalid_login_param_space_in_password) } else { passwordFieldTil.error = errorFormatter.toHumanReadable(error) diff --git a/vector/src/main/java/im/vector/riotx/features/settings/VectorSettingsGeneralFragment.kt b/vector/src/main/java/im/vector/riotx/features/settings/VectorSettingsGeneralFragment.kt index 68b341307a..87b27ce452 100644 --- a/vector/src/main/java/im/vector/riotx/features/settings/VectorSettingsGeneralFragment.kt +++ b/vector/src/main/java/im/vector/riotx/features/settings/VectorSettingsGeneralFragment.kt @@ -775,12 +775,18 @@ class VectorSettingsGeneralFragment : VectorSettingsBaseFragment() { showPasswordLoadingView(true) session.changePassword(oldPwd, newPwd, object : MatrixCallback { override fun onSuccess(data: Unit) { + if (!isAdded) { + return + } showPasswordLoadingView(false) dialog.dismiss() activity.toast(R.string.settings_password_updated) } override fun onFailure(failure: Throwable) { + if (!isAdded) { + return + } showPasswordLoadingView(false) if (failure.isInvalidPassword()) { activity.toast(R.string.settings_fail_to_update_password_invalid_current_password)