Экран продукта теперь открывается на редактирование. Победа.

This commit is contained in:
Данила Мочалов 2023-11-08 07:31:36 +04:00
parent d6d2ef8f52
commit 7872460be3
5 changed files with 53 additions and 11 deletions

View File

@ -29,5 +29,5 @@ interface ProductDao {
fun getItems() : Flow<List<ProductModel>>
@Query("select * from products where products.id = :id")
fun getById(id: Int): ProductModel
fun getById(id: Int): Flow<ProductModel>
}

View File

@ -8,10 +8,12 @@ import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument
import com.example.shawarma.screens.authorization.AuthorizationScreen
import com.example.shawarma.screens.cart.CartScreen
import com.example.shawarma.screens.discount.DiscountScreen
@ -61,8 +63,19 @@ fun MainNavBar() {
composable(ScreenPaths.products.name) {
ProductsScreen(navController)
}
composable(ScreenPaths.product.name) {
ProductScreen(navController)
composable(
route = ScreenPaths.product.name + "?productId={productId}",
arguments = listOf(
navArgument("productId") {
defaultValue = 0
type = NavType.IntType
}
)
) { navBackStackEntry ->
/* Extracting the id from the route */
val productId = navBackStackEntry.arguments?.getInt("productId")
ProductScreen(navController, productId)
}
}
}

View File

@ -15,17 +15,22 @@ import androidx.compose.material.Button
import androidx.compose.material.ButtonDefaults
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.zIndex
import androidx.navigation.NavHostController
import com.example.shawarma.data.db.AppDatabase
import com.example.shawarma.data.models.ProductModel
import com.example.shawarma.ui.theme.MarckFamily
import com.example.shawarma.ui.theme.MyLightYellow
import com.example.shawarma.ui.theme.MyMainBackground
@ -33,22 +38,42 @@ import com.example.shawarma.ui.theme.MyOrange
import com.example.shawarma.ui.theme.NunitoFamily
import com.example.shawarma.widgets.MyTextField
import com.example.shawarma.widgets.ShawarmaLogo2
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
@Composable
fun ProductScreen(navHostController: NavHostController) {
fun ProductScreen(navHostController: NavHostController, productId: Int?) {
Box(
contentAlignment = Alignment.TopCenter
) {
ProductWidget(navHostController)
ProductWidget(navHostController, productId)
ShawarmaLogo2()
}
}
@Composable
fun ProductWidget(navHostController: NavHostController) {
fun ProductWidget(navHostController: NavHostController, productId: Int?) {
val title = remember { mutableStateOf(TextFieldValue(""))}
val price = remember { mutableStateOf(TextFieldValue(""))}
val oldPrice = remember { mutableStateOf(TextFieldValue(""))}
if (productId != 0 && productId != null) {
val context = LocalContext.current
var product = remember {
mutableStateOf(ProductModel(id = -1, title = "", price = 0, oldPrice = null))
}
LaunchedEffect(Unit) {
withContext(Dispatchers.IO) {
AppDatabase.getInstance(context).productDao().getById(productId).collect { data ->
product.value = data
}
}
}
title.value = TextFieldValue(text = product.value.title)
price.value = TextFieldValue(text = product.value.price.toString())
oldPrice.value = TextFieldValue(text = product.value.oldPrice.toString())
}
Box(
modifier = Modifier
.clip(shape = RoundedCornerShape(30.dp))

View File

@ -118,7 +118,7 @@ fun ProductsList(navHostController: NavHostController){
)
{
items(products.size) { index ->
ProductItem(products[index])
ProductItem(products[index], navHostController)
Spacer(modifier = Modifier.height(20.dp))
if (index == products.size - 1) {
Spacer(modifier = Modifier.height(70.dp))
@ -130,7 +130,7 @@ fun ProductsList(navHostController: NavHostController){
}
@Composable
fun ProductItem(product: ProductModel){
fun ProductItem(product: ProductModel, navHostController: NavHostController){
Card(
border = BorderStroke(width = 2.dp, color = MyOrange),
shape = RoundedCornerShape(size = 20.dp),
@ -178,7 +178,11 @@ fun ProductItem(product: ProductModel){
colors = ButtonDefaults.buttonColors(
backgroundColor = MyLightYellow
),
onClick = { /*TODO*/ }
onClick = {
navHostController.navigate(
route = ScreenPaths.product.name + "?productId=" + product.id.toString()
)
}
) {
Icon(
painter = painterResource(id = R.drawable.pen_icon),

View File

@ -32,7 +32,7 @@ fun MyTextField(
placeholder = {
Text(
text = placeholder,
fontSize = 14.sp,
fontSize = 16.sp,
style = TextStyle(
fontFamily = JejuFamily
)
@ -41,7 +41,7 @@ fun MyTextField(
singleLine = singleLine,
shape = RoundedCornerShape(20.dp),
textStyle = TextStyle(
fontSize = 14.sp,
fontSize = 16.sp,
fontFamily = JejuFamily
),
colors = TextFieldDefaults.textFieldColors(