Сделал ошибку для регистраций и авторизаций, победа

This commit is contained in:
Кашин Максим 2023-12-25 01:12:18 +04:00
parent 7ed6a2bf09
commit dc0b81dc74
2 changed files with 42 additions and 4 deletions

View File

@ -32,6 +32,7 @@ import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.shape.CircleShape import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField import androidx.compose.foundation.text.BasicTextField
import androidx.compose.material.AlertDialog
import androidx.compose.material.icons.Icons import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Warning import androidx.compose.material.icons.filled.Warning
import androidx.compose.material3.Button import androidx.compose.material3.Button
@ -39,9 +40,11 @@ import androidx.compose.material3.Card
import androidx.compose.material3.Icon import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Snackbar
import androidx.compose.material3.Switch import androidx.compose.material3.Switch
import androidx.compose.material3.SwitchDefaults import androidx.compose.material3.SwitchDefaults
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
@ -89,6 +92,8 @@ fun UserProfile(
) { ) {
val context = LocalContext.current val context = LocalContext.current
var errorState by remember { mutableStateOf<String?>(null) }
var username by remember { mutableStateOf("") } var username by remember { mutableStateOf("") }
var password by remember { mutableStateOf("") } var password by remember { mutableStateOf("") }
var isRegistration by remember { mutableStateOf(false) } var isRegistration by remember { mutableStateOf(false) }
@ -162,13 +167,15 @@ fun UserProfile(
Button( Button(
onClick = { onClick = {
coroutineScope.launch { coroutineScope.launch {
val registrationSuccessful = val registrationSuccessful = registerUserViewModel.registerUser(username, password)
registerUserViewModel.registerUser(username, password)
if (registrationSuccessful) { if (registrationSuccessful) {
navController?.navigate(Screen.LoginScreen.route) navController?.navigate(Screen.LoginScreen.route)
} else { } else {
Log.d("User already exists.", "Registration failed.") Log.d("Регистрация","Ошибка")
// При других ошибках регистрации выводим сообщение
errorState = "Пользователь с таким логином уже существует"
} }
} }
}, },
modifier = Modifier modifier = Modifier
@ -177,6 +184,7 @@ fun UserProfile(
) { ) {
Text("Регистрация") Text("Регистрация")
} }
Text( Text(
text = "Войти", text = "Войти",
modifier = Modifier modifier = Modifier
@ -194,7 +202,8 @@ fun UserProfile(
currentUserViewModel.setArgument(user.uid.toString()) currentUserViewModel.setArgument(user.uid.toString())
navController?.navigate(Screen.LoginScreen.route) navController?.navigate(Screen.LoginScreen.route)
} else { } else {
Log.d("Авторизация", "Пароль не совпадает") // При ошибке авторизации выводим сообщение
errorState = "Ошибка авторизации: неверный логин или пароль"
} }
}, },
modifier = Modifier modifier = Modifier
@ -203,6 +212,7 @@ fun UserProfile(
) { ) {
Text("Вход") Text("Вход")
} }
Text( Text(
text = "Зарегистрироваться", text = "Зарегистрироваться",
modifier = Modifier modifier = Modifier
@ -213,6 +223,28 @@ fun UserProfile(
color = MaterialTheme.colorScheme.onSurface color = MaterialTheme.colorScheme.onSurface
) )
} }
errorState?.let { errorMessage ->
AlertDialog(
onDismissRequest = { errorState = null },
title = { Text(text = "Ошибка") },
text = { Text(errorMessage, color = MaterialTheme.colorScheme.onBackground) },
buttons = {
Row(
modifier = Modifier
.fillMaxWidth()
.padding(8.dp),
horizontalArrangement = Arrangement.End
) {
TextButton(onClick = { errorState = null }) {
Text("Закрыть", color = MaterialTheme.colorScheme.onBackground)
}
}
},
modifier = Modifier.padding(16.dp),
backgroundColor = MaterialTheme.colorScheme.background,
contentColor = MaterialTheme.colorScheme.onBackground
)
}
val switchColors = SwitchDefaults.colors( val switchColors = SwitchDefaults.colors(
checkedThumbColor = MaterialTheme.colorScheme.primary, // Change the color when the switch is checked checkedThumbColor = MaterialTheme.colorScheme.primary, // Change the color when the switch is checked
checkedTrackColor = MaterialTheme.colorScheme.secondary, // Change the color of the track when the switch is checked checkedTrackColor = MaterialTheme.colorScheme.secondary, // Change the color of the track when the switch is checked

View File

@ -120,6 +120,12 @@
"login": "привет 2", "login": "привет 2",
"role": "user", "role": "user",
"password": "привет 2" "password": "привет 2"
},
{
"id": 18,
"login": "q2",
"role": "user",
"password": "q"
} }
], ],
"rents": [ "rents": [