Skip to content

Commit 4bf0862

Browse files
committed
Fixed issue: duplicity after importing same zip again
1 parent a95f5ce commit 4bf0862

File tree

3 files changed

+43
-12
lines changed

3 files changed

+43
-12
lines changed

app/src/main/java/protect/card_locker/DBHelper.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,30 @@ public static LoyaltyCard getLoyaltyCard(Context context, SQLiteDatabase databas
561561
return card;
562562
}
563563

564+
public static List<LoyaltyCard> getLoyaltyCardsByCardId(Context context, SQLiteDatabase database, final String cardId) {
565+
List<LoyaltyCard> cards = new ArrayList<>();
566+
567+
Cursor data = database.query(
568+
LoyaltyCardDbIds.TABLE,
569+
null,
570+
whereAttrs(LoyaltyCardDbIds.CARD_ID),
571+
withArgs(cardId),
572+
null,
573+
null,
574+
null
575+
);
576+
577+
if (data.moveToFirst()) {
578+
do {
579+
LoyaltyCard card = LoyaltyCard.fromCursor(context, data);
580+
cards.add(card);
581+
} while (data.moveToNext());
582+
}
583+
584+
data.close();
585+
return cards;
586+
}
587+
564588
public static List<Group> getLoyaltyCardGroups(SQLiteDatabase database, final int id) {
565589
Cursor data = database.rawQuery("select * from " + LoyaltyCardDbGroups.TABLE + " g " +
566590
" LEFT JOIN " + LoyaltyCardDbIdsGroups.TABLE + " ig ON ig." + LoyaltyCardDbIdsGroups.groupID + " = g." + LoyaltyCardDbGroups.ID +

app/src/main/java/protect/card_locker/LoyaltyCard.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -582,8 +582,7 @@ public static LoyaltyCard fromCursor(Context context, Cursor cursor) {
582582
public static boolean isDuplicate(Context context, final LoyaltyCard a, final LoyaltyCard b) {
583583
// Note: Bitmap comparing is slow, be careful when calling this method
584584
// Skip lastUsed & zoomLevel*
585-
return a.id == b.id && // non-nullable int
586-
a.store.equals(b.store) && // non-nullable String
585+
return a.store.equals(b.store) && // non-nullable String
587586
a.note.equals(b.note) && // non-nullable String
588587
Utils.equals(a.validFrom, b.validFrom) && // nullable Date
589588
Utils.equals(a.expiry, b.expiry) && // nullable Date

app/src/main/java/protect/card_locker/importexport/CatimaImporter.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,17 @@ public Map<Integer, Integer> saveAndDeduplicate(Context context, SQLiteDatabase
124124
Set<String> existingImages = DBHelper.imageFiles(context, database);
125125

126126
for (LoyaltyCard card : data.cards) {
127-
LoyaltyCard existing = DBHelper.getLoyaltyCard(context, database, card.id);
128-
if (existing == null) {
129-
DBHelper.insertLoyaltyCard(database, card.id, card.store, card.note, card.validFrom, card.expiry, card.balance, card.balanceType,
130-
card.cardId, card.barcodeId, card.barcodeType, card.headerColor, card.starStatus, card.lastUsed, card.archiveStatus);
131-
} else if (!isDuplicate(context, existing, card, existingImages, imageChecksums)) {
127+
List<LoyaltyCard> candidates = DBHelper.getLoyaltyCardsByCardId(context, database, card.cardId);
128+
boolean duplicateFound = false;
129+
130+
for (LoyaltyCard existing : candidates) {
131+
if (isDuplicate(context, existing, card, existingImages, imageChecksums)) {
132+
duplicateFound = true;
133+
break;
134+
}
135+
}
136+
137+
if (!duplicateFound) {
132138
long newId = DBHelper.insertLoyaltyCard(database, card.store, card.note, card.validFrom, card.expiry, card.balance, card.balanceType,
133139
card.cardId, card.barcodeId, card.barcodeType, card.headerColor, card.starStatus, card.lastUsed, card.archiveStatus);
134140
idMap.put(card.id, (int) newId);
@@ -156,14 +162,16 @@ public boolean isDuplicate(Context context, final LoyaltyCard existing, final Lo
156162
return false;
157163
}
158164
for (ImageLocationType imageLocationType : ImageLocationType.values()) {
159-
String name = Utils.getCardImageFileName(existing.id, imageLocationType);
160-
boolean exists = existingImages.contains(name);
161-
if (exists != imageChecksums.containsKey(name)) {
165+
String nameExists = Utils.getCardImageFileName(existing.id, imageLocationType);
166+
String nameChecksum = nameExists.replaceFirst("card_\\d+_", "card_" + card.id + "_");
167+
168+
boolean exists = existingImages.contains(nameExists);
169+
if (exists != imageChecksums.containsKey(nameChecksum)) {
162170
return false;
163171
}
164172
if (exists) {
165-
File file = Utils.retrieveCardImageAsFile(context, name);
166-
if (!imageChecksums.get(name).equals(Utils.checksum(new FileInputStream(file)))) {
173+
File file = Utils.retrieveCardImageAsFile(context, nameExists);
174+
if (!imageChecksums.get(nameChecksum).equals(Utils.checksum(new FileInputStream(file)))) {
167175
return false;
168176
}
169177
}

0 commit comments

Comments
 (0)