Skip to content

Commit

Permalink
Refactor to allow updating a loyalty card from a bundle
Browse files Browse the repository at this point in the history
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
  • Loading branch information
TheLastProject committed Aug 20, 2024
1 parent 74aad20 commit 5feb68b
Show file tree
Hide file tree
Showing 17 changed files with 265 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 5 additions & 2 deletions app/src/main/java/protect/card_locker/BarcodeValues.java
Original file line number Diff line number Diff line change
@@ -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;
}
Expand All @@ -14,7 +17,7 @@ public void setNote(String note) {
mNote = note;
}

public String format() {
public @Nullable String format() {
return mFormat;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ public Flow.Publisher<Control> 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)
Expand Down Expand Up @@ -73,7 +73,7 @@ public Flow.Publisher<Control> createPublisherFor(@NonNull List<String> 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)
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/protect/card_locker/DBHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ public static Set<String> imageFiles(Context context, final SQLiteDatabase datab
Set<String> 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()) {
Expand Down Expand Up @@ -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();
Expand Down
191 changes: 173 additions & 18 deletions app/src/main/java/protect/card_locker/LoyaltyCard.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package protect.card_locker;

import android.database.Cursor;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;

Expand All @@ -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,
Expand Down Expand Up @@ -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));
Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ 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) {
// Invisible until we want to show something more
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) {
Expand Down Expand Up @@ -192,7 +192,7 @@ public ArrayList<LoyaltyCard> 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;
Expand Down
Loading

0 comments on commit 5feb68b

Please sign in to comment.