From 65ff36e9bc979087f10c4aed8190cf1bc37ca1b1 Mon Sep 17 00:00:00 2001 From: Danya_Mochalov Date: Wed, 22 Nov 2023 01:55:28 +0400 Subject: [PATCH] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=BF=D1=80=D0=BE=D0=B4=D1=83=D0=BA=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../screens/discount/DiscountScreen.kt | 3 +- .../screens/products/ProductScreen.kt | 37 +++++++------------ .../shawarma/viewmodels/ProductViewModel.kt | 19 ++++++++++ 3 files changed, 34 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/com/example/shawarma/screens/discount/DiscountScreen.kt b/app/src/main/java/com/example/shawarma/screens/discount/DiscountScreen.kt index e66095a..e9de194 100644 --- a/app/src/main/java/com/example/shawarma/screens/discount/DiscountScreen.kt +++ b/app/src/main/java/com/example/shawarma/screens/discount/DiscountScreen.kt @@ -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 { diff --git a/app/src/main/java/com/example/shawarma/screens/products/ProductScreen.kt b/app/src/main/java/com/example/shawarma/screens/products/ProductScreen.kt index 60c3b35..01bbaca 100644 --- a/app/src/main/java/com/example/shawarma/screens/products/ProductScreen.kt +++ b/app/src/main/java/com/example/shawarma/screens/products/ProductScreen.kt @@ -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() + 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 diff --git a/app/src/main/java/com/example/shawarma/viewmodels/ProductViewModel.kt b/app/src/main/java/com/example/shawarma/viewmodels/ProductViewModel.kt index 162b94c..b3438e9 100644 --- a/app/src/main/java/com/example/shawarma/viewmodels/ProductViewModel.kt +++ b/app/src/main/java/com/example/shawarma/viewmodels/ProductViewModel.kt @@ -18,10 +18,29 @@ class ProductViewModel @Inject constructor( val addingProductState: LiveData get() = _addingProductState + private val _product = MutableLiveData() + val product: LiveData + 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) + } + } } \ No newline at end of file