diff --git a/app/src/main/java/com/example/shawarma/data/interfaces/dao/OrderDao.kt b/app/src/main/java/com/example/shawarma/data/interfaces/dao/OrderDao.kt index d3787e9..a24f4a9 100644 --- a/app/src/main/java/com/example/shawarma/data/interfaces/dao/OrderDao.kt +++ b/app/src/main/java/com/example/shawarma/data/interfaces/dao/OrderDao.kt @@ -24,4 +24,6 @@ interface OrderDao { fun getByUserId(userId: Int): Flow> @Query("select * from orders where orders.user_id =:userId and orders.order_status = 'Неоплачено'") fun getUnpaidByUser(userId: Int) : Flow + @Query("select * from orders where orders.user_id =:userId and orders.order_status = 'Готовится'") + fun getPaidByUser(userId: Int) : Flow> } \ No newline at end of file diff --git a/app/src/main/java/com/example/shawarma/data/interfaces/dao/OrderProductDao.kt b/app/src/main/java/com/example/shawarma/data/interfaces/dao/OrderProductDao.kt index 721e08e..1dc25be 100644 --- a/app/src/main/java/com/example/shawarma/data/interfaces/dao/OrderProductDao.kt +++ b/app/src/main/java/com/example/shawarma/data/interfaces/dao/OrderProductDao.kt @@ -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) } \ No newline at end of file diff --git a/app/src/main/java/com/example/shawarma/data/models/OrderModel.kt b/app/src/main/java/com/example/shawarma/data/models/OrderModel.kt index f472094..44c231c 100644 --- a/app/src/main/java/com/example/shawarma/data/models/OrderModel.kt +++ b/app/src/main/java/com/example/shawarma/data/models/OrderModel.kt @@ -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") diff --git a/app/src/main/java/com/example/shawarma/data/repos/OrderProductRepository.kt b/app/src/main/java/com/example/shawarma/data/repos/OrderProductRepository.kt index 43a72ed..251db2e 100644 --- a/app/src/main/java/com/example/shawarma/data/repos/OrderProductRepository.kt +++ b/app/src/main/java/com/example/shawarma/data/repos/OrderProductRepository.kt @@ -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) + } + } \ No newline at end of file diff --git a/app/src/main/java/com/example/shawarma/data/repos/OrderRepository.kt b/app/src/main/java/com/example/shawarma/data/repos/OrderRepository.kt index 68a7644..6c181fb 100644 --- a/app/src/main/java/com/example/shawarma/data/repos/OrderRepository.kt +++ b/app/src/main/java/com/example/shawarma/data/repos/OrderRepository.kt @@ -30,4 +30,7 @@ class OrderRepository @Inject constructor( fun getUnpaidByUser(userId: Int) : Flow { return orderDao.getUnpaidByUser(userId) } + fun getPaidByUser(userId: Int) : Flow> { + return orderDao.getPaidByUser(userId) + } } \ No newline at end of file diff --git a/app/src/main/java/com/example/shawarma/screens/cart/CartScreen.kt b/app/src/main/java/com/example/shawarma/screens/cart/CartScreen.kt index 49681cd..be62878 100644 --- a/app/src/main/java/com/example/shawarma/screens/cart/CartScreen.kt +++ b/app/src/main/java/com/example/shawarma/screens/cart/CartScreen.kt @@ -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() } - LaunchedEffect(Unit) { - withContext(Dispatchers.IO) { - AppDatabase.getInstance(context).orderDao().getByUserId(1).collect { data -> - orders.clear() - orders.addAll(data) - } - } - } + val cartViewModel: CartViewModel = hiltViewModel() + cartViewModel.getOrders(preferencesManager.getData("user_id", "null")) - var unpaidOrder: OrderWithProducts? = null - val preparingOrders = mutableListOf() - - 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() + 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) - ) - } - } } - } } } \ No newline at end of file diff --git a/app/src/main/java/com/example/shawarma/viewmodels/CartViewModel.kt b/app/src/main/java/com/example/shawarma/viewmodels/CartViewModel.kt new file mode 100644 index 0000000..261b1a2 --- /dev/null +++ b/app/src/main/java/com/example/shawarma/viewmodels/CartViewModel.kt @@ -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() + val unpaidOrder: LiveData + get() = _unpaidOrder + private val _paidOrders = MutableLiveData>() + val paidOrders: LiveData> + 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) + } + + } +} \ No newline at end of file