Skip to content

Commit 8cf198e

Browse files
committed
add support for encoded codes csv
1 parent 7535308 commit 8cf198e

File tree

9 files changed

+88
-17
lines changed

9 files changed

+88
-17
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ buildscript {
99
}
1010

1111
dependencies {
12-
classpath 'com.android.tools.build:gradle:3.2.1'
12+
classpath 'com.android.tools.build:gradle:3.3.0'
1313
classpath 'gradle.plugin.com.github.jlouns:gradle-cross-platform-exec-plugin:0.5.0'
1414
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
1515
classpath "gradle.plugin.gmazzo:sqlite-plugin:0.2"
0 Bytes
Binary file not shown.

core/src/androidTest/java/io/snabble/sdk/EncodedCodesGeneratorTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,5 +351,34 @@ public void testExpensiveItemsSortedToBottom() throws IOException, Snabble.Snabb
351351
Assert.assertEquals(1, codes.size());
352352
Assert.assertEquals("8715700421698;4008400301020", codes.get(0));
353353
}
354+
355+
@Test
356+
@UiThreadTest
357+
public void testCSVFormat() throws IOException, Snabble.SnabbleException {
358+
withDb("demoDb_1_11.sqlite3");
359+
360+
EncodedCodesOptions options = new EncodedCodesOptions.Builder()
361+
.prefix("snabble;\n")
362+
.separator("\n")
363+
.suffix("")
364+
.repeatCodes(false)
365+
.countSeparator(";")
366+
.maxCodes(100)
367+
.build();
368+
369+
EncodedCodesGenerator generator = new EncodedCodesGenerator(options);
370+
371+
Product duplo = project.getProductDatabase().findBySku("49");
372+
project.getShoppingCart().add(duplo, 7, ScannableCode.parse(project, "4008400301020"));
373+
374+
Product heinz = project.getProductDatabase().findBySku("42");
375+
project.getShoppingCart().add(heinz, 1000, ScannableCode.parse(project, "8715700421698"));
376+
generator.add(project.getShoppingCart());
377+
378+
ArrayList<String> codes = generator.generate();
379+
Assert.assertEquals(1, codes.size());
380+
Assert.assertEquals("snabble;\n1000;8715700421698\n7;4008400301020", codes.get(0));
381+
}
382+
354383
}
355384

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ public enum PaymentMethod {
99
QRCODE_POS(false, false),
1010
@SerializedName("encodedCodes")
1111
ENCODED_CODES(true, false),
12+
@SerializedName("encodedCodesCSV")
13+
ENCODED_CODES_CSV(true, false),
1214
@SerializedName("teleCashDeDirectDebit")
1315
TELECASH_DIRECT_DEBIT(false, true);
1416

core/src/main/java/io/snabble/sdk/encodedcodes/EncodedCodesGenerator.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,23 @@ public int compare(ProductInfo p1, ProductInfo p2) {
130130
code.replace(7, 12, embeddedWeight.toString());
131131
code.setCharAt(6, Character.forDigit(EAN13.internalChecksum(code.toString()), 10));
132132
code.setCharAt(12, Character.forDigit(EAN13.checksum(code.substring(0, 12)), 10));
133-
addScannableCode(code.toString(), ageRestricted);
133+
134+
if (options.repeatCodes) {
135+
addScannableCode(code.toString(), ageRestricted);
136+
} else {
137+
addScannableCode("1" + options.countSeparator + code.toString(), ageRestricted);
138+
}
134139
}
135140
}
136141
} else {
137142
int q = productInfo.quantity;
138-
for (int j = 0; j < q; j++) {
139-
addScannableCode(productInfo.product.getTransmissionCode(productInfo.scannedCode), ageRestricted);
143+
String transmissionCode = productInfo.product.getTransmissionCode(productInfo.scannedCode);
144+
if (options.repeatCodes) {
145+
for (int j = 0; j < q; j++) {
146+
addScannableCode(transmissionCode, ageRestricted);
147+
}
148+
} else {
149+
addScannableCode(q + options.countSeparator + transmissionCode, ageRestricted);
140150
}
141151
}
142152
}

