Что то вынес, туда сюда кидаю, надо отчеты делать, а я фигней маюсь...
This commit is contained in:
parent
e4b66ae036
commit
0456d68b16
@ -17,4 +17,8 @@ class EntryUserViewModel(private val userRepository: UserRepository) : ViewModel
|
||||
userList=userRepository.getAllUsers()
|
||||
}
|
||||
}
|
||||
|
||||
fun loginUser(username: String, password: String): User? {
|
||||
return userList.find { it.login == username && it.password == password }
|
||||
}
|
||||
}
|
@ -23,4 +23,16 @@ class RegisterUserViewModel(private val userRepository: UserRepository) : ViewMo
|
||||
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
|
||||
}
|
||||
}
|
@ -79,50 +79,8 @@ fun UserProfile(
|
||||
// Проверяем, авторизован пользователь или нет
|
||||
if (getUser?.uid != null) {
|
||||
|
||||
Column(
|
||||
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)
|
||||
)
|
||||
LoginScreenProfile(currentUserViewModel, navController)
|
||||
|
||||
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 {
|
||||
|
||||
LazyColumn {
|
||||
@ -173,42 +131,12 @@ fun UserProfile(
|
||||
if (isRegistration) {
|
||||
Button(
|
||||
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 {
|
||||
val insertResult = async {
|
||||
registerUserViewModel.insertUser(newUser)
|
||||
}
|
||||
|
||||
insertResult.await()
|
||||
|
||||
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()))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
val registrationSuccessful = registerUserViewModel.registerUser(username, password)
|
||||
if (registrationSuccessful) {
|
||||
navController?.navigate(Screen.LoginScreen.route)
|
||||
} else {
|
||||
Log.d("User already exists.", "Registration failed.")
|
||||
}
|
||||
}
|
||||
},
|
||||
@ -230,21 +158,13 @@ fun UserProfile(
|
||||
} else {
|
||||
Button(
|
||||
onClick = {
|
||||
Log.d("Кнопка", "Тык")
|
||||
if (password.isNotEmpty()) {
|
||||
users_entry.value.forEach { user ->
|
||||
if (user.password == password) {
|
||||
val user = entryUserViewModel.loginUser(username, password)
|
||||
if (user != null) {
|
||||
currentUserViewModel.setArgument(user.uid.toString())
|
||||
navController?.navigate(Screen.LoginScreen.route)
|
||||
/* navController?.navigate(Screen.UserProfile.route)
|
||||
navController?.popBackStack()*/
|
||||
//navController.navigate(route = Graph.passUserId(user.id.toString())) {
|
||||
} else {
|
||||
Log.d("Авторизация", "Пароль не совпадает")
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
},
|
||||
modifier = Modifier
|
||||
.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
|
||||
fun UserProfilePreview() {
|
||||
PmudemoTheme {
|
||||
Surface(
|
||||
color = MaterialTheme.colorScheme.background
|
||||
fun LoginScreenProfile(currentUserViewModel: CurrentUserViewModel = viewModel(factory = AppViewModelProvider.Factory), navController: NavController?) {
|
||||
var getUser by remember { mutableStateOf(currentUserViewModel.user) }
|
||||
Column(
|
||||
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("Выйти")
|
||||
}
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
|
@ -55,7 +55,49 @@
|
||||
{
|
||||
"id": 9,
|
||||
"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": [
|
||||
@ -71,6 +113,19 @@
|
||||
"bikeId": 2
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": 10,
|
||||
"userId": 9,
|
||||
"items": [
|
||||
{
|
||||
"id": 6,
|
||||
"dateTime": 2020,
|
||||
"frozenWeight": 23.6,
|
||||
"count": 3,
|
||||
"bikeId": 3
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"items": [
|
||||
|
Loading…
Reference in New Issue
Block a user