From 7d0206ee55efd8cf7fb76446cf0652377857dccf Mon Sep 17 00:00:00 2001 From: danyasedoy Date: Sat, 2 Dec 2023 20:43:42 +0400 Subject: [PATCH] =?UTF-8?q?=D0=92=D0=B0=D0=BB=D0=B8=D0=B4=D0=B0=D1=86?= =?UTF-8?q?=D0=B8=D1=8F=20=D1=80=D0=B5=D0=B3=D0=B8=D1=81=D1=82=D1=80=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../registration/RegistrationScreen.kt | 48 ++++++++++++++++++- .../shawarma/viewmodels/UserViewModel.kt | 19 +++++++- 2 files changed, 63 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/example/shawarma/screens/registration/RegistrationScreen.kt b/app/src/main/java/com/example/shawarma/screens/registration/RegistrationScreen.kt index 21bb236..c87440d 100644 --- a/app/src/main/java/com/example/shawarma/screens/registration/RegistrationScreen.kt +++ b/app/src/main/java/com/example/shawarma/screens/registration/RegistrationScreen.kt @@ -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() 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 diff --git a/app/src/main/java/com/example/shawarma/viewmodels/UserViewModel.kt b/app/src/main/java/com/example/shawarma/viewmodels/UserViewModel.kt index f681967..31fe2b9 100644 --- a/app/src/main/java/com/example/shawarma/viewmodels/UserViewModel.kt +++ b/app/src/main/java/com/example/shawarma/viewmodels/UserViewModel.kt @@ -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) + } + } \ No newline at end of file