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