Валидация регистрации
This commit is contained in:
parent
505cbf19f3
commit
7d0206ee55
@ -9,10 +9,12 @@ import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material.AlertDialog
|
||||
import androidx.compose.material.Button
|
||||
import androidx.compose.material.ButtonDefaults
|
||||
import androidx.compose.material.Card
|
||||
import androidx.compose.material.Text
|
||||
import androidx.compose.material.TextButton
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.livedata.observeAsState
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
@ -21,6 +23,7 @@ import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
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.style.TextDecoration
|
||||
import androidx.compose.ui.unit.dp
|
||||
@ -30,6 +33,7 @@ import androidx.hilt.navigation.compose.hiltViewModel
|
||||
import androidx.navigation.NavHostController
|
||||
import com.example.shawarma.ui.theme.JejuFamily
|
||||
import com.example.shawarma.ui.theme.MyLightRed
|
||||
import com.example.shawarma.ui.theme.NunitoFamily
|
||||
import com.example.shawarma.utils.ScreenPaths
|
||||
import com.example.shawarma.viewmodels.UserViewModel
|
||||
import com.example.shawarma.widgets.MyTextField
|
||||
@ -49,6 +53,8 @@ fun RegistrationCard(navHostController: NavHostController){
|
||||
val password = remember { mutableStateOf(TextFieldValue("")) }
|
||||
val passwordRepeat = remember { mutableStateOf(TextFieldValue("")) }
|
||||
|
||||
val showError = remember { mutableStateOf(false) }
|
||||
|
||||
val userViewModel: UserViewModel = hiltViewModel<UserViewModel>()
|
||||
|
||||
if (userViewModel.registrationState.observeAsState().value == true) {
|
||||
@ -58,18 +64,56 @@ fun RegistrationCard(navHostController: NavHostController){
|
||||
}
|
||||
}
|
||||
}
|
||||
showError.value = userViewModel.registrationState.observeAsState().value == false
|
||||
|
||||
if (showError.value) {
|
||||
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.calmRegisterState()
|
||||
},
|
||||
buttons = {
|
||||
TextButton(
|
||||
onClick = {
|
||||
userViewModel.calmRegisterState()
|
||||
}
|
||||
) {
|
||||
Text("ОК")
|
||||
}
|
||||
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
Column(
|
||||
horizontalAlignment = Alignment.CenterHorizontally,
|
||||
modifier = Modifier
|
||||
.zIndex(2f)
|
||||
.verticalScroll(rememberScrollState())
|
||||
.imePadding().padding(10.dp)
|
||||
.imePadding()
|
||||
.padding(10.dp)
|
||||
.fillMaxHeight()
|
||||
) {
|
||||
Card(
|
||||
shape = RoundedCornerShape(20.dp),
|
||||
modifier = Modifier.size(275.dp, 380.dp + 72.dp).padding(top = 72.dp)
|
||||
modifier = Modifier
|
||||
.size(275.dp, 380.dp + 72.dp)
|
||||
.padding(top = 72.dp)
|
||||
) {
|
||||
Column(
|
||||
horizontalAlignment = Alignment.CenterHorizontally
|
||||
|
@ -40,9 +40,18 @@ class UserViewModel @Inject constructor(
|
||||
get() = _registrationState
|
||||
|
||||
fun register(login: String, password: String, passwordRepeat: String) {
|
||||
_registrationState.postValue(null)
|
||||
if (password != passwordRepeat) {
|
||||
// ругаться в ui
|
||||
_registrationState.postValue(false)
|
||||
return
|
||||
}
|
||||
if (login.length < 8 || login.length > 20) {
|
||||
_registrationState.postValue(false)
|
||||
return
|
||||
}
|
||||
if (password.length < 8 || password.length > 20) {
|
||||
_registrationState.postValue(false)
|
||||
return
|
||||
}
|
||||
viewModelScope.launch {
|
||||
withContext(Dispatchers.Main) {
|
||||
@ -56,11 +65,17 @@ class UserViewModel @Inject constructor(
|
||||
userRepository.insert(UserModel(null, login, password, "USER"))
|
||||
_registrationState.postValue(true)
|
||||
}
|
||||
}
|
||||
else {
|
||||
_registrationState.postValue(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
fun calmRegisterState() {
|
||||
_registrationState.postValue(null)
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user