Skip to content

Commit 90c4d79

Browse files
committed
add encoded codes sorter
1 parent 9121e2f commit 90c4d79

File tree

3 files changed

+92
-40
lines changed

3 files changed

+92
-40
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# Changelog
22
All notable changes to this project will be documented in this file.
33

4+
## [0.16.7]
5+
6+
### Added
7+
- Encoded codes can now be sorted by using EncodedCodesOptions.sorter
8+
49
## [0.16.6]
510

611
### Changed

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

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@
1313
import io.snabble.sdk.codes.templates.groups.EmbedGroup;
1414

1515
public class EncodedCodesGenerator {
16+
public class ProductInfo {
17+
Product product;
18+
int quantity;
19+
ScannedCode scannedCode;
20+
21+
public ProductInfo(Product product, int quantity, ScannedCode scannedCode) {
22+
this.product = product;
23+
this.quantity = quantity;
24+
this.scannedCode = scannedCode;
25+
}
26+
}
27+
1628
private StringBuilder stringBuilder;
1729
private EncodedCodesOptions options;
1830
private ArrayList<String> encodedCodes;
@@ -34,8 +46,27 @@ public void add(String code) {
3446
}
3547

3648
public void add(ShoppingCart shoppingCart) {
37-
addProducts(shoppingCart, false);
38-
addProducts(shoppingCart, true);
49+
List<ProductInfo> productInfos = new ArrayList<>();
50+
for (int i = 0; i < shoppingCart.size(); i++) {
51+
ShoppingCart.Item item = shoppingCart.get(i);
52+
Product product = item.getProduct();
53+
54+
productInfos.add(new ProductInfo(product,
55+
item.getQuantity(),
56+
item.getScannedCode()));
57+
}
58+
59+
if (options.sorter != null) {
60+
Collections.sort(productInfos, new Comparator<ProductInfo>() {
61+
@Override
62+
public int compare(ProductInfo o1, ProductInfo o2) {
63+
return options.sorter.compare(o1, o2);
64+
}
65+
});
66+
}
67+
68+
addProducts(productInfos, false);
69+
addProducts(productInfos, true);
3970
}
4071

4172
public void clear() {
@@ -44,14 +75,14 @@ public void clear() {
4475
}
4576

4677
public ArrayList<String> generate() {
47-
if (options.finalCode.length() != 0) {
78+
if (options.finalCode != null && options.finalCode.length() != 0) {
4879
append(options.finalCode);
4980
}
5081

5182
finishCode();
5283

5384
ArrayList<String> ret = new ArrayList<>();
54-
for (int i=0; i<encodedCodes.size(); i++) {
85+
for (int i = 0; i < encodedCodes.size(); i++) {
5586
String code = encodedCodes.get(i);
5687
code = code.replace("{qrCodeCount}", String.valueOf(encodedCodes.size()));
5788
code = code.replace("{qrCodeIndex}", String.valueOf(i + 1));
@@ -62,9 +93,9 @@ public ArrayList<String> generate() {
6293
return ret;
6394
}
6495

65-
private boolean hasAgeRestrictedCode(ShoppingCart shoppingCart) {
66-
for (int i = 0; i < shoppingCart.size(); i++) {
67-
Product product = shoppingCart.get(i).getProduct();
96+
private boolean hasAgeRestrictedCode(List<ProductInfo> productInfos) {
97+
for (int i = 0; i < productInfos.size(); i++) {
98+
Product product = productInfos.get(i).product;
6899

69100
if (product.getSaleRestriction().isAgeRestriction()
70101
&& product.getSaleRestriction().getValue() >= 16) {
@@ -84,33 +115,8 @@ private boolean isAgeRestricted(Product product) {
84115
return false;
85116
}
86117

87-
private class ProductInfo {
88-
Product product;
89-
int quantity;
90-
ScannedCode scannedCode;
91-
92-
public ProductInfo(Product product, int quantity, ScannedCode scannedCode) {
93-
this.product = product;
94-
this.quantity = quantity;
95-
this.scannedCode = scannedCode;
96-
}
97-
}
98-
99-
private void addProducts(final ShoppingCart shoppingCart, boolean ageRestricted) {
100-
hasAgeRestrictedCode = hasAgeRestrictedCode(shoppingCart);
101-
102-
List<ProductInfo> productInfos = new ArrayList<>();
103-
for (int i = 0; i < shoppingCart.size(); i++) {
104-
ShoppingCart.Item item = shoppingCart.get(i);
105-
Product product = item.getProduct();
106-
if (ageRestricted != isAgeRestricted(product)) {
107-
continue;
108-
}
109-
110-
productInfos.add(new ProductInfo(product,
111-
item.getQuantity(),
112-
item.getScannedCode()));
113-
}
118+
private void addProducts(final List<ProductInfo> productInfos, boolean ageRestricted) {
119+
hasAgeRestrictedCode = hasAgeRestrictedCode(productInfos);
114120

115121
for (ProductInfo productInfo : productInfos) {
116122
if (ageRestricted != isAgeRestricted(productInfo.product)) {

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

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
import android.util.SparseArray;
44

55
import com.google.gson.JsonObject;
6-
import com.google.gson.annotations.SerializedName;
76

87
import io.snabble.sdk.Project;
98
import io.snabble.sdk.utils.JsonUtils;
109

1110
public class EncodedCodesOptions {
11+
public interface Sorter {
12+
int compare(EncodedCodesGenerator.ProductInfo productInfo1,
13+
EncodedCodesGenerator.ProductInfo productInfo2);
14+
}
15+
1216
public static final int DEFAULT_MAX_CHARS = 2953;
1317
public static final int DEFAULT_MAX_CODES = 100;
1418

@@ -25,12 +29,14 @@ public class EncodedCodesOptions {
2529
public final String countSeparator;
2630
public final int maxSizeMm;
2731
public final Project project;
32+
public final Sorter sorter;
2833

2934
private EncodedCodesOptions(String prefix, SparseArray<String> prefixMap, String separator, String suffix, int maxChars,
3035
int maxCodes, String finalCode, String nextCode,
3136
String nextCodeWithCheck, boolean repeatCodes, String countSeparator,
3237
int maxSizeMm,
33-
Project project) {
38+
Project project,
39+
Sorter sorter) {
3440
this.prefix = prefix;
3541
this.prefixMap = prefixMap;
3642
this.separator = separator;
@@ -44,6 +50,7 @@ private EncodedCodesOptions(String prefix, SparseArray<String> prefixMap, String
4450
this.countSeparator = countSeparator;
4551
this.maxSizeMm = maxSizeMm;
4652
this.project = project;
53+
this.sorter = sorter;
4754
}
4855

4956
public static class Builder {
@@ -60,6 +67,7 @@ public static class Builder {
6067
private boolean repeatCodes = true;
6168
private String countSeparator = ";";
6269
private int maxSizeMm;
70+
private Sorter sorter;
6371

6472
public Builder(Project project) {
6573
this.project = project;
@@ -125,9 +133,15 @@ public Builder maxSizeMm(int maxSizeMm) {
125133
return this;
126134
}
127135

136+
public Builder maxSizeMm(Sorter sorter) {
137+
this.sorter = sorter;
138+
return this;
139+
}
140+
128141
public EncodedCodesOptions build() {
129142
return new EncodedCodesOptions(prefix, prefixMap, separator, suffix, maxChars, maxCodes,
130-
finalCode, nextCode, nextCodeWithCheck, repeatCodes, countSeparator, maxSizeMm, project);
143+
finalCode, nextCode, nextCodeWithCheck, repeatCodes, countSeparator,
144+
maxSizeMm, project, sorter);
131145
}
132146
}
133147

@@ -178,7 +192,7 @@ public static EncodedCodesOptions fromJsonObject(Project project, JsonObject jso
178192
.build();
179193
case "simple":
180194
default:
181-
return new EncodedCodesOptions.Builder(project)
195+
EncodedCodesOptions.Builder builder = new EncodedCodesOptions.Builder(project)
182196
.prefix(JsonUtils.getStringOpt(jsonObject, "prefix", ""))
183197
.suffix(JsonUtils.getStringOpt(jsonObject, "suffix", ""))
184198
.separator(separator)
@@ -187,8 +201,35 @@ public static EncodedCodesOptions fromJsonObject(Project project, JsonObject jso
187201
.finalCode(finalCode)
188202
.nextCode(JsonUtils.getStringOpt(jsonObject, "nextCode", ""))
189203
.nextCodeWithCheck(JsonUtils.getStringOpt(jsonObject, "nextCodeWithCheck", ""))
190-
.maxSizeMm(JsonUtils.getIntOpt(jsonObject, "maxSizeMM", -1))
191-
.build();
204+
.maxSizeMm(JsonUtils.getIntOpt(jsonObject, "maxSizeMM", -1));
205+
206+
if (project.getId().contains("knauber")) {
207+
builder.sorter = new Sorter() {
208+
@Override
209+
public int compare(EncodedCodesGenerator.ProductInfo productInfo1,
210+
EncodedCodesGenerator.ProductInfo productInfo2) {
211+
final String catchAll = "2030801009887";
212+
213+
String tc1 = productInfo1.product.getTransmissionCode(
214+
productInfo1.scannedCode.getTemplateName(),
215+
productInfo1.scannedCode.getLookupCode());
216+
217+
String tc2 = productInfo2.product.getTransmissionCode(
218+
productInfo2.scannedCode.getTemplateName(),
219+
productInfo2.scannedCode.getLookupCode());
220+
221+
if (catchAll.equals(tc1)) {
222+
return 1;
223+
} else if (catchAll.equals(tc2)) {
224+
return -1;
225+
} else {
226+
return 0;
227+
}
228+
}
229+
};
230+
}
231+
232+
return builder.build();
192233
}
193234
}
194235
}

0 commit comments

Comments
 (0)