Удаление продукта
This commit is contained in:
parent
73f4b57547
commit
81b1fbd33b
@ -13,7 +13,6 @@ 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
|
||||
@ -21,25 +20,20 @@ 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 androidx.hilt.navigation.compose.hiltViewModel
|
||||
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
|
||||
@ -47,9 +41,8 @@ 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.viewmodels.ProductsViewModel
|
||||
import com.example.shawarma.widgets.ShawarmaLogo2
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
@Composable
|
||||
fun ProductsScreen(navHostController: NavHostController) {
|
||||
@ -65,16 +58,9 @@ fun ProductsScreen(navHostController: NavHostController) {
|
||||
|
||||
@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)
|
||||
}
|
||||
}
|
||||
}
|
||||
val productsViewModel: ProductsViewModel = hiltViewModel<ProductsViewModel>()
|
||||
|
||||
val products = productsViewModel.products.observeAsState().value
|
||||
|
||||
Box(
|
||||
modifier = Modifier
|
||||
@ -117,11 +103,13 @@ fun ProductsList(navHostController: NavHostController){
|
||||
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))
|
||||
if (products != null) {
|
||||
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))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -131,6 +119,8 @@ fun ProductsList(navHostController: NavHostController){
|
||||
|
||||
@Composable
|
||||
fun ProductItem(product: ProductModel, navHostController: NavHostController){
|
||||
val productsViewModel: ProductsViewModel = hiltViewModel<ProductsViewModel>()
|
||||
|
||||
Card(
|
||||
border = BorderStroke(width = 2.dp, color = MyOrange),
|
||||
shape = RoundedCornerShape(size = 20.dp),
|
||||
@ -196,7 +186,9 @@ fun ProductItem(product: ProductModel, navHostController: NavHostController){
|
||||
colors = ButtonDefaults.buttonColors(
|
||||
backgroundColor = MyLightRed
|
||||
),
|
||||
onClick = { /*TODO*/ }
|
||||
onClick = {
|
||||
productsViewModel.deleteProduct(product)
|
||||
}
|
||||
) {
|
||||
Icon(
|
||||
painter = painterResource(id = R.drawable.trash),
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user