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

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.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.material.AlertDialog
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Warning
import androidx.compose.material3.Button
@ -39,9 +40,11 @@ import androidx.compose.material3.Card
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Snackbar
import androidx.compose.material3.Switch
import androidx.compose.material3.SwitchDefaults
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
@ -89,6 +92,8 @@ fun UserProfile(
) {
val context = LocalContext.current
var errorState by remember { mutableStateOf<String?>(null) }
var username by remember { mutableStateOf("") }
var password by remember { mutableStateOf("") }
var isRegistration by remember { mutableStateOf(false) }
@ -162,13 +167,15 @@ fun UserProfile(
Button(
onClick = {
coroutineScope.launch {
val registrationSuccessful =
registerUserViewModel.registerUser(username, password)
val registrationSuccessful = registerUserViewModel.registerUser(username, password)
if (registrationSuccessful) {
navController?.navigate(Screen.LoginScreen.route)
} else {
Log.d("User already exists.", "Registration failed.")
Log.d("Регистрация","Ошибка")
// При других ошибках регистрации выводим сообщение
errorState = "Пользователь с таким логином уже существует"
}
}
},
modifier = Modifier
@ -177,6 +184,7 @@ fun UserProfile(
) {
Text("Регистрация")
}
Text(
text = "Войти",
modifier = Modifier
@ -194,7 +202,8 @@ fun UserProfile(
currentUserViewModel.setArgument(user.uid.toString())
navController?.navigate(Screen.LoginScreen.route)
} else {
Log.d("Авторизация", "Пароль не совпадает")
// При ошибке авторизации выводим сообщение
errorState = "Ошибка авторизации: неверный логин или пароль"
}
},
modifier = Modifier
@ -203,6 +212,7 @@ fun UserProfile(
) {
Text("Вход")
}
Text(
text = "Зарегистрироваться",
modifier = Modifier
@ -213,6 +223,28 @@ fun UserProfile(
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(
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

View File

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