From 855aeb9176a03ce43be7f25a524b62eca65d3853 Mon Sep 17 00:00:00 2001 From: Fabian Bender <91562175+Fabtron@users.noreply.github.com> Date: Wed, 14 Aug 2024 16:46:53 +0200 Subject: [PATCH] Fix npe in shopping cart (#203) --- core/src/main/java/io/snabble/sdk/Unit.java | 4 ++- .../sdk/ui/cart/shoppingcart/Extensions.kt | 4 +-- .../shoppingcart/ShoppingCartViewModel.kt | 32 ++++++++++++++----- .../shoppingcart/product/model/ProductItem.kt | 14 ++++++-- 4 files changed, 40 insertions(+), 14 deletions(-) diff --git a/core/src/main/java/io/snabble/sdk/Unit.java b/core/src/main/java/io/snabble/sdk/Unit.java index ebd4e6088..c998dcbc4 100644 --- a/core/src/main/java/io/snabble/sdk/Unit.java +++ b/core/src/main/java/io/snabble/sdk/Unit.java @@ -1,5 +1,7 @@ package io.snabble.sdk; +import androidx.annotation.NonNull; + import java.math.BigDecimal; import java.math.MathContext; import java.util.ArrayList; @@ -195,7 +197,7 @@ private static void addConversion(Unit from, Unit to, int factor, int divisor) { *

* Returns the same value if a conversion is not possible. (e.g. KILOGRAM -> MILLIMETER) */ - public static BigDecimal convert(BigDecimal value, Unit from, Unit to) { + public static @NonNull BigDecimal convert(@NonNull BigDecimal value, @NonNull Unit from, @NonNull Unit to) { if (from == to) return value; for (Conversion conversion : conversions) { diff --git a/ui/src/main/java/io/snabble/sdk/ui/cart/shoppingcart/Extensions.kt b/ui/src/main/java/io/snabble/sdk/ui/cart/shoppingcart/Extensions.kt index b6ac420ca..f4194b232 100644 --- a/ui/src/main/java/io/snabble/sdk/ui/cart/shoppingcart/Extensions.kt +++ b/ui/src/main/java/io/snabble/sdk/ui/cart/shoppingcart/Extensions.kt @@ -9,8 +9,8 @@ import java.math.RoundingMode fun PriceModifier.convertPriceModifier( amount: Int, - weightedUnit: String?, - referencedUnit: String? + weightedUnit: String, + referencedUnit: String ): Int { val convertedValue = convert(BigDecimal(amount), fromString(weightedUnit), fromString(referencedUnit)) val mode = Snabble.checkedInProject.value?.roundingMode ?: RoundingMode.HALF_UP 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 9e49c7e9d..5485acad4 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 @@ -149,16 +149,32 @@ class ShoppingCartViewModel : ViewModel() { val discounts = mutableListOf() item.item.lineItem?.priceModifiers?.forEach { priceModifier -> val name = priceModifier.name.orEmpty() - val modifiedPrice = priceModifier - .convertPriceModifier( - item.quantity, - item.item.lineItem?.weightUnit, - item.item.lineItem?.referenceUnit - ) - .let { priceFormatter?.format(it).orEmpty() } + val weightUnit = item.item.lineItem?.weightUnit + val referenceUnit = item.item.lineItem?.referenceUnit + val modifiedPrice = if (weightUnit != null && referenceUnit != null) { + priceModifier + .convertPriceModifier( + amount = item.quantity, + weightedUnit = weightUnit, + referencedUnit = referenceUnit + ) + .let { priceFormatter?.format(it).orEmpty() } + } else { + (priceModifier.price * item.quantity).let { + priceFormatter?.format(it).orEmpty() + } + } // Set this to zero because the backend already subtracted the discount from the total price: val discountValue = 0 - discounts.add(DiscountItem(name = name, discount = modifiedPrice, discountValue = discountValue)) + modifiedPrice.let { + discounts.add( + DiscountItem( + name = name, + discount = modifiedPrice, + discountValue = discountValue + ) + ) + } } item.copy(discounts = item.discounts + discounts) } diff --git a/ui/src/main/java/io/snabble/sdk/ui/cart/shoppingcart/product/model/ProductItem.kt b/ui/src/main/java/io/snabble/sdk/ui/cart/shoppingcart/product/model/ProductItem.kt index 8a671341d..1a6b2a3dd 100644 --- a/ui/src/main/java/io/snabble/sdk/ui/cart/shoppingcart/product/model/ProductItem.kt +++ b/ui/src/main/java/io/snabble/sdk/ui/cart/shoppingcart/product/model/ProductItem.kt @@ -32,9 +32,17 @@ internal data class ProductItem( // price modifiers on top of the total price to get the unmodified total price. // In the other case we can directly work with the total price. val depositPrice = deposit?.depositPrice ?: 0 - val sumOfModifierPriceDiscounts = item.lineItem?.priceModifiers.orEmpty() - .sumOf { it.convertPriceModifier(quantity, unit, item.lineItem?.referenceUnit) } - .let(::abs) + val weightUnit = item.lineItem?.weightUnit + val referenceUnit = item.lineItem?.referenceUnit + val sumOfModifierPriceDiscounts = if (weightUnit != null && referenceUnit != null) { + item.lineItem?.priceModifiers.orEmpty() + .sumOf { it.convertPriceModifier(quantity, weightUnit, referenceUnit) } + .let(::abs) + } else { + item.lineItem?.priceModifiers.orEmpty() + .sumOf { it.price * quantity } + .let(::abs) + } return totalPrice + depositPrice + sumOfModifierPriceDiscounts }