Skip to content

Commit

Permalink
Fix npe in shopping cart (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabtron authored Aug 14, 2024
1 parent 6beebb9 commit 855aeb9
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 14 deletions.
4 changes: 3 additions & 1 deletion core/src/main/java/io/snabble/sdk/Unit.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.snabble.sdk;

import androidx.annotation.NonNull;

import java.math.BigDecimal;
import java.math.MathContext;
import java.util.ArrayList;
Expand Down Expand Up @@ -195,7 +197,7 @@ private static void addConversion(Unit from, Unit to, int factor, int divisor) {
* <p>
* 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,32 @@ class ShoppingCartViewModel : ViewModel() {
val discounts = mutableListOf<DiscountItem>()
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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 855aeb9

Please sign in to comment.