Удаление продукта

This commit is contained in:
Данила Мочалов 2023-11-22 01:32:52 +04:00
parent 73f4b57547
commit 81b1fbd33b
2 changed files with 54 additions and 26 deletions

View File

@ -13,7 +13,6 @@ import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.magnifier
import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Button import androidx.compose.material.Button
import androidx.compose.material.ButtonDefaults import androidx.compose.material.ButtonDefaults
@ -21,25 +20,20 @@ import androidx.compose.material.Card
import androidx.compose.material.Icon import androidx.compose.material.Icon
import androidx.compose.material.Text import androidx.compose.material.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import androidx.compose.ui.zIndex import androidx.compose.ui.zIndex
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController import androidx.navigation.NavHostController
import com.example.shawarma.R import com.example.shawarma.R
import com.example.shawarma.data.db.AppDatabase
import com.example.shawarma.data.models.ProductModel 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.MarckFamily
import com.example.shawarma.ui.theme.MyLightRed import com.example.shawarma.ui.theme.MyLightRed
import com.example.shawarma.ui.theme.MyLightYellow import com.example.shawarma.ui.theme.MyLightYellow
@ -47,9 +41,8 @@ import com.example.shawarma.ui.theme.MyMainBackground
import com.example.shawarma.ui.theme.MyOrange import com.example.shawarma.ui.theme.MyOrange
import com.example.shawarma.ui.theme.NunitoFamily import com.example.shawarma.ui.theme.NunitoFamily
import com.example.shawarma.utils.ScreenPaths import com.example.shawarma.utils.ScreenPaths
import com.example.shawarma.viewmodels.ProductsViewModel
import com.example.shawarma.widgets.ShawarmaLogo2 import com.example.shawarma.widgets.ShawarmaLogo2
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
@Composable @Composable
fun ProductsScreen(navHostController: NavHostController) { fun ProductsScreen(navHostController: NavHostController) {
@ -65,16 +58,9 @@ fun ProductsScreen(navHostController: NavHostController) {
@Composable @Composable
fun ProductsList(navHostController: NavHostController){ fun ProductsList(navHostController: NavHostController){
val context = LocalContext.current val productsViewModel: ProductsViewModel = hiltViewModel<ProductsViewModel>()
val products = remember { mutableStateListOf<ProductModel>() }
LaunchedEffect(Unit) { val products = productsViewModel.products.observeAsState().value
withContext(Dispatchers.IO) {
AppDatabase.getInstance(context).productDao().getItems().collect { data ->
products.clear()
products.addAll(data)
}
}
}
Box( Box(
modifier = Modifier modifier = Modifier
@ -117,11 +103,13 @@ fun ProductsList(navHostController: NavHostController){
modifier = Modifier.padding(top = 160.dp) modifier = Modifier.padding(top = 160.dp)
) )
{ {
items(products.size) { index -> if (products != null) {
ProductItem(products[index], navHostController) items(products.size) { index ->
Spacer(modifier = Modifier.height(20.dp)) ProductItem(products[index], navHostController)
if (index == products.size - 1) { Spacer(modifier = Modifier.height(20.dp))
Spacer(modifier = Modifier.height(70.dp)) if (index == products.size - 1) {
Spacer(modifier = Modifier.height(70.dp))
}
} }
} }
} }
@ -131,6 +119,8 @@ fun ProductsList(navHostController: NavHostController){
@Composable @Composable
fun ProductItem(product: ProductModel, navHostController: NavHostController){ fun ProductItem(product: ProductModel, navHostController: NavHostController){
val productsViewModel: ProductsViewModel = hiltViewModel<ProductsViewModel>()
Card( Card(
border = BorderStroke(width = 2.dp, color = MyOrange), border = BorderStroke(width = 2.dp, color = MyOrange),
shape = RoundedCornerShape(size = 20.dp), shape = RoundedCornerShape(size = 20.dp),
@ -196,7 +186,9 @@ fun ProductItem(product: ProductModel, navHostController: NavHostController){
colors = ButtonDefaults.buttonColors( colors = ButtonDefaults.buttonColors(
backgroundColor = MyLightRed backgroundColor = MyLightRed
), ),
onClick = { /*TODO*/ } onClick = {
productsViewModel.deleteProduct(product)
}
) { ) {
Icon( Icon(
painter = painterResource(id = R.drawable.trash), painter = painterResource(id = R.drawable.trash),

View File

@ -0,0 +1,36 @@
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.ProductModel
import com.example.shawarma.data.repos.ProductRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject
@HiltViewModel
class ProductsViewModel @Inject constructor(
private val productRepository: ProductRepository
) : ViewModel() {
private val _products = MutableLiveData<List<ProductModel>>()
val products: LiveData<List<ProductModel>>
get() = _products
init {
viewModelScope.launch {
productRepository.getItems().collect {
_products.postValue(it)
}
}
}
fun deleteProduct(product: ProductModel) {
viewModelScope.launch {
productRepository.delete(product)
}
}
}