Skip to content

Commit 7e57a18

Browse files
committed
Merge branch 'unit_hack'
2 parents ff2580a + 2c4233d commit 7e57a18

File tree

4 files changed

+91
-49
lines changed

4 files changed

+91
-49
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ private void setScannedCodeForEntry(Entry entry, ScannableCode scannedCode){
305305
entry.weight = scannedCode.getEmbeddedData();
306306
} else if(scannedCode.hasPriceData()){
307307
entry.price = scannedCode.getEmbeddedData();
308-
} else if(scannedCode.hasAmountData()){
308+
} else if(scannedCode.hasUnitData()){
309309
entry.amount = scannedCode.getEmbeddedData();
310310
}
311311
}

core/src/main/java/io/snabble/sdk/codes/EAN13.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public int getEmbeddedData() {
8484
}
8585

8686
@Override
87-
public boolean hasAmountData() {
87+
public boolean hasUnitData() {
8888
return hasUnitData;
8989
}
9090

core/src/main/java/io/snabble/sdk/codes/ScannableCode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public int getEmbeddedData() {
2323
return 0;
2424
}
2525

26-
public boolean hasAmountData() {
26+
public boolean hasUnitData() {
2727
return false;
2828
}
2929

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

Lines changed: 88 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@
2626
import io.snabble.sdk.Product;
2727
import io.snabble.sdk.ShoppingCart;
2828
import io.snabble.sdk.SnabbleSdk;
29+
import io.snabble.sdk.codes.EAN13;
2930
import io.snabble.sdk.codes.ScannableCode;
3031
import io.snabble.sdk.ui.PriceFormatter;
3132
import io.snabble.sdk.ui.R;
3233
import io.snabble.sdk.ui.SnabbleUI;
3334
import io.snabble.sdk.ui.telemetry.Telemetry;
3435
import io.snabble.sdk.ui.utils.InputFilterMinMax;
36+
import io.snabble.sdk.ui.utils.UIUtils;
3537

3638
class ProductConfirmationDialog {
3739
private Context context;
@@ -108,7 +110,7 @@ public void show(Product newProduct, ScannableCode scannedCode) {
108110
quantityAnnotation.setVisibility(View.GONE);
109111
}
110112

111-
if (scannedCode.hasEmbeddedData()) {
113+
if (scannedCode.hasEmbeddedData() && scannedCode.getEmbeddedData() > 0) {
112114
quantity.setEnabled(false);
113115
}
114116

@@ -125,7 +127,7 @@ public void show(Product newProduct, ScannableCode scannedCode) {
125127
minus.setVisibility(View.GONE);
126128
quantityAnnotation.setVisibility(View.GONE);
127129
price.setVisibility(View.GONE);
128-
} else if(scannedCode.hasAmountData()){
130+
} else if(scannedCode.hasUnitData()){
129131
quantityAnnotation.setVisibility(View.GONE);
130132
plus.setVisibility(View.GONE);
131133
minus.setVisibility(View.GONE);
@@ -153,11 +155,18 @@ public void show(Product newProduct, ScannableCode scannedCode) {
153155

154156
PriceFormatter priceFormatter = new PriceFormatter(SnabbleUI.getSdkInstance());
155157
quantity.setText(priceFormatter.format(scannedCode.getEmbeddedData()));
156-
} else if(scannedCode.hasAmountData()){
157-
quantityAnnotation.setVisibility(View.GONE);
158-
plus.setVisibility(View.GONE);
159-
minus.setVisibility(View.GONE);
160-
quantity.setText(String.valueOf(scannedCode.getEmbeddedData()));
158+
} else if(scannedCode.hasUnitData()){
159+
if(scannedCode.getEmbeddedData() == 0) {
160+
quantityAnnotation.setVisibility(View.GONE);
161+
plus.setVisibility(View.VISIBLE);
162+
minus.setVisibility(View.VISIBLE);
163+
quantity.setText("1");
164+
} else {
165+
quantityAnnotation.setVisibility(View.GONE);
166+
plus.setVisibility(View.GONE);
167+
minus.setVisibility(View.GONE);
168+
quantity.setText(String.valueOf(scannedCode.getEmbeddedData()));
169+
}
161170
}
162171
} else if (type == Product.Type.Article) {
163172
quantityAnnotation.setVisibility(View.GONE);
@@ -168,46 +177,44 @@ public void show(Product newProduct, ScannableCode scannedCode) {
168177
quantity.setText(""); // initial value ?
169178
}
170179

171-
if(!scannedCode.hasEmbeddedData()) {
172-
quantity.setFilters(new InputFilter[]{new InputFilterMinMax(1, ShoppingCart.MAX_QUANTITY)});
173-
quantity.setOnEditorActionListener(new TextView.OnEditorActionListener() {
174-
@Override
175-
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
176-
if (actionId == EditorInfo.IME_ACTION_DONE
177-
|| (event.getAction() == KeyEvent.ACTION_DOWN
178-
&& event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
179-
addToCart();
180-
return true;
181-
}
182-
183-
return false;
180+
quantity.setFilters(new InputFilter[]{new InputFilterMinMax(1, ShoppingCart.MAX_QUANTITY)});
181+
quantity.setOnEditorActionListener(new TextView.OnEditorActionListener() {
182+
@Override
183+
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
184+
if (actionId == EditorInfo.IME_ACTION_DONE
185+
|| (event.getAction() == KeyEvent.ACTION_DOWN
186+
&& event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
187+
addToCart();
188+
return true;
184189
}
185-
});
186190

187-
quantity.addTextChangedListener(new TextWatcher() {
188-
@Override
189-
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
191+
return false;
192+
}
193+
});
190194

191-
}
195+
quantity.addTextChangedListener(new TextWatcher() {
196+
@Override
197+
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
192198

193-
@Override
194-
public void onTextChanged(CharSequence s, int start, int before, int count) {
199+
}
195200

196-
}
201+
@Override
202+
public void onTextChanged(CharSequence s, int start, int before, int count) {
197203

198-
@Override
199-
public void afterTextChanged(Editable s) {
200-
// its possible that the callback gets called before a dismiss is dispatched
201-
// and when that happens the product is already null
202-
if (product == null) {
203-
dismiss();
204-
return;
205-
}
206-
207-
updatePrice();
204+
}
205+
206+
@Override
207+
public void afterTextChanged(Editable s) {
208+
// its possible that the callback gets called before a dismiss is dispatched
209+
// and when that happens the product is already null
210+
if (product == null) {
211+
dismiss();
212+
return;
208213
}
209-
});
210-
}
214+
215+
updatePrice();
216+
}
217+
});
211218

212219
updatePrice();
213220

@@ -288,7 +295,7 @@ private void updatePrice() {
288295
if(q > 1){
289296
if(scannedCode.hasWeighData() || product.getType() == Product.Type.UserWeighed){
290297
price.setText(String.format("%sg * %s = %s", String.valueOf(q), singlePrice, priceText));
291-
} else if(scannedCode.hasAmountData()){
298+
} else if(scannedCode.hasUnitData()){
292299
price.setText(String.format("%s * %s = %s",
293300
String.valueOf(q),
294301
priceFormatter.format(product.getPrice()),
@@ -327,24 +334,59 @@ private void addToCart() {
327334
int q = getQuantity();
328335

329336
if(scannedCode.hasEmbeddedData()){
330-
shoppingCart.add(product, 1, scannedCode);
337+
// generate new code when the embedded data contains 0
338+
if (scannedCode.getEmbeddedData() == 0) {
339+
generateNewCodeWithEmbeddedData();
340+
}
341+
342+
// if the user entered 0, shake
343+
if(scannedCode.getEmbeddedData() == 0) {
344+
shake();
345+
return;
346+
} else {
347+
shoppingCart.add(product, 1, scannedCode);
348+
}
331349
} else if (product.getType() == Product.Type.Article) {
332350
shoppingCart.setQuantity(product, q, scannedCode);
333351
} else if(product.getType() == Product.Type.UserWeighed){
334352
if(q > 0) {
335353
shoppingCart.add(product, q, scannedCode);
336354
} else {
337-
TranslateAnimation shake = new TranslateAnimation(0, 10, 0, 0);
338-
shake.setDuration(500);
339-
shake.setInterpolator(new CycleInterpolator(7));
340-
quantity.startAnimation(shake);
355+
shake();
341356
return;
342357
}
343358
}
344359

345360
dismiss();
346361
}
347362

363+
private void shake() {
364+
float density = context.getResources().getDisplayMetrics().density;
365+
TranslateAnimation shake = new TranslateAnimation(0, 3 * density, 0, 0);
366+
shake.setDuration(500);
367+
shake.setInterpolator(new CycleInterpolator(7));
368+
quantity.startAnimation(shake);
369+
}
370+
371+
private void generateNewCodeWithEmbeddedData() {
372+
String code = scannedCode.getCode();
373+
374+
StringBuilder stringBuilder = new StringBuilder();
375+
stringBuilder.append(code.substring(0, code.length() - 6));
376+
String dataStr = String.valueOf(getQuantity());
377+
378+
int remaining = 5 - dataStr.length();
379+
for(int i=0; i<remaining; i++) {
380+
stringBuilder.append("0");
381+
}
382+
383+
stringBuilder.append(dataStr);
384+
stringBuilder.replace(6, 7, String.valueOf(EAN13.internalChecksum(stringBuilder.toString())));
385+
stringBuilder.append(String.valueOf(EAN13.checksum(stringBuilder.toString())));
386+
387+
scannedCode = ScannableCode.parse(SnabbleUI.getSdkInstance(), stringBuilder.toString());
388+
}
389+
348390
private int getQuantity() {
349391
try {
350392
return Integer.parseInt(quantity.getText().toString());

0 commit comments

Comments
 (0)