Skip to content

Commit 48c4f5e

Browse files
committed
addewd configurable rounding mode
1 parent 46b4504 commit 48c4f5e

File tree

7 files changed

+29
-13
lines changed

7 files changed

+29
-13
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ allprojects {
2424
}
2525

2626
project.ext {
27-
sdkVersion='0.6.3'
27+
sdkVersion='0.6.4'
2828
versionCode=1
2929

3030
compileSdkVersion=27

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,6 @@ public Type getType() {
156156
return type;
157157
}
158158

159-
public int getPriceForQuantity(int quantity) {
160-
return getPriceForQuantity(quantity, RoundingMode.UP);
161-
}
162-
163159
public int getPriceForQuantity(int quantity, RoundingMode roundingMode){
164160
if (type == Product.Type.UserWeighed || type == Product.Type.PreWeighed) {
165161
BigDecimal pricePerUnit = new BigDecimal(price)

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,15 @@ public void setScannedCode(ScannableCode scannedCode){
5252
private transient List<ShoppingCartListener> listeners = new CopyOnWriteArrayList<>();
5353
private transient ProductDatabase productDatabase;
5454
private transient Handler handler = new Handler(Looper.getMainLooper());
55+
private transient SnabbleSdk sdkInstance;
5556

5657
protected ShoppingCart() {
5758
//empty constructor for gson
5859
}
5960

6061
void initWithSdkInstance(SnabbleSdk sdkInstance) {
62+
this.sdkInstance = sdkInstance;
63+
6164
productDatabase = sdkInstance.getProductDatabase();
6265
productDatabase.addOnDatabaseUpdateListener(new ProductDatabase.OnDatabaseUpdateListener() {
6366
@Override
@@ -353,13 +356,13 @@ public int getTotalPrice() {
353356
Product product = e.product;
354357

355358
if(e.weight != null){
356-
sum += product.getPriceForQuantity(e.weight);
359+
sum += product.getPriceForQuantity(e.weight, sdkInstance.getRoundingMode());
357360
} else if(e.price != null){
358361
sum += e.price;
359362
} else if(e.amount != null){
360363
sum += product.getPrice() * e.amount;
361364
} else {
362-
sum += product.getPriceForQuantity(e.quantity);
365+
sum += product.getPriceForQuantity(e.quantity, sdkInstance.getRoundingMode());
363366
}
364367
}
365368

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.os.Bundle;
66

77
import java.io.File;
8+
import java.math.RoundingMode;
89
import java.util.Arrays;
910
import java.util.Currency;
1011
import java.util.List;
@@ -22,7 +23,6 @@
2223

2324
public class SnabbleSdk {
2425

25-
2626
public static class Config {
2727
/**
2828
* The endpoint url of the snabble backend. For example "snabble.io" for the Production environment.
@@ -135,6 +135,8 @@ public static class Config {
135135
public String[] weighPrefixes = new String[0];
136136

137137
public String[] amountPrefixes = new String[0];
138+
139+
public RoundingMode roundingMode = RoundingMode.UP;
138140
}
139141

140142
private String endpointBaseUrl;
@@ -166,6 +168,8 @@ public static class Config {
166168
private String[] weighPrefixes = new String[0];
167169
private String[] amountPrefixes = new String[0];
168170

171+
private RoundingMode roundingMode;
172+
169173
private SnabbleSdk() {
170174

171175
}
@@ -264,6 +268,8 @@ private void init(final Application app,
264268
pricePrefixes = Arrays.copyOf(config.pricePrefixes, config.pricePrefixes.length);
265269
amountPrefixes = Arrays.copyOf(config.amountPrefixes, config.amountPrefixes.length);
266270

271+
roundingMode = config.roundingMode != null ? config.roundingMode : RoundingMode.UP;
272+
267273
updateShops();
268274

269275
if (config.bundledMetadataAssetPath != null) {
@@ -535,6 +541,10 @@ public Locale getCurrencyLocale() {
535541
return currencyLocale;
536542
}
537543

544+
public RoundingMode getRoundingMode() {
545+
return roundingMode;
546+
}
547+
538548
public Checkout getCheckout() {
539549
return checkout;
540550
}

ui/src/main/java/io/snabble/sdk/ui/PriceFormatter.java

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

33
import java.math.BigDecimal;
4+
import java.math.RoundingMode;
45
import java.text.NumberFormat;
56
import java.util.Currency;
67

@@ -25,7 +26,7 @@ public String format(int price) {
2526

2627
BigDecimal bigDecimal = new BigDecimal(price);
2728
BigDecimal divider = new BigDecimal(10).pow(fractionDigits);
28-
BigDecimal dividedPrice = bigDecimal.divide(divider, fractionDigits, BigDecimal.ROUND_HALF_UP);
29+
BigDecimal dividedPrice = bigDecimal.divide(divider, fractionDigits, sdkInstance.getRoundingMode());
2930

3031
return numberFormat.format(dividedPrice.doubleValue());
3132
}

ui/src/main/java/io/snabble/sdk/ui/cart/ShoppingCartView.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,12 +359,14 @@ public void bindTo(final int position) {
359359
priceFormatter.format(product.getPrice() * embeddedAmount)));
360360
} else if(embeddedWeight != null){
361361
priceTextView.setText(String.format(" * %s = %s", price,
362-
priceFormatter.format(product.getPriceForQuantity(embeddedWeight))));
362+
priceFormatter.format(product.getPriceForQuantity(embeddedWeight,
363+
SnabbleUI.getSdkInstance().getRoundingMode()))));
363364
} else if (quantity == 1) {
364365
priceTextView.setText(" " + price);
365366
} else {
366367
priceTextView.setText(String.format(" * %s = %s", price,
367-
priceFormatter.format(product.getPriceForQuantity(quantity))));
368+
priceFormatter.format(product.getPriceForQuantity(quantity,
369+
SnabbleUI.getSdkInstance().getRoundingMode()))));
368370
}
369371

370372
if (type == Product.Type.UserWeighed || embeddedWeight != null) {

ui/src/main/java/io/snabble/sdk/ui/scanner/ProductConfirmationDialog.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,9 +310,13 @@ private void addToCart() {
310310

311311
private void updatePayText() {
312312
if (checkout.isAvailable()) {
313+
SnabbleSdk sdkInstance = SnabbleUI.getSdkInstance();
314+
313315
int totalPrice = shoppingCart.getTotalPrice();
314-
totalPrice -= product.getPriceForQuantity(shoppingCart.getQuantity(product));
315-
totalPrice += product.getPriceForQuantity(getQuantity());
316+
totalPrice -= product.getPriceForQuantity(shoppingCart.getQuantity(product),
317+
sdkInstance.getRoundingMode());
318+
totalPrice += product.getPriceForQuantity(getQuantity(),
319+
sdkInstance.getRoundingMode());
316320
String formattedTotalPrice = priceFormatter.format(totalPrice);
317321
payNow.setVisibility(totalPrice > 0 ? View.VISIBLE : View.INVISIBLE);
318322
payNow.setText(String.format(Locale.getDefault(), payNowText, formattedTotalPrice));

0 commit comments

Comments
 (0)