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