Compare commits

...

3 Commits

11 changed files with 379 additions and 199 deletions

View File

@ -24,4 +24,6 @@ interface OrderDao {
fun getByUserId(userId: Int): Flow<List<OrderWithProducts>>
@Query("select * from orders where orders.user_id =:userId and orders.order_status = 'Неоплачено'")
fun getUnpaidByUser(userId: Int) : Flow<OrderWithProducts>
@Query("select * from orders where orders.user_id =:userId and orders.order_status = 'Готовится'")
fun getPaidByUser(userId: Int) : Flow<List<OrderWithProducts>>
}

View File

@ -1,6 +1,7 @@
package com.example.shawarma.data.interfaces.dao
import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.Query
import androidx.room.Update
@ -11,7 +12,8 @@ interface OrderProductDao {
suspend fun insert(order: OrderProductModel)
@Update
suspend fun update(order: OrderProductModel)
@Query("delete from order_product WHERE product_id = :productId")
suspend fun deleteByProductId(productId: Int);
@Delete
suspend fun delete(order: OrderProductModel)
}

View File

@ -21,7 +21,7 @@ data class OrderModel(
@ColumnInfo(name = "id")
val id: Int?,
@ColumnInfo(name = "order_status")
val status: String,
var status: String,
@ColumnInfo(name = "user_id", index = true)
val userId: Int?,
@ColumnInfo(name = "date")

View File

@ -13,4 +13,8 @@ class OrderProductRepository @Inject constructor(
suspend fun update(order: OrderProductModel) {
return orderProductDao.update(order)
}
suspend fun delete(order: OrderProductModel) {
return orderProductDao.delete(order)
}
}

View File

@ -30,4 +30,7 @@ class OrderRepository @Inject constructor(
fun getUnpaidByUser(userId: Int) : Flow<OrderWithProducts?> {
return orderDao.getUnpaidByUser(userId)
}
fun getPaidByUser(userId: Int) : Flow<List<OrderWithProducts>> {
return orderDao.getPaidByUser(userId)
}
}

View File

@ -7,6 +7,7 @@ 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.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
@ -20,12 +21,9 @@ import androidx.compose.foundation.verticalScroll
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.MutableState
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
@ -33,23 +31,20 @@ 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 com.example.shawarma.R
import com.example.shawarma.data.db.AppDatabase
import com.example.shawarma.data.models.OrderStatus
import androidx.hilt.navigation.compose.hiltViewModel
import com.example.shawarma.data.models.OrderWithProducts
import com.example.shawarma.data.sharedpref.PreferencesManager
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.viewmodels.CartViewModel
import com.example.shawarma.widgets.ShawarmaLogo2
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
@Composable
fun CartScreen() {
@ -63,29 +58,13 @@ fun CartScreen() {
@Composable
fun CartWidget(){
val preferencesManager = PreferencesManager(LocalContext.current)
val context = LocalContext.current
val orders = remember { mutableStateListOf<OrderWithProducts>() }
LaunchedEffect(Unit) {
withContext(Dispatchers.IO) {
AppDatabase.getInstance(context).orderDao().getByUserId(1).collect { data ->
orders.clear()
orders.addAll(data)
}
}
}
val cartViewModel: CartViewModel = hiltViewModel<CartViewModel>()
cartViewModel.getOrders(preferencesManager.getData("user_id", "null"))
var unpaidOrder: OrderWithProducts? = null
val preparingOrders = mutableListOf<OrderWithProducts>()
for(order in orders) {
if (order.order.status == OrderStatus.Неоплачено.toString()) {
unpaidOrder = order
}
if (order.order.status == OrderStatus.Готовится.toString() || order.order.status == OrderStatus.Готово.toString()) {
preparingOrders.add(order)
}
}
var unpaidOrder: OrderWithProducts? = cartViewModel.unpaidOrder.observeAsState().value
val preparingOrders = cartViewModel.paidOrders.observeAsState().value
Box(
modifier = Modifier
@ -94,7 +73,6 @@ fun CartWidget(){
.fillMaxSize()
.background(color = MyMainBackground)
.zIndex(2f),
contentAlignment = Alignment.TopCenter
) {
Text(
@ -110,7 +88,6 @@ fun CartWidget(){
.padding(top = 55.dp)
.verticalScroll(rememberScrollState())
) {
Text(
text = "Оплачено:",
fontFamily = NunitoFamily,
@ -123,8 +100,10 @@ fun CartWidget(){
.width(340.dp)
.height(200.dp)
) {
items(preparingOrders.size) { index ->
if (preparingOrders != null) {
items(preparingOrders.size) { index ->
PaidItem(preparingOrders[index])
}
}
}
Text(
@ -137,9 +116,7 @@ fun CartWidget(){
if (unpaidOrder != null) {
CartItem(order = unpaidOrder)
}
Spacer(modifier = Modifier.height(20.dp))
Button(
colors = ButtonDefaults.buttonColors(
backgroundColor = Color(0xFF91FF87)
@ -147,7 +124,11 @@ fun CartWidget(){
shape = RoundedCornerShape(20.dp),
border = BorderStroke(2.dp, Color(0x66000000)),
modifier = Modifier.size(240.dp, 60.dp),
onClick = { /*TODO*/ }
onClick = {
if (unpaidOrder != null) {
cartViewModel.payForOrder(unpaidOrder)
}
}
) {
Text(
"Оплатить",
@ -156,10 +137,7 @@ fun CartWidget(){
fontWeight = FontWeight(700),
)
}
Spacer(modifier = Modifier.height(70.dp))
}
}
}
@ -170,63 +148,65 @@ fun PaidItem(order : OrderWithProducts) {
border = BorderStroke(width = 2.dp, color = MyOrange),
shape = RoundedCornerShape(size = 20.dp),
backgroundColor = Color.White,
modifier = Modifier.size(340.dp, 100.dp)
modifier = Modifier.size(340.dp, 150.dp)
) {
LazyColumn {
Row(
horizontalArrangement = Arrangement.SpaceBetween,
modifier = Modifier
.fillMaxWidth()
.padding(start = 20.dp, end = 20.dp)
){
Text(
text = "Статус: ",
fontFamily = NunitoFamily,
fontSize = 20.sp,
modifier = Modifier.padding(top = 15.dp),
fontWeight = FontWeight.Bold,
color = Color.Gray
)
Text(
text = order.order.status,
fontFamily = NunitoFamily,
fontSize = 20.sp,
modifier = Modifier.padding(top = 15.dp),
fontWeight = FontWeight.Bold,
color = Color.Gray
)
}
LazyColumn(
modifier = Modifier.padding(top = 40.dp)
) {
items(order.orderWithProducts.size) { index ->
if (index == 0) {
if (order.orderWithProducts.isNotEmpty()) {
Row(
horizontalArrangement = Arrangement.SpaceBetween,
modifier = Modifier
.fillMaxWidth()
.padding(start = 20.dp, end = 20.dp)
){
.padding(horizontal = 20.dp)
) {
Text(
text = "Статус: ",
text = order.orderWithProducts[index].product.title,
fontFamily = NunitoFamily,
fontSize = 20.sp,
modifier = Modifier.padding(top = 15.dp),
fontWeight = FontWeight.Bold,
color = Color.Gray
fontWeight = FontWeight.Bold
)
Text(
text = order.order.status,
text = "x" + order.orderWithProducts[index].orderProductModel.quantity,
fontFamily = NunitoFamily,
fontSize = 20.sp,
modifier = Modifier.padding(top = 15.dp),
fontWeight = FontWeight.Bold,
color = Color.Gray
fontWeight = FontWeight.Bold
)
Text(
text = order.orderWithProducts[index].orderProductModel.totalPrice.toString() + " руб.",
fontFamily = NunitoFamily,
fontSize = 20.sp,
modifier = Modifier.padding(top = 15.dp),
fontWeight = FontWeight.Bold
)
}
}
Row(
horizontalArrangement = Arrangement.SpaceBetween,
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 20.dp)
) {
Text(
text = order.orderWithProducts[index].product.title,
fontFamily = NunitoFamily,
fontSize = 20.sp,
modifier = Modifier.padding(top = 15.dp),
fontWeight = FontWeight.Bold
)
Text(
text = "x" + order.orderWithProducts[index].orderProductModel.quantity,
fontFamily = NunitoFamily,
fontSize = 20.sp,
modifier = Modifier.padding(top = 15.dp),
fontWeight = FontWeight.Bold
)
Text(
text = order.orderWithProducts[index].orderProductModel.totalPrice.toString() + " руб.",
fontFamily = NunitoFamily,
fontSize = 20.sp,
modifier = Modifier.padding(top = 15.dp),
fontWeight = FontWeight.Bold
)
}
}
}
}
@ -235,6 +215,8 @@ fun PaidItem(order : OrderWithProducts) {
@Composable
fun CartItem(order : OrderWithProducts) {
val cartViewModel: CartViewModel = hiltViewModel<CartViewModel>()
Card(
border = BorderStroke(width = 2.dp, color = MyOrange),
shape = RoundedCornerShape(size = 20.dp),
@ -245,13 +227,17 @@ fun CartItem(order : OrderWithProducts) {
horizontalAlignment = Alignment.CenterHorizontally
) {
items(order.orderWithProducts.size) {index ->
var count = remember { mutableStateOf(0)}
count.value = order.orderWithProducts[index].orderProductModel.quantity
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.SpaceBetween,
modifier = Modifier.padding(20.dp).size(340.dp, 60.dp)
modifier = Modifier
.padding(20.dp)
.size(340.dp, 80.dp)
) {
Column(
modifier = Modifier.fillMaxWidth(0.5f)
modifier = Modifier.fillMaxWidth()
) {
Row(
horizontalArrangement = Arrangement.SpaceBetween,
@ -264,39 +250,70 @@ fun CartItem(order : OrderWithProducts) {
fontWeight = FontWeight.Bold
)
Text(
text = "x" + order.orderWithProducts[index].orderProductModel.quantity,
text = order.orderWithProducts[index].orderProductModel.totalPrice.toString() + " руб.",
fontFamily = NunitoFamily,
fontSize = 18.sp,
fontWeight = FontWeight.Bold
)
}
Row(
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier
.fillMaxWidth()
) {
Button(
colors = ButtonDefaults.buttonColors(
backgroundColor = MyLightYellow
),
shape = RoundedCornerShape(size = 20.dp),
modifier = Modifier
.fillMaxHeight()
.weight(1f)
.fillMaxSize(0.5f),
onClick = {
cartViewModel.removeProductFromOrder(order, index)
count.value -= 1
}
) {
Text(
text = "-",
fontSize = 18.sp,
fontWeight = FontWeight.Bold
)
}
Spacer(modifier = Modifier.fillMaxWidth(0.2f))
Text(
text = "x" + count.value + " ",
fontFamily = NunitoFamily,
fontSize = 20.sp,
fontWeight = FontWeight.Bold
)
Spacer(modifier = Modifier.fillMaxWidth(0.2f))
Button(
colors = ButtonDefaults.buttonColors(
backgroundColor = MyLightYellow
),
shape = RoundedCornerShape(size = 20.dp),
modifier = Modifier
.fillMaxHeight()
.weight(1f)
.fillMaxSize(0.5f),
onClick = {
cartViewModel.addProductToOrder(order, index)
count.value += 1
}
) {
Text(
text = "+",
fontSize = 18.sp,
fontWeight = FontWeight.Bold
)
}
}
Text(
text = order.orderWithProducts[index].orderProductModel.totalPrice.toString() + " руб.",
fontFamily = NunitoFamily,
fontSize = 20.sp,
fontWeight = FontWeight.Bold
)
}
Button(
colors = ButtonDefaults.buttonColors(
backgroundColor = MyLightRed
),
shape = RoundedCornerShape(size = 10.dp),
modifier = Modifier
.size(100.dp, 60.dp)
.fillMaxSize(0.5f),
onClick = { /*TODO*/ }
) {
Icon(
painter = painterResource(id = R.drawable.trash),
contentDescription = "Delete",
modifier = Modifier.size(42.dp)
)
}
}
}
}
}
}

View File

@ -20,9 +20,7 @@ import androidx.compose.material.ButtonDefaults
import androidx.compose.material.Card
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.runtime.livedata.observeAsState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
@ -34,18 +32,18 @@ import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.zIndex
import androidx.hilt.navigation.compose.hiltViewModel
import com.example.shawarma.R
import com.example.shawarma.data.db.AppDatabase
import com.example.shawarma.data.models.ProductModel
import com.example.shawarma.data.sharedpref.PreferencesManager
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.MyPriceBackground
import com.example.shawarma.ui.theme.NunitoFamily
import com.example.shawarma.viewmodels.HomeViewModel
import com.example.shawarma.widgets.ShawarmaLogo2
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
@Composable
fun DiscountScreen() {
@ -60,17 +58,9 @@ fun DiscountScreen() {
@Composable
fun DiscountList(){
val homeViewModel: HomeViewModel = hiltViewModel<HomeViewModel>()
val context = LocalContext.current
val products = remember { mutableStateListOf<ProductModel>() }
LaunchedEffect(Unit) {
withContext(Dispatchers.IO) {
AppDatabase.getInstance(context).productDao().getDiscounts().collect { data ->
products.clear()
products.addAll(data)
}
}
}
val products = homeViewModel.discounts.observeAsState().value
Box(
modifier = Modifier
@ -93,19 +83,9 @@ fun DiscountList(){
modifier = Modifier.padding(top = 80.dp)
)
{
items(products.size) { index ->
if (index % 2 == 0 && index == products.size - 1) {
Row(
horizontalArrangement = Arrangement.SpaceAround,
modifier = Modifier
.fillMaxWidth()
.padding(top = 10.dp)
) {
DiscountCard(products[index])
}
}
else {
if (index % 2 != 1) {
if (products != null) {
items(products.size) { index ->
if (index % 2 == 0 && index == products.size - 1) {
Row(
horizontalArrangement = Arrangement.SpaceAround,
modifier = Modifier
@ -113,12 +93,23 @@ fun DiscountList(){
.padding(top = 10.dp)
) {
DiscountCard(products[index])
DiscountCard(products[index+1])
}
} else {
if (index % 2 != 1) {
Row(
horizontalArrangement = Arrangement.SpaceAround,
modifier = Modifier
.fillMaxWidth()
.padding(top = 10.dp)
) {
DiscountCard(products[index])
DiscountCard(products[index+1])
}
}
}
}
if (index == products.size - 1) {
Spacer(modifier = Modifier.height(70.dp))
if (index == products.size - 1) {
Spacer(modifier = Modifier.height(70.dp))
}
}
}
}
@ -127,6 +118,8 @@ fun DiscountList(){
@Composable
fun DiscountCard(product : ProductModel){
val homeViewModel: HomeViewModel = hiltViewModel<HomeViewModel>()
val preferencesManager = PreferencesManager(LocalContext.current)
Card(
shape = RoundedCornerShape(20.dp),
backgroundColor = Color.White,
@ -181,7 +174,9 @@ fun DiscountCard(product : ProductModel){
}
Button(
onClick = { /*TODO*/ },
onClick = {
product.id?.let { homeViewModel.addProductToCart(it, preferencesManager.getData("user_id", "null")) }
},
colors = ButtonDefaults.buttonColors(
backgroundColor = MyLightYellow,
contentColor = Color.Black

View File

@ -5,8 +5,6 @@ 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.ExperimentalLayoutApi
import androidx.compose.foundation.layout.FlowRow
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
@ -22,31 +20,31 @@ import androidx.compose.foundation.verticalScroll
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.runtime.livedata.observeAsState
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 com.example.shawarma.data.db.AppDatabase
import androidx.hilt.navigation.compose.hiltViewModel
import com.example.shawarma.R
import com.example.shawarma.data.models.OrderStatus
import com.example.shawarma.data.models.OrderWithProducts
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.viewmodels.OrdersViewModel
import com.example.shawarma.widgets.ShawarmaLogo2
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.text.SimpleDateFormat
@Composable
@ -61,33 +59,11 @@ fun OrdersScreen() {
@Composable
fun OrdersList(){
val context = LocalContext.current
val allOrders = remember { mutableStateListOf<OrderWithProducts>() }
LaunchedEffect(Unit) {
withContext(Dispatchers.IO) {
AppDatabase.getInstance(context).orderDao().getAll().collect { data ->
allOrders.clear()
allOrders.addAll(data)
}
}
}
val preparingOrders = mutableListOf<OrderWithProducts>()
val preparedOrders = mutableListOf<OrderWithProducts>()
val processedOrders = mutableListOf<OrderWithProducts>()
for (order in allOrders){
if (order.order.status == OrderStatus.Готовится.toString()) {
preparingOrders.add(order)
}
if (order.order.status == OrderStatus.Готово.toString()) {
preparedOrders.add(order)
}
if (order.order.status == OrderStatus.Выдано.toString()) {
processedOrders.add(order)
}
}
val ordersViewModel = hiltViewModel<OrdersViewModel>()
val preparingOrders = ordersViewModel.preparingOrders.observeAsState().value
val preparedOrders = ordersViewModel.preparedOrders.observeAsState().value
val processedOrders = ordersViewModel.processedOrders.observeAsState().value
Box(
modifier = Modifier
@ -125,10 +101,11 @@ fun OrdersList(){
.width(340.dp)
.height(250.dp)
) {
if (preparingOrders.size != 0) {
items(preparingOrders.size) {
index ->
PreparingItem(preparingOrders[index])
if (preparingOrders != null) {
if (preparingOrders.isNotEmpty()) {
items(preparingOrders.size) { index ->
PreparingItem(preparingOrders[index])
}
}
}
}
@ -144,10 +121,11 @@ fun OrdersList(){
.width(340.dp)
.height(250.dp)
) {
if (preparedOrders.size != 0) {
items(preparedOrders.size) {
index ->
PreparedItem(preparedOrders[index])
if (preparedOrders != null) {
if (preparedOrders.isNotEmpty()) {
items(preparedOrders.size) { index ->
PreparedItem(preparedOrders[index])
}
}
}
}
@ -163,9 +141,11 @@ fun OrdersList(){
.width(340.dp)
.height(250.dp)
) {
if (processedOrders.size != 0) {
items(processedOrders.size) {index ->
ProcessedItem(processedOrders[index])
if (processedOrders != null) {
if (processedOrders.isNotEmpty()) {
items(processedOrders.size) {index ->
ProcessedItem(processedOrders[index])
}
}
}
}
@ -178,6 +158,8 @@ fun OrdersList(){
@Composable
fun PreparingItem(order : OrderWithProducts){
val ordersViewModel = hiltViewModel<OrdersViewModel>()
Card(
border = BorderStroke(width = 2.dp, color = MyOrange),
shape = RoundedCornerShape(size = 20.dp),
@ -190,7 +172,9 @@ fun PreparingItem(order : OrderWithProducts){
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.SpaceBetween,
modifier = Modifier.padding(20.dp).size(340.dp,60.dp)
modifier = Modifier
.padding(20.dp)
.size(340.dp, 60.dp)
){
Column(
modifier = Modifier.fillMaxWidth(0.5f)
@ -213,7 +197,9 @@ fun PreparingItem(order : OrderWithProducts){
modifier = Modifier
.size(170.dp, 80.dp)
.fillMaxSize(),
onClick = { /*TODO*/ }
onClick = {
ordersViewModel.changeOrderStatus(order, OrderStatus.Готово.name)
}
) {
Text(
text = "Готово!",
@ -226,7 +212,9 @@ fun PreparingItem(order : OrderWithProducts){
}
LazyColumn(
modifier = Modifier.fillMaxWidth().padding(horizontal = 20.dp)
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 20.dp)
){
items(order.orderWithProducts.size) {index ->
Row(
@ -257,6 +245,8 @@ fun PreparingItem(order : OrderWithProducts){
@Composable
fun PreparedItem(order : OrderWithProducts){
val ordersViewModel = hiltViewModel<OrdersViewModel>()
Card(
border = BorderStroke(width = 2.dp, color = MyOrange),
shape = RoundedCornerShape(size = 20.dp),
@ -269,7 +259,9 @@ fun PreparedItem(order : OrderWithProducts){
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.SpaceBetween,
modifier = Modifier.padding(20.dp).size(340.dp,60.dp)
modifier = Modifier
.padding(20.dp)
.size(340.dp, 60.dp)
){
Column(
modifier = Modifier.fillMaxWidth(0.5f)
@ -292,7 +284,9 @@ fun PreparedItem(order : OrderWithProducts){
modifier = Modifier
.size(170.dp, 80.dp)
.fillMaxSize(),
onClick = { /*TODO*/ }
onClick = {
ordersViewModel.changeOrderStatus(order, OrderStatus.Выдано.name)
}
) {
Text(
text = "Выдано!",
@ -305,7 +299,9 @@ fun PreparedItem(order : OrderWithProducts){
}
LazyColumn(
modifier = Modifier.fillMaxWidth().padding(horizontal = 20.dp)
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 20.dp)
){
items(order.orderWithProducts.size) {index ->
Row(
@ -336,6 +332,8 @@ fun PreparedItem(order : OrderWithProducts){
@Composable
fun ProcessedItem(order : OrderWithProducts){
val ordersViewModel = hiltViewModel<OrdersViewModel>()
Card(
border = BorderStroke(width = 2.dp, color = MyOrange),
shape = RoundedCornerShape(size = 20.dp),
@ -348,10 +346,12 @@ fun ProcessedItem(order : OrderWithProducts){
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.SpaceBetween,
modifier = Modifier.padding(20.dp).size(340.dp,60.dp)
modifier = Modifier
.padding(20.dp)
.size(340.dp, 60.dp)
){
Column(
modifier = Modifier.fillMaxWidth(0.5f)
modifier = Modifier.fillMaxWidth(0.3f)
){
val localDateFormat = SimpleDateFormat("HH:mm")
val time = localDateFormat.format(order.order.date)
@ -364,7 +364,7 @@ fun ProcessedItem(order : OrderWithProducts){
}
Column(
modifier = Modifier.fillMaxWidth(0.5f)
modifier = Modifier.fillMaxWidth(0.3f)
){
Text(
text = order.order.date.toString(),
@ -373,11 +373,29 @@ fun ProcessedItem(order : OrderWithProducts){
fontWeight = FontWeight.Bold
)
}
Button(
modifier = Modifier
.size(80.dp)
,
colors = ButtonDefaults.buttonColors(MyLightRed),
shape = RoundedCornerShape(20.dp),
onClick = {
ordersViewModel.deleteOrder(order)
}
) {
Icon(
painter = painterResource(id = R.drawable.trash),
contentDescription = "trash",
modifier = Modifier.size(80.dp)
)
}
}
LazyColumn(
modifier = Modifier.fillMaxWidth().padding(horizontal = 20.dp)
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 20.dp)
){
items(order.orderWithProducts.size) {index ->
Row(

View File

@ -0,0 +1,73 @@
package com.example.shawarma.viewmodels
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.shawarma.data.models.OrderStatus
import com.example.shawarma.data.models.OrderWithProducts
import com.example.shawarma.data.repos.OrderProductRepository
import com.example.shawarma.data.repos.OrderRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject
@HiltViewModel
class CartViewModel @Inject constructor(
private val orderRepository: OrderRepository,
private val orderProductRepository: OrderProductRepository
) : ViewModel(){
private val _unpaidOrder = MutableLiveData<OrderWithProducts>()
val unpaidOrder: LiveData<OrderWithProducts>
get() = _unpaidOrder
private val _paidOrders = MutableLiveData<List<OrderWithProducts>>()
val paidOrders: LiveData<List<OrderWithProducts>>
get() = _paidOrders
fun getOrders(userId: String) {
if (userId == "null") return
viewModelScope.launch {
orderRepository.getUnpaidByUser(userId.toInt()).collect {
_unpaidOrder.postValue(it)
}
}
viewModelScope.launch {
orderRepository.getPaidByUser(userId.toInt()).collect {
_paidOrders.postValue(it)
}
}
}
fun payForOrder(order: OrderWithProducts) {
val model = order.order
model.status = OrderStatus.Готовится.name
viewModelScope.launch {
orderRepository.update(model)
}
}
fun removeProductFromOrder(order: OrderWithProducts, productId: Int) {
val model = order.orderWithProducts[productId].orderProductModel
if(model.quantity == 1) {
// delete
viewModelScope.launch {
orderProductRepository.delete(model)
}
}
else{
// update
model.quantity -= 1
viewModelScope.launch {
orderProductRepository.update(model)
}
}
}
fun addProductToOrder(order: OrderWithProducts, productId: Int) {
val model = order.orderWithProducts[productId].orderProductModel
// update
model.quantity += 1
viewModelScope.launch {
orderProductRepository.update(model)
}
}
}

View File

@ -27,12 +27,21 @@ class HomeViewModel @Inject constructor(
val products: LiveData<List<ProductModel>>
get() = _products
private val _discounts= MutableLiveData<List<ProductModel>>()
val discounts: LiveData<List<ProductModel>>
get() = _discounts
init {
viewModelScope.launch {
productRepository.getAll().collect {
_products.postValue(it)
}
}
viewModelScope.launch {
productRepository.getDiscounts().collect {
_discounts.postValue(it)
}
}
}
fun addProductToCart(productId: Int, userId: String) {

View File

@ -0,0 +1,57 @@
package com.example.shawarma.viewmodels
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.shawarma.data.models.OrderStatus
import com.example.shawarma.data.models.OrderWithProducts
import com.example.shawarma.data.repos.OrderProductRepository
import com.example.shawarma.data.repos.OrderRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject
@HiltViewModel
class OrdersViewModel @Inject constructor(
private val orderRepository: OrderRepository,
private val orderProductRepository: OrderProductRepository
) : ViewModel() {
private val _preparingOrders = MutableLiveData<List<OrderWithProducts>>()
private val _preparedOrders = MutableLiveData<List<OrderWithProducts>>()
private val _processedOrders = MutableLiveData<List<OrderWithProducts>>()
val preparingOrders: LiveData<List<OrderWithProducts>>
get() = _preparingOrders
val preparedOrders: LiveData<List<OrderWithProducts>>
get() = _preparedOrders
val processedOrders: LiveData<List<OrderWithProducts>>
get() = _processedOrders
init {
viewModelScope.launch {
orderRepository.getAll().collect { allOrders ->
_preparingOrders.postValue(allOrders.filter { it.order.status == OrderStatus.Готовится.name })
_preparedOrders.postValue(allOrders.filter { it.order.status == OrderStatus.Готово.name })
_processedOrders.postValue(allOrders.filter { it.order.status == OrderStatus.Выдано.name })
}
}
}
fun changeOrderStatus(order: OrderWithProducts, newStatus: String) {
val model = order.order
model.status = newStatus
viewModelScope.launch{
orderRepository.update(model)
}
}
fun deleteOrder(order: OrderWithProducts) {
viewModelScope.launch{
orderRepository.delete(order.order)
}
}
}