Пытаюсь сделать авторизацию, пока что не робит :(

This commit is contained in:
Максим 2023-11-23 16:52:30 +03:00
parent 96d09edf51
commit 0cc1d56a3a
7 changed files with 79 additions and 61 deletions

View File

@ -23,6 +23,7 @@ import com.example.labwork.pages.product.ListProduct
import com.example.labwork.pages.user.RegisteryOrLogin
import com.example.labwork.repository.BicycleRepository
import com.example.labwork.viewmodel.BicycleViewModel
import com.example.labwork.viewmodel.UserViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
@ -32,8 +33,8 @@ fun ScreenInfo() {
}
@Composable
fun ScreenProfile(userDao: UserDao, navHostController: NavHostController) {
RegisteryOrLogin(userDao, navHostController)
fun ScreenProfile(userViewModel: UserViewModel, navHostController: NavHostController) {
RegisteryOrLogin(userViewModel, navHostController)
}
@Composable

View File

@ -31,8 +31,7 @@ fun SlideGraph(
NavHost(navController = navHostController, startDestination = "Profile") {
composable("Profile") {
//ScreenProfile(userViewModel, navHostController = navHostController)
ScreenListProduct(bicycleViewModel, navHostController = navHostController)
ScreenProfile(userViewModel, navHostController = navHostController)
}
composable("Info") {
ScreenInfo()

View File

@ -39,28 +39,29 @@ 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.GlobalScope
import kotlinx.coroutines.launch
@Composable
fun LoginPage(navController: NavController, userDao: UserDao, navHostController: NavHostController) {
var email by remember { mutableStateOf("") }
var password by remember { mutableStateOf("") }
var userPassword by remember { mutableStateOf("") }
var user by remember { mutableStateOf<User?>(null) }
fun LoginPage(
navController: NavController,
navHostController: NavHostController,
userViewModel: UserViewModel
) {
val email by remember { mutableStateOf("") }
val password by remember { mutableStateOf("") }
var showPassword by remember { mutableStateOf(false) }
var acceptLogin by remember { mutableStateOf(false) }
val acceptLogin by remember { mutableStateOf(false) }
var user by remember { mutableStateOf<User?>(null) }
if (acceptLogin == true) {
if (acceptLogin) {
user?.let { userProfile ->
ProfileForm(item = userProfile, userDao = userDao, navHostController = navHostController)
ProfileForm(item = userProfile, userViewModel = userViewModel, navHostController = navHostController)
}
}
else {
} else {
Column(
modifier = Modifier
.fillMaxSize()
.padding(16.dp),
modifier = Modifier.fillMaxSize().padding(16.dp),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
@ -73,11 +74,9 @@ fun LoginPage(navController: NavController, userDao: UserDao, navHostController:
TextField(
value = email,
onValueChange = { email = it },
onValueChange = { userViewModel.setEmail(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,
@ -94,11 +93,9 @@ fun LoginPage(navController: NavController, userDao: UserDao, navHostController:
TextField(
value = password,
onValueChange = { password = it },
onValueChange = { userViewModel.setPassword(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,
@ -112,9 +109,7 @@ fun LoginPage(navController: NavController, userDao: UserDao, navHostController:
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
@ -129,22 +124,18 @@ fun LoginPage(navController: NavController, userDao: UserDao, navHostController:
Button(
onClick = {
GlobalScope.launch {
userPassword = userDao.getPasswordByEmail(email)
user = userDao.getUserByEmail(email)
if (userPassword == password) {
acceptLogin = true
//navHostController.navigate("Profile")
}
}
userViewModel.login()
},
colors = ButtonDefaults.buttonColors(backgroundColor = LightBluePolitech,),
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 16.dp)
modifier = Modifier.fillMaxWidth().padding(16.dp),
shape = RoundedCornerShape(16.dp),
colors = ButtonDefaults.buttonColors(backgroundColor = LightBluePolitech)
) {
Text(text = "Авторизоваться", color = Color.White)
Text(
text = "Войти",
color = Color.White,
fontSize = 18.sp,
textAlign = TextAlign.Center
)
}
Spacer(modifier = Modifier.height(8.dp))

View File

@ -20,21 +20,25 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
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.GlobalScope
import kotlinx.coroutines.launch
@Composable
fun ProfileForm(item: User, userDao: UserDao, navHostController: NavHostController) {
fun ProfileForm(item: User, userViewModel: UserViewModel, navHostController: NavHostController) {
var email by remember { mutableStateOf(item.email) }
var name by remember { mutableStateOf(item.name) }
var password by remember { mutableStateOf(item.password) }
val viewModel = viewModel<UserViewModel>()
Column(
modifier = Modifier.fillMaxWidth()
) {
@ -74,14 +78,7 @@ fun ProfileForm(item: User, userDao: UserDao, navHostController: NavHostControll
.fillMaxWidth()
.padding(9.dp),
onClick = {
GlobalScope.launch {
val newUser = userDao.getUserById(item.id!!)
newUser.email = email
newUser.name = name
newUser.password = password
userDao.updateUser(newUser)
}
viewModel.updateUser(User(item.id!!, email, name, password))
navHostController.navigate("ListProduct")
},
shape = RoundedCornerShape(15.dp)
@ -111,5 +108,5 @@ fun ProfileForm(item: User, userDao: UserDao, navHostController: NavHostControll
)
}
}
}

View File

@ -38,17 +38,18 @@ import com.example.labwork.R
import com.example.labwork.database.DAO.BicycleDao
import com.example.labwork.database.DAO.UserDao
import com.example.labwork.ui.theme.LightBluePolitech
import com.example.labwork.viewmodel.UserViewModel
@Composable
fun RegisteryOrLogin(userDao: UserDao, navHostController: NavHostController) {
fun RegisteryOrLogin(userViewModel: UserViewModel, navHostController: NavHostController) {
val navController = rememberNavController()
NavHost(navController, startDestination = "login") {
composable("login") {
LoginPage(navController, userDao, navHostController)
LoginPage(navController = navController, userViewModel = userViewModel, navHostController = navHostController)
}
composable("register") {
RegisteryPage(navController, userDao, navHostController)
RegisteryPage(navController = navController, userViewModel = userViewModel, navHostController = navHostController)
}
}
}

View File

@ -35,11 +35,12 @@ 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.GlobalScope
import kotlinx.coroutines.launch
@Composable
fun RegisteryPage(navController: NavController, userDao: UserDao, navHostController: NavHostController) {
fun RegisteryPage(navController: NavController, userViewModel: UserViewModel, navHostController: NavHostController) {
var username by remember { mutableStateOf("") }
var password by remember { mutableStateOf("") }
var confirmPassword by remember { mutableStateOf("") }
@ -146,10 +147,8 @@ fun RegisteryPage(navController: NavController, userDao: UserDao, navHostControl
Button(
onClick = {
if (password == confirmPassword) {
GlobalScope.launch {
val newUser = User(null, name = username, email = email, password = password)
userDao.insertUser(newUser)
}
val newUser = User(null, name = username, email = email, password = password)
userViewModel.insertUser(newUser)
navController.navigate("login")
}
},

View File

@ -12,6 +12,15 @@ 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()
@ -42,4 +51,25 @@ class UserViewModel(private val userRepository: UserRepository) : ViewModel() {
userRepository.deleteUser(user)
}
}
}
fun setEmail(email: String) {
_email.value = email
}
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
}
}
}
}