From 929633e4dda0382a93bf10a82a5d9414f3627c47 Mon Sep 17 00:00:00 2001 From: Sylvia van Os Date: Sun, 6 Jun 2021 21:13:44 +0200 Subject: [PATCH] Ask to update barcode value if card ID changes --- CHANGELOG.md | 1 + .../card_locker/LoyaltyCardEditActivity.java | 107 +++++++++++++----- app/src/main/res/values/strings.xml | 4 + 3 files changed, 84 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b7dd7e37..340c9fd38 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ Changes: - Support new PDF417 export from Voucher Vault - Support copying multiple barcodes at once - Support sharing multiple loyalty cards at once +- Ask to update barcode value if card ID changes ## v1.13 (2021-04-10) diff --git a/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java b/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java index 41788cf61..64b89d2e9 100644 --- a/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java +++ b/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java @@ -93,6 +93,7 @@ public class LoyaltyCardEditActivity extends AppCompatActivity ImportURIHelper importUriHelper; boolean hasChanged = false; + String tempStoredOldBarcodeValue = null; boolean initDone = false; AlertDialog confirmExitDialog = null; @@ -171,12 +172,9 @@ public class LoyaltyCardEditActivity extends AppCompatActivity enterButton = findViewById(R.id.enterButton); - warnOnInvalidBarcodeType = new Runnable() { - @Override - public void run() { - if (!(boolean) barcodeImage.getTag()) { - Toast.makeText(LoyaltyCardEditActivity.this, getString(R.string.wrongValueForBarcodeType), Toast.LENGTH_LONG).show(); - } + warnOnInvalidBarcodeType = () -> { + if (!(boolean) barcodeImage.getTag()) { + Toast.makeText(LoyaltyCardEditActivity.this, getString(R.string.wrongValueForBarcodeType), Toast.LENGTH_LONG).show(); } }; @@ -322,15 +320,30 @@ public class LoyaltyCardEditActivity extends AppCompatActivity cardIdFieldView.addTextChangedListener(new TextWatcher() { @Override - public void beforeTextChanged(CharSequence s, int start, int count, int after) { } + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + if (initDone) { + if (tempStoredOldBarcodeValue == null) { + // We changed the card ID, save the current barcode ID in a temp + // variable and make sure to ask the user later if they also want to + // update the barcode ID + if (barcodeIdField.getTag() == null) { + // If it is set to "same as Card ID", temp-save the value before the + // Card ID change + tempStoredOldBarcodeValue = s.toString(); + } else { + // Otherwise, set the temp value to the current field value + tempStoredOldBarcodeValue = barcodeIdField.getText().toString(); + } + + barcodeIdField.setText(tempStoredOldBarcodeValue); + barcodeIdField.setTag(tempStoredOldBarcodeValue); + } + } + } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { hasChanged = true; - - if (!s.toString().isEmpty()) { - generateOrHideBarcode(); - } } @Override @@ -350,6 +363,10 @@ public class LoyaltyCardEditActivity extends AppCompatActivity hasChanged = true; if (s.toString().equals(getString(R.string.sameAsCardId))) { + // If the user manually changes the barcode again make sure we disable the + // request to update it to match the card id (if changed) + tempStoredOldBarcodeValue = null; + barcodeIdField.setTag(null); } else if (s.toString().equals(getString(R.string.setBarcodeId))) { if (!lastValue.toString().equals(getString(R.string.setBarcodeId))) { @@ -365,19 +382,15 @@ public class LoyaltyCardEditActivity extends AppCompatActivity } builder.setView(input); - builder.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - barcodeIdField.setTag(input.getText().toString()); - barcodeIdField.setText(input.getText()); - } - }); - builder.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - dialog.cancel(); - } + builder.setPositiveButton(getString(R.string.ok), (dialog, which) -> { + // If the user manually changes the barcode again make sure we disable the + // request to update it to match the card id (if changed) + tempStoredOldBarcodeValue = null; + + barcodeIdField.setTag(input.getText().toString()); + barcodeIdField.setText(input.getText()); }); + builder.setNegativeButton(getString(R.string.cancel), (dialog, which) -> dialog.cancel()); AlertDialog dialog = builder.create(); dialog.show(); dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); @@ -635,11 +648,6 @@ public class LoyaltyCardEditActivity extends AppCompatActivity thumbnail.setOnClickListener(new ColorSelectListener(headingColorValue)); - if (!initDone) { - hasChanged = false; - initDone = true; - } - // Update from intent if (barcodeType != null) { try { @@ -661,6 +669,11 @@ public class LoyaltyCardEditActivity extends AppCompatActivity } } + if (!initDone) { + hasChanged = false; + initDone = true; + } + generateOrHideBarcode(); enterButton.setOnClickListener(new EditCardIdAndBarcode()); @@ -693,8 +706,36 @@ public class LoyaltyCardEditActivity extends AppCompatActivity askBeforeQuitIfChanged(); } + private void askBarcodeChange(Runnable callback) { + new AlertDialog.Builder(this) + .setTitle(R.string.updateBarcodeQuestionTitle) + .setMessage(R.string.updateBarcodeQuestionText) + .setPositiveButton(R.string.yes, (dialog, which) -> { + barcodeIdField.setText(R.string.sameAsCardId); + tempStoredOldBarcodeValue = null; + + if (callback != null) { + callback.run(); + } + }) + .setNegativeButton(R.string.no, (dialog, which) -> { + barcodeIdField.setText(tempStoredOldBarcodeValue); + tempStoredOldBarcodeValue = null; + + if (callback != null) { + callback.run(); + } + }) + .show(); + } + private void askBeforeQuitIfChanged() { if (!hasChanged) { + if (tempStoredOldBarcodeValue != null) { + askBarcodeChange(this::askBeforeQuitIfChanged); + return; + } + finish(); return; } @@ -819,6 +860,11 @@ public class LoyaltyCardEditActivity extends AppCompatActivity private void doSave() { + if (tempStoredOldBarcodeValue != null) { + askBarcodeChange(this::doSave); + return; + } + String store = storeFieldEdit.getText().toString(); String note = noteFieldEdit.getText().toString(); Date expiry = (Date) expiryField.getTag(); @@ -1008,6 +1054,11 @@ public class LoyaltyCardEditActivity extends AppCompatActivity } private void showPart(String part) { + if (tempStoredOldBarcodeValue != null) { + askBarcodeChange(() -> showPart(part)); + return; + } + View cardPart = findViewById(R.id.cardPart); View barcodePart = findViewById(R.id.barcodePart); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5d2e9f116..3df6a26d6 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -177,4 +177,8 @@ The value is not valid for the selected barcode type Copied card IDs to clipboard I want to share some cards with you + Update barcode value? + You changed the card ID. Do you want to also update the barcode to use the same value? + Yes + No