Skip to content

Commit

Permalink
refactor: Updated string resources, improved error handling, added er…
Browse files Browse the repository at this point in the history
…ror 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.
  • Loading branch information
Mihai-Cristian Condrea committed Jan 1, 2025
1 parent d158292 commit d231f70
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 73 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ShoppingCartItemsTable> = 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<Int, Int> = emptyMap()
)
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ShoppingCartItemsTable?> =
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
Expand All @@ -61,11 +77,16 @@ fun AddNewCartItemAlertDialog(

@Composable
fun AddNewCartItemAlertDialogContent(
cartId : Int , newCartItem : MutableState<ShoppingCartItemsTable?>
cartId : Int ,
newCartItem : MutableState<ShoppingCartItemsTable?> ,
initialName : String ,
initialPrice : String ,
initialQuantity : String ,
existingCartItem : ShoppingCartItemsTable?
) {
val nameText : MutableState<String> = remember { mutableStateOf(value = "") }
val priceText : MutableState<String> = remember { mutableStateOf(value = "") }
val quantityText : MutableState<String> = remember { mutableStateOf(value = "") }
val nameText : MutableState<String> = remember { mutableStateOf(value = initialName) }
val priceText : MutableState<String> = remember { mutableStateOf(value = initialPrice) }
val quantityText : MutableState<String> = remember { mutableStateOf(value = initialQuantity) }

val nameFocusRequester : FocusRequester = remember { FocusRequester() }
val priceFocusRequester : FocusRequester = remember { FocusRequester() }
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -157,7 +156,7 @@ fun PreferenceItem(
}) , verticalAlignment = Alignment.CenterVertically
) {
icon?.let {
Spacer(modifier = Modifier.width(16.dp))
LargeHorizontalSpacer()
Icon(it , contentDescription = null)
}
Column(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit d231f70

Please sign in to comment.