Сделал супер шутку "личного кабинета"

This commit is contained in:
Кашин Максим 2023-11-22 22:49:55 +04:00
parent 3e68677d23
commit eb3bc2ea3d
3 changed files with 233 additions and 96 deletions

View File

@ -8,8 +8,8 @@ import androidx.room.PrimaryKey
data class User( data class User(
@PrimaryKey(autoGenerate = true) @PrimaryKey(autoGenerate = true)
val id: Int?, val id: Int?,
val name: String, var name: String,
val email: String, var email: String,
val password: String, var password: String,
) )

View File

@ -9,6 +9,7 @@ import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Button import androidx.compose.material.Button
import androidx.compose.material.ButtonDefaults import androidx.compose.material.ButtonDefaults
import androidx.compose.material.IconButton 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.TextStyle
import androidx.compose.ui.text.input.PasswordVisualTransformation import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.text.input.VisualTransformation 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.dp
import androidx.compose.ui.unit.sp 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.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.ui.theme.LightBluePolitech
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -42,9 +47,16 @@ fun LoginPage(navController: NavController, userDao: UserDao, navHostController:
var email by remember { mutableStateOf("") } var email by remember { mutableStateOf("") }
var password by remember { mutableStateOf("") } var password by remember { mutableStateOf("") }
var userPassword by remember { mutableStateOf("") } var userPassword by remember { mutableStateOf("") }
var user by remember { mutableStateOf<User?>(null) }
var showPassword by remember { mutableStateOf(false) } var showPassword by remember { mutableStateOf(false) }
var acceptLogin by remember { mutableStateOf(false) } var acceptLogin by remember { mutableStateOf(false) }
if (acceptLogin == true) {
user?.let { userProfile ->
ProfileForm(item = userProfile, userDao = userDao, navHostController = navHostController)
}
}
else {
Column( Column(
modifier = Modifier modifier = Modifier
.fillMaxSize() .fillMaxSize()
@ -104,8 +116,9 @@ fun LoginPage(navController: NavController, userDao: UserDao, navHostController:
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
),
contentDescription = "LogoVissable", contentDescription = "LogoVissable",
modifier = Modifier.size(24.dp) modifier = Modifier.size(24.dp)
) )
@ -118,13 +131,15 @@ fun LoginPage(navController: NavController, userDao: UserDao, navHostController:
onClick = { onClick = {
GlobalScope.launch { GlobalScope.launch {
userPassword = userDao.getPasswordByEmail(email) userPassword = userDao.getPasswordByEmail(email)
} user = userDao.getUserByEmail(email)
if(userPassword == password) {
if (userPassword == password) {
acceptLogin = true acceptLogin = true
navHostController.navigate("Profile") //navHostController.navigate("Profile")
}
} }
}, },
colors = ButtonDefaults.buttonColors(backgroundColor = LightBluePolitech, ), colors = ButtonDefaults.buttonColors(backgroundColor = LightBluePolitech,),
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.padding(horizontal = 16.dp) .padding(horizontal = 16.dp)
@ -143,4 +158,5 @@ fun LoginPage(navController: NavController, userDao: UserDao, navHostController:
Text(text = "Регистрация", color = Color.White) Text(text = "Регистрация", color = Color.White)
} }
} }
}
} }

View File

@ -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
)
}
}
}