From 4ca7f7131932ed35e316b7354e52bc0f5c02ab37 Mon Sep 17 00:00:00 2001 From: Sylvia van Os Date: Tue, 20 Aug 2024 23:06:43 +0200 Subject: [PATCH] Refactor to allow updating a loyalty card from a bundle This allows us to send any (partial) loyalty card into the edit activity, granting us greater flexibility in what kind of scan result we can parse --- .../card_locker/BarcodeSelectorActivity.java | 2 +- .../protect/card_locker/BarcodeValues.java | 7 +- .../card_locker/CardShortcutConfigure.java | 2 +- .../CardsOnPowerScreenService.java | 8 +- .../java/protect/card_locker/DBHelper.java | 4 +- .../java/protect/card_locker/LoyaltyCard.java | 191 ++++++++++++++++-- .../card_locker/LoyaltyCardCursorAdapter.java | 6 +- .../card_locker/LoyaltyCardEditActivity.java | 104 ++++------ .../card_locker/LoyaltyCardViewActivity.java | 14 +- .../protect/card_locker/MainActivity.java | 9 +- .../card_locker/ManageGroupCursorAdapter.java | 2 +- .../protect/card_locker/ScanActivity.java | 6 +- .../protect/card_locker/ShortcutHelper.java | 3 +- .../importexport/CatimaExporter.java | 6 +- .../protect/card_locker/ImportExportTest.java | 8 +- .../LoyaltyCardCursorAdapterTest.java | 8 +- .../LoyaltyCardViewActivityTest.java | 6 +- 17 files changed, 265 insertions(+), 121 deletions(-) diff --git a/app/src/main/java/protect/card_locker/BarcodeSelectorActivity.java b/app/src/main/java/protect/card_locker/BarcodeSelectorActivity.java index f51ef7e0ec..5d6806d8dc 100644 --- a/app/src/main/java/protect/card_locker/BarcodeSelectorActivity.java +++ b/app/src/main/java/protect/card_locker/BarcodeSelectorActivity.java @@ -71,7 +71,7 @@ public void onTextChanged(CharSequence s, int start, int before, int count) { }); final Bundle b = getIntent().getExtras(); - final String initialCardId = b != null ? b.getString("initialCardId") : null; + final String initialCardId = b != null ? b.getString(LoyaltyCard.BUNDLE_LOYALTY_CARD_CARD_ID) : null; if (initialCardId != null) { cardId.setText(initialCardId); diff --git a/app/src/main/java/protect/card_locker/BarcodeValues.java b/app/src/main/java/protect/card_locker/BarcodeValues.java index e9b745fd54..9fb55b9c48 100644 --- a/app/src/main/java/protect/card_locker/BarcodeValues.java +++ b/app/src/main/java/protect/card_locker/BarcodeValues.java @@ -1,11 +1,14 @@ package protect.card_locker; +import androidx.annotation.Nullable; + public class BarcodeValues { + @Nullable private final String mFormat; private final String mContent; private String mNote; - public BarcodeValues(String format, String content) { + public BarcodeValues(@Nullable String format, String content) { mFormat = format; mContent = content; } @@ -14,7 +17,7 @@ public void setNote(String note) { mNote = note; } - public String format() { + public @Nullable String format() { return mFormat; } diff --git a/app/src/main/java/protect/card_locker/CardShortcutConfigure.java b/app/src/main/java/protect/card_locker/CardShortcutConfigure.java index 823acbeacc..14d8d7e85b 100644 --- a/app/src/main/java/protect/card_locker/CardShortcutConfigure.java +++ b/app/src/main/java/protect/card_locker/CardShortcutConfigure.java @@ -61,7 +61,7 @@ public void onCreate(Bundle bundle) { private void onClickAction(int position) { Cursor selected = DBHelper.getLoyaltyCardCursor(mDatabase, DBHelper.LoyaltyCardArchiveFilter.All); selected.moveToPosition(position); - LoyaltyCard loyaltyCard = LoyaltyCard.toLoyaltyCard(selected); + LoyaltyCard loyaltyCard = LoyaltyCard.fromCursor(selected); Log.d(TAG, "Creating shortcut for card " + loyaltyCard.store + "," + loyaltyCard.id); diff --git a/app/src/main/java/protect/card_locker/CardsOnPowerScreenService.java b/app/src/main/java/protect/card_locker/CardsOnPowerScreenService.java index ece22037c7..1e21fc6b82 100644 --- a/app/src/main/java/protect/card_locker/CardsOnPowerScreenService.java +++ b/app/src/main/java/protect/card_locker/CardsOnPowerScreenService.java @@ -42,10 +42,10 @@ public Flow.Publisher createPublisherForAllAvailable() { Cursor loyaltyCardCursor = DBHelper.getLoyaltyCardCursor(mDatabase, DBHelper.LoyaltyCardArchiveFilter.Unarchived); return subscriber -> { while (loyaltyCardCursor.moveToNext()) { - LoyaltyCard card = LoyaltyCard.toLoyaltyCard(loyaltyCardCursor); + LoyaltyCard card = LoyaltyCard.fromCursor(loyaltyCardCursor); Intent openIntent = new Intent(this, LoyaltyCardViewActivity.class) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) - .putExtra("id", card.id); + .putExtra(LoyaltyCardViewActivity.BUNDLE_ID, card.id); PendingIntent pendingIntent = PendingIntent.getActivity(getBaseContext(), card.id, openIntent, PendingIntent.FLAG_IMMUTABLE); subscriber.onNext( new Control.StatelessBuilder(PREFIX + card.id, pendingIntent) @@ -73,7 +73,7 @@ public Flow.Publisher createPublisherFor(@NonNull List controlI if (card != null) { Intent openIntent = new Intent(this, LoyaltyCardViewActivity.class) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) - .putExtra("id", card.id); + .putExtra(LoyaltyCardViewActivity.BUNDLE_ID, card.id); PendingIntent pendingIntent = PendingIntent.getActivity(getBaseContext(), card.id, openIntent, PendingIntent.FLAG_IMMUTABLE); control = new Control.StatefulBuilder(controlId, pendingIntent) .setTitle(card.store) @@ -129,7 +129,7 @@ public void performControlAction(@NonNull String controlId, @NonNull ControlActi consumer.accept(ControlAction.RESPONSE_OK); Intent openIntent = new Intent(this, LoyaltyCardViewActivity.class) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) - .putExtra("id", controlIdToCardId(controlId)); + .putExtra(LoyaltyCardViewActivity.BUNDLE_ID, controlIdToCardId(controlId)); startActivity(openIntent); closePowerScreenOnAndroid11(); diff --git a/app/src/main/java/protect/card_locker/DBHelper.java b/app/src/main/java/protect/card_locker/DBHelper.java index 56b2c4a711..7fd52962d3 100644 --- a/app/src/main/java/protect/card_locker/DBHelper.java +++ b/app/src/main/java/protect/card_locker/DBHelper.java @@ -332,7 +332,7 @@ public static Set imageFiles(Context context, final SQLiteDatabase datab Set files = new HashSet<>(); Cursor cardCursor = getLoyaltyCardCursor(database); while (cardCursor.moveToNext()) { - LoyaltyCard card = LoyaltyCard.toLoyaltyCard(cardCursor); + LoyaltyCard card = LoyaltyCard.fromCursor(cardCursor); for (ImageLocationType imageLocationType : ImageLocationType.values()) { String name = Utils.getCardImageFileName(card.id, imageLocationType); if (Utils.retrieveCardImageAsFile(context, name).exists()) { @@ -542,7 +542,7 @@ public static LoyaltyCard getLoyaltyCard(SQLiteDatabase database, final int id) if (data.getCount() == 1) { data.moveToFirst(); - card = LoyaltyCard.toLoyaltyCard(data); + card = LoyaltyCard.fromCursor(data); } data.close(); diff --git a/app/src/main/java/protect/card_locker/LoyaltyCard.java b/app/src/main/java/protect/card_locker/LoyaltyCard.java index 95e68f0775..5c2144521b 100644 --- a/app/src/main/java/protect/card_locker/LoyaltyCard.java +++ b/app/src/main/java/protect/card_locker/LoyaltyCard.java @@ -1,6 +1,7 @@ package protect.card_locker; import android.database.Cursor; +import android.os.Bundle; import android.os.Parcel; import android.os.Parcelable; @@ -10,30 +11,88 @@ import java.math.BigDecimal; import java.util.Currency; import java.util.Date; +import java.util.HashMap; +import java.util.Map; public class LoyaltyCard implements Parcelable { - public final int id; - public final String store; - public final String note; + public int id; + public String store; + public String note; @Nullable - public final Date validFrom; + public Date validFrom; @Nullable - public final Date expiry; - public final BigDecimal balance; + public Date expiry; + public BigDecimal balance; @Nullable - public final Currency balanceType; - public final String cardId; + public Currency balanceType; + public String cardId; @Nullable - public final String barcodeId; + public String barcodeId; @Nullable - public final CatimaBarcode barcodeType; + public CatimaBarcode barcodeType; @Nullable - public final Integer headerColor; - public final int starStatus; - public final int archiveStatus; - public final long lastUsed; + public Integer headerColor; + public int starStatus; + public int archiveStatus; + public long lastUsed; public int zoomLevel; + public static final String BUNDLE_LOYALTY_CARD_ID = "loyaltyCardId"; + public static final String BUNDLE_LOYALTY_CARD_STORE = "loyaltyCardStore"; + public static final String BUNDLE_LOYALTY_CARD_NOTE = "loyaltyCardNote"; + public static final String BUNDLE_LOYALTY_CARD_VALID_FROM = "loyaltyCardValidFrom"; + public static final String BUNDLE_LOYALTY_CARD_EXPIRY = "loyaltyCardExpiry"; + public static final String BUNDLE_LOYALTY_CARD_BALANCE = "loyaltyCardBalance"; + public static final String BUNDLE_LOYALTY_CARD_BALANCE_TYPE = "loyaltyCardBalanceType"; + public static final String BUNDLE_LOYALTY_CARD_CARD_ID = "loyaltyCardCardId"; + public static final String BUNDLE_LOYALTY_CARD_BARCODE_ID = "loyaltyCardBarcodeId"; + public static final String BUNDLE_LOYALTY_CARD_BARCODE_TYPE = "loyaltyCardBarcodeType"; + public static final String BUNDLE_LOYALTY_CARD_HEADER_COLOR = "loyaltyCardHeaderColor"; + public static final String BUNDLE_LOYALTY_CARD_STAR_STATUS = "loyaltyCardStarStatus"; + public static final String BUNDLE_LOYALTY_CARD_ARCHIVE_STATUS = "loyaltyCardArchiveStatus"; + public static final String BUNDLE_LOYALTY_CARD_LAST_USED = "loyaltyCardLastUsed"; + public static final String BUNDLE_LOYALTY_CARD_ZOOM_LEVEL = "loyaltyCardZoomLevel"; + + /** + * Create a loyalty card object with default values + */ + public LoyaltyCard() { + this.id = -1; + this.store = ""; + this.note = ""; + this.validFrom = null; + this.expiry = null; + this.balance = new BigDecimal("0"); + this.balanceType = null; + this.cardId = ""; + this.barcodeId = null; + this.barcodeType = null; + this.headerColor = null; + this.starStatus = 0; + this.lastUsed = 0; + this.zoomLevel = 100; + this.archiveStatus = 0; + } + + /** + * Create a new loyalty card + * + * @param id + * @param store + * @param note + * @param validFrom + * @param expiry + * @param balance + * @param balanceType + * @param cardId + * @param barcodeId + * @param barcodeType + * @param headerColor + * @param starStatus + * @param lastUsed + * @param zoomLevel + * @param archiveStatus + */ public LoyaltyCard(final int id, final String store, final String note, @Nullable final Date validFrom, @Nullable final Date expiry, final BigDecimal balance, @Nullable final Currency balanceType, final String cardId, @Nullable final String barcodeId, @Nullable final CatimaBarcode barcodeType, @@ -97,7 +156,103 @@ public void writeToParcel(Parcel parcel, int i) { parcel.writeInt(archiveStatus); } - public static LoyaltyCard toLoyaltyCard(Cursor cursor) { + public static LoyaltyCard fromBundle(Bundle bundle) { + // Grab default card + LoyaltyCard loyaltyCard = new LoyaltyCard(); + + // Update from bundle + loyaltyCard.updateFromBundle(bundle); + + // Return updated version + return loyaltyCard; + } + + public void updateFromBundle(Bundle bundle) { + if (bundle.containsKey(BUNDLE_LOYALTY_CARD_ID)) { + id = bundle.getInt(BUNDLE_LOYALTY_CARD_ID); + } + if (bundle.containsKey(BUNDLE_LOYALTY_CARD_STORE)) { + store = bundle.getString(BUNDLE_LOYALTY_CARD_STORE); + } + if (bundle.containsKey(BUNDLE_LOYALTY_CARD_NOTE)) { + note = bundle.getString(BUNDLE_LOYALTY_CARD_NOTE); + } + if (bundle.containsKey(BUNDLE_LOYALTY_CARD_VALID_FROM)) { + long tmpValidFrom = bundle.getLong(BUNDLE_LOYALTY_CARD_VALID_FROM, -1); + validFrom = tmpValidFrom != -1 ? new Date(tmpValidFrom) : null; + } + if (bundle.containsKey(BUNDLE_LOYALTY_CARD_EXPIRY)) { + long tmpExpiry = bundle.getLong(BUNDLE_LOYALTY_CARD_EXPIRY, -1); + expiry = tmpExpiry != -1 ? new Date(tmpExpiry) : null; + } + if (bundle.containsKey(BUNDLE_LOYALTY_CARD_BALANCE)) { + balance = new BigDecimal(bundle.getString(BUNDLE_LOYALTY_CARD_BALANCE)); + } + if (bundle.containsKey(BUNDLE_LOYALTY_CARD_BALANCE_TYPE)) { + String tmpBalanceType = bundle.getString(BUNDLE_LOYALTY_CARD_BALANCE_TYPE, null); + balanceType = tmpBalanceType != null ? Currency.getInstance(tmpBalanceType) : null; + } + if (bundle.containsKey(BUNDLE_LOYALTY_CARD_CARD_ID)) { + cardId = bundle.getString(BUNDLE_LOYALTY_CARD_CARD_ID); + } + if (bundle.containsKey(BUNDLE_LOYALTY_CARD_BARCODE_ID)) { + barcodeId = bundle.getString(BUNDLE_LOYALTY_CARD_BARCODE_ID); + } + if (bundle.containsKey(BUNDLE_LOYALTY_CARD_BARCODE_TYPE)) { + String tmpBarcodeType = bundle.getString(BUNDLE_LOYALTY_CARD_BARCODE_TYPE, null); + barcodeType = tmpBarcodeType != null ? CatimaBarcode.fromName(tmpBarcodeType) : null; + } + if (bundle.containsKey(BUNDLE_LOYALTY_CARD_HEADER_COLOR)) { + int tmpHeaderColor = bundle.getInt(BUNDLE_LOYALTY_CARD_HEADER_COLOR, -1); + headerColor = tmpHeaderColor != -1 ? tmpHeaderColor : null; + } + if (bundle.containsKey(BUNDLE_LOYALTY_CARD_STAR_STATUS)) { + starStatus = bundle.getInt(BUNDLE_LOYALTY_CARD_STAR_STATUS); + } + if (bundle.containsKey(BUNDLE_LOYALTY_CARD_ARCHIVE_STATUS)) { + archiveStatus = bundle.getInt(BUNDLE_LOYALTY_CARD_ARCHIVE_STATUS); + } + if (bundle.containsKey(BUNDLE_LOYALTY_CARD_LAST_USED)) { + lastUsed = bundle.getLong(BUNDLE_LOYALTY_CARD_LAST_USED); + } + if (bundle.containsKey(BUNDLE_LOYALTY_CARD_ZOOM_LEVEL)) { + zoomLevel = bundle.getInt(BUNDLE_LOYALTY_CARD_ZOOM_LEVEL); + } + } + + public Bundle toBundle() { + Bundle bundle = new Bundle(); + + bundle.putInt(BUNDLE_LOYALTY_CARD_ID, id); + bundle.putString(BUNDLE_LOYALTY_CARD_STORE, store); + bundle.putString(BUNDLE_LOYALTY_CARD_NOTE, note); + if (validFrom != null) { + bundle.putLong(BUNDLE_LOYALTY_CARD_VALID_FROM, validFrom.getTime()); + } + if (expiry != null) { + bundle.putLong(BUNDLE_LOYALTY_CARD_EXPIRY, expiry.getTime()); + } + bundle.putString(BUNDLE_LOYALTY_CARD_BALANCE, balance.toString()); + if (balanceType != null) { + bundle.putString(BUNDLE_LOYALTY_CARD_BALANCE_TYPE, balanceType.toString()); + } + bundle.putString(BUNDLE_LOYALTY_CARD_CARD_ID, cardId); + bundle.putString(BUNDLE_LOYALTY_CARD_BARCODE_ID, barcodeId); + if (barcodeType != null) { + bundle.putString(BUNDLE_LOYALTY_CARD_BARCODE_TYPE, barcodeType.name()); + } + if (headerColor != null) { + bundle.putInt(BUNDLE_LOYALTY_CARD_HEADER_COLOR, headerColor); + } + bundle.putInt(BUNDLE_LOYALTY_CARD_STAR_STATUS, starStatus); + bundle.putInt(BUNDLE_LOYALTY_CARD_ARCHIVE_STATUS, archiveStatus); + bundle.putLong(BUNDLE_LOYALTY_CARD_LAST_USED, lastUsed); + bundle.putInt(BUNDLE_LOYALTY_CARD_ZOOM_LEVEL, zoomLevel); + + return bundle; + } + + public static LoyaltyCard fromCursor(Cursor cursor) { int id = cursor.getInt(cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.ID)); String store = cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.STORE)); String note = cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.NOTE)); @@ -106,10 +261,10 @@ public static LoyaltyCard toLoyaltyCard(Cursor cursor) { BigDecimal balance = new BigDecimal(cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.BALANCE))); String cardId = cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.CARD_ID)); String barcodeId = cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.BARCODE_ID)); - int starred = cursor.getInt(cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.STAR_STATUS)); + int starStatus = cursor.getInt(cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.STAR_STATUS)); long lastUsed = cursor.getLong(cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.LAST_USED)); int zoomLevel = cursor.getInt(cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.ZOOM_LEVEL)); - int archived = cursor.getInt(cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.ARCHIVE_STATUS)); + int archiveStatus = cursor.getInt(cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.ARCHIVE_STATUS)); int barcodeTypeColumn = cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.BARCODE_TYPE); int balanceTypeColumn = cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.BALANCE_TYPE); @@ -141,7 +296,7 @@ public static LoyaltyCard toLoyaltyCard(Cursor cursor) { headerColor = cursor.getInt(headerColorColumn); } - return new LoyaltyCard(id, store, note, validFrom, expiry, balance, balanceType, cardId, barcodeId, barcodeType, headerColor, starred, lastUsed, zoomLevel, archived); + return new LoyaltyCard(id, store, note, validFrom, expiry, balance, balanceType, cardId, barcodeId, barcodeType, headerColor, starStatus, lastUsed, zoomLevel, archiveStatus); } public static boolean isDuplicate(final LoyaltyCard a, final LoyaltyCard b) { diff --git a/app/src/main/java/protect/card_locker/LoyaltyCardCursorAdapter.java b/app/src/main/java/protect/card_locker/LoyaltyCardCursorAdapter.java index 6b807b7ecc..5ecb89d012 100644 --- a/app/src/main/java/protect/card_locker/LoyaltyCardCursorAdapter.java +++ b/app/src/main/java/protect/card_locker/LoyaltyCardCursorAdapter.java @@ -79,7 +79,7 @@ public LoyaltyCardListItemViewHolder onCreateViewHolder(@NonNull ViewGroup input public LoyaltyCard getCard(int position) { mCursor.moveToPosition(position); - return LoyaltyCard.toLoyaltyCard(mCursor); + return LoyaltyCard.fromCursor(mCursor); } public void onBindViewHolder(LoyaltyCardListItemViewHolder inputHolder, Cursor inputCursor) { @@ -87,7 +87,7 @@ public void onBindViewHolder(LoyaltyCardListItemViewHolder inputHolder, Cursor i boolean showDivider = false; inputHolder.mDivider.setVisibility(View.GONE); - LoyaltyCard loyaltyCard = LoyaltyCard.toLoyaltyCard(inputCursor); + LoyaltyCard loyaltyCard = LoyaltyCard.fromCursor(inputCursor); Bitmap icon = Utils.retrieveCardImage(mContext, loyaltyCard.id, ImageLocationType.icon); if (mLoyaltyCardListDisplayOptions.showingNameBelowThumbnail() && icon != null) { @@ -192,7 +192,7 @@ public ArrayList getSelectedItems() { int i; for (i = 0; i < mSelectedItems.size(); i++) { mCursor.moveToPosition(mSelectedItems.keyAt(i)); - result.add(LoyaltyCard.toLoyaltyCard(mCursor)); + result.add(LoyaltyCard.fromCursor(mCursor)); } return result; diff --git a/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java b/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java index bd12131209..4875e23213 100644 --- a/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java +++ b/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java @@ -127,9 +127,6 @@ public class LoyaltyCardEditActivity extends CatimaAppCompatActivity implements public static final String BUNDLE_DUPLICATE_ID = "duplicateId"; public static final String BUNDLE_UPDATE = "update"; public static final String BUNDLE_OPEN_SET_ICON_MENU = "openSetIconMenu"; - public static final String BUNDLE_CARDID = "cardId"; - public static final String BUNDLE_BARCODEID = "barcodeId"; - public static final String BUNDLE_BARCODETYPE = "barcodeType"; public static final String BUNDLE_ADDGROUP = "addGroup"; TabLayout tabs; @@ -162,9 +159,6 @@ public class LoyaltyCardEditActivity extends CatimaAppCompatActivity implements boolean updateLoyaltyCard; boolean duplicateFromLoyaltyCardId; boolean openSetIconMenu; - String cardId; - String barcodeId; - String barcodeType; String addGroup; Uri importLoyaltyCardUri = null; @@ -183,7 +177,7 @@ public class LoyaltyCardEditActivity extends CatimaAppCompatActivity implements HashMap currencies = new HashMap<>(); HashMap currencySymbols = new HashMap<>(); - LoyaltyCard tempLoyaltyCard; + LoyaltyCard tempLoyaltyCard = new LoyaltyCard(); LoyaltyCardField tempLoyaltyCardField; ActivityResultLauncher mPhotoTakerLauncher; @@ -247,18 +241,38 @@ protected void updateTempState(LoyaltyCardField fieldName, Object value) { private void extractIntentFields(Intent intent) { final Bundle b = intent.getExtras(); + + addGroup = b != null ? b.getString(BUNDLE_ADDGROUP) : null; + openSetIconMenu = b != null && b.getBoolean(BUNDLE_OPEN_SET_ICON_MENU, false); + loyaltyCardId = b != null ? b.getInt(BUNDLE_ID) : 0; updateLoyaltyCard = b != null && b.getBoolean(BUNDLE_UPDATE, false); duplicateFromLoyaltyCardId = b != null && b.getBoolean(BUNDLE_DUPLICATE_ID, false); + importLoyaltyCardUri = intent.getData(); - openSetIconMenu = b != null && b.getBoolean(BUNDLE_OPEN_SET_ICON_MENU, false); - - cardId = b != null ? b.getString(BUNDLE_CARDID) : null; - barcodeId = b != null ? b.getString(BUNDLE_BARCODEID) : null; - barcodeType = b != null ? b.getString(BUNDLE_BARCODETYPE) : null; - addGroup = b != null ? b.getString(BUNDLE_ADDGROUP) : null; + // If we have to import a loyalty card, do so + if (updateLoyaltyCard || duplicateFromLoyaltyCardId) { + tempLoyaltyCard = DBHelper.getLoyaltyCard(mDatabase, loyaltyCardId); + if (tempLoyaltyCard == null) { + Log.w(TAG, "Could not lookup loyalty card " + loyaltyCardId); + Toast.makeText(this, R.string.noCardExistsError, Toast.LENGTH_LONG).show(); + finish(); + return; + } + } else if (importLoyaltyCardUri != null) { + try { + tempLoyaltyCard = importUriHelper.parse(importLoyaltyCardUri); + } catch (InvalidObjectException ex) { + Toast.makeText(this, R.string.failedParsingImportUriError, Toast.LENGTH_LONG).show(); + finish(); + return; + } + } - importLoyaltyCardUri = intent.getData(); + // If the intent contains any loyalty card fields, override those fields in our current temp card + if (b != null) { + tempLoyaltyCard.updateFromBundle(b); + } Log.d(TAG, "Edit activity: id=" + loyaltyCardId + ", updateLoyaltyCard=" + updateLoyaltyCard); @@ -652,9 +666,11 @@ public void onTabReselected(TabLayout.Tab tab) { Utils.makeUserChooseBarcodeFromList(this, barcodeValuesList, new BarcodeValuesListDisambiguatorCallback() { @Override public void onUserChoseBarcode(BarcodeValues barcodeValues) { - cardId = barcodeValues.content(); - barcodeType = barcodeValues.format(); - barcodeId = ""; + Bundle bundle = new Bundle(); + bundle.putString(LoyaltyCard.BUNDLE_LOYALTY_CARD_CARD_ID, barcodeValues.content()); + bundle.putString(LoyaltyCard.BUNDLE_LOYALTY_CARD_BARCODE_TYPE, barcodeValues.format()); + bundle.putString(LoyaltyCard.BUNDLE_LOYALTY_CARD_BARCODE_ID, ""); + tempLoyaltyCard.updateFromBundle(bundle); } @Override @@ -796,30 +812,6 @@ protected void onResume() { onResuming = true; - if (tempLoyaltyCard == null) { - if (updateLoyaltyCard || duplicateFromLoyaltyCardId) { - tempLoyaltyCard = DBHelper.getLoyaltyCard(mDatabase, loyaltyCardId); - if (tempLoyaltyCard == null) { - Log.w(TAG, "Could not lookup loyalty card " + loyaltyCardId); - Toast.makeText(this, R.string.noCardExistsError, Toast.LENGTH_LONG).show(); - finish(); - return; - } - } else if (importLoyaltyCardUri != null) { - try { - tempLoyaltyCard = importUriHelper.parse(importLoyaltyCardUri); - } catch (InvalidObjectException ex) { - Toast.makeText(this, R.string.failedParsingImportUriError, Toast.LENGTH_LONG).show(); - finish(); - return; - } - } else { - // New card, use default values - tempLoyaltyCard = new LoyaltyCard(-1, "", "", null, null, new BigDecimal("0"), null, "", null, null, null, 0, Utils.getUnixTime(), 100,0); - - } - } - if (!initDone) { if (updateLoyaltyCard) { setTitle(R.string.editCardTitle); @@ -918,32 +910,28 @@ protected void onResume() { updateTempState(LoyaltyCardField.headerColor, tempLoyaltyCard.store.isEmpty() ? Utils.getRandomHeaderColor(this) : Utils.getHeaderColor(this, tempLoyaltyCard)); } - // Update from intent - if (barcodeType != null) { + // Fix up some fields + // TODO: Double check if necessary and if can't reflow differently + if (tempLoyaltyCard.barcodeType != null) { try { - barcodeTypeField.setText(CatimaBarcode.fromName(barcodeType).prettyName()); + barcodeTypeField.setText(tempLoyaltyCard.barcodeType.prettyName()); } catch (IllegalArgumentException e) { barcodeTypeField.setText(getString(R.string.noBarcode)); } } - if (cardId != null) { - cardIdFieldView.setText(cardId); + if (tempLoyaltyCard.cardId != null) { + cardIdFieldView.setText(tempLoyaltyCard.cardId); } - if (barcodeId != null) { - if (!barcodeId.isEmpty()) { - barcodeIdField.setText(barcodeId); + if (tempLoyaltyCard.barcodeId != null) { + if (!tempLoyaltyCard.barcodeId.isEmpty()) { + barcodeIdField.setText(tempLoyaltyCard.barcodeId); } else { barcodeIdField.setText(getString(R.string.sameAsCardId)); } } - // Empty intent values - barcodeType = null; - cardId = null; - barcodeId = null; - // Initialization has finished if (!initDone) { initDone = true; @@ -1255,7 +1243,7 @@ class EditCardIdAndBarcode implements View.OnClickListener { public void onClick(View v) { Intent i = new Intent(getApplicationContext(), ScanActivity.class); final Bundle b = new Bundle(); - b.putString(LoyaltyCardEditActivity.BUNDLE_CARDID, cardIdFieldView.getText().toString()); + b.putString(LoyaltyCard.BUNDLE_LOYALTY_CARD_CARD_ID, cardIdFieldView.getText().toString()); i.putExtras(b); mCardIdAndBarCodeEditorLauncher.launch(i); } @@ -1594,8 +1582,6 @@ private void doSave() { e.printStackTrace(); } - Log.i(TAG, "Set " + loyaltyCardId + " to " + cardId + " (update: " + updateLoyaltyCard + ")"); - DBHelper.setLoyaltyCardGroups(mDatabase, loyaltyCardId, selectedGroups); ShortcutHelper.updateShortcuts(this, DBHelper.getLoyaltyCard(mDatabase, loyaltyCardId)); @@ -1693,10 +1679,6 @@ public void startCropperUri(Uri sourceUri) { } private void generateBarcode() { - if (tempLoyaltyCard == null) { - return; - } - mTasks.flushTaskList(TaskHandler.TYPE.BARCODE, true, false, false); String cardIdString = tempLoyaltyCard.barcodeId != null ? tempLoyaltyCard.barcodeId : tempLoyaltyCard.cardId; diff --git a/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java b/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java index 9792a0bfde..caa0f2e2d9 100644 --- a/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java +++ b/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java @@ -97,6 +97,10 @@ public class LoyaltyCardViewActivity extends CatimaAppCompatActivity implements static final String STATE_IMAGEINDEX = "imageIndex"; static final String STATE_FULLSCREEN = "isFullscreen"; + static final String BUNDLE_ID = "id"; + static final String BUNDLE_CARDLIST = "cardList"; + static final String BUNDLE_TRANSITION_RIGHT = "transition_right"; + final private TaskHandler mTasks = new TaskHandler(); Runnable barcodeImageGenerationFinishedCallback; @@ -181,8 +185,8 @@ enum ImageType { private void extractIntentFields(Intent intent) { final Bundle b = intent.getExtras(); - loyaltyCardId = b != null ? b.getInt("id") : 0; - cardList = b != null ? b.getIntegerArrayList("cardList") : null; + loyaltyCardId = b != null ? b.getInt(BUNDLE_ID) : 0; + cardList = b != null ? b.getIntegerArrayList(BUNDLE_CARDLIST) : null; Log.d(TAG, "View activity: id=" + loyaltyCardId); } @@ -208,7 +212,7 @@ protected void onCreate(Bundle savedInstanceState) { return; } - int transitionRight = incomingIntentExtras.getInt("transition_right", -1); + int transitionRight = incomingIntentExtras.getInt(BUNDLE_TRANSITION_RIGHT, -1); if (transitionRight == 1) { // right side transition overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left); @@ -572,8 +576,8 @@ private void prevNextCard(boolean next) { // Restart activity with new card id and index Intent intent = getIntent(); Bundle b = intent.getExtras(); - b.putInt("id", loyaltyCardId); - b.putInt("transition_right", transitionRight ? 1 : 0); + b.putInt(BUNDLE_ID, loyaltyCardId); + b.putInt(BUNDLE_TRANSITION_RIGHT, transitionRight ? 1 : 0); intent.putExtras(b); intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); diff --git a/app/src/main/java/protect/card_locker/MainActivity.java b/app/src/main/java/protect/card_locker/MainActivity.java index a10f1178da..bd27f01f1a 100644 --- a/app/src/main/java/protect/card_locker/MainActivity.java +++ b/app/src/main/java/protect/card_locker/MainActivity.java @@ -428,8 +428,9 @@ private void processBarcodeValuesList(List barcodeValuesList, Str public void onUserChoseBarcode(BarcodeValues barcodeValues) { Intent newIntent = new Intent(getApplicationContext(), LoyaltyCardEditActivity.class); Bundle newBundle = new Bundle(); - newBundle.putString(LoyaltyCardEditActivity.BUNDLE_BARCODETYPE, barcodeValues.format()); - newBundle.putString(LoyaltyCardEditActivity.BUNDLE_CARDID, barcodeValues.content()); + newBundle.putString(LoyaltyCard.BUNDLE_LOYALTY_CARD_BARCODE_TYPE, barcodeValues.format()); + newBundle.putString(LoyaltyCard.BUNDLE_LOYALTY_CARD_CARD_ID, barcodeValues.content()); + newBundle.putString(LoyaltyCard.BUNDLE_LOYALTY_CARD_BARCODE_ID, null); if (group != null) { newBundle.putString(LoyaltyCardEditActivity.BUNDLE_ADDGROUP, group); } @@ -781,14 +782,14 @@ public void onRowClicked(int inputPosition) { Intent intent = new Intent(this, LoyaltyCardViewActivity.class); intent.setAction(""); final Bundle b = new Bundle(); - b.putInt("id", loyaltyCard.id); + b.putInt(LoyaltyCardViewActivity.BUNDLE_ID, loyaltyCard.id); ArrayList cardList = new ArrayList<>(); for (int i = 0; i < mAdapter.getItemCount(); i++) { cardList.add(mAdapter.getCard(i).id); } - b.putIntegerArrayList("cardList", cardList); + b.putIntegerArrayList(LoyaltyCardViewActivity.BUNDLE_CARDLIST, cardList); intent.putExtras(b); startActivity(intent); diff --git a/app/src/main/java/protect/card_locker/ManageGroupCursorAdapter.java b/app/src/main/java/protect/card_locker/ManageGroupCursorAdapter.java index 363d9c11f7..258d5ddec5 100644 --- a/app/src/main/java/protect/card_locker/ManageGroupCursorAdapter.java +++ b/app/src/main/java/protect/card_locker/ManageGroupCursorAdapter.java @@ -33,7 +33,7 @@ public void swapCursor(Cursor inputCursor) { @Override public void onBindViewHolder(LoyaltyCardListItemViewHolder inputHolder, Cursor inputCursor) { - LoyaltyCard loyaltyCard = LoyaltyCard.toLoyaltyCard(inputCursor); + LoyaltyCard loyaltyCard = LoyaltyCard.fromCursor(inputCursor); Boolean overlayValue = mInGroupOverlay.get(loyaltyCard.id); if ((overlayValue != null ? overlayValue : isLoyaltyCardInGroup(loyaltyCard.id))) { mAnimationItemsIndex.put(inputCursor.getPosition(), true); diff --git a/app/src/main/java/protect/card_locker/ScanActivity.java b/app/src/main/java/protect/card_locker/ScanActivity.java index 9c177751e9..ad3e59eb90 100644 --- a/app/src/main/java/protect/card_locker/ScanActivity.java +++ b/app/src/main/java/protect/card_locker/ScanActivity.java @@ -82,7 +82,7 @@ public class ScanActivity extends CatimaAppCompatActivity { private void extractIntentFields(Intent intent) { final Bundle b = intent.getExtras(); - cardId = b != null ? b.getString(LoyaltyCardEditActivity.BUNDLE_CARDID) : null; + cardId = b != null ? b.getString(LoyaltyCard.BUNDLE_LOYALTY_CARD_CARD_ID) : null; addGroup = b != null ? b.getString(LoyaltyCardEditActivity.BUNDLE_ADDGROUP) : null; Log.d(TAG, "Scan activity: id=" + cardId); } @@ -338,7 +338,7 @@ private void addWithoutBarcode() { // Buttons builder.setPositiveButton(getString(R.string.ok), (dialog, which) -> { - returnResult(input.getText().toString(), ""); + returnResult(input.getText().toString(), null); }); builder.setNegativeButton(getString(R.string.cancel), (dialog, which) -> dialog.cancel()); AlertDialog dialog = builder.create(); @@ -373,7 +373,7 @@ public void addManually() { Intent i = new Intent(getApplicationContext(), BarcodeSelectorActivity.class); if (cardId != null) { final Bundle b = new Bundle(); - b.putString("initialCardId", cardId); + b.putString(LoyaltyCard.BUNDLE_LOYALTY_CARD_CARD_ID, cardId); i.putExtras(b); } manualAddLauncher.launch(i); diff --git a/app/src/main/java/protect/card_locker/ShortcutHelper.java b/app/src/main/java/protect/card_locker/ShortcutHelper.java index 4df23de2c8..36a28aa2dc 100644 --- a/app/src/main/java/protect/card_locker/ShortcutHelper.java +++ b/app/src/main/java/protect/card_locker/ShortcutHelper.java @@ -133,8 +133,7 @@ static ShortcutInfoCompat.Builder createShortcutBuilder(Context context, Loyalty // one replace it. intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_SINGLE_TOP); final Bundle bundle = new Bundle(); - bundle.putInt("id", loyaltyCard.id); - bundle.putBoolean("view", true); + bundle.putInt(LoyaltyCardViewActivity.BUNDLE_ID, loyaltyCard.id); intent.putExtras(bundle); Bitmap iconBitmap = Utils.retrieveCardImage(context, loyaltyCard.id, ImageLocationType.icon); diff --git a/app/src/main/java/protect/card_locker/importexport/CatimaExporter.java b/app/src/main/java/protect/card_locker/importexport/CatimaExporter.java index 263782d8df..530f9c194b 100644 --- a/app/src/main/java/protect/card_locker/importexport/CatimaExporter.java +++ b/app/src/main/java/protect/card_locker/importexport/CatimaExporter.java @@ -64,7 +64,7 @@ public void exportData(Context context, SQLiteDatabase database, OutputStream ou Cursor cardCursor = DBHelper.getLoyaltyCardCursor(database); while (cardCursor.moveToNext()) { // For each card - LoyaltyCard card = LoyaltyCard.toLoyaltyCard(cardCursor); + LoyaltyCard card = LoyaltyCard.fromCursor(cardCursor); // For each image for (ImageLocationType imageLocationType : ImageLocationType.values()) { @@ -142,7 +142,7 @@ private void writeCSV(SQLiteDatabase database, OutputStreamWriter output) throws Cursor cardCursor = DBHelper.getLoyaltyCardCursor(database); while (cardCursor.moveToNext()) { - LoyaltyCard card = LoyaltyCard.toLoyaltyCard(cardCursor); + LoyaltyCard card = LoyaltyCard.fromCursor(cardCursor); printer.printRecord(card.id, card.store, @@ -176,7 +176,7 @@ private void writeCSV(SQLiteDatabase database, OutputStreamWriter output) throws Cursor cardCursor2 = DBHelper.getLoyaltyCardCursor(database); while (cardCursor2.moveToNext()) { - LoyaltyCard card = LoyaltyCard.toLoyaltyCard(cardCursor2); + LoyaltyCard card = LoyaltyCard.fromCursor(cardCursor2); for (Group group : DBHelper.getLoyaltyCardGroups(database, card.id)) { printer.printRecord(card.id, group._id); diff --git a/app/src/test/java/protect/card_locker/ImportExportTest.java b/app/src/test/java/protect/card_locker/ImportExportTest.java index 34d8c6d3d1..c90bff3db5 100644 --- a/app/src/test/java/protect/card_locker/ImportExportTest.java +++ b/app/src/test/java/protect/card_locker/ImportExportTest.java @@ -174,7 +174,7 @@ private void checkLoyaltyCards() { int index = 1; while (cursor.moveToNext()) { - LoyaltyCard card = LoyaltyCard.toLoyaltyCard(cursor); + LoyaltyCard card = LoyaltyCard.fromCursor(cursor); String expectedStore = String.format("store, \"%4d", index); String expectedNote = String.format("note, \"%4d", index); @@ -200,7 +200,7 @@ private void checkLoyaltyCardsAndDuplicates(int numCards) { Cursor cursor = DBHelper.getLoyaltyCardCursor(mDatabase); while (cursor.moveToNext()) { - LoyaltyCard card = LoyaltyCard.toLoyaltyCard(cursor); + LoyaltyCard card = LoyaltyCard.fromCursor(cursor); // ID goes up for duplicates (b/c the cursor orders by store), down for originals int index = card.id > numCards ? card.id - numCards : numCards - card.id + 1; @@ -236,7 +236,7 @@ private void checkLoyaltyCardsFiveStarred() { while (index < 10) { cursor.moveToNext(); - LoyaltyCard card = LoyaltyCard.toLoyaltyCard(cursor); + LoyaltyCard card = LoyaltyCard.fromCursor(cursor); String expectedStore = String.format("store, \"%4d", index); String expectedNote = String.format("note, \"%4d", index); @@ -258,7 +258,7 @@ private void checkLoyaltyCardsFiveStarred() { index = 1; while (cursor.moveToNext() && index < 5) { - LoyaltyCard card = LoyaltyCard.toLoyaltyCard(cursor); + LoyaltyCard card = LoyaltyCard.fromCursor(cursor); String expectedStore = String.format("store, \"%4d", index); String expectedNote = String.format("note, \"%4d", index); diff --git a/app/src/test/java/protect/card_locker/LoyaltyCardCursorAdapterTest.java b/app/src/test/java/protect/card_locker/LoyaltyCardCursorAdapterTest.java index 4d6ce4cd5c..1ada6674d5 100644 --- a/app/src/test/java/protect/card_locker/LoyaltyCardCursorAdapterTest.java +++ b/app/src/test/java/protect/card_locker/LoyaltyCardCursorAdapterTest.java @@ -137,7 +137,7 @@ public void TestCursorAdapterStarring() { assertEquals(4, cursor.getCount()); assertTrue(cursor.moveToFirst()); - LoyaltyCard loyaltyCard = LoyaltyCard.toLoyaltyCard(cursor); + LoyaltyCard loyaltyCard = LoyaltyCard.fromCursor(cursor); assertEquals("storeD", loyaltyCard.store); View view = createView(cursor); ConstraintLayout star = view.findViewById(R.id.star); @@ -146,7 +146,7 @@ public void TestCursorAdapterStarring() { assertEquals(View.GONE, archive.getVisibility()); assertTrue(cursor.moveToNext()); - loyaltyCard = LoyaltyCard.toLoyaltyCard(cursor); + loyaltyCard = LoyaltyCard.fromCursor(cursor); assertEquals("storeC", loyaltyCard.store); view = createView(cursor); star = view.findViewById(R.id.star); @@ -155,7 +155,7 @@ public void TestCursorAdapterStarring() { assertEquals(View.GONE, archive.getVisibility()); assertTrue(cursor.moveToNext()); - loyaltyCard = LoyaltyCard.toLoyaltyCard(cursor); + loyaltyCard = LoyaltyCard.fromCursor(cursor); assertEquals("storeB", loyaltyCard.store); view = createView(cursor); star = view.findViewById(R.id.star); @@ -164,7 +164,7 @@ public void TestCursorAdapterStarring() { assertEquals(View.VISIBLE, archive.getVisibility()); assertTrue(cursor.moveToNext()); - loyaltyCard = LoyaltyCard.toLoyaltyCard(cursor); + loyaltyCard = LoyaltyCard.fromCursor(cursor); assertEquals("storeA", loyaltyCard.store); view = createView(cursor); star = view.findViewById(R.id.star); diff --git a/app/src/test/java/protect/card_locker/LoyaltyCardViewActivityTest.java b/app/src/test/java/protect/card_locker/LoyaltyCardViewActivityTest.java index 2d54f36261..88f278921c 100644 --- a/app/src/test/java/protect/card_locker/LoyaltyCardViewActivityTest.java +++ b/app/src/test/java/protect/card_locker/LoyaltyCardViewActivityTest.java @@ -586,15 +586,15 @@ public void startWithoutParametersCaptureBarcodeCancel() throws IOException { private ActivityController createActivityWithLoyaltyCard(boolean editMode) { Intent intent = new Intent(); final Bundle bundle = new Bundle(); - bundle.putInt("id", 1); Class clazz; if (editMode) { - bundle.putBoolean("update", true); + bundle.putInt(LoyaltyCardEditActivity.BUNDLE_ID, 1); + bundle.putBoolean(LoyaltyCardEditActivity.BUNDLE_UPDATE, true); clazz = LoyaltyCardEditActivity.class; } else { - bundle.putBoolean("view", true); + bundle.putInt(LoyaltyCardViewActivity.BUNDLE_ID, 1); clazz = LoyaltyCardViewActivity.class; }