ViewModel заказов

This commit is contained in:
Данила Мочалов 2023-11-27 01:30:59 +04:00
parent 6a2e730973
commit 505cbf19f3
2 changed files with 131 additions and 56 deletions

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,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)
}
}
}