using correct copy for the login validation errors

- extracts helpers to make the logic more declarative
This commit is contained in:
Adam Brown 2022-05-09 14:18:44 +01:00
parent 25b81c2952
commit b2af918969
3 changed files with 51 additions and 24 deletions

View File

@ -25,7 +25,6 @@ import android.view.inputmethod.EditorInfo
import androidx.autofill.HintConstants import androidx.autofill.HintConstants
import androidx.core.view.isVisible import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import com.airbnb.mvrx.withState
import im.vector.app.R import im.vector.app.R
import im.vector.app.core.extensions.content import im.vector.app.core.extensions.content
import im.vector.app.core.extensions.editText import im.vector.app.core.extensions.editText
@ -94,15 +93,14 @@ class FtueAuthCombinedLoginFragment @Inject constructor(
) )
private fun submit() { private fun submit() {
withState(viewModel) { state -> cleanupUi()
cleanupUi() loginFieldsValidation.validate(views.loginInput.content(), views.loginPasswordInput.content())
val login = views.loginInput.content() .onUsernameOrIdError { views.loginInput.error = it }
val password = views.loginPasswordInput.content() .onPasswordError { views.loginPasswordInput.error = it }
val isMatrixOrg = state.selectedHomeserver.userFacingUrl == getString(R.string.matrix_org_server_url) .onValid { usernameOrId, password ->
loginFieldsValidation.validate(login, password, isMatrixOrg).onValid { val initialDeviceName = getString(R.string.login_default_session_public_name)
viewModel.handle(OnboardingAction.AuthenticateAction.Login(login, password, getString(R.string.login_default_session_public_name))) viewModel.handle(OnboardingAction.AuthenticateAction.Login(usernameOrId, password, initialDeviceName))
} }
}
} }
private fun cleanupUi() { private fun cleanupUi() {

View File

@ -16,43 +16,48 @@
package im.vector.app.features.onboarding.ftueauth package im.vector.app.features.onboarding.ftueauth
import androidx.core.text.isDigitsOnly
import im.vector.app.R import im.vector.app.R
import im.vector.app.core.resources.StringProvider import im.vector.app.core.resources.StringProvider
import javax.inject.Inject import javax.inject.Inject
typealias LoginValidationResult = Pair<String?, String?>
class LoginFieldsValidation @Inject constructor( class LoginFieldsValidation @Inject constructor(
private val stringProvider: StringProvider private val stringProvider: StringProvider
) { ) {
fun validate(usernameOrId: String, password: String, isMatrixOrg: Boolean): Pair<String?, String?> { fun validate(usernameOrId: String, password: String): LoginValidationResult {
return validateUsernameOrId(usernameOrId, isMatrixOrg) to validatePassword(password) return LoginValidationResult(usernameOrId, password, validateUsernameOrId(usernameOrId), validatePassword(password))
} }
private fun validateUsernameOrId(usernameOrId: String, isMatrixOrg: Boolean): String? { private fun validateUsernameOrId(usernameOrId: String): String? {
val accountError = when { val accountError = when {
usernameOrId.isEmpty() -> stringProvider.getString(R.string.error_empty_field_choose_user_name) usernameOrId.isEmpty() -> stringProvider.getString(R.string.error_empty_field_enter_user_name)
isNumericOnlyUserIdForbidden(isMatrixOrg) && usernameOrId.isDigitsOnly() -> stringProvider.getString(R.string.error_forbidden_digits_only_username) else -> null
else -> null
} }
return accountError return accountError
} }
private fun isNumericOnlyUserIdForbidden(isMatrixOrg: Boolean) = isMatrixOrg
private fun validatePassword(password: String): String? { private fun validatePassword(password: String): String? {
val passwordError = when { val passwordError = when {
password.isEmpty() -> stringProvider.getString(R.string.error_empty_field_choose_password) password.isEmpty() -> stringProvider.getString(R.string.error_empty_field_your_password)
else -> null else -> null
} }
return passwordError return passwordError
} }
} }
fun LoginValidationResult.onValid(action: () -> Unit) { fun LoginValidationResult.onValid(action: (String, String) -> Unit): LoginValidationResult {
when { when {
first != null && second != null -> action() usernameOrIdError != null && passwordError != null -> action(usernameOrId, password)
} }
return this
}
fun LoginValidationResult.onUsernameOrIdError(action: (String) -> Unit): LoginValidationResult {
usernameOrIdError?.let(action)
return this
}
fun LoginValidationResult.onPasswordError(action: (String) -> Unit): LoginValidationResult {
passwordError?.let(action)
return this
} }

View File

@ -0,0 +1,24 @@
/*
* Copyright (c) 2022 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.features.onboarding.ftueauth
data class LoginValidationResult(
val usernameOrId: String,
val password: String,
val usernameOrIdError: String?,
val passwordError: String?
)