From 7872460be3a3956d3839cb79a07ca2d3514cd9d6 Mon Sep 17 00:00:00 2001 From: Danya_Mochalov Date: Wed, 8 Nov 2023 07:31:36 +0400 Subject: [PATCH] =?UTF-8?q?=D0=AD=D0=BA=D1=80=D0=B0=D0=BD=20=D0=BF=D1=80?= =?UTF-8?q?=D0=BE=D0=B4=D1=83=D0=BA=D1=82=D0=B0=20=D1=82=D0=B5=D0=BF=D0=B5?= =?UTF-8?q?=D1=80=D1=8C=20=D0=BE=D1=82=D0=BA=D1=80=D1=8B=D0=B2=D0=B0=D0=B5?= =?UTF-8?q?=D1=82=D1=81=D1=8F=20=D0=BD=D0=B0=20=D1=80=D0=B5=D0=B4=D0=B0?= =?UTF-8?q?=D0=BA=D1=82=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5.?= =?UTF-8?q?=20=D0=9F=D0=BE=D0=B1=D0=B5=D0=B4=D0=B0.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../example/shawarma/data/dao/ProductDao.kt | 2 +- .../example/shawarma/screens/MainNavBar.kt | 17 ++++++++-- .../screens/products/ProductScreen.kt | 31 +++++++++++++++++-- .../screens/products/ProductsScreen.kt | 10 ++++-- .../example/shawarma/widgets/MyTextField.kt | 4 +-- 5 files changed, 53 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/com/example/shawarma/data/dao/ProductDao.kt b/app/src/main/java/com/example/shawarma/data/dao/ProductDao.kt index f8d01d9..53a2276 100644 --- a/app/src/main/java/com/example/shawarma/data/dao/ProductDao.kt +++ b/app/src/main/java/com/example/shawarma/data/dao/ProductDao.kt @@ -29,5 +29,5 @@ interface ProductDao { fun getItems() : Flow> @Query("select * from products where products.id = :id") - fun getById(id: Int): ProductModel + fun getById(id: Int): Flow } \ No newline at end of file diff --git a/app/src/main/java/com/example/shawarma/screens/MainNavBar.kt b/app/src/main/java/com/example/shawarma/screens/MainNavBar.kt index 22e24a8..feb306b 100644 --- a/app/src/main/java/com/example/shawarma/screens/MainNavBar.kt +++ b/app/src/main/java/com/example/shawarma/screens/MainNavBar.kt @@ -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) } } } diff --git a/app/src/main/java/com/example/shawarma/screens/products/ProductScreen.kt b/app/src/main/java/com/example/shawarma/screens/products/ProductScreen.kt index 88935fc..780d101 100644 --- a/app/src/main/java/com/example/shawarma/screens/products/ProductScreen.kt +++ b/app/src/main/java/com/example/shawarma/screens/products/ProductScreen.kt @@ -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)) diff --git a/app/src/main/java/com/example/shawarma/screens/products/ProductsScreen.kt b/app/src/main/java/com/example/shawarma/screens/products/ProductsScreen.kt index b28cafd..58437d3 100644 --- a/app/src/main/java/com/example/shawarma/screens/products/ProductsScreen.kt +++ b/app/src/main/java/com/example/shawarma/screens/products/ProductsScreen.kt @@ -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), diff --git a/app/src/main/java/com/example/shawarma/widgets/MyTextField.kt b/app/src/main/java/com/example/shawarma/widgets/MyTextField.kt index 78e7cd0..ef9a46a 100644 --- a/app/src/main/java/com/example/shawarma/widgets/MyTextField.kt +++ b/app/src/main/java/com/example/shawarma/widgets/MyTextField.kt @@ -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(