я не знаю каким чудом, но я сделал добавление

This commit is contained in:
Кашин Максим 2023-11-20 20:41:00 +04:00
parent 4df3eff3b1
commit 9c8d38f9f4
3 changed files with 118 additions and 9 deletions

View File

@ -59,12 +59,12 @@ abstract class AppDatabase : RoomDatabase() {
userDao.insertUser(user2)
// Создание велосипедов
val bicycle1 = Bicycle(1, "Trek", "Велосипед 1", "Black", 1)
val bicycle2 = Bicycle(2, "Giant", "Велосипед 2", "Blue", 2)
val bicycle3 = Bicycle(3, "Trek", "Велосипед 3", "Black", 1)
val bicycle4 = Bicycle(4, "Giant", "Велосипед 4", "Blue", 2)
val bicycle5 = Bicycle(5, "Trek", "Велосипед 5", "Black", 1)
val bicycle6 = Bicycle(6, "Giant", "Велосипед 6", "Blue", 2)
val bicycle1 = Bicycle(brand = "Trek", model = "Велосипед 1", color = "Black", userId = null, id = null)
val bicycle2 = Bicycle(brand = "Trek", model = "Велосипед 2", color = "Black", userId = null, id = null)
val bicycle3 = Bicycle(brand = "Trek", model = "Велосипед 3", color = "Black", userId = null, id = null)
val bicycle4 = Bicycle(brand = "Trek", model = "Велосипед 4", color = "Black", userId = null, id = null)
val bicycle5 = Bicycle(brand = "Trek", model = "Велосипед 5", color = "Black", userId = null, id = null)
val bicycle6 = Bicycle(brand = "Trek", model = "Велосипед 6", color = "Black", userId = null, id = null)
// Вставка велосипедов в базу данных
bicycleDao.insertBicycle(bicycle1)

View File

@ -5,9 +5,10 @@ import androidx.room.PrimaryKey
@Entity(tableName = "bicycles")
data class Bicycle(
@PrimaryKey val id: Int,
@PrimaryKey(autoGenerate = true)
val id: Int?,
val brand: String,
val model: String,
val color: String,
val userId: Int
val userId: Int?
)

View File

@ -19,6 +19,8 @@ import androidx.compose.material.Button
import androidx.compose.material.ButtonDefaults
import androidx.compose.material.Card
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
@ -50,6 +52,112 @@ fun ListProduct(item: Bicycle, bicycleDao: BicycleDao, navHostController: NavHos
var isFullAbout by remember { mutableStateOf(false) }
val scale by animateFloatAsState(if (isFullAbout) 1f else 0f)
val textSize by animateDpAsState(if (isFullAbout) 18.dp else 24.dp)
val isFormVisible = remember { mutableStateOf(false) }
var brand by remember { mutableStateOf("") }
var model by remember { mutableStateOf("") }
var color by remember { mutableStateOf("") }
// Внутри вашего кода Composable обработчик нажатия кнопки "Добавить"
Button(
colors = ButtonDefaults.buttonColors(backgroundColor = LightBluePolitech),
modifier = Modifier
.fillMaxWidth()
.padding(9.dp),
onClick = {
// Установите значение состояния открытия формы как true
isFormVisible.value = true
},
shape = RoundedCornerShape(15.dp)
) {
Text(
text = "Добавить",
color = Color.White,
fontSize = 10.sp,
textAlign = TextAlign.Center
)
}
// После кнопки "Добавить" добавьте код, который будет отображать форму при isFormVisible равном true
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 = Bicycle(null, brand, model, color, null)
bicycleDao.insertBicycle(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
)
}
}
}
Card(
modifier = Modifier