Абалдеть, я не знаю, что сделал, на атворизация заработала
This commit is contained in:
parent
0cc1d56a3a
commit
dc5cc2e862
@ -1,5 +1,6 @@
|
||||
package com.example.labwork.pages.user
|
||||
|
||||
import android.widget.Toast
|
||||
import androidx.compose.foundation.Image
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
@ -34,12 +35,11 @@ import androidx.compose.ui.unit.sp
|
||||
import androidx.navigation.NavController
|
||||
import androidx.navigation.NavHostController
|
||||
import com.example.labwork.R
|
||||
import com.example.labwork.database.DAO.BicycleDao
|
||||
import com.example.labwork.database.DAO.UserDao
|
||||
import com.example.labwork.models.Bicycle
|
||||
import com.example.labwork.models.User
|
||||
import com.example.labwork.ui.theme.LightBluePolitech
|
||||
import com.example.labwork.viewmodel.UserViewModel
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
@ -49,19 +49,24 @@ fun LoginPage(
|
||||
navHostController: NavHostController,
|
||||
userViewModel: UserViewModel
|
||||
) {
|
||||
val email by remember { mutableStateOf("") }
|
||||
val password by remember { mutableStateOf("") }
|
||||
var email by remember { mutableStateOf("") }
|
||||
var password by remember { mutableStateOf("") }
|
||||
var showPassword by remember { mutableStateOf(false) }
|
||||
val acceptLogin by remember { mutableStateOf(false) }
|
||||
var user by remember { mutableStateOf<User?>(null) }
|
||||
var acceptLogin by remember { mutableStateOf(false) }
|
||||
|
||||
if (acceptLogin) {
|
||||
if (acceptLogin == true) {
|
||||
user?.let { userProfile ->
|
||||
ProfileForm(item = userProfile, userViewModel = userViewModel, navHostController = navHostController)
|
||||
}
|
||||
} else {
|
||||
//navHostController.navigate("Info")
|
||||
}
|
||||
|
||||
else {
|
||||
Column(
|
||||
modifier = Modifier.fillMaxSize().padding(16.dp),
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.padding(16.dp),
|
||||
verticalArrangement = Arrangement.Center,
|
||||
horizontalAlignment = Alignment.CenterHorizontally
|
||||
) {
|
||||
@ -74,9 +79,11 @@ fun LoginPage(
|
||||
|
||||
TextField(
|
||||
value = email,
|
||||
onValueChange = { userViewModel.setEmail(it) },
|
||||
onValueChange = { email = it },
|
||||
label = { Text("Почта") },
|
||||
modifier = Modifier.fillMaxWidth().padding(horizontal = 16.dp),
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(horizontal = 16.dp),
|
||||
textStyle = TextStyle(fontSize = 16.sp),
|
||||
colors = TextFieldDefaults.textFieldColors(
|
||||
cursorColor = LightBluePolitech,
|
||||
@ -93,9 +100,11 @@ fun LoginPage(
|
||||
|
||||
TextField(
|
||||
value = password,
|
||||
onValueChange = { userViewModel.setPassword(it) },
|
||||
onValueChange = { password = it },
|
||||
label = { Text("Пароль") },
|
||||
modifier = Modifier.fillMaxWidth().padding(horizontal = 16.dp),
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(horizontal = 16.dp),
|
||||
textStyle = TextStyle(fontSize = 16.sp),
|
||||
colors = TextFieldDefaults.textFieldColors(
|
||||
cursorColor = LightBluePolitech,
|
||||
@ -109,7 +118,9 @@ fun LoginPage(
|
||||
singleLine = true,
|
||||
visualTransformation = if (showPassword) VisualTransformation.None else PasswordVisualTransformation(),
|
||||
trailingIcon = {
|
||||
IconButton(onClick = { showPassword = !showPassword }) {
|
||||
IconButton(
|
||||
onClick = { showPassword = !showPassword }
|
||||
) {
|
||||
Image(
|
||||
painter = if (showPassword) painterResource(R.drawable.baseline_visibility) else painterResource(
|
||||
R.drawable.baseline_visibility_off
|
||||
@ -124,18 +135,20 @@ fun LoginPage(
|
||||
|
||||
Button(
|
||||
onClick = {
|
||||
userViewModel.login()
|
||||
GlobalScope.launch {
|
||||
val result = userViewModel.login(email, password)
|
||||
user = userViewModel.getUserByEmail(email)
|
||||
if (result) {
|
||||
acceptLogin = true
|
||||
}
|
||||
}
|
||||
},
|
||||
modifier = Modifier.fillMaxWidth().padding(16.dp),
|
||||
shape = RoundedCornerShape(16.dp),
|
||||
colors = ButtonDefaults.buttonColors(backgroundColor = LightBluePolitech)
|
||||
colors = ButtonDefaults.buttonColors(backgroundColor = LightBluePolitech,),
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(horizontal = 16.dp)
|
||||
) {
|
||||
Text(
|
||||
text = "Войти",
|
||||
color = Color.White,
|
||||
fontSize = 18.sp,
|
||||
textAlign = TextAlign.Center
|
||||
)
|
||||
Text(text = "Авторизоваться", color = Color.White)
|
||||
}
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
|
||||
|
@ -37,8 +37,6 @@ fun ProfileForm(item: User, userViewModel: UserViewModel, navHostController: Nav
|
||||
var name by remember { mutableStateOf(item.name) }
|
||||
var password by remember { mutableStateOf(item.password) }
|
||||
|
||||
val viewModel = viewModel<UserViewModel>()
|
||||
|
||||
Column(
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
) {
|
||||
@ -78,7 +76,7 @@ fun ProfileForm(item: User, userViewModel: UserViewModel, navHostController: Nav
|
||||
.fillMaxWidth()
|
||||
.padding(9.dp),
|
||||
onClick = {
|
||||
viewModel.updateUser(User(item.id!!, email, name, password))
|
||||
userViewModel.updateUser(User(item.id!!, email, name, password))
|
||||
navHostController.navigate("ListProduct")
|
||||
},
|
||||
shape = RoundedCornerShape(15.dp)
|
||||
|
@ -12,15 +12,6 @@ class UserViewModel(private val userRepository: UserRepository) : ViewModel() {
|
||||
private val _users = MutableLiveData<List<User>>()
|
||||
val users: LiveData<List<User>> get() = _users
|
||||
|
||||
private val _loggedInUser = MutableLiveData<User>()
|
||||
val loggedInUser: LiveData<User> get() = _loggedInUser
|
||||
|
||||
private val _email = MutableLiveData<String>()
|
||||
val email: LiveData<String> get() = _email
|
||||
|
||||
private val _password = MutableLiveData<String>()
|
||||
val password: LiveData<String> get() = _password
|
||||
|
||||
fun getAllUsers() {
|
||||
viewModelScope.launch {
|
||||
_users.value = userRepository.getAllUsers()
|
||||
@ -30,7 +21,6 @@ class UserViewModel(private val userRepository: UserRepository) : ViewModel() {
|
||||
fun getUserById(userId: Int) {
|
||||
viewModelScope.launch {
|
||||
val user = userRepository.getUserById(userId)
|
||||
// Обработка полученного пользователя
|
||||
}
|
||||
}
|
||||
|
||||
@ -52,24 +42,15 @@ class UserViewModel(private val userRepository: UserRepository) : ViewModel() {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun setEmail(email: String) {
|
||||
_email.value = email
|
||||
suspend fun login(email: String, password: String): Boolean {
|
||||
var isSuccess = false
|
||||
val user = userRepository.getUserByEmail(email)
|
||||
isSuccess = user != null && user.password == password
|
||||
return isSuccess
|
||||
}
|
||||
|
||||
fun setPassword(password: String) {
|
||||
_password.value = password
|
||||
}
|
||||
|
||||
fun login() {
|
||||
val email = _email.value
|
||||
val password = _password.value
|
||||
|
||||
if (email != null && password != null) {
|
||||
viewModelScope.launch {
|
||||
val user = userRepository.getUserByEmailAndPassword(email, password)
|
||||
_loggedInUser.value = user
|
||||
}
|
||||
}
|
||||
suspend fun getUserByEmail(email: String): User? {
|
||||
return userRepository.getUserByEmail(email)
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user