Compare commits

...

4 Commits

12 changed files with 402 additions and 13 deletions

View File

@ -20,7 +20,7 @@ interface OrderDao {
@Delete
suspend fun delete(order: OrderModel)
@Query("select * from orders where orders.user_id is not null")
@Query("select * from orders")
fun getAll() : Flow<List<OrderWithProducts>>
@Query("select * from orders where orders.id =:id")
fun getById(id: Int): Flow<List<OrderWithProducts>>

View File

@ -25,6 +25,9 @@ interface ProductDao {
@Query("select * from products where products.product_old_price is not null")
fun getDiscounts() : Flow<List<ProductModel>>
@Query("select * from products")
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,15 +8,19 @@ 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
import com.example.shawarma.screens.home.HomeScreen
import com.example.shawarma.screens.orders.OrdersScreen
import com.example.shawarma.screens.products.ProductScreen
import com.example.shawarma.screens.products.ProductsScreen
import com.example.shawarma.screens.registration.RegistrationScreen
import com.example.shawarma.ui.theme.MyLightYellow
import com.example.shawarma.utils.ScreenPaths
@ -56,6 +60,23 @@ fun MainNavBar() {
composable(ScreenPaths.orders.name) {
OrdersScreen()
}
composable(ScreenPaths.products.name) {
ProductsScreen(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

@ -98,10 +98,10 @@ fun AuthorizationCard(navHostController: NavHostController) {
Button(
onClick = {
navHostController.navigate(ScreenPaths.home.name) {
popUpTo(ScreenPaths.home.name) {
inclusive = true
}
}},
popUpTo(ScreenPaths.authorization.name) {
inclusive = true
}
} },
colors = ButtonDefaults.buttonColors(MyLightRed, Color.White),
shape = RoundedCornerShape(20.dp),
modifier = Modifier

View File

@ -36,10 +36,7 @@ import androidx.compose.ui.unit.sp
import androidx.compose.ui.zIndex
import com.example.shawarma.R
import com.example.shawarma.data.db.AppDatabase
import com.example.shawarma.data.models.DiscountProductModel
import com.example.shawarma.data.models.ProductModel
import com.example.shawarma.data.models.getAllDiscountProducts
import com.example.shawarma.screens.home.HomeList
import com.example.shawarma.ui.theme.MarckFamily
import com.example.shawarma.ui.theme.MyLightYellow
import com.example.shawarma.ui.theme.MyMainBackground
@ -149,6 +146,7 @@ fun DiscountCard(product : ProductModel){
.size(160.dp, 36.dp)
) {
Row(
horizontalArrangement = Arrangement.SpaceBetween
){
Box(
modifier = Modifier.padding(start = 12.dp)

View File

@ -0,0 +1,155 @@
package com.example.shawarma.screens.products
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.shape.RoundedCornerShape
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
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, productId: Int?) {
Box(
contentAlignment = Alignment.TopCenter
) {
ProductWidget(navHostController, productId)
ShawarmaLogo2()
}
}
@Composable
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))
.padding(top = 100.dp)
.fillMaxSize()
.background(color = MyMainBackground)
.zIndex(2f),
contentAlignment = Alignment.TopCenter
){
Text(
text = "Товар",
fontFamily = MarckFamily,
fontSize = 40.sp,
modifier = Modifier.padding(top = 15.dp)
)
Column(
modifier = Modifier
.padding(top = 60.dp),
horizontalAlignment = Alignment.CenterHorizontally
) {
MyTextField(
text = title,
onTextChanged = {title.value = it},
modifier = Modifier
.padding(
top = (20).dp
)
.size(300.dp, (60).dp),
placeholder = "Наименование",
singleLine = true
)
MyTextField(
text = price,
onTextChanged = {price.value = it},
modifier = Modifier
.padding(
top = (20).dp
)
.size(300.dp, (60).dp),
placeholder = "Цена",
singleLine = true
)
MyTextField(
text = oldPrice,
onTextChanged = {oldPrice.value = it},
modifier = Modifier
.padding(
top = (20).dp
)
.size(300.dp, (60).dp),
placeholder = "Старая цена",
singleLine = true
)
Button(
modifier = Modifier
.padding(top = 80.dp)
.size(300.dp, 50.dp),
colors = ButtonDefaults.buttonColors(
backgroundColor = MyLightYellow
),
border = BorderStroke(2.dp, color = MyOrange),
shape = RoundedCornerShape(20.dp),
onClick = {
// TODO
navHostController.popBackStack()
}
) {
Text(
text = "Добавить",
fontFamily = NunitoFamily,
fontSize = 24.sp,
fontWeight = FontWeight.Bold
)
}
}
}
}

View File

@ -0,0 +1,209 @@
package com.example.shawarma.screens.products
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.magnifier
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Button
import androidx.compose.material.ButtonDefaults
import androidx.compose.material.Card
import androidx.compose.material.Icon
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateListOf
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.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
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.R
import com.example.shawarma.data.db.AppDatabase
import com.example.shawarma.data.models.ProductModel
import com.example.shawarma.screens.home.HomeList
import com.example.shawarma.screens.home.ProductCard
import com.example.shawarma.ui.theme.MarckFamily
import com.example.shawarma.ui.theme.MyLightRed
import com.example.shawarma.ui.theme.MyLightYellow
import com.example.shawarma.ui.theme.MyMainBackground
import com.example.shawarma.ui.theme.MyOrange
import com.example.shawarma.ui.theme.NunitoFamily
import com.example.shawarma.utils.ScreenPaths
import com.example.shawarma.widgets.ShawarmaLogo2
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
@Composable
fun ProductsScreen(navHostController: NavHostController) {
Box(
contentAlignment = Alignment.TopCenter
) {
ProductsList(navHostController)
ShawarmaLogo2()
}
}
@Composable
fun ProductsList(navHostController: NavHostController){
val context = LocalContext.current
val products = remember { mutableStateListOf<ProductModel>() }
LaunchedEffect(Unit) {
withContext(Dispatchers.IO) {
AppDatabase.getInstance(context).productDao().getItems().collect { data ->
products.clear()
products.addAll(data)
}
}
}
Box(
modifier = Modifier
.clip(shape = RoundedCornerShape(30.dp))
.padding(top = 100.dp)
.fillMaxSize()
.background(color = MyMainBackground)
.zIndex(2f),
contentAlignment = Alignment.TopCenter
){
Text(
text = "Товары",
fontFamily = MarckFamily,
fontSize = 40.sp,
modifier = Modifier.padding(top = 15.dp)
)
Button(
modifier = Modifier
.padding(top = 80.dp)
.size(340.dp, 60.dp),
colors = ButtonDefaults.buttonColors(
backgroundColor = MyLightYellow
),
border = BorderStroke(2.dp, color = MyOrange),
shape = RoundedCornerShape(20.dp),
onClick = {
navHostController.navigate(ScreenPaths.product.name)
}
) {
Text(
text = "Добавить товар",
fontFamily = NunitoFamily,
fontSize = 24.sp,
fontWeight = FontWeight.Bold
)
}
LazyColumn(
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier.padding(top = 160.dp)
)
{
items(products.size) { index ->
ProductItem(products[index], navHostController)
Spacer(modifier = Modifier.height(20.dp))
if (index == products.size - 1) {
Spacer(modifier = Modifier.height(70.dp))
}
}
}
}
}
@Composable
fun ProductItem(product: ProductModel, navHostController: NavHostController){
Card(
border = BorderStroke(width = 2.dp, color = MyOrange),
shape = RoundedCornerShape(size = 20.dp),
backgroundColor = Color.White,
modifier = Modifier.size(340.dp, 100.dp)
) {
Row(
modifier = Modifier
.padding(20.dp),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.SpaceAround
) {
Column(
modifier = Modifier.fillMaxWidth(0.5f)
) {
Text(
text = product.title,
fontFamily = NunitoFamily,
fontSize = 20.sp,
fontWeight = FontWeight.Bold
)
if (product.oldPrice != null) {
Text(
text = product.price.toString() + " руб.",
fontFamily = NunitoFamily,
fontSize = 20.sp,
fontWeight = FontWeight.Bold,
color = Color.Red
)
}
else {
Text(
text = product.price.toString() + " руб.",
fontFamily = NunitoFamily,
fontSize = 20.sp,
fontWeight = FontWeight.Bold
)
}
}
Button(
modifier = Modifier.size(60.dp),
shape = RoundedCornerShape(10.dp),
colors = ButtonDefaults.buttonColors(
backgroundColor = MyLightYellow
),
onClick = {
navHostController.navigate(
route = ScreenPaths.product.name + "?productId=" + product.id.toString()
)
}
) {
Icon(
painter = painterResource(id = R.drawable.pen_icon),
contentDescription = "pen",
modifier = Modifier.fillMaxSize()
)
}
Button(
modifier = Modifier.size(60.dp),
shape = RoundedCornerShape(10.dp),
colors = ButtonDefaults.buttonColors(
backgroundColor = MyLightRed
),
onClick = { /*TODO*/ }
) {
Icon(
painter = painterResource(id = R.drawable.trash),
contentDescription = "trash",
modifier = Modifier.fillMaxSize()
)
}
}
}
}

View File

@ -7,4 +7,6 @@ sealed class BottomNavItem(var title:String, var icon:Int, var screen_route:Stri
object Discount : BottomNavItem("Discount", R.drawable.discount_icon,ScreenPaths.discount.name)
object Cart : BottomNavItem("Cart", R.drawable.cart_icon,ScreenPaths.cart.name)
object Orders: BottomNavItem("Orders", R.drawable.orders_icon,ScreenPaths.orders.name)
object Products: BottomNavItem("Products", R.drawable.pen_icon, ScreenPaths.products.name)
}

View File

@ -1,5 +1,5 @@
package com.example.shawarma.utils
enum class ScreenPaths {
authorization, registration, home, discount, cart, orders
authorization, registration, home, discount, cart, orders, products, product
}

View File

@ -23,7 +23,8 @@ fun BottomNavBar(navController: NavController) {
BottomNavItem.Discount,
BottomNavItem.Home,
BottomNavItem.Cart,
BottomNavItem.Orders
BottomNavItem.Orders,
BottomNavItem.Products
)
BottomNavigation(
backgroundColor = Color.White,

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(

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.9 KiB