From ccf3d1f3d612e71794fd2047ccf89b2f0856245c Mon Sep 17 00:00:00 2001 From: Sylvia van Os Date: Tue, 31 Dec 2019 18:23:45 +0100 Subject: [PATCH] Fix converting loyalty card to barcodeless The LoyaltyCardEditActivity assumes on many places that an empty string means it doesn't know a value yet. This patch ensures that the BarcodeSelectorActivity returns a special string so that the LoyaltyCardEditActivity can distinguish explicitly picking no barcode from a not yet populated field. --- .../card_locker/BarcodeSelectorActivity.java | 2 +- .../card_locker/LoyaltyCardEditActivity.java | 79 +++++++++++-------- 2 files changed, 48 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/protect/card_locker/BarcodeSelectorActivity.java b/app/src/main/java/protect/card_locker/BarcodeSelectorActivity.java index 18becdcc6..60ca02386 100644 --- a/app/src/main/java/protect/card_locker/BarcodeSelectorActivity.java +++ b/app/src/main/java/protect/card_locker/BarcodeSelectorActivity.java @@ -145,7 +145,7 @@ public class BarcodeSelectorActivity extends AppCompatActivity public void onClick(View view) { Log.d(TAG, "Selected no barcode"); Intent result = new Intent(); - result.putExtra(BARCODE_FORMAT, ""); + result.putExtra(BARCODE_FORMAT, LoyaltyCardEditActivity.NO_BARCODE); result.putExtra(BARCODE_CONTENTS, cardId); BarcodeSelectorActivity.this.setResult(RESULT_OK, result); finish(); diff --git a/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java b/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java index f5397409c..1ac8bb02d 100644 --- a/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java +++ b/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java @@ -35,6 +35,7 @@ import java.io.InvalidObjectException; public class LoyaltyCardEditActivity extends AppCompatActivity { private static final String TAG = "CardLocker"; + protected static final String NO_BARCODE = "_NO_BARCODE_"; private static final int SELECT_BARCODE_REQUEST = 1; @@ -229,42 +230,49 @@ public class LoyaltyCardEditActivity extends AppCompatActivity if(cardIdFieldView.getText().length() > 0 && barcodeTypeField.getText().length() > 0) { - String formatString = barcodeTypeField.getText().toString(); - final BarcodeFormat format = BarcodeFormat.valueOf(formatString); - final String cardIdString = cardIdFieldView.getText().toString(); - - if(barcodeImage.getHeight() == 0) + if(barcodeTypeField.getText().equals(NO_BARCODE)) { - Log.d(TAG, "ImageView size is not known known at start, waiting for load"); - // The size of the ImageView is not yet available as it has not - // yet been drawn. Wait for it to be drawn so the size is available. - barcodeImage.getViewTreeObserver().addOnGlobalLayoutListener( - new ViewTreeObserver.OnGlobalLayoutListener() - { - @Override - public void onGlobalLayout() - { - if (Build.VERSION.SDK_INT < 16) - { - barcodeImage.getViewTreeObserver().removeGlobalOnLayoutListener(this); - } - else - { - barcodeImage.getViewTreeObserver().removeOnGlobalLayoutListener(this); - } - - Log.d(TAG, "ImageView size now known"); - new BarcodeImageWriterTask(barcodeImage, cardIdString, format).execute(); - } - }); + barcodeImageLayout.setVisibility(View.GONE); } else { - Log.d(TAG, "ImageView size known known, creating barcode"); - new BarcodeImageWriterTask(barcodeImage, cardIdString, format).execute(); - } + String formatString = barcodeTypeField.getText().toString(); + final BarcodeFormat format = BarcodeFormat.valueOf(formatString); + final String cardIdString = cardIdFieldView.getText().toString(); - barcodeImageLayout.setVisibility(View.VISIBLE); + if(barcodeImage.getHeight() == 0) + { + Log.d(TAG, "ImageView size is not known known at start, waiting for load"); + // The size of the ImageView is not yet available as it has not + // yet been drawn. Wait for it to be drawn so the size is available. + barcodeImage.getViewTreeObserver().addOnGlobalLayoutListener( + new ViewTreeObserver.OnGlobalLayoutListener() + { + @Override + public void onGlobalLayout() + { + if (Build.VERSION.SDK_INT < 16) + { + barcodeImage.getViewTreeObserver().removeGlobalOnLayoutListener(this); + } + else + { + barcodeImage.getViewTreeObserver().removeOnGlobalLayoutListener(this); + } + + Log.d(TAG, "ImageView size now known"); + new BarcodeImageWriterTask(barcodeImage, cardIdString, format).execute(); + } + }); + } + else + { + Log.d(TAG, "ImageView size known known, creating barcode"); + new BarcodeImageWriterTask(barcodeImage, cardIdString, format).execute(); + } + + barcodeImageLayout.setVisibility(View.VISIBLE); + } } View.OnClickListener captureCallback = new View.OnClickListener() @@ -366,6 +374,13 @@ public class LoyaltyCardEditActivity extends AppCompatActivity String cardId = cardIdFieldView.getText().toString(); String barcodeType = barcodeTypeField.getText().toString(); + // We do not want to save the no barcode string to the database + // it is simply an empty there for no barcode + if(barcodeType.equals(NO_BARCODE)) + { + barcodeType = ""; + } + if(store.isEmpty()) { Snackbar.make(storeFieldEdit, R.string.noStoreError, Snackbar.LENGTH_LONG).show(); @@ -484,7 +499,7 @@ public class LoyaltyCardEditActivity extends AppCompatActivity } if(contents != null && contents.isEmpty() == false && - format != null) + format != null && format.isEmpty() == false) { Log.i(TAG, "Read barcode id: " + contents); Log.i(TAG, "Read format: " + format);