Что то вынес, туда сюда кидаю, надо отчеты делать, а я фигней маюсь...

This commit is contained in:
Кашин Максим 2023-12-24 17:18:59 +04:00
parent e4b66ae036
commit 0456d68b16
4 changed files with 126 additions and 102 deletions

View File

@ -17,4 +17,8 @@ class EntryUserViewModel(private val userRepository: UserRepository) : ViewModel
userList=userRepository.getAllUsers() userList=userRepository.getAllUsers()
} }
} }
fun loginUser(username: String, password: String): User? {
return userList.find { it.login == username && it.password == password }
}
} }

View File

@ -23,4 +23,16 @@ class RegisterUserViewModel(private val userRepository: UserRepository) : ViewMo
userRepository.insertUser(user) userRepository.insertUser(user)
} }
suspend fun registerUser(username: String, password: String): Boolean {
if (password.isNotEmpty() && username.isNotEmpty()) {
val existingUser = _users.value?.find { it.login == username }
if (existingUser == null) {
val newUser = User(null, username, password)
insertUser(newUser)
setUserList()
return true
}
}
return false
}
} }

View File

@ -79,50 +79,8 @@ fun UserProfile(
// Проверяем, авторизован пользователь или нет // Проверяем, авторизован пользователь или нет
if (getUser?.uid != null) { if (getUser?.uid != null) {
Column( LoginScreenProfile(currentUserViewModel, navController)
modifier = Modifier
.fillMaxSize()
.padding(16.dp),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
// Display the user's photo (replace the Image composable with your logic)
Image(
painter = painterResource(id = R.drawable.ic_launcher_foreground), // Replace with your photo
contentDescription = null,
modifier = Modifier
.size(120.dp)
.clip(CircleShape)
.background(MaterialTheme.colorScheme.primary)
)
Spacer(modifier = Modifier.height(16.dp))
// Display the logged-in message
Text(
text = "Вы авторизованы под аккаунтом: ${getUser?.login}",
//style = MaterialTheme.typography.h6,
textAlign = TextAlign.Center
)
Spacer(modifier = Modifier.height(16.dp))
// "Logout" button
Button(
onClick = {
// Perform logout action
currentUserViewModel.setNullUser()
navController?.navigate(Screen.LoginScreen.route)
// Navigate back to the login screen
//navController?.popBackStack()
},
modifier = Modifier
.fillMaxWidth()
.padding(8.dp)
) {
Text("Выйти")
}
}
} else { } else {
LazyColumn { LazyColumn {
@ -173,42 +131,12 @@ fun UserProfile(
if (isRegistration) { if (isRegistration) {
Button( Button(
onClick = { onClick = {
var isExist = false;
if (password.isNotEmpty() && username.isNotEmpty()) {
users.value.forEach { user ->
if (user.login == username) {
Log.d(
"User already exist. User id: ",
user.uid.toString()
)
isExist = true
}
}
if (!isExist) {
val newUser = User(null, username, password)
coroutineScope.launch { coroutineScope.launch {
val insertResult = async { val registrationSuccessful = registerUserViewModel.registerUser(username, password)
registerUserViewModel.insertUser(newUser) if (registrationSuccessful) {
} navController?.navigate(Screen.LoginScreen.route)
} else {
insertResult.await() Log.d("User already exists.", "Registration failed.")
registerUserViewModel.setUserList()
registerUserViewModel.users.observeForever { userList ->
users.value = userList
Log.println(
Log.ASSERT,
"UsersList",
users.value.toString()
)
users.value?.forEach { user ->
if (user.password == password) {
currentUserViewModel.setArgument(user.uid.toString())
//navController.navigate(route = Graph.passUserId(user.id.toString()))
}
}
}
}
} }
} }
}, },
@ -230,21 +158,13 @@ fun UserProfile(
} else { } else {
Button( Button(
onClick = { onClick = {
Log.d("Кнопка", "Тык") val user = entryUserViewModel.loginUser(username, password)
if (password.isNotEmpty()) { if (user != null) {
users_entry.value.forEach { user ->
if (user.password == password) {
currentUserViewModel.setArgument(user.uid.toString()) currentUserViewModel.setArgument(user.uid.toString())
navController?.navigate(Screen.LoginScreen.route) navController?.navigate(Screen.LoginScreen.route)
/* navController?.navigate(Screen.UserProfile.route)
navController?.popBackStack()*/
//navController.navigate(route = Graph.passUserId(user.id.toString())) {
} else { } else {
Log.d("Авторизация", "Пароль не совпадает") Log.d("Авторизация", "Пароль не совпадает")
} }
}
}
}, },
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
@ -328,15 +248,48 @@ fun LoginScreen(navController: NavController?) {
} }
} }
/*@Preview(name = "Light Mode", showBackground = true, uiMode = Configuration.UI_MODE_NIGHT_NO)
@Preview(name = "Dark Mode", showBackground = true, uiMode = Configuration.UI_MODE_NIGHT_YES)
@Composable @Composable
fun UserProfilePreview() { fun LoginScreenProfile(currentUserViewModel: CurrentUserViewModel = viewModel(factory = AppViewModelProvider.Factory), navController: NavController?) {
PmudemoTheme { var getUser by remember { mutableStateOf(currentUserViewModel.user) }
Surface( Column(
color = MaterialTheme.colorScheme.background modifier = Modifier
.fillMaxSize()
.padding(16.dp),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) { ) {
UserProfile(navController = null, isDarkTheme = remember { mutableStateOf(true) }) Image(
painter = painterResource(id = R.drawable.ic_launcher_foreground),
contentDescription = null,
modifier = Modifier
.size(120.dp)
.clip(CircleShape)
.background(MaterialTheme.colorScheme.primary)
)
Spacer(modifier = Modifier.height(16.dp))
Text(
text = "Вы авторизованы под аккаунтом: ${getUser?.login}",
textAlign = TextAlign.Center
)
Spacer(modifier = Modifier.height(16.dp))
Button(
onClick = {
// Perform logout action
currentUserViewModel.setNullUser()
navController?.navigate(Screen.LoginScreen.route)
// Navigate back to the login screen
//navController?.popBackStack()
},
modifier = Modifier
.fillMaxWidth()
.padding(8.dp)
) {
Text("Выйти")
} }
} }
}*/ }

View File

@ -55,7 +55,49 @@
{ {
"id": 9, "id": 9,
"login": "привет", "login": "привет",
"password": "привет" "password": "привет",
"items": [
{
"id": 6,
"count": 1,
"bikeId": 3
}
]
},
{
"id": 10,
"login": "q",
"password": "q"
},
{
"id": 11,
"login": "пр",
"password": "пр"
},
{
"id": 12,
"login": "лог",
"password": "логин"
},
{
"id": 13,
"login": "привето",
"password": "приаето"
},
{
"id": 14,
"login": "123",
"password": "123"
},
{
"id": 15,
"login": "лол",
"password": "лол"
},
{
"id": 16,
"login": "хелп",
"password": "хелло"
} }
], ],
"rents": [ "rents": [
@ -71,6 +113,19 @@
"bikeId": 2 "bikeId": 2
} }
] ]
},
{
"id": 10,
"userId": 9,
"items": [
{
"id": 6,
"dateTime": 2020,
"frozenWeight": 23.6,
"count": 3,
"bikeId": 3
}
]
} }
], ],
"items": [ "items": [