From 0cc1d56a3a4d029a566a56e3a4680cde40c8cd66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=D0=B8=D0=BC?= Date: Thu, 23 Nov 2023 16:52:30 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=8B=D1=82=D0=B0=D1=8E=D1=81=D1=8C=20?= =?UTF-8?q?=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=D1=82=D1=8C=20=D0=B0=D0=B2=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D1=8E,=20=D0=BF?= =?UTF-8?q?=D0=BE=D0=BA=D0=B0=20=D1=87=D1=82=D0=BE=20=D0=BD=D0=B5=20=D1=80?= =?UTF-8?q?=D0=BE=D0=B1=D0=B8=D1=82=20:(?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../labwork/button_navigation/Screens.kt | 5 +- .../labwork/button_navigation/SlideGraph.kt | 3 +- .../example/labwork/pages/user/LoginPage.kt | 67 ++++++++----------- .../example/labwork/pages/user/ProfileForm.kt | 17 ++--- .../example/labwork/pages/user/RegOrLog.kt | 7 +- .../labwork/pages/user/RegisteryPage.kt | 9 ++- .../labwork/viewmodel/UserViewModel.kt | 32 ++++++++- 7 files changed, 79 insertions(+), 61 deletions(-) diff --git a/app/src/main/java/com/example/labwork/button_navigation/Screens.kt b/app/src/main/java/com/example/labwork/button_navigation/Screens.kt index 6c20ba9..b1dc33f 100644 --- a/app/src/main/java/com/example/labwork/button_navigation/Screens.kt +++ b/app/src/main/java/com/example/labwork/button_navigation/Screens.kt @@ -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 diff --git a/app/src/main/java/com/example/labwork/button_navigation/SlideGraph.kt b/app/src/main/java/com/example/labwork/button_navigation/SlideGraph.kt index 73e7d08..ddf2027 100644 --- a/app/src/main/java/com/example/labwork/button_navigation/SlideGraph.kt +++ b/app/src/main/java/com/example/labwork/button_navigation/SlideGraph.kt @@ -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() diff --git a/app/src/main/java/com/example/labwork/pages/user/LoginPage.kt b/app/src/main/java/com/example/labwork/pages/user/LoginPage.kt index ecdd263..1c8df09 100644 --- a/app/src/main/java/com/example/labwork/pages/user/LoginPage.kt +++ b/app/src/main/java/com/example/labwork/pages/user/LoginPage.kt @@ -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(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(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)) diff --git a/app/src/main/java/com/example/labwork/pages/user/ProfileForm.kt b/app/src/main/java/com/example/labwork/pages/user/ProfileForm.kt index d9a9f36..fd96fad 100644 --- a/app/src/main/java/com/example/labwork/pages/user/ProfileForm.kt +++ b/app/src/main/java/com/example/labwork/pages/user/ProfileForm.kt @@ -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() + 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 ) } } - } + diff --git a/app/src/main/java/com/example/labwork/pages/user/RegOrLog.kt b/app/src/main/java/com/example/labwork/pages/user/RegOrLog.kt index 5f0b281..f2ee65c 100644 --- a/app/src/main/java/com/example/labwork/pages/user/RegOrLog.kt +++ b/app/src/main/java/com/example/labwork/pages/user/RegOrLog.kt @@ -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) } } } diff --git a/app/src/main/java/com/example/labwork/pages/user/RegisteryPage.kt b/app/src/main/java/com/example/labwork/pages/user/RegisteryPage.kt index 58d8c9e..466e0f0 100644 --- a/app/src/main/java/com/example/labwork/pages/user/RegisteryPage.kt +++ b/app/src/main/java/com/example/labwork/pages/user/RegisteryPage.kt @@ -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") } }, diff --git a/app/src/main/java/com/example/labwork/viewmodel/UserViewModel.kt b/app/src/main/java/com/example/labwork/viewmodel/UserViewModel.kt index cea78cb..64f05a6 100644 --- a/app/src/main/java/com/example/labwork/viewmodel/UserViewModel.kt +++ b/app/src/main/java/com/example/labwork/viewmodel/UserViewModel.kt @@ -12,6 +12,15 @@ class UserViewModel(private val userRepository: UserRepository) : ViewModel() { private val _users = MutableLiveData>() val users: LiveData> get() = _users + private val _loggedInUser = MutableLiveData() + val loggedInUser: LiveData get() = _loggedInUser + + private val _email = MutableLiveData() + val email: LiveData get() = _email + + private val _password = MutableLiveData() + val password: LiveData get() = _password + fun getAllUsers() { viewModelScope.launch { _users.value = userRepository.getAllUsers() @@ -42,4 +51,25 @@ class UserViewModel(private val userRepository: UserRepository) : ViewModel() { userRepository.deleteUser(user) } } -} \ No newline at end of file + + + 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 + } + } + } +}