Изменение продукта

This commit is contained in:
Данила Мочалов 2023-11-22 01:55:28 +04:00
parent 81b1fbd33b
commit 65ff36e9bc
3 changed files with 34 additions and 25 deletions

View File

@ -37,7 +37,6 @@ import androidx.compose.ui.zIndex
import com.example.shawarma.R import com.example.shawarma.R
import com.example.shawarma.data.db.AppDatabase 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.ProductCard
import com.example.shawarma.ui.theme.MarckFamily import com.example.shawarma.ui.theme.MarckFamily
import com.example.shawarma.ui.theme.MyLightYellow import com.example.shawarma.ui.theme.MyLightYellow
import com.example.shawarma.ui.theme.MyMainBackground import com.example.shawarma.ui.theme.MyMainBackground
@ -102,7 +101,7 @@ fun DiscountList(){
.fillMaxWidth() .fillMaxWidth()
.padding(top = 10.dp) .padding(top = 10.dp)
) { ) {
ProductCard(products[index]) DiscountCard(products[index])
} }
} }
else { else {

View File

@ -14,14 +14,12 @@ import androidx.compose.material.Button
import androidx.compose.material.ButtonDefaults import androidx.compose.material.ButtonDefaults
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.livedata.observeAsState
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember 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.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.TextFieldValue import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
@ -29,8 +27,6 @@ import androidx.compose.ui.unit.sp
import androidx.compose.ui.zIndex import androidx.compose.ui.zIndex
import androidx.hilt.navigation.compose.hiltViewModel import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController import androidx.navigation.NavHostController
import com.example.shawarma.data.db.AppDatabase
import com.example.shawarma.data.models.ProductModel
import com.example.shawarma.ui.theme.MarckFamily import com.example.shawarma.ui.theme.MarckFamily
import com.example.shawarma.ui.theme.MyLightYellow import com.example.shawarma.ui.theme.MyLightYellow
import com.example.shawarma.ui.theme.MyMainBackground import com.example.shawarma.ui.theme.MyMainBackground
@ -40,8 +36,6 @@ import com.example.shawarma.utils.ScreenPaths
import com.example.shawarma.viewmodels.ProductViewModel import com.example.shawarma.viewmodels.ProductViewModel
import com.example.shawarma.widgets.MyTextField import com.example.shawarma.widgets.MyTextField
import com.example.shawarma.widgets.ShawarmaLogo2 import com.example.shawarma.widgets.ShawarmaLogo2
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
@Composable @Composable
fun ProductScreen(navHostController: NavHostController, productId: Int?) { fun ProductScreen(navHostController: NavHostController, productId: Int?) {
@ -60,6 +54,7 @@ fun ProductWidget(navHostController: NavHostController, productId: Int?) {
val oldPrice = remember { mutableStateOf(TextFieldValue(""))} val oldPrice = remember { mutableStateOf(TextFieldValue(""))}
val productViewModel: ProductViewModel = hiltViewModel<ProductViewModel>() val productViewModel: ProductViewModel = hiltViewModel<ProductViewModel>()
val product = productViewModel.product.observeAsState().value
if (productViewModel.addingProductState.observeAsState().value == true) { if (productViewModel.addingProductState.observeAsState().value == true) {
navHostController.navigate(ScreenPaths.products.name) { navHostController.navigate(ScreenPaths.products.name) {
@ -69,21 +64,14 @@ fun ProductWidget(navHostController: NavHostController, productId: Int?) {
} }
} }
if (productId != 0 && productId != null) { if (productId != null) {
val context = LocalContext.current productViewModel.getProduct(productId)
var product = remember { }
mutableStateOf(ProductModel(id = -1, title = "", price = 0, oldPrice = null))
} if (product != null) {
LaunchedEffect(Unit) { title.value = TextFieldValue(text = product.title)
withContext(Dispatchers.IO) { price.value = TextFieldValue(text = product.price.toString())
AppDatabase.getInstance(context).productDao().getById(productId).collect { data -> oldPrice.value = TextFieldValue(text = product.oldPrice.toString())
product.value = data
}
}
}
title.value = TextFieldValue(text = product.value.title)
price.value = TextFieldValue(text = product.value.price.toString())
oldPrice.value = TextFieldValue(text = product.value.oldPrice.toString())
} }
Box( Box(
@ -151,11 +139,14 @@ fun ProductWidget(navHostController: NavHostController, productId: Int?) {
border = BorderStroke(2.dp, color = MyOrange), border = BorderStroke(2.dp, color = MyOrange),
shape = RoundedCornerShape(20.dp), shape = RoundedCornerShape(20.dp),
onClick = { onClick = {
productViewModel.addProduct(title.value.text, price.value.text.toInt(), if (!oldPrice.value.text.isNullOrEmpty()) oldPrice.value.text.toInt() else null) if (product == null)
productViewModel.addProduct(title.value.text, price.value.text.toInt(), if (!oldPrice.value.text.isNullOrEmpty()) oldPrice.value.text.toInt() else null)
else
productViewModel.updateProduct(title.value.text, price.value.text.toInt(), if (!oldPrice.value.text.isNullOrEmpty()) oldPrice.value.text.toInt() else null)
} }
) { ) {
Text( Text(
text = "Добавить", text = if (product != null) "Изменить" else "Добавить",
fontFamily = NunitoFamily, fontFamily = NunitoFamily,
fontSize = 24.sp, fontSize = 24.sp,
fontWeight = FontWeight.Bold fontWeight = FontWeight.Bold

View File

@ -18,10 +18,29 @@ class ProductViewModel @Inject constructor(
val addingProductState: LiveData<Boolean?> val addingProductState: LiveData<Boolean?>
get() = _addingProductState get() = _addingProductState
private val _product = MutableLiveData<ProductModel>()
val product: LiveData<ProductModel>
get() = _product
fun addProduct(name: String, price: Int, oldPrice: Int? = null) { fun addProduct(name: String, price: Int, oldPrice: Int? = null) {
viewModelScope.launch { viewModelScope.launch {
productRepository.insert(ProductModel(null, name, price, oldPrice)) productRepository.insert(ProductModel(null, name, price, oldPrice))
_addingProductState.postValue(true) _addingProductState.postValue(true)
} }
} }
fun getProduct(productId: Int) {
viewModelScope.launch {
productRepository.getById(productId).collect {
_product.postValue(it)
}
}
}
fun updateProduct(name: String, price: Int, oldPrice: Int? = null) {
viewModelScope.launch {
productRepository.update(ProductModel(product.value?.id, name, price, oldPrice))
_addingProductState.postValue(true)
}
}
} }