diff --git a/CHANGELOG.md b/CHANGELOG.md index 863cb73f2..83abac29c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ All notable changes to this project will be documented in this file. ### Removed ### Fixed +## [0.80.14] +### Fixed +* ui: fix jumping of the shopping cart it an item is used with a deposit (APPS-2177) + ## [0.80.13] ### Added * ui: Add new headline option for the payment options (APPS-1915) diff --git a/core/src/main/java/io/snabble/sdk/shoppingcart/ShoppingCart.kt b/core/src/main/java/io/snabble/sdk/shoppingcart/ShoppingCart.kt index 521886421..9288d25ad 100644 --- a/core/src/main/java/io/snabble/sdk/shoppingcart/ShoppingCart.kt +++ b/core/src/main/java/io/snabble/sdk/shoppingcart/ShoppingCart.kt @@ -23,6 +23,7 @@ import io.snabble.sdk.shoppingcart.data.cart.BackendCartCustomer import io.snabble.sdk.shoppingcart.data.cart.BackendCartRequiredInformation import io.snabble.sdk.shoppingcart.data.item.BackendCartItem import io.snabble.sdk.shoppingcart.data.item.BackendCartItemType +import io.snabble.sdk.shoppingcart.data.item.Deposit import io.snabble.sdk.shoppingcart.data.item.DepositReturnVoucher import io.snabble.sdk.shoppingcart.data.item.ItemType import io.snabble.sdk.shoppingcart.data.listener.ShoppingCartListener @@ -996,6 +997,11 @@ class ShoppingCart( */ var depositReturnVoucher: DepositReturnVoucher? = null + /** + * Returns the deposit associated with the shopping cart item. + */ + var deposit : Deposit? = null + // The local generated UUID of a coupon which which will be used by the backend var backendCouponId: String? = null diff --git a/core/src/main/java/io/snabble/sdk/shoppingcart/ShoppingCartUpdater.kt b/core/src/main/java/io/snabble/sdk/shoppingcart/ShoppingCartUpdater.kt index b0b9e57d6..b4c320af4 100644 --- a/core/src/main/java/io/snabble/sdk/shoppingcart/ShoppingCartUpdater.kt +++ b/core/src/main/java/io/snabble/sdk/shoppingcart/ShoppingCartUpdater.kt @@ -17,6 +17,7 @@ import io.snabble.sdk.checkout.Price import io.snabble.sdk.checkout.SignedCheckoutInfo import io.snabble.sdk.checkout.Violation import io.snabble.sdk.codes.ScannedCode.Companion.parseDefault +import io.snabble.sdk.shoppingcart.data.item.Deposit import io.snabble.sdk.shoppingcart.data.item.ItemType import io.snabble.sdk.utils.GsonHolder import io.snabble.sdk.utils.Logger @@ -160,7 +161,7 @@ internal class ShoppingCartUpdater( addLineItemsAsCartItems(filter { it.type == LineItemType.DISCOUNT && it.discountType != "cart" }) addLineItemsAsCartItems(filter { it.type == LineItemType.COUPON }) - addLineItemsAsCartItems(filter { it.type == LineItemType.DEPOSIT }) + addDepositToItem(filter { it.type == LineItemType.DEPOSIT }) addDepositReturnsToVoucher(filter { it.type == LineItemType.DEPOSIT_RETURN }) } @@ -179,12 +180,20 @@ internal class ShoppingCartUpdater( } } + private fun addDepositToItem(deposits: List) { + deposits + .forEach { deposit -> + val item = cart.getByItemId(deposit.refersTo) + item?.deposit = Deposit(deposit) + } + } + private fun addDepositReturnsToVoucher(depositReturnItems: List) { depositReturnItems .groupBy { it.refersTo } .forEach { (refersTo, items) -> val drv = cart.getByItemId(refersTo) - drv?.depositReturnVoucher = drv?.depositReturnVoucher?.copy(lineItems = items) + drv?.depositReturnVoucher = drv.depositReturnVoucher?.copy(lineItems = items) } } diff --git a/core/src/main/java/io/snabble/sdk/shoppingcart/data/item/Deposit.kt b/core/src/main/java/io/snabble/sdk/shoppingcart/data/item/Deposit.kt new file mode 100644 index 000000000..bf2098867 --- /dev/null +++ b/core/src/main/java/io/snabble/sdk/shoppingcart/data/item/Deposit.kt @@ -0,0 +1,7 @@ +package io.snabble.sdk.shoppingcart.data.item + +import io.snabble.sdk.checkout.LineItem + +data class Deposit( + val lineItem: LineItem +) diff --git a/ui/src/main/java/io/snabble/sdk/ui/cart/shoppingcart/ShoppingCartViewModel.kt b/ui/src/main/java/io/snabble/sdk/ui/cart/shoppingcart/ShoppingCartViewModel.kt index 5e7d22d94..ea5f40fba 100644 --- a/ui/src/main/java/io/snabble/sdk/ui/cart/shoppingcart/ShoppingCartViewModel.kt +++ b/ui/src/main/java/io/snabble/sdk/ui/cart/shoppingcart/ShoppingCartViewModel.kt @@ -6,7 +6,6 @@ import androidx.lifecycle.viewModelScope import io.snabble.sdk.PriceFormatter import io.snabble.sdk.Project import io.snabble.sdk.Snabble -import io.snabble.sdk.checkout.LineItemType import io.snabble.sdk.shoppingcart.ShoppingCart import io.snabble.sdk.shoppingcart.data.item.ItemType import io.snabble.sdk.shoppingcart.data.listener.SimpleShoppingCartListener @@ -106,8 +105,6 @@ class ShoppingCartViewModel : ViewModel() { filter { it.type == ItemType.PRODUCT }.let { cartItems.addProducts(it) } filter { it.type == ItemType.DEPOSIT_RETURN_VOUCHER }.let { cartItems.addDepositReturnItems(it) } - filter { it.lineItem?.type == LineItemType.DEPOSIT }.let { cartItems.addDepositsToProducts(it) } - filter { it.isDiscount && it.lineItem?.discountType != "cart" }.let { cartItems.addDiscountsToProducts(it) } filter { it.isDiscount && it.lineItem?.discountType == "cart" }.let { cartItems.addCartDiscount(it) } } @@ -182,10 +179,16 @@ class ShoppingCartViewModel : ViewModel() { isAgeRestricted = item.isAgeRestricted, minimumAge = item.minimumAge, item = item, - totalPrice = item.lineItem?.totalPrice ?: 0 + totalPrice = item.totalPrice, + deposit = item.deposit?.let { + DepositItem( + depositPrice = item.quantity * it.lineItem.price, + depositText = it.lineItem.name, + depositPriceText = priceFormatter?.format(item.quantity * it.lineItem.price) + ) + } ) - } - ) + }) } private fun MutableList.addPriceModifiersAsDiscountsProducts() = replaceAll { item -> @@ -243,23 +246,6 @@ class ShoppingCartViewModel : ViewModel() { } } - private fun MutableList.addDepositsToProducts(deposit: List) = - deposit.forEach { item -> - firstOrNull { it.item.id == item.lineItem?.refersTo }?.let { - remove(it) - val product = it as? ProductItem ?: return@forEach - add( - product.copy( - deposit = DepositItem( - depositPriceText = item.totalPriceText, - depositText = item.displayName, - depositPrice = item.totalPrice - ), - ) - ) - } - } - private fun MutableList.addCartDiscount(cartDiscount: List) = cartDiscount.forEach { item -> val discount = item.priceText ?: return@forEach