Изменение продукта
This commit is contained in:
parent
81b1fbd33b
commit
65ff36e9bc
@ -37,7 +37,6 @@ import androidx.compose.ui.zIndex
|
||||
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.ProductCard
|
||||
import com.example.shawarma.ui.theme.MarckFamily
|
||||
import com.example.shawarma.ui.theme.MyLightYellow
|
||||
import com.example.shawarma.ui.theme.MyMainBackground
|
||||
@ -102,7 +101,7 @@ fun DiscountList(){
|
||||
.fillMaxWidth()
|
||||
.padding(top = 10.dp)
|
||||
) {
|
||||
ProductCard(products[index])
|
||||
DiscountCard(products[index])
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
@ -14,14 +14,12 @@ import androidx.compose.material.Button
|
||||
import androidx.compose.material.ButtonDefaults
|
||||
import androidx.compose.material.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.livedata.observeAsState
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.text.input.TextFieldValue
|
||||
import androidx.compose.ui.unit.dp
|
||||
@ -29,8 +27,6 @@ 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.data.db.AppDatabase
|
||||
import com.example.shawarma.data.models.ProductModel
|
||||
import com.example.shawarma.ui.theme.MarckFamily
|
||||
import com.example.shawarma.ui.theme.MyLightYellow
|
||||
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.widgets.MyTextField
|
||||
import com.example.shawarma.widgets.ShawarmaLogo2
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
@Composable
|
||||
fun ProductScreen(navHostController: NavHostController, productId: Int?) {
|
||||
@ -60,6 +54,7 @@ fun ProductWidget(navHostController: NavHostController, productId: Int?) {
|
||||
val oldPrice = remember { mutableStateOf(TextFieldValue(""))}
|
||||
|
||||
val productViewModel: ProductViewModel = hiltViewModel<ProductViewModel>()
|
||||
val product = productViewModel.product.observeAsState().value
|
||||
|
||||
if (productViewModel.addingProductState.observeAsState().value == true) {
|
||||
navHostController.navigate(ScreenPaths.products.name) {
|
||||
@ -69,21 +64,14 @@ fun ProductWidget(navHostController: NavHostController, productId: Int?) {
|
||||
}
|
||||
}
|
||||
|
||||
if (productId != 0 && productId != null) {
|
||||
val context = LocalContext.current
|
||||
var product = remember {
|
||||
mutableStateOf(ProductModel(id = -1, title = "", price = 0, oldPrice = null))
|
||||
}
|
||||
LaunchedEffect(Unit) {
|
||||
withContext(Dispatchers.IO) {
|
||||
AppDatabase.getInstance(context).productDao().getById(productId).collect { data ->
|
||||
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())
|
||||
if (productId != null) {
|
||||
productViewModel.getProduct(productId)
|
||||
}
|
||||
|
||||
if (product != null) {
|
||||
title.value = TextFieldValue(text = product.title)
|
||||
price.value = TextFieldValue(text = product.price.toString())
|
||||
oldPrice.value = TextFieldValue(text = product.oldPrice.toString())
|
||||
}
|
||||
|
||||
Box(
|
||||
@ -151,11 +139,14 @@ fun ProductWidget(navHostController: NavHostController, productId: Int?) {
|
||||
border = BorderStroke(2.dp, color = MyOrange),
|
||||
shape = RoundedCornerShape(20.dp),
|
||||
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 = if (product != null) "Изменить" else "Добавить",
|
||||
fontFamily = NunitoFamily,
|
||||
fontSize = 24.sp,
|
||||
fontWeight = FontWeight.Bold
|
||||
|
@ -18,10 +18,29 @@ class ProductViewModel @Inject constructor(
|
||||
val addingProductState: LiveData<Boolean?>
|
||||
get() = _addingProductState
|
||||
|
||||
private val _product = MutableLiveData<ProductModel>()
|
||||
val product: LiveData<ProductModel>
|
||||
get() = _product
|
||||
|
||||
fun addProduct(name: String, price: Int, oldPrice: Int? = null) {
|
||||
viewModelScope.launch {
|
||||
productRepository.insert(ProductModel(null, name, price, oldPrice))
|
||||
_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)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user