core/src/main/java/io/snabble/sdk/encodedcodes/EncodedCodesOptions.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@ public class EncodedCodesOptions {
99
public final String finalCode;
1010
public final String nextCode;
1111
public final String nextCodeWithCheck;
12+
public final boolean repeatCodes;
13+
public final String countSeparator;
1214

1315
private EncodedCodesOptions(String prefix, String separator, String suffix, int maxChars,
14-
int maxCodes, String finalCode, String nextCode, String nextCodeWithCheck) {
16+
int maxCodes, String finalCode, String nextCode,
17+
String nextCodeWithCheck, boolean repeatCodes, String countSeparator) {
1518
this.prefix = prefix;
1619
this.separator = separator;
1720
this.suffix = suffix;
@@ -20,6 +23,8 @@ private EncodedCodesOptions(String prefix, String separator, String suffix, int
2023
this.finalCode = finalCode;
2124
this.nextCode = nextCode;
2225
this.nextCodeWithCheck = nextCodeWithCheck;
26+
this.repeatCodes = repeatCodes;
27+
this.countSeparator = countSeparator;
2328
}
2429

2530
public static class Builder {
@@ -31,6 +36,8 @@ public static class Builder {
3136
private String nextCode = "";
3237
private String nextCodeWithCheck = "";
3338
private int maxCodes = 100;
39+
private boolean repeatCodes = true;
40+
private String countSeparator = ";";
3441

3542
public Builder prefix(String prefix) {
3643
this.prefix = prefix;
@@ -72,9 +79,19 @@ public Builder nextCodeWithCheck(String nextCodeWithCheck) {
7279
return this;
7380
}
7481

82+
public Builder repeatCodes(boolean repeatCodes) {
83+
this.repeatCodes = repeatCodes;
84+
return this;
85+
}
86+
87+
public Builder countSeparator(String countSeparator) {
88+
this.countSeparator = countSeparator;
89+
return this;
90+
}
91+
7592
public EncodedCodesOptions build() {
7693
return new EncodedCodesOptions(prefix, separator, suffix, maxChars, maxCodes,
77-
finalCode, nextCode, nextCodeWithCheck);
94+
finalCode, nextCode, nextCodeWithCheck, repeatCodes, countSeparator);
7895
}
7996
}
8097
}

ui/src/main/java/io/snabble/sdk/ui/checkout/CheckoutEncodedCodesView.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import io.snabble.sdk.Project;
2121
import io.snabble.sdk.encodedcodes.EncodedCodesGenerator;
2222
import io.snabble.sdk.PriceFormatter;
23+
import io.snabble.sdk.encodedcodes.EncodedCodesOptions;
2324
import io.snabble.sdk.ui.R;
2425
import io.snabble.sdk.ui.SnabbleUI;
2526
import io.snabble.sdk.ui.SnabbleUICallback;
@@ -37,27 +38,25 @@ class CheckoutEncodedCodesView extends FrameLayout implements View.OnLayoutChang
3738

3839
public CheckoutEncodedCodesView(Context context) {
3940
super(context);
40-
inflateView();
41-
}
4241

43-
public CheckoutEncodedCodesView(Context context, AttributeSet attrs) {
44-
super(context, attrs);
45-
inflateView();
42+
project = SnabbleUI.getProject();
43+
inflateView(project.getEncodedCodesOptions());
4644
}
4745

48-
public CheckoutEncodedCodesView(Context context, AttributeSet attrs, int defStyleAttr) {
49-
super(context, attrs, defStyleAttr);
50-
inflateView();
46+
public CheckoutEncodedCodesView(Context context, EncodedCodesOptions options) {
47+
super(context);
48+
49+
project = SnabbleUI.getProject();
50+
inflateView(options);
5151
}
5252

53-
private void inflateView() {
53+
private void inflateView(EncodedCodesOptions options) {
5454
inflate(getContext(), R.layout.view_checkout_encodedcodes, this);
5555

5656
scrollContainer = findViewById(R.id.scroll_container);
5757
scrollView = findViewById(R.id.scroll_view);
5858

59-
project = SnabbleUI.getProject();
60-
encodedCodesGenerator = new EncodedCodesGenerator(project.getEncodedCodesOptions());
59+
encodedCodesGenerator = new EncodedCodesGenerator(options);
6160

6261
Button paidButton = findViewById(R.id.paid);
6362
paidButton.setOnClickListener(new OnClickListener() {

ui/src/main/java/io/snabble/sdk/ui/checkout/CheckoutView.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import io.snabble.sdk.Checkout;
1919
import io.snabble.sdk.PaymentMethod;
2020
import io.snabble.sdk.Snabble;
21+
import io.snabble.sdk.encodedcodes.EncodedCodesOptions;
2122
import io.snabble.sdk.ui.KeyguardHandler;
2223
import io.snabble.sdk.ui.R;
2324
import io.snabble.sdk.ui.SnabbleUI;
@@ -157,6 +158,18 @@ public void onKeyguardResult(int resultCode) {
157158
case ENCODED_CODES:
158159
displayView(new CheckoutEncodedCodesView(getContext()));
159160
break;
161+
case ENCODED_CODES_CSV:
162+
EncodedCodesOptions options = new EncodedCodesOptions.Builder()
163+
.prefix("snabble;\n")
164+
.separator("\n")
165+
.suffix("")
166+
.repeatCodes(false)
167+
.countSeparator(";")
168+
.maxCodes(100)
169+
.build();
170+
171+
displayView(new CheckoutEncodedCodesView(getContext(), options));
172+
break;
160173
}
161174
}
162175

ui/src/main/java/io/snabble/sdk/ui/checkout/PaymentMethodView.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class PaymentMethodView extends FrameLayout implements PaymentCredentialsStore.C
4343
icons.put(PaymentMethod.TELECASH_DIRECT_DEBIT, R.drawable.ic_pm_sepa);
4444
icons.put(PaymentMethod.QRCODE_POS, R.drawable.ic_pm_checkstand);
4545
icons.put(PaymentMethod.ENCODED_CODES, R.drawable.ic_pm_checkstand);
46+
icons.put(PaymentMethod.ENCODED_CODES_CSV, R.drawable.ic_pm_checkstand);
4647
}
4748

4849
private Checkout checkout;

0 commit comments

Comments
 (0)