Skip to content

Commit

Permalink
fix: Corrected cart creation with correct ID
Browse files Browse the repository at this point in the history
Resolved an issue with incorrect cart ID assignment:

- Fixed the logic to ensure the correct ID is used when creating a new cart.
  • Loading branch information
Mihai-Cristian Condrea committed Nov 9, 2024
1 parent 2e01f4f commit 726b497
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ fun HomeScreen(context : Context , view : View , viewModel : HomeViewModel) {
.weight(1f)
.padding(bottom = uiState.fabAdHeight)
) {
items(uiState.carts) { cart ->
items(items = uiState.carts , key = { cart -> cart.cartId }) { cart ->
CartItemComposable(cart ,
onDelete = { viewModel.openDeleteCartDialog(it) } ,
onCardClick = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,9 @@ class HomeViewModel(application : Application) : BaseViewModel(application) {

fun addCart(cart : ShoppingCartTable) {
viewModelScope.launch(coroutineExceptionHandler) {
repository.addCartRepository(cart) {
_uiState.update {
val newList = it.carts.toMutableList()
newList.add(cart)
it.copy(carts = newList)
repository.addCartRepository(cart) { addedCart ->
_uiState.update { currentState ->
currentState.copy(carts = (currentState.carts + addedCart).toMutableList())
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ class HomeRepository(application : Application) :
}
}

suspend fun addCartRepository(cart : ShoppingCartTable , onSuccess : () -> Unit) {
suspend fun addCartRepository(cart : ShoppingCartTable , onSuccess : (ShoppingCartTable) -> Unit , ) {
withContext(Dispatchers.IO) {
addCartImplementation(cart)
val addedCart = addCartImplementation(cart = cart)
withContext(Dispatchers.Main) {
onSuccess()
onSuccess(addedCart)
}
}
}

suspend fun deleteCartRepository(cart : ShoppingCartTable , onSuccess : () -> Unit) {
withContext(Dispatchers.IO) {
deleteCartImplementation(cart)
deleteCartImplementation(cart = cart)
withContext(Dispatchers.Main) {
onSuccess()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,20 @@ abstract class HomeRepositoryImplementation(val application : Application) {
}

fun openCartImplementation(cart : ShoppingCartTable) {
println("Shopping Cart Calculator -> HomeRepositoryImplementation: Opening cart with ID: ${cart.cartId}")

application.startActivity(
Intent(application , CartActivity::class.java).putExtra("cartId" , cart.cartId)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK)
)
}

suspend fun addCartImplementation(cart : ShoppingCartTable) {
AppCoreManager.database.newCartDao().insert(cart)
suspend fun addCartImplementation(cart : ShoppingCartTable) : ShoppingCartTable {
return cart.copy(cartId = AppCoreManager.database.newCartDao().insert(cart = cart).toInt())
}

suspend fun deleteCartImplementation(cart : ShoppingCartTable) {
with(AppCoreManager.database) {
newCartDao().delete(cart)
shoppingCartItemsDao().deleteItemsFromCart(cart.cartId)
newCartDao().delete(cart = cart)
shoppingCartItemsDao().deleteItemsFromCart(cartId = cart.cartId)
}
}
}

0 comments on commit 726b497

Please sign in to comment.