diff --git a/app/src/main/java/com/example/labwork/models/User.kt b/app/src/main/java/com/example/labwork/models/User.kt index 49c050e..5f3e6a5 100644 --- a/app/src/main/java/com/example/labwork/models/User.kt +++ b/app/src/main/java/com/example/labwork/models/User.kt @@ -8,8 +8,8 @@ import androidx.room.PrimaryKey data class User( @PrimaryKey(autoGenerate = true) val id: Int?, - val name: String, - val email: String, - val password: String, + var name: String, + var email: String, + var password: String, ) 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 c74420d..ecdd263 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 @@ -9,6 +9,7 @@ import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size +import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material.Button import androidx.compose.material.ButtonDefaults import androidx.compose.material.IconButton @@ -27,12 +28,16 @@ import androidx.compose.ui.res.painterResource import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.input.PasswordVisualTransformation import androidx.compose.ui.text.input.VisualTransformation +import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.unit.dp 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 kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch @@ -42,105 +47,116 @@ fun LoginPage(navController: NavController, userDao: UserDao, navHostController: var email by remember { mutableStateOf("") } var password by remember { mutableStateOf("") } var userPassword by remember { mutableStateOf("") } + var user by remember { mutableStateOf(null) } var showPassword by remember { mutableStateOf(false) } var acceptLogin by remember { mutableStateOf(false) } - Column( - modifier = Modifier - .fillMaxSize() - .padding(16.dp), - verticalArrangement = Arrangement.Center, - horizontalAlignment = Alignment.CenterHorizontally - ) { - Image( - painter = painterResource(R.drawable.logo_ulstu), - contentDescription = "Logo", - modifier = Modifier.size(200.dp) - ) - Spacer(modifier = Modifier.height(16.dp)) - - TextField( - value = email, - onValueChange = { email = it }, - label = { Text("Почта") }, - modifier = Modifier - .fillMaxWidth() - .padding(horizontal = 16.dp), - textStyle = TextStyle(fontSize = 16.sp), - colors = TextFieldDefaults.textFieldColors( - cursorColor = LightBluePolitech, - backgroundColor = Color.White, - textColor = LightBluePolitech, - unfocusedLabelColor = LightBluePolitech, - focusedIndicatorColor = LightBluePolitech, - unfocusedIndicatorColor = LightBluePolitech, - focusedLabelColor = LightBluePolitech - ), - singleLine = true - ) - Spacer(modifier = Modifier.height(8.dp)) - - TextField( - value = password, - onValueChange = { password = it }, - label = { Text("Пароль") }, - modifier = Modifier - .fillMaxWidth() - .padding(horizontal = 16.dp), - textStyle = TextStyle(fontSize = 16.sp), - colors = TextFieldDefaults.textFieldColors( - cursorColor = LightBluePolitech, - backgroundColor = Color.White, - textColor = LightBluePolitech, - unfocusedLabelColor = LightBluePolitech, - focusedIndicatorColor = LightBluePolitech, - unfocusedIndicatorColor = LightBluePolitech, - focusedLabelColor = LightBluePolitech - ), - singleLine = true, - visualTransformation = if (showPassword) VisualTransformation.None else PasswordVisualTransformation(), - trailingIcon = { - IconButton( - onClick = { showPassword = !showPassword } - ) { - Image( - painter = if(showPassword) painterResource(R.drawable.baseline_visibility) else painterResource( - R.drawable.baseline_visibility_off), - contentDescription = "LogoVissable", - modifier = Modifier.size(24.dp) - ) - } - } - ) - Spacer(modifier = Modifier.height(8.dp)) - - Button( - onClick = { - GlobalScope.launch { - userPassword = userDao.getPasswordByEmail(email) - } - if(userPassword == password) { - acceptLogin = true - navHostController.navigate("Profile") - } - }, - colors = ButtonDefaults.buttonColors(backgroundColor = LightBluePolitech, ), - modifier = Modifier - .fillMaxWidth() - .padding(horizontal = 16.dp) - ) { - Text(text = "Авторизоваться", color = Color.White) + if (acceptLogin == true) { + user?.let { userProfile -> + ProfileForm(item = userProfile, userDao = userDao, navHostController = navHostController) } - Spacer(modifier = Modifier.height(8.dp)) - - Button( - onClick = { navController.navigate("register") }, - colors = ButtonDefaults.buttonColors(backgroundColor = LightBluePolitech), + } + else { + Column( modifier = Modifier - .fillMaxWidth() - .padding(horizontal = 16.dp) + .fillMaxSize() + .padding(16.dp), + verticalArrangement = Arrangement.Center, + horizontalAlignment = Alignment.CenterHorizontally ) { - Text(text = "Регистрация", color = Color.White) + Image( + painter = painterResource(R.drawable.logo_ulstu), + contentDescription = "Logo", + modifier = Modifier.size(200.dp) + ) + Spacer(modifier = Modifier.height(16.dp)) + + TextField( + value = email, + onValueChange = { email = it }, + label = { Text("Почта") }, + modifier = Modifier + .fillMaxWidth() + .padding(horizontal = 16.dp), + textStyle = TextStyle(fontSize = 16.sp), + colors = TextFieldDefaults.textFieldColors( + cursorColor = LightBluePolitech, + backgroundColor = Color.White, + textColor = LightBluePolitech, + unfocusedLabelColor = LightBluePolitech, + focusedIndicatorColor = LightBluePolitech, + unfocusedIndicatorColor = LightBluePolitech, + focusedLabelColor = LightBluePolitech + ), + singleLine = true + ) + Spacer(modifier = Modifier.height(8.dp)) + + TextField( + value = password, + onValueChange = { password = it }, + label = { Text("Пароль") }, + modifier = Modifier + .fillMaxWidth() + .padding(horizontal = 16.dp), + textStyle = TextStyle(fontSize = 16.sp), + colors = TextFieldDefaults.textFieldColors( + cursorColor = LightBluePolitech, + backgroundColor = Color.White, + textColor = LightBluePolitech, + unfocusedLabelColor = LightBluePolitech, + focusedIndicatorColor = LightBluePolitech, + unfocusedIndicatorColor = LightBluePolitech, + focusedLabelColor = LightBluePolitech + ), + singleLine = true, + visualTransformation = if (showPassword) VisualTransformation.None else PasswordVisualTransformation(), + trailingIcon = { + IconButton( + onClick = { showPassword = !showPassword } + ) { + Image( + painter = if (showPassword) painterResource(R.drawable.baseline_visibility) else painterResource( + R.drawable.baseline_visibility_off + ), + contentDescription = "LogoVissable", + modifier = Modifier.size(24.dp) + ) + } + } + ) + Spacer(modifier = Modifier.height(8.dp)) + + Button( + onClick = { + GlobalScope.launch { + 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(horizontal = 16.dp) + ) { + Text(text = "Авторизоваться", color = Color.White) + } + Spacer(modifier = Modifier.height(8.dp)) + + Button( + onClick = { navController.navigate("register") }, + colors = ButtonDefaults.buttonColors(backgroundColor = LightBluePolitech), + modifier = Modifier + .fillMaxWidth() + .padding(horizontal = 16.dp) + ) { + Text(text = "Регистрация", color = Color.White) + } } } } \ No newline at end of file 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 new file mode 100644 index 0000000..74ec181 --- /dev/null +++ b/app/src/main/java/com/example/labwork/pages/user/ProfileForm.kt @@ -0,0 +1,121 @@ +package com.example.labwork.pages.user + +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.size +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.material.Button +import androidx.compose.material.ButtonDefaults +import androidx.compose.material.Text +import androidx.compose.material.TextField +import androidx.compose.material.TextFieldDefaults +import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue +import androidx.compose.ui.Modifier +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.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 kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.launch + +@Composable +fun ProfileForm(item: User, userDao: UserDao, navHostController: NavHostController) { + val isFormVisible = remember { mutableStateOf(false) } + var email by remember { mutableStateOf(item.email) } + var name by remember { mutableStateOf(item.name) } + var password by remember { mutableStateOf(item.password) } + + Column( + modifier = Modifier.fillMaxWidth() + ) { + // Поле ввода для бренда + TextField( + value = email, + onValueChange = { email = it }, + placeholder = { Text("Почта") }, + modifier = Modifier + .fillMaxWidth() + .padding(horizontal = 16.dp, vertical = 8.dp), + colors = TextFieldDefaults.textFieldColors(backgroundColor = Color.White) + ) + + // Поле ввода для модели + TextField( + value = name, + onValueChange = { name = it }, + placeholder = { Text("Имя") }, + modifier = Modifier + .fillMaxWidth() + .padding(horizontal = 16.dp, vertical = 8.dp), + colors = TextFieldDefaults.textFieldColors(backgroundColor = Color.White) + ) + + // Поле ввода для цвета + TextField( + value = password, + onValueChange = { password = it }, + placeholder = { Text("Пароль") }, + modifier = Modifier + .fillMaxWidth() + .padding(horizontal = 16.dp, vertical = 8.dp), + colors = TextFieldDefaults.textFieldColors(backgroundColor = Color.White) + ) + + Button( + colors = ButtonDefaults.buttonColors(backgroundColor = LightBluePolitech), + modifier = Modifier + .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) + } + isFormVisible.value = false + navHostController.navigate("ListProduct") + }, + shape = RoundedCornerShape(15.dp) + ) { + Text( + text = "Принять", + color = Color.White, + fontSize = 10.sp, + textAlign = TextAlign.Center + ) + } + Button( + colors = ButtonDefaults.buttonColors(backgroundColor = LightBluePolitech), + modifier = Modifier + .fillMaxWidth() + .padding(9.dp), + onClick = { + isFormVisible.value = false + navHostController.navigate("ListProduct") + }, + shape = RoundedCornerShape(15.dp) + ) { + Text( + text = "Отменить", + color = Color.White, + fontSize = 10.sp, + textAlign = TextAlign.Center + ) + } + } + +}