Skip to content

Commit ff2e320

Browse files
committed
Configurable encoded codes prefix/seperator/suffix
1 parent 945c3ba commit ff2e320

File tree

3 files changed

+47
-12
lines changed

3 files changed

+47
-12
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ public static class Config {
118118
*/
119119
public boolean productDbDownloadIfMissing = true;
120120

121+
public String encodedCodesPrefix = null;
122+
public String encodedCodesSeperator = null;
123+
public String encodedCodesSuffix = null;
124+
121125
public RoundingMode roundingMode = RoundingMode.HALF_UP;
122126
}
123127

@@ -150,6 +154,10 @@ public static class Config {
150154
private String[] weighPrefixes = new String[0];
151155
private String[] unitPrefixes = new String[0];
152156

157+
private String encodedCodesPrefix = null;
158+
private String encodedCodesSeperator = null;
159+
private String encodedCodesSuffix = null;
160+
153161
private RoundingMode roundingMode;
154162

155163
private SnabbleSdk() {
@@ -248,6 +256,10 @@ private void init(final Application app,
248256

249257
roundingMode = config.roundingMode != null ? config.roundingMode : RoundingMode.HALF_UP;
250258

259+
encodedCodesPrefix = config.encodedCodesPrefix != null ? config.encodedCodesPrefix : "";
260+
encodedCodesSeperator = config.encodedCodesSeperator != null ? config.encodedCodesSeperator : "\n";
261+
encodedCodesSuffix = config.encodedCodesSuffix != null ? config.encodedCodesSuffix : "";
262+
251263
updateShops();
252264

253265
if (config.bundledMetadataAssetPath != null) {
@@ -446,6 +458,18 @@ public String[] getUnitPrefixes() {
446458
return unitPrefixes;
447459
}
448460

461+
public String getEncodedCodesPrefix() {
462+
return encodedCodesPrefix;
463+
}
464+
465+
public String getEncodedCodesSeperator() {
466+
return encodedCodesSeperator;
467+
}
468+
469+
public String getEncodedCodesSuffix() {
470+
return encodedCodesSuffix;
471+
}
472+
449473
String absoluteUrl(String url) {
450474
if (url.startsWith("http")) {
451475
return url;

sample/src/main/java/io/snabble/testapp/App.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ public void init(final InitCallback callback) {
6262
config.productDbBundledSchemaVersionMajor = getBundledMajor();
6363
config.productDbBundledSchemaVersionMinor = getBundledMinor();
6464
config.productDbDownloadIfMissing = false;
65+
config.encodedCodesPrefix = "";
66+
config.encodedCodesSeperator = "\n";
67+
config.encodedCodesSuffix = "";
6568

6669
SnabbleSdk.setup(this, config, new SnabbleSdk.SetupCompletionListener() {
6770
@Override

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

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class CheckoutEncodedCodesView extends FrameLayout implements View.OnLayoutChang
2626
private View scrollContainer;
2727
private ScrollView scrollView;
2828
private TextView explanationText;
29+
private SnabbleSdk sdkInstance;
2930

3031
public CheckoutEncodedCodesView(Context context) {
3132
super(context);
@@ -48,7 +49,7 @@ private void inflateView() {
4849
scrollContainer = findViewById(R.id.scroll_container);
4950
scrollView = findViewById(R.id.scroll_view);
5051

51-
SnabbleSdk sdkInstance = SnabbleUI.getSdkInstance();
52+
sdkInstance = SnabbleUI.getSdkInstance();
5253
ShoppingCart shoppingCart = sdkInstance.getShoppingCart();
5354

5455
Button paidButton = findViewById(R.id.paid);
@@ -121,33 +122,34 @@ public CodeListView(@NonNull Context context) {
121122

122123
stringBuilder = new StringBuilder();
123124

124-
SnabbleSdk sdkInstance = SnabbleUI.getSdkInstance();
125125
Checkout checkout = sdkInstance.getCheckout();
126126
ShoppingCart shoppingCart = sdkInstance.getShoppingCart();
127127

128128
int h = scrollContainer.getHeight();
129129
barcodeHeight = h - h / 5;
130130

131131
for (String code : checkout.getCodes()) {
132-
addLine(code);
132+
addScannableCode(code);
133133
}
134134

135135
for (int i = 0; i < shoppingCart.size(); i++) {
136136
for (int j = 0; j < shoppingCart.getQuantity(i); j++) {
137-
addLine(shoppingCart.getScannedCode(i));
137+
addScannableCode(shoppingCart.getScannedCode(i));
138138
}
139139
}
140140

141141
if(getChildCount() == 0){
142142
barcodeHeight = h;
143143
}
144144

145-
addCode(stringBuilder.toString());
146-
145+
generateView();
147146
updateExplanationText(getChildCount());
148147
}
149148

150-
private void addCode(String code) {
149+
private void generateView() {
150+
stringBuilder.append(sdkInstance.getEncodedCodesSuffix());
151+
String code = stringBuilder.toString();
152+
151153
BarcodeView barcodeView = new BarcodeView(getContext());
152154
barcodeView.setFormat(BarcodeFormat.QR_CODE);
153155

@@ -163,13 +165,19 @@ private void addCode(String code) {
163165
stringBuilder = new StringBuilder();
164166
}
165167

166-
private void addLine(String line) {
167-
if (stringBuilder.length() + (line.length() + 1) > MAX_CHARS) {
168-
addCode(stringBuilder.toString());
168+
private void addScannableCode(String scannableCode) {
169+
int requiredLength = scannableCode.length() + sdkInstance.getEncodedCodesSuffix().length() + 1;
170+
if (stringBuilder.length() + (requiredLength) > MAX_CHARS) {
171+
generateView();
172+
}
173+
174+
if(stringBuilder.length() == 0){
175+
stringBuilder.append(sdkInstance.getEncodedCodesPrefix());
176+
} else {
177+
stringBuilder.append(sdkInstance.getEncodedCodesSeperator());
169178
}
170179

171-
stringBuilder.append(line);
172-
stringBuilder.append("\n");
180+
stringBuilder.append(scannableCode);
173181
}
174182
}
175183
}

0 commit comments

Comments
 (0)