From 99a38499423ac48f8d7f49485d74ec2cfdeb0af5 Mon Sep 17 00:00:00 2001 From: Sylvia van Os Date: Thu, 28 Jan 2021 19:31:46 +0100 Subject: [PATCH] Fix missing lookup table update for groups update --- CHANGELOG.md | 1 + .../java/protect/card_locker/DBHelper.java | 64 ++++++++++++++----- .../protect/card_locker/DatabaseTest.java | 32 +++++++++- 3 files changed, 80 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f788aa33..70b585758 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ Changes: - Add support for scaling the barcode when moving to top to fit even more small scanners - Fix bottom sheet jumping after switching to fullscreen - Make header in loyalty card view small in landscape mode +- Fix cards not staying in group when group gets renamed ## v1.7.1 (2021-01-18) diff --git a/app/src/main/java/protect/card_locker/DBHelper.java b/app/src/main/java/protect/card_locker/DBHelper.java index 1f0506054..1c91b48e6 100644 --- a/app/src/main/java/protect/card_locker/DBHelper.java +++ b/app/src/main/java/protect/card_locker/DBHelper.java @@ -4,6 +4,7 @@ import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteException; import android.database.sqlite.SQLiteOpenHelper; import android.graphics.Color; @@ -532,33 +533,66 @@ public class DBHelper extends SQLiteOpenHelper { if (newName.isEmpty()) return false; + boolean success = false; + SQLiteDatabase db = getWritableDatabase(); - ContentValues contentValues = new ContentValues(); - contentValues.put(LoyaltyCardDbGroups.ID, newName); + ContentValues groupContentValues = new ContentValues(); + groupContentValues.put(LoyaltyCardDbGroups.ID, newName); + + ContentValues lookupContentValues = new ContentValues(); + lookupContentValues.put(LoyaltyCardDbIdsGroups.groupID, newName); + + db.beginTransaction(); try { - int rowsUpdated = db.update(LoyaltyCardDbGroups.TABLE, contentValues, + // Update group name + int groupsChanged = db.update(LoyaltyCardDbGroups.TABLE, groupContentValues, LoyaltyCardDbGroups.ID + "=?", new String[]{groupName}); - return (rowsUpdated == 1); - } catch (android.database.sqlite.SQLiteConstraintException _e) { - return false; + + // Also update lookup tables + db.update(LoyaltyCardDbIdsGroups.TABLE, lookupContentValues, + LoyaltyCardDbIdsGroups.groupID + "=?", + new String[]{groupName}); + + if (groupsChanged == 1) { + db.setTransactionSuccessful(); + success = true; + } + } catch (SQLiteException e) { + } finally { + db.endTransaction(); } + + return success; } public boolean deleteGroup(final String groupName) { + boolean success = false; + SQLiteDatabase db = getWritableDatabase(); - // Delete group - int rowsDeleted = db.delete(LoyaltyCardDbGroups.TABLE, - LoyaltyCardDbGroups.ID + " = ? ", - new String[]{groupName}); - // And delete lookup table entries associated with this group - db.delete(LoyaltyCardDbIdsGroups.TABLE, - LoyaltyCardDbIdsGroups.groupID + " = ? ", - new String[]{groupName}); + db.beginTransaction(); + try { + // Delete group + int groupsDeleted = db.delete(LoyaltyCardDbGroups.TABLE, + LoyaltyCardDbGroups.ID + " = ? ", + new String[]{groupName}); - return (rowsDeleted == 1); + // And delete lookup table entries associated with this group + db.delete(LoyaltyCardDbIdsGroups.TABLE, + LoyaltyCardDbIdsGroups.groupID + " = ? ", + new String[]{groupName}); + + if (groupsDeleted == 1) { + db.setTransactionSuccessful(); + success = true; + } + } finally { + db.endTransaction(); + } + + return success; } public int getGroupCardCount(final String groupName) diff --git a/app/src/test/java/protect/card_locker/DatabaseTest.java b/app/src/test/java/protect/card_locker/DatabaseTest.java index 3e3c65c6f..c8db55818 100644 --- a/app/src/test/java/protect/card_locker/DatabaseTest.java +++ b/app/src/test/java/protect/card_locker/DatabaseTest.java @@ -286,23 +286,51 @@ public class DatabaseTest @Test public void updateGroup() { - long id = db.insertGroup("group one"); + // Create card + assertEquals(0, db.getLoyaltyCardCount()); + long id = db.insertLoyaltyCard("store", "note", null, "cardId", BarcodeFormat.UPC_A.toString(), DEFAULT_HEADER_COLOR, 0); boolean result = (id != -1); assertTrue(result); + assertEquals(1, db.getLoyaltyCardCount()); + + // Create group + long groupId = db.insertGroup("group one"); + result = (groupId != -1); + assertTrue(result); assertEquals(1, db.getGroupCount()); + // Add card to group + Group group = db.getGroup("group one"); + List groupList1 = new ArrayList<>(); + groupList1.add(group); + db.setLoyaltyCardGroups(1, groupList1); + + // Ensure the card has one group and the group has one card + List cardGroups = db.getLoyaltyCardGroups((int) id); + assertEquals(1, cardGroups.size()); + assertEquals("group one", cardGroups.get(0)._id); + assertEquals(1, db.getGroupCardCount("group one")); + + // Rename group result = db.updateGroup("group one", "group one renamed"); assertTrue(result); assertEquals(1, db.getGroupCount()); // Group one no longer exists - Group group = db.getGroup("group one"); + group = db.getGroup("group one"); assertNull(group); // But group one renamed does Group group2 = db.getGroup("group one renamed"); assertNotNull(group2); assertEquals("group one renamed", group2._id); + + // And card is in "group one renamed" + // Ensure the card has one group and the group has one card + cardGroups = db.getLoyaltyCardGroups((int) id); + assertEquals(1, cardGroups.size()); + assertEquals("group one renamed", cardGroups.get(0)._id); + assertEquals(1, db.getGroupCardCount("group one renamed")); } @Test