Валидация авторизации

This commit is contained in:
Данила Мочалов 2023-12-02 22:23:24 +04:00
parent 7d0206ee55
commit 963ef167c8
3 changed files with 51 additions and 7 deletions

View File

@ -9,10 +9,12 @@ import androidx.compose.foundation.layout.size
import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll import androidx.compose.foundation.verticalScroll
import androidx.compose.material.AlertDialog
import androidx.compose.material.Button import androidx.compose.material.Button
import androidx.compose.material.ButtonDefaults import androidx.compose.material.ButtonDefaults
import androidx.compose.material.Card import androidx.compose.material.Card
import androidx.compose.material.Text import androidx.compose.material.Text
import androidx.compose.material.TextButton
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.livedata.observeAsState import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
@ -22,6 +24,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.TextFieldValue import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.text.style.TextDecoration import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
@ -32,6 +35,7 @@ import androidx.navigation.NavHostController
import com.example.shawarma.data.sharedpref.PreferencesManager import com.example.shawarma.data.sharedpref.PreferencesManager
import com.example.shawarma.ui.theme.JejuFamily import com.example.shawarma.ui.theme.JejuFamily
import com.example.shawarma.ui.theme.MyLightRed import com.example.shawarma.ui.theme.MyLightRed
import com.example.shawarma.ui.theme.NunitoFamily
import com.example.shawarma.utils.ScreenPaths import com.example.shawarma.utils.ScreenPaths
import com.example.shawarma.viewmodels.UserViewModel import com.example.shawarma.viewmodels.UserViewModel
import com.example.shawarma.widgets.MyTextField import com.example.shawarma.widgets.MyTextField
@ -63,6 +67,40 @@ fun AuthorizationCard(navHostController: NavHostController) {
} }
} }
if (userViewModel.authorizationState.observeAsState().value == false) {
AlertDialog(
title = {
Text(
text = "Ошибка входа в аккаунт",
fontFamily = NunitoFamily,
fontWeight = FontWeight.Bold,
fontSize = 20.sp
)
},
text = {
Text(
text = "Проверьте корректность введенных данных",
fontFamily = NunitoFamily,
fontWeight = FontWeight.Normal,
fontSize = 16.sp
)
},
onDismissRequest = {
userViewModel.calmAuthorizationState()
},
buttons = {
TextButton(
onClick = {
userViewModel.calmAuthorizationState()
}
) {
Text("ОК")
}
}
)
}
Column( Column(
horizontalAlignment = Alignment.CenterHorizontally, horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier modifier = Modifier

View File

@ -53,8 +53,6 @@ fun RegistrationCard(navHostController: NavHostController){
val password = remember { mutableStateOf(TextFieldValue("")) } val password = remember { mutableStateOf(TextFieldValue("")) }
val passwordRepeat = remember { mutableStateOf(TextFieldValue("")) } val passwordRepeat = remember { mutableStateOf(TextFieldValue("")) }
val showError = remember { mutableStateOf(false) }
val userViewModel: UserViewModel = hiltViewModel<UserViewModel>() val userViewModel: UserViewModel = hiltViewModel<UserViewModel>()
if (userViewModel.registrationState.observeAsState().value == true) { if (userViewModel.registrationState.observeAsState().value == true) {
@ -64,9 +62,8 @@ fun RegistrationCard(navHostController: NavHostController){
} }
} }
} }
showError.value = userViewModel.registrationState.observeAsState().value == false
if (showError.value) { if (userViewModel.registrationState.observeAsState().value == false) {
AlertDialog( AlertDialog(
title = { title = {
Text( Text(
@ -85,12 +82,12 @@ fun RegistrationCard(navHostController: NavHostController){
) )
}, },
onDismissRequest = { onDismissRequest = {
userViewModel.calmRegisterState() userViewModel.calmRegistrationState()
}, },
buttons = { buttons = {
TextButton( TextButton(
onClick = { onClick = {
userViewModel.calmRegisterState() userViewModel.calmRegistrationState()
} }
) { ) {
Text("ОК") Text("ОК")

View File

@ -19,6 +19,9 @@ class UserViewModel @Inject constructor(
private val _userModel = MutableLiveData<UserModel?>() private val _userModel = MutableLiveData<UserModel?>()
val userModel: LiveData<UserModel?> val userModel: LiveData<UserModel?>
get() = _userModel get() = _userModel
private val _authorizationState = MutableLiveData<Boolean?>()
val authorizationState: LiveData<Boolean?>
get() = _authorizationState
fun login(login: String, password: String){ fun login(login: String, password: String){
viewModelScope.launch { viewModelScope.launch {
@ -26,15 +29,21 @@ class UserViewModel @Inject constructor(
userRepository.login(login, password).collect() { user -> userRepository.login(login, password).collect() { user ->
if (user != null) { if (user != null) {
_userModel.postValue(user) _userModel.postValue(user)
_authorizationState.postValue(true)
} }
else { else {
_userModel.postValue(null) _userModel.postValue(null)
_authorizationState.postValue(false)
} }
} }
} }
} }
} }
fun calmAuthorizationState() {
_authorizationState.postValue(null)
}
private val _registrationState = MutableLiveData<Boolean?>() private val _registrationState = MutableLiveData<Boolean?>()
val registrationState: LiveData<Boolean?> val registrationState: LiveData<Boolean?>
get() = _registrationState get() = _registrationState
@ -74,7 +83,7 @@ class UserViewModel @Inject constructor(
} }
} }
fun calmRegisterState() { fun calmRegistrationState() {
_registrationState.postValue(null) _registrationState.postValue(null)
} }