mirror of
https://github.com/element-hq/element-android
synced 2024-11-28 05:31:21 +03:00
using correct copy for the login validation errors
- extracts helpers to make the logic more declarative
This commit is contained in:
parent
25b81c2952
commit
b2af918969
3 changed files with 51 additions and 24 deletions
|
@ -25,7 +25,6 @@ import android.view.inputmethod.EditorInfo
|
|||
import androidx.autofill.HintConstants
|
||||
import androidx.core.view.isVisible
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import com.airbnb.mvrx.withState
|
||||
import im.vector.app.R
|
||||
import im.vector.app.core.extensions.content
|
||||
import im.vector.app.core.extensions.editText
|
||||
|
@ -94,15 +93,14 @@ class FtueAuthCombinedLoginFragment @Inject constructor(
|
|||
)
|
||||
|
||||
private fun submit() {
|
||||
withState(viewModel) { state ->
|
||||
cleanupUi()
|
||||
val login = views.loginInput.content()
|
||||
val password = views.loginPasswordInput.content()
|
||||
val isMatrixOrg = state.selectedHomeserver.userFacingUrl == getString(R.string.matrix_org_server_url)
|
||||
loginFieldsValidation.validate(login, password, isMatrixOrg).onValid {
|
||||
viewModel.handle(OnboardingAction.AuthenticateAction.Login(login, password, getString(R.string.login_default_session_public_name)))
|
||||
}
|
||||
}
|
||||
cleanupUi()
|
||||
loginFieldsValidation.validate(views.loginInput.content(), views.loginPasswordInput.content())
|
||||
.onUsernameOrIdError { views.loginInput.error = it }
|
||||
.onPasswordError { views.loginPasswordInput.error = it }
|
||||
.onValid { usernameOrId, password ->
|
||||
val initialDeviceName = getString(R.string.login_default_session_public_name)
|
||||
viewModel.handle(OnboardingAction.AuthenticateAction.Login(usernameOrId, password, initialDeviceName))
|
||||
}
|
||||
}
|
||||
|
||||
private fun cleanupUi() {
|
||||
|
|
|
@ -16,43 +16,48 @@
|
|||
|
||||
package im.vector.app.features.onboarding.ftueauth
|
||||
|
||||
import androidx.core.text.isDigitsOnly
|
||||
import im.vector.app.R
|
||||
import im.vector.app.core.resources.StringProvider
|
||||
import javax.inject.Inject
|
||||
|
||||
typealias LoginValidationResult = Pair<String?, String?>
|
||||
|
||||
class LoginFieldsValidation @Inject constructor(
|
||||
private val stringProvider: StringProvider
|
||||
) {
|
||||
|
||||
fun validate(usernameOrId: String, password: String, isMatrixOrg: Boolean): Pair<String?, String?> {
|
||||
return validateUsernameOrId(usernameOrId, isMatrixOrg) to validatePassword(password)
|
||||
fun validate(usernameOrId: String, password: String): LoginValidationResult {
|
||||
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 {
|
||||
usernameOrId.isEmpty() -> stringProvider.getString(R.string.error_empty_field_choose_user_name)
|
||||
isNumericOnlyUserIdForbidden(isMatrixOrg) && usernameOrId.isDigitsOnly() -> stringProvider.getString(R.string.error_forbidden_digits_only_username)
|
||||
else -> null
|
||||
usernameOrId.isEmpty() -> stringProvider.getString(R.string.error_empty_field_enter_user_name)
|
||||
else -> null
|
||||
}
|
||||
return accountError
|
||||
}
|
||||
|
||||
private fun isNumericOnlyUserIdForbidden(isMatrixOrg: Boolean) = isMatrixOrg
|
||||
|
||||
private fun validatePassword(password: String): String? {
|
||||
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
|
||||
}
|
||||
return passwordError
|
||||
}
|
||||
}
|
||||
|
||||
fun LoginValidationResult.onValid(action: () -> Unit) {
|
||||
fun LoginValidationResult.onValid(action: (String, String) -> Unit): LoginValidationResult {
|
||||
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
|
||||
}
|
||||
|
|
|
@ -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?
|
||||
)
|
Loading…
Reference in a new issue