Skip to content

Commit 855aeb9

Browse files
authored
Fix npe in shopping cart (#203)
1 parent 6beebb9 commit 855aeb9

File tree

4 files changed

+40
-14
lines changed

4 files changed

+40
-14
lines changed

core/src/main/java/io/snabble/sdk/Unit.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.snabble.sdk;
22

3+
import androidx.annotation.NonNull;
4+
35
import java.math.BigDecimal;
46
import java.math.MathContext;
57
import java.util.ArrayList;
@@ -195,7 +197,7 @@ private static void addConversion(Unit from, Unit to, int factor, int divisor) {
195197
* <p>
196198
* Returns the same value if a conversion is not possible. (e.g. KILOGRAM -> MILLIMETER)
197199
*/
198-
public static BigDecimal convert(BigDecimal value, Unit from, Unit to) {
200+
public static @NonNull BigDecimal convert(@NonNull BigDecimal value, @NonNull Unit from, @NonNull Unit to) {
199201
if (from == to) return value;
200202

201203
for (Conversion conversion : conversions) {

ui/src/main/java/io/snabble/sdk/ui/cart/shoppingcart/Extensions.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import java.math.RoundingMode
99

1010
fun PriceModifier.convertPriceModifier(
1111
amount: Int,
12-
weightedUnit: String?,
13-
referencedUnit: String?
12+
weightedUnit: String,
13+
referencedUnit: String
1414
): Int {
1515
val convertedValue = convert(BigDecimal(amount), fromString(weightedUnit), fromString(referencedUnit))
1616
val mode = Snabble.checkedInProject.value?.roundingMode ?: RoundingMode.HALF_UP

ui/src/main/java/io/snabble/sdk/ui/cart/shoppingcart/ShoppingCartViewModel.kt

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,32 @@ class ShoppingCartViewModel : ViewModel() {
149149
val discounts = mutableListOf<DiscountItem>()
150150
item.item.lineItem?.priceModifiers?.forEach { priceModifier ->
151151
val name = priceModifier.name.orEmpty()
152-
val modifiedPrice = priceModifier
153-
.convertPriceModifier(
154-
item.quantity,
155-
item.item.lineItem?.weightUnit,
156-
item.item.lineItem?.referenceUnit
157-
)
158-
.let { priceFormatter?.format(it).orEmpty() }
152+
val weightUnit = item.item.lineItem?.weightUnit
153+
val referenceUnit = item.item.lineItem?.referenceUnit
154+
val modifiedPrice = if (weightUnit != null && referenceUnit != null) {
155+
priceModifier
156+
.convertPriceModifier(
157+
amount = item.quantity,
158+
weightedUnit = weightUnit,
159+
referencedUnit = referenceUnit
160+
)
161+
.let { priceFormatter?.format(it).orEmpty() }
162+
} else {
163+
(priceModifier.price * item.quantity).let {
164+
priceFormatter?.format(it).orEmpty()
165+
}
166+
}
159167
// Set this to zero because the backend already subtracted the discount from the total price:
160168
val discountValue = 0
161-
discounts.add(DiscountItem(name = name, discount = modifiedPrice, discountValue = discountValue))
169+
modifiedPrice.let {
170+
discounts.add(
171+
DiscountItem(
172+
name = name,
173+
discount = modifiedPrice,
174+
discountValue = discountValue
175+
)
176+
)
177+
}
162178
}
163179
item.copy(discounts = item.discounts + discounts)
164180
}

ui/src/main/java/io/snabble/sdk/ui/cart/shoppingcart/product/model/ProductItem.kt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,17 @@ internal data class ProductItem(
3232
// price modifiers on top of the total price to get the unmodified total price.
3333
// In the other case we can directly work with the total price.
3434
val depositPrice = deposit?.depositPrice ?: 0
35-
val sumOfModifierPriceDiscounts = item.lineItem?.priceModifiers.orEmpty()
36-
.sumOf { it.convertPriceModifier(quantity, unit, item.lineItem?.referenceUnit) }
37-
.let(::abs)
35+
val weightUnit = item.lineItem?.weightUnit
36+
val referenceUnit = item.lineItem?.referenceUnit
37+
val sumOfModifierPriceDiscounts = if (weightUnit != null && referenceUnit != null) {
38+
item.lineItem?.priceModifiers.orEmpty()
39+
.sumOf { it.convertPriceModifier(quantity, weightUnit, referenceUnit) }
40+
.let(::abs)
41+
} else {
42+
item.lineItem?.priceModifiers.orEmpty()
43+
.sumOf { it.price * quantity }
44+
.let(::abs)
45+
}
3846
return totalPrice + depositPrice + sumOfModifierPriceDiscounts
3947
}
4048

0 commit comments

Comments
 (0)