Compare commits

...

2 Commits

3 changed files with 107 additions and 4 deletions

View File

@ -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
@ -22,6 +24,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
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
@ -32,6 +35,7 @@ import androidx.navigation.NavHostController
import com.example.shawarma.data.sharedpref.PreferencesManager
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
@ -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(
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier

View File

@ -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
@ -59,17 +63,54 @@ fun RegistrationCard(navHostController: NavHostController){
}
}
if (userViewModel.registrationState.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.calmRegistrationState()
},
buttons = {
TextButton(
onClick = {
userViewModel.calmRegistrationState()
}
) {
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

View File

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