Skip to content

Commit

Permalink
Ask to update barcode value if card ID changes
Browse files Browse the repository at this point in the history
  • Loading branch information
TheLastProject committed Jun 6, 2021
1 parent eec7359 commit 929633e
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 28 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
107 changes: 79 additions & 28 deletions app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public class LoyaltyCardEditActivity extends AppCompatActivity
ImportURIHelper importUriHelper;

boolean hasChanged = false;
String tempStoredOldBarcodeValue = null;
boolean initDone = false;
AlertDialog confirmExitDialog = null;

Expand Down Expand Up @@ -171,12 +172,9 @@ protected void onCreate(Bundle savedInstanceState)

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

Expand Down Expand Up @@ -322,15 +320,30 @@ public void afterTextChanged(Editable s) {

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
Expand All @@ -350,6 +363,10 @@ public void onTextChanged(CharSequence s, int start, int before, int count) {
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))) {
Expand All @@ -365,19 +382,15 @@ public void onTextChanged(CharSequence s, int start, int before, int count) {
}
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);
Expand Down Expand Up @@ -635,11 +648,6 @@ else if(importLoyaltyCardUri != null)

thumbnail.setOnClickListener(new ColorSelectListener(headingColorValue));

if (!initDone) {
hasChanged = false;
initDone = true;
}

// Update from intent
if (barcodeType != null) {
try {
Expand All @@ -661,6 +669,11 @@ else if(importLoyaltyCardUri != null)
}
}

if (!initDone) {
hasChanged = false;
initDone = true;
}

generateOrHideBarcode();

enterButton.setOnClickListener(new EditCardIdAndBarcode());
Expand Down Expand Up @@ -693,8 +706,36 @@ public void onBackPressed() {
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;
}
Expand Down Expand Up @@ -819,6 +860,11 @@ public void onDateSet(DatePicker view, int year, int month, int day) {

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();
Expand Down Expand Up @@ -1008,6 +1054,11 @@ private void generateIcon(String store) {
}

private void showPart(String part) {
if (tempStoredOldBarcodeValue != null) {
askBarcodeChange(() -> showPart(part));
return;
}

View cardPart = findViewById(R.id.cardPart);
View barcodePart = findViewById(R.id.barcodePart);

Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,8 @@
<string name="wrongValueForBarcodeType">The value is not valid for the selected barcode type</string>
<string name="copy_to_clipboard_multiple_toast">Copied card IDs to clipboard</string>
<string name="intent_import_card_from_url_share_multiple_text">I want to share some cards with you</string>
<string name="updateBarcodeQuestionTitle">Update barcode value?</string>
<string name="updateBarcodeQuestionText">You changed the card ID. Do you want to also update the barcode to use the same value?</string>
<string name="yes">Yes</string>
<string name="no">No</string>
</resources>

0 comments on commit 929633e

Please sign in to comment.