Пытаюсь сделать авторизацию, пока что не робит :(
This commit is contained in:
parent
96d09edf51
commit
0cc1d56a3a
@ -23,6 +23,7 @@ import com.example.labwork.pages.product.ListProduct
|
|||||||
import com.example.labwork.pages.user.RegisteryOrLogin
|
import com.example.labwork.pages.user.RegisteryOrLogin
|
||||||
import com.example.labwork.repository.BicycleRepository
|
import com.example.labwork.repository.BicycleRepository
|
||||||
import com.example.labwork.viewmodel.BicycleViewModel
|
import com.example.labwork.viewmodel.BicycleViewModel
|
||||||
|
import com.example.labwork.viewmodel.UserViewModel
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
|
|
||||||
@ -32,8 +33,8 @@ fun ScreenInfo() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun ScreenProfile(userDao: UserDao, navHostController: NavHostController) {
|
fun ScreenProfile(userViewModel: UserViewModel, navHostController: NavHostController) {
|
||||||
RegisteryOrLogin(userDao, navHostController)
|
RegisteryOrLogin(userViewModel, navHostController)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
|
@ -31,8 +31,7 @@ fun SlideGraph(
|
|||||||
|
|
||||||
NavHost(navController = navHostController, startDestination = "Profile") {
|
NavHost(navController = navHostController, startDestination = "Profile") {
|
||||||
composable("Profile") {
|
composable("Profile") {
|
||||||
//ScreenProfile(userViewModel, navHostController = navHostController)
|
ScreenProfile(userViewModel, navHostController = navHostController)
|
||||||
ScreenListProduct(bicycleViewModel, navHostController = navHostController)
|
|
||||||
}
|
}
|
||||||
composable("Info") {
|
composable("Info") {
|
||||||
ScreenInfo()
|
ScreenInfo()
|
||||||
|
@ -39,28 +39,29 @@ import com.example.labwork.database.DAO.UserDao
|
|||||||
import com.example.labwork.models.Bicycle
|
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 kotlinx.coroutines.GlobalScope
|
import kotlinx.coroutines.GlobalScope
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun LoginPage(navController: NavController, userDao: UserDao, navHostController: NavHostController) {
|
fun LoginPage(
|
||||||
var email by remember { mutableStateOf("") }
|
navController: NavController,
|
||||||
var password by remember { mutableStateOf("") }
|
navHostController: NavHostController,
|
||||||
var userPassword by remember { mutableStateOf("") }
|
userViewModel: UserViewModel
|
||||||
var user by remember { mutableStateOf<User?>(null) }
|
) {
|
||||||
|
val email by remember { mutableStateOf("") }
|
||||||
|
val password by remember { mutableStateOf("") }
|
||||||
var showPassword by remember { mutableStateOf(false) }
|
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 ->
|
user?.let { userProfile ->
|
||||||
ProfileForm(item = userProfile, userDao = userDao, navHostController = navHostController)
|
ProfileForm(item = userProfile, userViewModel = userViewModel, navHostController = navHostController)
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
Column(
|
Column(
|
||||||
modifier = Modifier
|
modifier = Modifier.fillMaxSize().padding(16.dp),
|
||||||
.fillMaxSize()
|
|
||||||
.padding(16.dp),
|
|
||||||
verticalArrangement = Arrangement.Center,
|
verticalArrangement = Arrangement.Center,
|
||||||
horizontalAlignment = Alignment.CenterHorizontally
|
horizontalAlignment = Alignment.CenterHorizontally
|
||||||
) {
|
) {
|
||||||
@ -73,11 +74,9 @@ fun LoginPage(navController: NavController, userDao: UserDao, navHostController:
|
|||||||
|
|
||||||
TextField(
|
TextField(
|
||||||
value = email,
|
value = email,
|
||||||
onValueChange = { email = it },
|
onValueChange = { userViewModel.setEmail(it) },
|
||||||
label = { Text("Почта") },
|
label = { Text("Почта") },
|
||||||
modifier = Modifier
|
modifier = Modifier.fillMaxWidth().padding(horizontal = 16.dp),
|
||||||
.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,
|
||||||
@ -94,11 +93,9 @@ fun LoginPage(navController: NavController, userDao: UserDao, navHostController:
|
|||||||
|
|
||||||
TextField(
|
TextField(
|
||||||
value = password,
|
value = password,
|
||||||
onValueChange = { password = it },
|
onValueChange = { userViewModel.setPassword(it) },
|
||||||
label = { Text("Пароль") },
|
label = { Text("Пароль") },
|
||||||
modifier = Modifier
|
modifier = Modifier.fillMaxWidth().padding(horizontal = 16.dp),
|
||||||
.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,
|
||||||
@ -112,9 +109,7 @@ fun LoginPage(navController: NavController, userDao: UserDao, navHostController:
|
|||||||
singleLine = true,
|
singleLine = true,
|
||||||
visualTransformation = if (showPassword) VisualTransformation.None else PasswordVisualTransformation(),
|
visualTransformation = if (showPassword) VisualTransformation.None else PasswordVisualTransformation(),
|
||||||
trailingIcon = {
|
trailingIcon = {
|
||||||
IconButton(
|
IconButton(onClick = { showPassword = !showPassword }) {
|
||||||
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
|
||||||
@ -129,22 +124,18 @@ fun LoginPage(navController: NavController, userDao: UserDao, navHostController:
|
|||||||
|
|
||||||
Button(
|
Button(
|
||||||
onClick = {
|
onClick = {
|
||||||
GlobalScope.launch {
|
userViewModel.login()
|
||||||
userPassword = userDao.getPasswordByEmail(email)
|
|
||||||
user = userDao.getUserByEmail(email)
|
|
||||||
|
|
||||||
if (userPassword == password) {
|
|
||||||
acceptLogin = true
|
|
||||||
//navHostController.navigate("Profile")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
colors = ButtonDefaults.buttonColors(backgroundColor = LightBluePolitech,),
|
modifier = Modifier.fillMaxWidth().padding(16.dp),
|
||||||
modifier = Modifier
|
shape = RoundedCornerShape(16.dp),
|
||||||
.fillMaxWidth()
|
colors = ButtonDefaults.buttonColors(backgroundColor = LightBluePolitech)
|
||||||
.padding(horizontal = 16.dp)
|
|
||||||
) {
|
) {
|
||||||
Text(text = "Авторизоваться", color = Color.White)
|
Text(
|
||||||
|
text = "Войти",
|
||||||
|
color = Color.White,
|
||||||
|
fontSize = 18.sp,
|
||||||
|
textAlign = TextAlign.Center
|
||||||
|
)
|
||||||
}
|
}
|
||||||
Spacer(modifier = Modifier.height(8.dp))
|
Spacer(modifier = Modifier.height(8.dp))
|
||||||
|
|
||||||
|
@ -20,21 +20,25 @@ import androidx.compose.ui.graphics.Color
|
|||||||
import androidx.compose.ui.text.style.TextAlign
|
import androidx.compose.ui.text.style.TextAlign
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import androidx.compose.ui.unit.sp
|
import androidx.compose.ui.unit.sp
|
||||||
|
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||||
import androidx.navigation.NavHostController
|
import androidx.navigation.NavHostController
|
||||||
import com.example.labwork.database.DAO.BicycleDao
|
import com.example.labwork.database.DAO.BicycleDao
|
||||||
import com.example.labwork.database.DAO.UserDao
|
import com.example.labwork.database.DAO.UserDao
|
||||||
import com.example.labwork.models.Bicycle
|
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 kotlinx.coroutines.GlobalScope
|
import kotlinx.coroutines.GlobalScope
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
@Composable
|
@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 email by remember { mutableStateOf(item.email) }
|
||||||
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()
|
||||||
) {
|
) {
|
||||||
@ -74,14 +78,7 @@ fun ProfileForm(item: User, userDao: UserDao, navHostController: NavHostControll
|
|||||||
.fillMaxWidth()
|
.fillMaxWidth()
|
||||||
.padding(9.dp),
|
.padding(9.dp),
|
||||||
onClick = {
|
onClick = {
|
||||||
GlobalScope.launch {
|
viewModel.updateUser(User(item.id!!, email, name, password))
|
||||||
val newUser = userDao.getUserById(item.id!!)
|
|
||||||
newUser.email = email
|
|
||||||
newUser.name = name
|
|
||||||
newUser.password = password
|
|
||||||
|
|
||||||
userDao.updateUser(newUser)
|
|
||||||
}
|
|
||||||
navHostController.navigate("ListProduct")
|
navHostController.navigate("ListProduct")
|
||||||
},
|
},
|
||||||
shape = RoundedCornerShape(15.dp)
|
shape = RoundedCornerShape(15.dp)
|
||||||
@ -111,5 +108,5 @@ fun ProfileForm(item: User, userDao: UserDao, navHostController: NavHostControll
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,17 +38,18 @@ import com.example.labwork.R
|
|||||||
import com.example.labwork.database.DAO.BicycleDao
|
import com.example.labwork.database.DAO.BicycleDao
|
||||||
import com.example.labwork.database.DAO.UserDao
|
import com.example.labwork.database.DAO.UserDao
|
||||||
import com.example.labwork.ui.theme.LightBluePolitech
|
import com.example.labwork.ui.theme.LightBluePolitech
|
||||||
|
import com.example.labwork.viewmodel.UserViewModel
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun RegisteryOrLogin(userDao: UserDao, navHostController: NavHostController) {
|
fun RegisteryOrLogin(userViewModel: UserViewModel, navHostController: NavHostController) {
|
||||||
val navController = rememberNavController()
|
val navController = rememberNavController()
|
||||||
|
|
||||||
NavHost(navController, startDestination = "login") {
|
NavHost(navController, startDestination = "login") {
|
||||||
composable("login") {
|
composable("login") {
|
||||||
LoginPage(navController, userDao, navHostController)
|
LoginPage(navController = navController, userViewModel = userViewModel, navHostController = navHostController)
|
||||||
}
|
}
|
||||||
composable("register") {
|
composable("register") {
|
||||||
RegisteryPage(navController, userDao, navHostController)
|
RegisteryPage(navController = navController, userViewModel = userViewModel, navHostController = navHostController)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,11 +35,12 @@ import com.example.labwork.database.DAO.UserDao
|
|||||||
import com.example.labwork.models.Bicycle
|
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 kotlinx.coroutines.GlobalScope
|
import kotlinx.coroutines.GlobalScope
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun RegisteryPage(navController: NavController, userDao: UserDao, navHostController: NavHostController) {
|
fun RegisteryPage(navController: NavController, userViewModel: UserViewModel, navHostController: NavHostController) {
|
||||||
var username by remember { mutableStateOf("") }
|
var username by remember { mutableStateOf("") }
|
||||||
var password by remember { mutableStateOf("") }
|
var password by remember { mutableStateOf("") }
|
||||||
var confirmPassword by remember { mutableStateOf("") }
|
var confirmPassword by remember { mutableStateOf("") }
|
||||||
@ -146,10 +147,8 @@ fun RegisteryPage(navController: NavController, userDao: UserDao, navHostControl
|
|||||||
Button(
|
Button(
|
||||||
onClick = {
|
onClick = {
|
||||||
if (password == confirmPassword) {
|
if (password == confirmPassword) {
|
||||||
GlobalScope.launch {
|
val newUser = User(null, name = username, email = email, password = password)
|
||||||
val newUser = User(null, name = username, email = email, password = password)
|
userViewModel.insertUser(newUser)
|
||||||
userDao.insertUser(newUser)
|
|
||||||
}
|
|
||||||
navController.navigate("login")
|
navController.navigate("login")
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -12,6 +12,15 @@ 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()
|
||||||
@ -42,4 +51,25 @@ class UserViewModel(private val userRepository: UserRepository) : ViewModel() {
|
|||||||
userRepository.deleteUser(user)
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user