Абалдеть, я сделал редактирование....

This commit is contained in:
Кашин Максим 2023-11-21 01:04:47 +04:00
parent 0b8c882823
commit d9cb297f65
6 changed files with 145 additions and 41 deletions

View File

@ -22,7 +22,7 @@ interface BicycleDao {
suspend fun getAllBicycles(): List<Bicycle>
@Query("SELECT * FROM bicycles WHERE id = :bicycleId")
suspend fun getBicycleById(bicycleId: Int): Bicycle
suspend fun getBicycleById(bicycleId: Int?): Bicycle
@Query("SELECT * FROM bicycles WHERE userId = :userId")
suspend fun getBicyclesByUserId(userId: Int): List<Bicycle>

View File

@ -7,8 +7,8 @@ import androidx.room.PrimaryKey
data class Bicycle(
@PrimaryKey(autoGenerate = true)
val id: Int?,
val brand: String,
val model: String,
val color: String,
var brand: String,
var model: String,
var color: String,
val userId: Int?
)

View File

@ -12,3 +12,4 @@ data class User(
val email: String,
val password: String,
)

View File

@ -3,6 +3,7 @@ package com.example.labwork.pages.product
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
@ -130,5 +131,117 @@ fun FormNewProduct(bicycleDao: BicycleDao, navHostController: NavHostController)
}
}
}
}
@Composable
fun FormUpdateProduct(item:Bicycle, bicycleDao: BicycleDao, navHostController: NavHostController) {
val isFormVisible = remember { mutableStateOf(false) }
var brand by remember { mutableStateOf(item.brand) }
var model by remember { mutableStateOf(item.model) }
var color by remember { mutableStateOf(item.color) }
Button(
colors = ButtonDefaults.buttonColors(backgroundColor = LightBluePolitech),
modifier = Modifier
.padding(start = 9.dp, bottom = 9.dp)
.size(height = 32.dp, width = 128.dp),
onClick = {
// Установите значение состояния открытия формы как true
isFormVisible.value = true
},
shape = RoundedCornerShape(15.dp)
) {
Text(
text = "Редактировать",
color = Color.White,
fontSize = 10.sp,
textAlign = TextAlign.Center
)
}
if (isFormVisible.value) {
Column(
modifier = Modifier.fillMaxWidth()
) {
// Поле ввода для бренда
TextField(
value = brand,
onValueChange = { brand = it },
placeholder = { Text("Бренд") },
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 16.dp, vertical = 8.dp),
colors = TextFieldDefaults.textFieldColors(backgroundColor = Color.White)
)
// Поле ввода для модели
TextField(
value = model,
onValueChange = { model = it },
placeholder = { Text("Модель") },
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 16.dp, vertical = 8.dp),
colors = TextFieldDefaults.textFieldColors(backgroundColor = Color.White)
)
// Поле ввода для цвета
TextField(
value = color,
onValueChange = { color = 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 newBicycle = bicycleDao.getBicycleById(item.id)
newBicycle.model = model
newBicycle.color = color
newBicycle.brand = brand
bicycleDao.updateBicycle(newBicycle)
}
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
)
}
}
}
}

View File

@ -86,35 +86,13 @@ fun ListProduct(item: Bicycle, bicycleDao: BicycleDao, navHostController: NavHos
textAlign = TextAlign.Center,
)
}
Row {
Column {
FormUpdateProduct(item,bicycleDao, navHostController)
Button(
colors = ButtonDefaults.buttonColors(backgroundColor = LightBluePolitech),
modifier = Modifier
.padding(start = 9.dp, bottom = 9.dp)
.size(height = 32.dp, width = 40.dp),
onClick = {
},
shape = RoundedCornerShape(15.dp)
) {
Text(
text = "+",
color = Color.White,
fontSize = 10.sp,
textAlign = TextAlign.Center,
)
}
Text(
text = "1000",
color = Color.Black,
fontSize = 14.sp,
textAlign = TextAlign.Center,
modifier = Modifier.padding(start = 8.dp, top = 4.dp)
)
Button(
colors = ButtonDefaults.buttonColors(backgroundColor = LightBluePolitech),
modifier = Modifier
.padding(start = 9.dp, bottom = 9.dp)
.size(height = 32.dp, width = 40.dp),
.size(height = 32.dp, width = 128.dp),
onClick = {
// СУПЕР ЖОСКИЙ КОСТЫЛЬ ЭТО ВООБЩЕ ТРЕШ
GlobalScope.launch {
@ -125,7 +103,7 @@ fun ListProduct(item: Bicycle, bicycleDao: BicycleDao, navHostController: NavHos
shape = RoundedCornerShape(15.dp)
) {
Text(
text = "-",
text = "Удалить",
color = Color.White,
fontSize = 10.sp,
textAlign = TextAlign.Center,
@ -144,22 +122,33 @@ fun ListProduct(item: Bicycle, bicycleDao: BicycleDao, navHostController: NavHos
.fillMaxWidth()
.padding(top = 8.dp)
)
AnimatedVisibility(
visible = isFullAbout,
enter = fadeIn() + expandIn(),
exit = fadeOut() + shrinkOut(),
modifier = Modifier.scale(scale)
) {
Text(
color = Color.Black,
text = item.brand,
maxLines = 10,
fontSize = 14.sp,
textAlign = TextAlign.Center,
modifier = Modifier
.fillMaxWidth()
.padding(bottom = 8.dp)
)
Column {
Text(
color = Color.Black,
text = item.brand,
maxLines = 10,
fontSize = 14.sp,
textAlign = TextAlign.Center,
modifier = Modifier
.fillMaxWidth()
)
Text(
color = Color.Black,
text = item.color,
maxLines = 10,
fontSize = 14.sp,
textAlign = TextAlign.Center,
modifier = Modifier
.fillMaxWidth()
)
}
}
}
}

View File

@ -120,7 +120,8 @@ fun LoginPage(navController: NavController, userDao: UserDao, navHostController:
userPassword = userDao.getPasswordByEmail(email)
}
if(userPassword == password) {
navHostController.navigate("ListProduct")
acceptLogin = true
navHostController.navigate("Profile")
}
},
colors = ButtonDefaults.buttonColors(backgroundColor = LightBluePolitech, ),