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.
This commit is contained in:
Sylvia van Os
2019-12-31 18:23:45 +01:00
parent 330ded0b5d
commit ccf3d1f3d6
2 changed files with 48 additions and 33 deletions

View File

@@ -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();

View File

@@ -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);