From d231f70dca3e7714e83275bf81dd191fde29f59d Mon Sep 17 00:00:00 2001 From: Mihai-Cristian Condrea Date: Wed, 1 Jan 2025 10:15:09 +0200 Subject: [PATCH] refactor: Updated string resources, improved error handling, added error dialogs, and enhanced cart item management Significantly improved error handling, user experience, and cart item management: - Updated all string resources for better clarity, consistency, and localization. - Enhanced the app's error handling mechanism for better user experience. - Added informative error dialogs to provide users with clear and actionable feedback in case of issues. - Added a new option to edit an entire cart item via swiping it, providing users with more flexibility. --- CHANGELOG.md | 1 + app/build.gradle.kts | 2 +- .../{UiCartModel.kt => UiCartScreen.kt} | 4 +- .../dialogs/AddNewCartAlertDialog.kt | 3 +- .../dialogs/AddNewCartItemAlertDialog.kt | 59 +++++++--- .../dialogs/DeleteCartAlertDialog.kt | 3 +- .../dialogs/DeleteCartItemAlertDialog.kt | 3 +- .../dialogs/SelectCurrencyAlertDialog.kt | 3 +- .../ui/components/preferences/Preferences.kt | 13 +-- .../ui/screens/cart/CartScreen.kt | 105 +++++++++++------- .../ui/screens/cart/CartViewModel.kt | 31 +++++- 11 files changed, 154 insertions(+), 73 deletions(-) rename app/src/main/kotlin/com/d4rk/cartcalculator/data/model/ui/screens/{UiCartModel.kt => UiCartScreen.kt} (81%) diff --git a/CHANGELOG.md b/CHANGELOG.md index c0f76bc..b065724 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ # Version 1.2.0: +- **New**: Added a new option to edit an entire cart item via swiping it, providing users with more flexibility. - **New**: Added an "Open Cart After Creation" feature in settings, allowing you to automatically view newly created carts. - **Minor**: Improved UI handling when the keyboard appears, ensuring smoother interactions during diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 7a0d193..06e27f3 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -15,7 +15,7 @@ android { applicationId = "com.d4rk.cartcalculator" minSdk = 23 targetSdk = 35 - versionCode = 78 + versionCode = 79 versionName = "1.2.0" testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" resourceConfigurations += listOf( diff --git a/app/src/main/kotlin/com/d4rk/cartcalculator/data/model/ui/screens/UiCartModel.kt b/app/src/main/kotlin/com/d4rk/cartcalculator/data/model/ui/screens/UiCartScreen.kt similarity index 81% rename from app/src/main/kotlin/com/d4rk/cartcalculator/data/model/ui/screens/UiCartModel.kt rename to app/src/main/kotlin/com/d4rk/cartcalculator/data/model/ui/screens/UiCartScreen.kt index 874228e..44e8b96 100644 --- a/app/src/main/kotlin/com/d4rk/cartcalculator/data/model/ui/screens/UiCartModel.kt +++ b/app/src/main/kotlin/com/d4rk/cartcalculator/data/model/ui/screens/UiCartScreen.kt @@ -3,13 +3,15 @@ package com.d4rk.cartcalculator.data.model.ui.screens import com.d4rk.cartcalculator.data.database.table.ShoppingCartItemsTable import com.d4rk.cartcalculator.data.database.table.ShoppingCartTable -data class UiCartModel( +data class UiCartScreen( val selectedCurrency: String = "" , val totalPrice: Double = 0.0 , val cartItems: List = emptyList() , val cart: ShoppingCartTable? = null , + val currentCartItemForEdit: ShoppingCartItemsTable? = null, val openDialog: Boolean = false , val openDeleteDialog: Boolean = false , + val openEditDialog: Boolean = false, val currentCartItemForDeletion: ShoppingCartItemsTable? = null , val itemQuantities: Map = emptyMap() ) diff --git a/app/src/main/kotlin/com/d4rk/cartcalculator/ui/components/dialogs/AddNewCartAlertDialog.kt b/app/src/main/kotlin/com/d4rk/cartcalculator/ui/components/dialogs/AddNewCartAlertDialog.kt index cb7e34c..2318fd1 100644 --- a/app/src/main/kotlin/com/d4rk/cartcalculator/ui/components/dialogs/AddNewCartAlertDialog.kt +++ b/app/src/main/kotlin/com/d4rk/cartcalculator/ui/components/dialogs/AddNewCartAlertDialog.kt @@ -25,6 +25,7 @@ import androidx.compose.ui.text.input.KeyboardCapitalization import androidx.compose.ui.unit.dp import com.d4rk.cartcalculator.R import com.d4rk.cartcalculator.data.database.table.ShoppingCartTable +import com.d4rk.cartcalculator.ui.components.spacers.MediumVerticalSpacer import java.util.Date @Composable @@ -86,7 +87,7 @@ fun AddNewCartAlertDialogContent( placeholder = { Text(text = stringResource(id = R.string.shopping_cart)) }) Spacer(modifier = Modifier.height(24.dp)) Icon(imageVector = Icons.Outlined.Info , contentDescription = null) - Spacer(modifier = Modifier.height(12.dp)) + MediumVerticalSpacer() Text(text = stringResource(id = R.string.summary_cart_dialog)) } newCart.value = diff --git a/app/src/main/kotlin/com/d4rk/cartcalculator/ui/components/dialogs/AddNewCartItemAlertDialog.kt b/app/src/main/kotlin/com/d4rk/cartcalculator/ui/components/dialogs/AddNewCartItemAlertDialog.kt index 7efd687..42ae18e 100644 --- a/app/src/main/kotlin/com/d4rk/cartcalculator/ui/components/dialogs/AddNewCartItemAlertDialog.kt +++ b/app/src/main/kotlin/com/d4rk/cartcalculator/ui/components/dialogs/AddNewCartItemAlertDialog.kt @@ -29,15 +29,31 @@ import androidx.compose.ui.text.input.KeyboardType import androidx.compose.ui.unit.dp import com.d4rk.cartcalculator.R import com.d4rk.cartcalculator.data.database.table.ShoppingCartItemsTable +import com.d4rk.cartcalculator.ui.components.spacers.MediumVerticalSpacer @Composable fun AddNewCartItemAlertDialog( - cartId : Int , onDismiss : () -> Unit , onCartCreated : (ShoppingCartItemsTable) -> Unit + cartId : Int , + onDismiss : () -> Unit , + onCartCreated : (ShoppingCartItemsTable) -> Unit , + existingCartItem : ShoppingCartItemsTable? = null ) { + val newCartItem : MutableState = remember { mutableStateOf(value = null) } + val initialName : String = existingCartItem?.name ?: "" + val initialPrice : String = existingCartItem?.price ?: "" + val initialQuantity : String = existingCartItem?.quantity?.toString() ?: "" + AlertDialog(onDismissRequest = onDismiss , text = { - AddNewCartItemAlertDialogContent(cartId = cartId , newCartItem = newCartItem) + AddNewCartItemAlertDialogContent( + cartId = cartId , + newCartItem = newCartItem , + initialName = initialName , + initialPrice = initialPrice , + initialQuantity = initialQuantity , + existingCartItem = existingCartItem + ) } , icon = { Icon( Icons.Outlined.ShoppingBag , contentDescription = null @@ -61,11 +77,16 @@ fun AddNewCartItemAlertDialog( @Composable fun AddNewCartItemAlertDialogContent( - cartId : Int , newCartItem : MutableState + cartId : Int , + newCartItem : MutableState , + initialName : String , + initialPrice : String , + initialQuantity : String , + existingCartItem : ShoppingCartItemsTable? ) { - val nameText : MutableState = remember { mutableStateOf(value = "") } - val priceText : MutableState = remember { mutableStateOf(value = "") } - val quantityText : MutableState = remember { mutableStateOf(value = "") } + val nameText : MutableState = remember { mutableStateOf(value = initialName) } + val priceText : MutableState = remember { mutableStateOf(value = initialPrice) } + val quantityText : MutableState = remember { mutableStateOf(value = initialQuantity) } val nameFocusRequester : FocusRequester = remember { FocusRequester() } val priceFocusRequester : FocusRequester = remember { FocusRequester() } @@ -113,21 +134,29 @@ fun AddNewCartItemAlertDialogContent( Spacer(modifier = Modifier.height(height = 24.dp)) Icon(imageVector = Icons.Outlined.Info , contentDescription = null) - Spacer(modifier = Modifier.height(height = 12.dp)) + MediumVerticalSpacer() Text(text = stringResource(id = R.string.dialog_info_cart_item)) } if (nameText.value.isNotBlank() && priceText.value.isNotBlank() && quantityText.value.isNotBlank()) { - val price : Double? = - priceText.value.replace(oldChar = ',' , newChar = '.').toDoubleOrNull() + val price : Double? = priceText.value.replace(oldChar = ',' , newChar = '.').toDoubleOrNull() val quantity : Int? = quantityText.value.toIntOrNull() if (price != null && quantity != null) { - newCartItem.value = ShoppingCartItemsTable( - cartId = cartId , - name = nameText.value , - price = price.toString() , - quantity = quantity - ) + newCartItem.value = if (existingCartItem != null) { + val updatedItem = existingCartItem.copy( + name = nameText.value , price = price.toString() , quantity = quantity + ) + updatedItem + } + else { + val newItem = ShoppingCartItemsTable( + cartId = cartId , + name = nameText.value , + price = price.toString() , + quantity = quantity + ) + newItem + } } else { newCartItem.value = null diff --git a/app/src/main/kotlin/com/d4rk/cartcalculator/ui/components/dialogs/DeleteCartAlertDialog.kt b/app/src/main/kotlin/com/d4rk/cartcalculator/ui/components/dialogs/DeleteCartAlertDialog.kt index fd0e5e0..0f97be7 100644 --- a/app/src/main/kotlin/com/d4rk/cartcalculator/ui/components/dialogs/DeleteCartAlertDialog.kt +++ b/app/src/main/kotlin/com/d4rk/cartcalculator/ui/components/dialogs/DeleteCartAlertDialog.kt @@ -18,6 +18,7 @@ import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp import com.d4rk.cartcalculator.R import com.d4rk.cartcalculator.data.database.table.ShoppingCartTable +import com.d4rk.cartcalculator.ui.components.spacers.MediumVerticalSpacer @Composable fun DeleteCartAlertDialog( @@ -61,7 +62,7 @@ fun DeleteCartAlertDialogContent(cart : ShoppingCartTable?) { ) Spacer(modifier = Modifier.height(height = 24.dp)) Icon(imageVector = Icons.Outlined.Info , contentDescription = null) - Spacer(modifier = Modifier.height(height = 12.dp)) + MediumVerticalSpacer() Text( text = stringResource(id= R.string.delete_cart_warning , cart?.name ?: "") , style = MaterialTheme.typography.bodyLarge diff --git a/app/src/main/kotlin/com/d4rk/cartcalculator/ui/components/dialogs/DeleteCartItemAlertDialog.kt b/app/src/main/kotlin/com/d4rk/cartcalculator/ui/components/dialogs/DeleteCartItemAlertDialog.kt index 5523595..9d085e1 100644 --- a/app/src/main/kotlin/com/d4rk/cartcalculator/ui/components/dialogs/DeleteCartItemAlertDialog.kt +++ b/app/src/main/kotlin/com/d4rk/cartcalculator/ui/components/dialogs/DeleteCartItemAlertDialog.kt @@ -18,6 +18,7 @@ import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp import com.d4rk.cartcalculator.R import com.d4rk.cartcalculator.data.database.table.ShoppingCartItemsTable +import com.d4rk.cartcalculator.ui.components.spacers.MediumVerticalSpacer @Composable fun DeleteCartItemAlertDialog( @@ -65,7 +66,7 @@ fun DeleteCartItemAlertDialogContent(cartItem: ShoppingCartItemsTable) { ) Spacer(modifier = Modifier.height(24.dp)) Icon(imageVector = Icons.Outlined.Info, contentDescription = null) - Spacer(modifier = Modifier.height(12.dp)) + MediumVerticalSpacer() Text( text = stringResource(id= R.string.delete_cart_item_warning, cartItem.name), style = MaterialTheme.typography.bodyLarge diff --git a/app/src/main/kotlin/com/d4rk/cartcalculator/ui/components/dialogs/SelectCurrencyAlertDialog.kt b/app/src/main/kotlin/com/d4rk/cartcalculator/ui/components/dialogs/SelectCurrencyAlertDialog.kt index f4c5b34..7f03a1f 100644 --- a/app/src/main/kotlin/com/d4rk/cartcalculator/ui/components/dialogs/SelectCurrencyAlertDialog.kt +++ b/app/src/main/kotlin/com/d4rk/cartcalculator/ui/components/dialogs/SelectCurrencyAlertDialog.kt @@ -30,6 +30,7 @@ import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp import com.d4rk.cartcalculator.R import com.d4rk.cartcalculator.data.datastore.DataStore +import com.d4rk.cartcalculator.ui.components.spacers.MediumVerticalSpacer import kotlinx.coroutines.flow.firstOrNull @Composable @@ -96,7 +97,7 @@ fun SelectCurrencyAlertDialogContent( } Spacer(modifier = Modifier.height(height = 24.dp)) Icon(imageVector = Icons.Outlined.Info , contentDescription = null) - Spacer(modifier = Modifier.height(height = 12.dp)) + MediumVerticalSpacer() Text(text = stringResource(id = R.string.dialog_info_currency)) } diff --git a/app/src/main/kotlin/com/d4rk/cartcalculator/ui/components/preferences/Preferences.kt b/app/src/main/kotlin/com/d4rk/cartcalculator/ui/components/preferences/Preferences.kt index f04e27b..2dbcecc 100644 --- a/app/src/main/kotlin/com/d4rk/cartcalculator/ui/components/preferences/Preferences.kt +++ b/app/src/main/kotlin/com/d4rk/cartcalculator/ui/components/preferences/Preferences.kt @@ -6,12 +6,10 @@ import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row -import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size -import androidx.compose.foundation.layout.width import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.Check @@ -33,6 +31,7 @@ import androidx.compose.ui.platform.LocalView import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.dp +import com.d4rk.cartcalculator.ui.components.spacers.LargeHorizontalSpacer /** * Creates a clickable card with a title and a switch for app preference screens. @@ -157,7 +156,7 @@ fun PreferenceItem( }) , verticalAlignment = Alignment.CenterVertically ) { icon?.let { - Spacer(modifier = Modifier.width(16.dp)) + LargeHorizontalSpacer() Icon(it , contentDescription = null) } Column( @@ -212,9 +211,9 @@ fun SwitchPreferenceItem( }) , verticalAlignment = Alignment.CenterVertically ) { icon?.let { - Spacer(modifier = Modifier.width(16.dp)) + LargeHorizontalSpacer() Icon(it , contentDescription = null) - Spacer(modifier = Modifier.width(16.dp)) + LargeHorizontalSpacer() } Column( modifier = Modifier @@ -270,9 +269,9 @@ fun SwitchPreferenceItemWithDivider( }) , verticalAlignment = Alignment.CenterVertically ) { icon?.let { - Spacer(modifier = Modifier.width(16.dp)) + LargeHorizontalSpacer() Icon(it , contentDescription = null) - Spacer(modifier = Modifier.width(16.dp)) + LargeHorizontalSpacer() } Column( modifier = Modifier diff --git a/app/src/main/kotlin/com/d4rk/cartcalculator/ui/screens/cart/CartScreen.kt b/app/src/main/kotlin/com/d4rk/cartcalculator/ui/screens/cart/CartScreen.kt index b71ee80..a91eee9 100644 --- a/app/src/main/kotlin/com/d4rk/cartcalculator/ui/screens/cart/CartScreen.kt +++ b/app/src/main/kotlin/com/d4rk/cartcalculator/ui/screens/cart/CartScreen.kt @@ -66,7 +66,7 @@ import androidx.lifecycle.viewmodel.compose.viewModel import com.d4rk.cartcalculator.R import com.d4rk.cartcalculator.data.database.table.ShoppingCartItemsTable import com.d4rk.cartcalculator.data.model.ui.error.UiErrorModel -import com.d4rk.cartcalculator.data.model.ui.screens.UiCartModel +import com.d4rk.cartcalculator.data.model.ui.screens.UiCartScreen import com.d4rk.cartcalculator.ui.components.ads.AdBanner import com.d4rk.cartcalculator.ui.components.dialogs.AddNewCartItemAlertDialog import com.d4rk.cartcalculator.ui.components.dialogs.DeleteCartItemAlertDialog @@ -86,7 +86,7 @@ fun CartScreen(activity : CartActivity , cartId : Int) { val scrollBehavior = TopAppBarDefaults.enterAlwaysScrollBehavior(rememberTopAppBarState()) val primaryColor = MaterialTheme.colorScheme.primary - val uiState : UiCartModel by viewModel.uiState.collectAsState() + val uiState : UiCartScreen by viewModel.uiState.collectAsState() val isLoading : Boolean by viewModel.isLoading.collectAsState() val visibilityStates by viewModel.visibilityStates.collectAsState() @@ -294,6 +294,21 @@ fun CartScreen(activity : CartActivity , cartId : Int) { }) } } + + if (uiState.openEditDialog) { + val currentItem = uiState.currentCartItemForEdit + if (currentItem != null) { + AddNewCartItemAlertDialog( + cartId = cartId, + existingCartItem = currentItem, + onDismiss = { viewModel.toggleEditDialog(cartItem = null) }, + onCartCreated = { updatedCartItem -> + viewModel.editCartItem(cartItem = updatedCartItem) + viewModel.toggleEditDialog(cartItem = null) + } + ) + } + } } } @@ -326,14 +341,14 @@ fun CartScreen(activity : CartActivity , cartId : Int) { @OptIn(ExperimentalFoundationApi::class) @Composable fun CartItemComposable( - viewModel : CartViewModel , - cartItem : ShoppingCartItemsTable , - onMinusClick : (ShoppingCartItemsTable) -> Unit , - onPlusClick : (ShoppingCartItemsTable) -> Unit , - uiState : UiCartModel , - modifier : Modifier , + viewModel: CartViewModel , + cartItem: ShoppingCartItemsTable , + onMinusClick: (ShoppingCartItemsTable) -> Unit , + onPlusClick: (ShoppingCartItemsTable) -> Unit , + uiState: UiCartScreen , + modifier: Modifier , ) { - val view : View = LocalView.current + val view: View = LocalView.current var checkedState by remember { mutableStateOf(cartItem.isChecked) } @@ -341,29 +356,37 @@ fun CartItemComposable( val interactionSource = remember { MutableInteractionSource() } val dismissState = rememberSwipeToDismissBoxState(confirmValueChange = { - if (it == SwipeToDismissBoxValue.StartToEnd || it == SwipeToDismissBoxValue.EndToStart) { - ! uiState.openDeleteDialog - } - else { - true + when (it) { + SwipeToDismissBoxValue.StartToEnd -> { + viewModel.toggleEditDialog(cartItem = cartItem) + false + } + SwipeToDismissBoxValue.EndToStart -> { + viewModel.toggleDeleteDialog(cartItem = cartItem) + false + } + else -> true } }) - LaunchedEffect(key1 = dismissState.targetValue , key2 = dismissState.currentValue) { + LaunchedEffect(key1 = dismissState.targetValue, key2 = dismissState.currentValue) { when { dismissState.currentValue == dismissState.targetValue -> { dismissState.reset() } - dismissState.targetValue != SwipeToDismissBoxValue.Settled -> { - viewModel.toggleDeleteDialog(cartItem = cartItem) + if (dismissState.targetValue == SwipeToDismissBoxValue.StartToEnd) { + viewModel.toggleEditDialog(cartItem = cartItem) + } else if (dismissState.targetValue == SwipeToDismissBoxValue.EndToStart) { + viewModel.toggleDeleteDialog(cartItem = cartItem) + } } } } - SwipeToDismissBox(modifier = modifier.hapticSwipeToDismissBox(dismissState) , - state = dismissState , - backgroundContent = {} , + SwipeToDismissBox(modifier = modifier.hapticSwipeToDismissBox(dismissState), + state = dismissState, + backgroundContent = {}, content = { Box( modifier = modifier @@ -371,36 +394,36 @@ fun CartItemComposable( .padding(all = 24.dp) ) { Row( - modifier = Modifier.fillMaxSize() , - horizontalArrangement = Arrangement.SpaceBetween , + modifier = Modifier.fillMaxSize(), + horizontalArrangement = Arrangement.SpaceBetween, ) { Row { Checkbox(modifier = Modifier .bounceClick() - .padding(end = 16.dp) , - checked = checkedState , + .padding(end = 16.dp), + checked = checkedState, onCheckedChange = { isChecked -> view.playSoundEffect(SoundEffectConstants.CLICK) checkedState = isChecked viewModel.onItemCheckedChange( - cartItem , isChecked + cartItem, isChecked ) }) Column { Text( - text = cartItem.name , + text = cartItem.name, style = MaterialTheme.typography.bodyLarge ) Row { Text( text = String.format( - Locale.US , "%.1f" , cartItem.price.toFloat() - ).removeSuffix(".0") , + Locale.US, "%.1f", cartItem.price.toFloat() + ).removeSuffix(".0"), ) Spacer(modifier = Modifier.width(4.dp)) Text( - text = uiState.selectedCurrency , + text = uiState.selectedCurrency, style = MaterialTheme.typography.bodyLarge ) } @@ -413,37 +436,37 @@ fun CartItemComposable( .size(40.dp) .clip(CircleShape) .combinedClickable( - interactionSource = interactionSource , - indication = remember { ripple() } , + interactionSource = interactionSource, + indication = remember { ripple() }, onClick = { view.playSoundEffect(SoundEffectConstants.CLICK) onMinusClick(cartItem) - } , + }, onLongClick = { viewModel.toggleDeleteDialog(cartItem = cartItem) - } , + }, )) { Icon( - imageVector = Icons.Outlined.RemoveCircleOutline , - contentDescription = stringResource(id = R.string.decrease_quantity) , + imageVector = Icons.Outlined.RemoveCircleOutline, + contentDescription = stringResource(id = R.string.decrease_quantity), modifier = Modifier.align(Alignment.Center) ) } Text( - text = quantityState.toString() , - style = MaterialTheme.typography.bodyMedium , + text = quantityState.toString(), + style = MaterialTheme.typography.bodyMedium, modifier = Modifier .padding(horizontal = 16.dp) .animateContentSize() ) - IconButton(modifier = Modifier.bounceClick() , onClick = { + IconButton(modifier = Modifier.bounceClick(), onClick = { view.playSoundEffect(SoundEffectConstants.CLICK) onPlusClick(cartItem) }) { Icon( - imageVector = Icons.Outlined.AddCircleOutline , + imageVector = Icons.Outlined.AddCircleOutline, contentDescription = stringResource(id = R.string.increase_quantity) ) } @@ -451,6 +474,4 @@ fun CartItemComposable( } } }) - - -} \ No newline at end of file +} diff --git a/app/src/main/kotlin/com/d4rk/cartcalculator/ui/screens/cart/CartViewModel.kt b/app/src/main/kotlin/com/d4rk/cartcalculator/ui/screens/cart/CartViewModel.kt index 5037e5d..9c25d45 100644 --- a/app/src/main/kotlin/com/d4rk/cartcalculator/ui/screens/cart/CartViewModel.kt +++ b/app/src/main/kotlin/com/d4rk/cartcalculator/ui/screens/cart/CartViewModel.kt @@ -5,7 +5,7 @@ import androidx.lifecycle.viewModelScope import com.d4rk.cartcalculator.data.database.table.ShoppingCartItemsTable import com.d4rk.cartcalculator.data.database.table.ShoppingCartTable import com.d4rk.cartcalculator.data.datastore.DataStore -import com.d4rk.cartcalculator.data.model.ui.screens.UiCartModel +import com.d4rk.cartcalculator.data.model.ui.screens.UiCartScreen import com.d4rk.cartcalculator.ui.screens.cart.repository.CartRepository import com.d4rk.cartcalculator.ui.viewmodel.BaseViewModel import kotlinx.coroutines.delay @@ -19,8 +19,8 @@ class CartViewModel(application : Application) : BaseViewModel(application) { private val repository : CartRepository = CartRepository() - private val _uiState : MutableStateFlow = MutableStateFlow(UiCartModel()) - val uiState : StateFlow = _uiState + private val _uiState : MutableStateFlow = MutableStateFlow(UiCartScreen()) + val uiState : StateFlow = _uiState fun loadCart(cartId : Int) { viewModelScope.launch(context = coroutineExceptionHandler) { @@ -118,6 +118,20 @@ class CartViewModel(application : Application) : BaseViewModel(application) { } } + fun editCartItem(cartItem: ShoppingCartItemsTable) { + viewModelScope.launch(context = coroutineExceptionHandler) { + repository.updateCartItemRepository(cartItem = cartItem) { + _uiState.update { currentState -> + val updatedCartItems = currentState.cartItems.map { item -> + if (item.itemId == cartItem.itemId) cartItem else item + } + currentState.copy(cartItems = updatedCartItems) + } + calculateTotalPrice() + } + } + } + fun deleteCartItem(cartItem : ShoppingCartItemsTable) { viewModelScope.launch(coroutineExceptionHandler) { repository.deleteCartItemRepository(cartItem = cartItem) { @@ -176,4 +190,15 @@ class CartViewModel(application : Application) : BaseViewModel(application) { } } } + + fun toggleEditDialog(cartItem: ShoppingCartItemsTable?) { + viewModelScope.launch(context = coroutineExceptionHandler) { + _uiState.update { currentState -> + currentState.copy( + openEditDialog = cartItem != null, + currentCartItemForEdit = cartItem + ) + } + } + } } \ No newline at end of file