Localize AuthViewModel validation and error messages
Some checks failed
Android CI / android (push) Has started running
Android Release / release (push) Has been cancelled
CI / test (push) Has been cancelled

This commit is contained in:
2026-03-11 20:42:33 +03:00
parent 6e9e580b3f
commit d54dc9fe8b
3 changed files with 47 additions and 13 deletions

View File

@@ -1,7 +1,9 @@
package ru.daemonlord.messenger.ui.auth
import android.content.Context
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
@@ -15,6 +17,7 @@ import ru.daemonlord.messenger.domain.auth.usecase.RegisterUseCase
import ru.daemonlord.messenger.domain.auth.usecase.RestoreSessionUseCase
import ru.daemonlord.messenger.domain.common.AppError
import ru.daemonlord.messenger.domain.common.AppResult
import ru.daemonlord.messenger.R
import javax.inject.Inject
@HiltViewModel
@@ -24,6 +27,7 @@ class AuthViewModel @Inject constructor(
private val registerUseCase: RegisterUseCase,
private val loginUseCase: LoginUseCase,
private val logoutUseCase: LogoutUseCase,
@ApplicationContext private val context: Context,
) : ViewModel() {
private val _uiState = MutableStateFlow(AuthUiState())
@@ -95,7 +99,7 @@ class AuthViewModel @Inject constructor(
fun continueWithEmail() {
val email = uiState.value.email.trim().lowercase()
if (email.isBlank()) {
_uiState.update { it.copy(errorMessage = "Enter email.") }
_uiState.update { it.copy(errorMessage = context.getString(R.string.auth_error_enter_email)) }
return
}
viewModelScope.launch {
@@ -114,7 +118,11 @@ class AuthViewModel @Inject constructor(
isLoading = false,
step = if (result.data.registered) AuthStep.PASSWORD else AuthStep.REGISTER,
errorMessage = null,
successMessage = if (result.data.registered) null else "This email is not registered. Complete sign up.",
successMessage = if (result.data.registered) {
null
} else {
context.getString(R.string.auth_info_email_not_registered)
},
)
}
}
@@ -143,7 +151,7 @@ class AuthViewModel @Inject constructor(
private fun register() {
val state = uiState.value
if (state.name.isBlank() || state.username.isBlank() || state.password.isBlank()) {
_uiState.update { it.copy(errorMessage = "Name, username and password are required.") }
_uiState.update { it.copy(errorMessage = context.getString(R.string.auth_error_register_fields_required)) }
return
}
viewModelScope.launch {
@@ -161,7 +169,7 @@ class AuthViewModel @Inject constructor(
it.copy(
isLoading = false,
step = AuthStep.PASSWORD,
successMessage = "Account created. Use password to sign in.",
successMessage = context.getString(R.string.auth_info_account_created),
errorMessage = null,
)
}
@@ -182,7 +190,7 @@ class AuthViewModel @Inject constructor(
private fun loginWithoutOtp() {
val state = uiState.value
if (state.password.isBlank()) {
_uiState.update { it.copy(errorMessage = "Password is required.") }
_uiState.update { it.copy(errorMessage = context.getString(R.string.auth_error_password_required)) }
return
}
viewModelScope.launch {
@@ -209,7 +217,7 @@ class AuthViewModel @Inject constructor(
isLoading = false,
step = AuthStep.OTP,
errorMessage = null,
successMessage = "Enter 2FA code or recovery code.",
successMessage = context.getString(R.string.auth_info_enter_2fa_or_recovery),
)
}
} else {
@@ -232,11 +240,11 @@ class AuthViewModel @Inject constructor(
val otpCode = state.otpCode.trim().ifBlank { null }
val recoveryCode = state.recoveryCode.trim().ifBlank { null }
if (!state.useRecoveryCode && otpCode.isNullOrBlank()) {
_uiState.update { it.copy(errorMessage = "Enter 2FA code.") }
_uiState.update { it.copy(errorMessage = context.getString(R.string.auth_error_enter_2fa_code)) }
return
}
if (state.useRecoveryCode && recoveryCode.isNullOrBlank()) {
_uiState.update { it.copy(errorMessage = "Enter recovery code.") }
_uiState.update { it.copy(errorMessage = context.getString(R.string.auth_error_enter_recovery_code)) }
return
}
viewModelScope.launch {
@@ -348,11 +356,11 @@ class AuthViewModel @Inject constructor(
private fun AppError.toUiMessage(): String {
return when (this) {
AppError.InvalidCredentials -> "Invalid email or password."
AppError.Network -> "Network error. Check your connection."
AppError.Unauthorized -> "Session expired. Please sign in again."
is AppError.Server -> message ?: "Server error. Please try again."
is AppError.Unknown -> "Unknown error. Please try again."
AppError.InvalidCredentials -> context.getString(R.string.auth_error_invalid_credentials)
AppError.Network -> context.getString(R.string.auth_error_network)
AppError.Unauthorized -> context.getString(R.string.auth_error_session_expired)
is AppError.Server -> message ?: context.getString(R.string.auth_error_server)
is AppError.Unknown -> context.getString(R.string.auth_error_unknown)
}
}
}

View File

@@ -232,6 +232,19 @@
<string name="auth_send_reset_link">Отправить ссылку сброса</string>
<string name="auth_new_password">Новый пароль</string>
<string name="auth_reset_with_token">Сбросить по токену</string>
<string name="auth_error_enter_email">Введите email.</string>
<string name="auth_info_email_not_registered">Этот email не зарегистрирован. Завершите регистрацию.</string>
<string name="auth_error_register_fields_required">Нужны имя, имя пользователя и пароль.</string>
<string name="auth_info_account_created">Аккаунт создан. Используйте пароль для входа.</string>
<string name="auth_error_password_required">Требуется пароль.</string>
<string name="auth_info_enter_2fa_or_recovery">Введите код 2FA или код восстановления.</string>
<string name="auth_error_enter_2fa_code">Введите код 2FA.</string>
<string name="auth_error_enter_recovery_code">Введите код восстановления.</string>
<string name="auth_error_invalid_credentials">Неверный email или пароль.</string>
<string name="auth_error_network">Ошибка сети. Проверьте подключение.</string>
<string name="auth_error_session_expired">Сессия истекла. Войдите снова.</string>
<string name="auth_error_server">Ошибка сервера. Попробуйте снова.</string>
<string name="auth_error_unknown">Неизвестная ошибка. Попробуйте снова.</string>
<string name="contacts_title">Контакты</string>
<string name="contacts_search_label">Поиск контактов/пользователей</string>

View File

@@ -232,6 +232,19 @@
<string name="auth_send_reset_link">Send reset link</string>
<string name="auth_new_password">New password</string>
<string name="auth_reset_with_token">Reset with token</string>
<string name="auth_error_enter_email">Enter email.</string>
<string name="auth_info_email_not_registered">This email is not registered. Complete sign up.</string>
<string name="auth_error_register_fields_required">Name, username and password are required.</string>
<string name="auth_info_account_created">Account created. Use password to sign in.</string>
<string name="auth_error_password_required">Password is required.</string>
<string name="auth_info_enter_2fa_or_recovery">Enter 2FA code or recovery code.</string>
<string name="auth_error_enter_2fa_code">Enter 2FA code.</string>
<string name="auth_error_enter_recovery_code">Enter recovery code.</string>
<string name="auth_error_invalid_credentials">Invalid email or password.</string>
<string name="auth_error_network">Network error. Check your connection.</string>
<string name="auth_error_session_expired">Session expired. Please sign in again.</string>
<string name="auth_error_server">Server error. Please try again.</string>
<string name="auth_error_unknown">Unknown error. Please try again.</string>
<string name="contacts_title">Contacts</string>
<string name="contacts_search_label">Search contacts/users</string>