From b8823ff909694e06eae4d4431601ef2d8e04d71c Mon Sep 17 00:00:00 2001 From: Altonss <66519591+Altonss@users.noreply.github.com> Date: Sat, 24 Sep 2022 01:38:10 +0200 Subject: [PATCH 01/41] First changes to DBHelper to adapt to the new group id --- .../java/protect/card_locker/DBHelper.java | 122 +++++++++++++----- 1 file changed, 93 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/protect/card_locker/DBHelper.java b/app/src/main/java/protect/card_locker/DBHelper.java index 7ae2f28bd4..a0849b1020 100644 --- a/app/src/main/java/protect/card_locker/DBHelper.java +++ b/app/src/main/java/protect/card_locker/DBHelper.java @@ -21,11 +21,12 @@ public class DBHelper extends SQLiteOpenHelper { public static final String DATABASE_NAME = "Catima.db"; public static final int ORIGINAL_DATABASE_VERSION = 1; - public static final int DATABASE_VERSION = 15; + public static final int DATABASE_VERSION = 16; public static class LoyaltyCardDbGroups { public static final String TABLE = "groups"; public static final String ID = "_id"; + public static final String NAME = "name"; public static final String ORDER = "orderId"; } @@ -86,7 +87,8 @@ public DBHelper(Context context) { public void onCreate(SQLiteDatabase db) { // create table for card groups db.execSQL("CREATE TABLE " + LoyaltyCardDbGroups.TABLE + "(" + - LoyaltyCardDbGroups.ID + " TEXT primary key not null," + + LoyaltyCardDbGroups.ID + " INTEGER primary key autoincrement," + + LoyaltyCardDbGroups.NAME + " TEXT not null," + LoyaltyCardDbGroups.ORDER + " INTEGER DEFAULT '0')"); // create table for cards @@ -110,7 +112,7 @@ public void onCreate(SQLiteDatabase db) { // create associative table for cards in groups db.execSQL("CREATE TABLE " + LoyaltyCardDbIdsGroups.TABLE + "(" + LoyaltyCardDbIdsGroups.cardID + " INTEGER," + - LoyaltyCardDbIdsGroups.groupID + " TEXT," + + LoyaltyCardDbIdsGroups.groupID + " INTEGER," + "primary key (" + LoyaltyCardDbIdsGroups.cardID + "," + LoyaltyCardDbIdsGroups.groupID + "))"); // create FTS search table @@ -314,6 +316,77 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("ALTER TABLE " + LoyaltyCardDbIds.TABLE + " ADD COLUMN " + LoyaltyCardDbIds.ARCHIVE_STATUS + " INTEGER DEFAULT '0' "); } + + if (oldVersion < 16 && newVersion >= 16) { + // SQLite doesn't support modify column + // So we need to create temp columns + // https://www.sqlite.org/faq.html#q11 + db.beginTransaction(); + + db.execSQL("CREATE TEMPORARY TABLE tmpDbGroups (" + + LoyaltyCardDbGroups.NAME + " TEXT not null," + + LoyaltyCardDbGroups.ORDER + " INTEGER DEFAULT '0' )"); + + db.execSQL("INSERT INTO tmpDbGroups (" + + LoyaltyCardDbGroups.NAME + " ," + + LoyaltyCardDbGroups.ORDER + ")" + + " SELECT " + + LoyaltyCardDbGroups.ID + " ," + + LoyaltyCardDbGroups.ORDER + " ," + + " FROM " + LoyaltyCardDbGroups.TABLE); + + db.execSQL("DROP TABLE " + LoyaltyCardDbGroups.TABLE); + + db.execSQL("CREATE TEMPORARY TABLE tmpDbIdsGroups (" + + LoyaltyCardDbIdsGroups.cardID + " INTEGER," + + LoyaltyCardDbIdsGroups.groupID + " TEXT," + + "primary key (" + LoyaltyCardDbIdsGroups.cardID + "," + LoyaltyCardDbIdsGroups.groupID + "))"); + + db.execSQL("INSERT INTO tmpDbIdsGroups (" + + LoyaltyCardDbIdsGroups.cardID + " ," + + LoyaltyCardDbIdsGroups.groupID + ")" + + " SELECT " + + LoyaltyCardDbIdsGroups.cardID + " ," + + LoyaltyCardDbIdsGroups.groupID + " ," + + " FROM " + LoyaltyCardDbIdsGroups.TABLE); + + db.execSQL("DROP TABLE " + LoyaltyCardDbIdsGroups.TABLE); + + db.execSQL("CREATE TABLE " + LoyaltyCardDbGroups.TABLE + "(" + + LoyaltyCardDbGroups.ID + " INTEGER primary key autoincrement," + + LoyaltyCardDbGroups.NAME + " TEXT not null," + + LoyaltyCardDbGroups.ORDER + " INTEGER DEFAULT '0' )"); + + db.execSQL("INSERT INTO " + LoyaltyCardDbGroups.TABLE + "(" + + LoyaltyCardDbGroups.ID + " ," + + LoyaltyCardDbGroups.NAME + " ," + + LoyaltyCardDbGroups.ORDER + ")" + + " SELECT " + + LoyaltyCardDbGroups.ID + " ," + + LoyaltyCardDbGroups.NAME + " ," + + LoyaltyCardDbGroups.ORDER + + " FROM tmpDbGroups"); + + db.execSQL("CREATE TABLE " + LoyaltyCardDbIdsGroups.TABLE + "(" + + LoyaltyCardDbIdsGroups.cardID + " INTEGER," + + LoyaltyCardDbIdsGroups.groupID + " INTEGER," + + "primary key (" + LoyaltyCardDbIdsGroups.cardID + "," + LoyaltyCardDbIdsGroups.groupID + "))"); + + db.execSQL("INSERT INTO " + LoyaltyCardDbIdsGroups.TABLE + "(" + + LoyaltyCardDbIdsGroups.cardID + " ," + + LoyaltyCardDbIdsGroups.groupID + ")" + + " SELECT " + + LoyaltyCardDbIdsGroups.cardID + " ," + + LoyaltyCardDbGroups.ID + + " FROM tmpDbIdsGroups AS idsGroups JOIN tmpDbGroups AS groups ON " + + "tmp." + LoyaltyCardDbIdsGroups.groupID "=" "groups." + LoyaltyCardDbGroups.NAME); + + db.execSQL("DROP TABLE tmpDbGroups"); + db.execSQL("DROP TABLE tmpDbIdsGroups"); + + db.setTransactionSuccessful(); + db.endTransaction(); + } } private static ContentValues generateFTSContentValues(final int id, final String store, final String note) { @@ -581,14 +654,14 @@ public static int getArchivedCardsCount(SQLiteDatabase database) { whereAttrs(LoyaltyCardDbIds.ARCHIVE_STATUS), withArgs(1)); } - public static int getArchivedCardsCount(SQLiteDatabase database, final String groupName) { + public static int getArchivedCardsCount(SQLiteDatabase database, final int groupId) { Cursor data = database.rawQuery( "select * from " + LoyaltyCardDbIds.TABLE + " c " + " LEFT JOIN " + LoyaltyCardDbIdsGroups.TABLE + " cg " + " ON c." + LoyaltyCardDbIds.ID + " = cg." + LoyaltyCardDbIdsGroups.cardID + " where " + LoyaltyCardDbIds.ARCHIVE_STATUS + " = 1" + " AND " + LoyaltyCardDbIdsGroups.groupID + "= ?", - withArgs(groupName) + withArgs(groupId) ); int count = data.getCount(); @@ -696,7 +769,7 @@ public static int getLoyaltyCardCount(SQLiteDatabase database) { */ public static Cursor getGroupCursor(SQLiteDatabase database) { return database.rawQuery("select * from " + LoyaltyCardDbGroups.TABLE + - " ORDER BY " + LoyaltyCardDbGroups.ORDER + " ASC," + LoyaltyCardDbGroups.ID + " COLLATE NOCASE ASC", null, null); + " ORDER BY " + LoyaltyCardDbGroups.ORDER + " ASC," + LoyaltyCardDbGroups.ID + " ASC", null, null); } public static List getGroups(SQLiteDatabase database) { @@ -726,16 +799,15 @@ public static void reorderGroups(SQLiteDatabase database, final List grou contentValues.put(LoyaltyCardDbGroups.ORDER, order); database.update(LoyaltyCardDbGroups.TABLE, contentValues, - whereAttrs(LoyaltyCardDbGroups.ID), - withArgs(group._id)); + LoyaltyCardDbGroups.ID + "=" + group._id); order++; } } - public static Group getGroup(SQLiteDatabase database, final String groupName) { + public static Group getGroup(SQLiteDatabase database, final int groupId) { Cursor data = database.query(LoyaltyCardDbGroups.TABLE, null, - whereAttrs(LoyaltyCardDbGroups.ID), withArgs(groupName), null, null, null); + LoyaltyCardDbGroups.ID + "=" + groupId, null, null, null); Group group = null; if (data.getCount() == 1) { @@ -751,9 +823,9 @@ public static int getGroupCount(SQLiteDatabase database) { return (int) DatabaseUtils.queryNumEntries(database, LoyaltyCardDbGroups.TABLE); } - public static List getGroupCardIds(SQLiteDatabase database, final String groupName) { + public static List getGroupCardIds(SQLiteDatabase database, final int groupId) { Cursor data = database.query(LoyaltyCardDbIdsGroups.TABLE, withArgs(LoyaltyCardDbIdsGroups.cardID), - whereAttrs(LoyaltyCardDbIdsGroups.groupID), withArgs(groupName), null, null, null); + LoyaltyCardDbIdsGroups.groupID + "=" + groupId, null, null, null); List cardIds = new ArrayList<>(); if (!data.moveToFirst()) { @@ -775,18 +847,18 @@ public static long insertGroup(SQLiteDatabase database, final String name) { if (name.isEmpty()) return -1; ContentValues contentValues = new ContentValues(); - contentValues.put(LoyaltyCardDbGroups.ID, name); + contentValues.put(LoyaltyCardDbGroups.NAME, name); contentValues.put(LoyaltyCardDbGroups.ORDER, getGroupCount(database)); return database.insert(LoyaltyCardDbGroups.TABLE, null, contentValues); } - public static boolean updateGroup(SQLiteDatabase database, final String groupName, final String newName) { + public static boolean updateGroup(SQLiteDatabase database, final int groupId, final String newName) { if (newName.isEmpty()) return false; boolean success = false; ContentValues groupContentValues = new ContentValues(); - groupContentValues.put(LoyaltyCardDbGroups.ID, newName); + groupContentValues.put(LoyaltyCardDbGroups.NAME, newName); ContentValues lookupContentValues = new ContentValues(); lookupContentValues.put(LoyaltyCardDbIdsGroups.groupID, newName); @@ -795,13 +867,7 @@ public static boolean updateGroup(SQLiteDatabase database, final String groupNam try { // Update group name int groupsChanged = database.update(LoyaltyCardDbGroups.TABLE, groupContentValues, - whereAttrs(LoyaltyCardDbGroups.ID), - withArgs(groupName)); - - // Also update lookup tables - database.update(LoyaltyCardDbIdsGroups.TABLE, lookupContentValues, - whereAttrs(LoyaltyCardDbIdsGroups.groupID), - withArgs(groupName)); + LoyaltyCardDbGroups.ID + "=" + groupId); if (groupsChanged == 1) { database.setTransactionSuccessful(); @@ -815,20 +881,18 @@ public static boolean updateGroup(SQLiteDatabase database, final String groupNam return success; } - public static boolean deleteGroup(SQLiteDatabase database, final String groupName) { + public static boolean deleteGroup(SQLiteDatabase database, final int groupId) { boolean success = false; database.beginTransaction(); try { // Delete group int groupsDeleted = database.delete(LoyaltyCardDbGroups.TABLE, - whereAttrs(LoyaltyCardDbGroups.ID), - withArgs(groupName)); + LoyaltyCardDbGroups.ID + "=" + groupId); // And delete lookup table entries associated with this group database.delete(LoyaltyCardDbIdsGroups.TABLE, - whereAttrs(LoyaltyCardDbIdsGroups.groupID), - withArgs(groupName)); + LoyaltyCardDbIdsGroups.groupID + "=" + groupId); if (groupsDeleted == 1) { database.setTransactionSuccessful(); @@ -844,9 +908,9 @@ public static boolean deleteGroup(SQLiteDatabase database, final String groupNam return success; } - public static int getGroupCardCount(SQLiteDatabase database, final String groupName) { + public static int getGroupCardCount(SQLiteDatabase database, final int groupId) { return (int) DatabaseUtils.queryNumEntries(database, LoyaltyCardDbIdsGroups.TABLE, - whereAttrs(LoyaltyCardDbIdsGroups.groupID), withArgs(groupName)); + LoyaltyCardDbIdsGroups.groupID + "=" + groupId); } static private String whereAttrs(String... attrs) { From 1c16bd8956db8cbcc030ddb5bac40056e919c43f Mon Sep 17 00:00:00 2001 From: Altonss <66519591+Altonss@users.noreply.github.com> Date: Sat, 24 Sep 2022 01:45:52 +0200 Subject: [PATCH 02/41] Update DBHelper.java Fix typo --- app/src/main/java/protect/card_locker/DBHelper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/protect/card_locker/DBHelper.java b/app/src/main/java/protect/card_locker/DBHelper.java index a0849b1020..3fa0676e7d 100644 --- a/app/src/main/java/protect/card_locker/DBHelper.java +++ b/app/src/main/java/protect/card_locker/DBHelper.java @@ -379,7 +379,7 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { LoyaltyCardDbIdsGroups.cardID + " ," + LoyaltyCardDbGroups.ID + " FROM tmpDbIdsGroups AS idsGroups JOIN tmpDbGroups AS groups ON " + - "tmp." + LoyaltyCardDbIdsGroups.groupID "=" "groups." + LoyaltyCardDbGroups.NAME); + "tmp." + LoyaltyCardDbIdsGroups.groupID + "=" + "groups." + LoyaltyCardDbGroups.NAME); db.execSQL("DROP TABLE tmpDbGroups"); db.execSQL("DROP TABLE tmpDbIdsGroups"); From a89ccff5648a6a67d43160afd0c40f1f183b4dd1 Mon Sep 17 00:00:00 2001 From: Altonss <_> Date: Mon, 26 Sep 2022 23:54:27 +0200 Subject: [PATCH 03/41] Improve DBHelper and adapt Group.java to id changes --- .../java/protect/card_locker/DBHelper.java | 18 +++++++++--------- .../main/java/protect/card_locker/Group.java | 13 ++++++++----- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/protect/card_locker/DBHelper.java b/app/src/main/java/protect/card_locker/DBHelper.java index 3fa0676e7d..1f6f578e4c 100644 --- a/app/src/main/java/protect/card_locker/DBHelper.java +++ b/app/src/main/java/protect/card_locker/DBHelper.java @@ -88,7 +88,7 @@ public void onCreate(SQLiteDatabase db) { // create table for card groups db.execSQL("CREATE TABLE " + LoyaltyCardDbGroups.TABLE + "(" + LoyaltyCardDbGroups.ID + " INTEGER primary key autoincrement," + - LoyaltyCardDbGroups.NAME + " TEXT not null," + + LoyaltyCardDbGroups.NAME + " TEXT unique not null," + LoyaltyCardDbGroups.ORDER + " INTEGER DEFAULT '0')"); // create table for cards @@ -332,7 +332,7 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { LoyaltyCardDbGroups.ORDER + ")" + " SELECT " + LoyaltyCardDbGroups.ID + " ," + - LoyaltyCardDbGroups.ORDER + " ," + + LoyaltyCardDbGroups.ORDER + " FROM " + LoyaltyCardDbGroups.TABLE); db.execSQL("DROP TABLE " + LoyaltyCardDbGroups.TABLE); @@ -347,7 +347,7 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { LoyaltyCardDbIdsGroups.groupID + ")" + " SELECT " + LoyaltyCardDbIdsGroups.cardID + " ," + - LoyaltyCardDbIdsGroups.groupID + " ," + + LoyaltyCardDbIdsGroups.groupID + " FROM " + LoyaltyCardDbIdsGroups.TABLE); db.execSQL("DROP TABLE " + LoyaltyCardDbIdsGroups.TABLE); @@ -799,7 +799,7 @@ public static void reorderGroups(SQLiteDatabase database, final List grou contentValues.put(LoyaltyCardDbGroups.ORDER, order); database.update(LoyaltyCardDbGroups.TABLE, contentValues, - LoyaltyCardDbGroups.ID + "=" + group._id); + whereAttrs(LoyaltyCardDbGroups.ID), withArgs(group._id)); order++; } @@ -807,7 +807,7 @@ public static void reorderGroups(SQLiteDatabase database, final List grou public static Group getGroup(SQLiteDatabase database, final int groupId) { Cursor data = database.query(LoyaltyCardDbGroups.TABLE, null, - LoyaltyCardDbGroups.ID + "=" + groupId, null, null, null); + whereAttrs(LoyaltyCardDbGroups.ID), withArgs(groupId), null, null, null); Group group = null; if (data.getCount() == 1) { @@ -825,7 +825,7 @@ public static int getGroupCount(SQLiteDatabase database) { public static List getGroupCardIds(SQLiteDatabase database, final int groupId) { Cursor data = database.query(LoyaltyCardDbIdsGroups.TABLE, withArgs(LoyaltyCardDbIdsGroups.cardID), - LoyaltyCardDbIdsGroups.groupID + "=" + groupId, null, null, null); + whereAttrs(LoyaltyCardDbIdsGroups.groupID), withArgs(groupId), null, null, null); List cardIds = new ArrayList<>(); if (!data.moveToFirst()) { @@ -867,7 +867,7 @@ public static boolean updateGroup(SQLiteDatabase database, final int groupId, fi try { // Update group name int groupsChanged = database.update(LoyaltyCardDbGroups.TABLE, groupContentValues, - LoyaltyCardDbGroups.ID + "=" + groupId); + whereAttrs(LoyaltyCardDbGroups.ID), withArgs(groupId)); if (groupsChanged == 1) { database.setTransactionSuccessful(); @@ -888,11 +888,11 @@ public static boolean deleteGroup(SQLiteDatabase database, final int groupId) { try { // Delete group int groupsDeleted = database.delete(LoyaltyCardDbGroups.TABLE, - LoyaltyCardDbGroups.ID + "=" + groupId); + whereAttrs(LoyaltyCardDbGroups.ID), withArgs(groupId)); // And delete lookup table entries associated with this group database.delete(LoyaltyCardDbIdsGroups.TABLE, - LoyaltyCardDbIdsGroups.groupID + "=" + groupId); + whereAttrs(LoyaltyCardDbIdsGroups.groupID), withArgs(groupId)); if (groupsDeleted == 1) { database.setTransactionSuccessful(); diff --git a/app/src/main/java/protect/card_locker/Group.java b/app/src/main/java/protect/card_locker/Group.java index 90d8da69d6..c24b410e31 100644 --- a/app/src/main/java/protect/card_locker/Group.java +++ b/app/src/main/java/protect/card_locker/Group.java @@ -5,19 +5,22 @@ import androidx.annotation.Nullable; public class Group { - public final String _id; + public final int _id; + public final String name; public final int order; - public Group(final String _id, final int order) { + public Group(final int _id, final String name, final int order) { this._id = _id; + this.name = name; this.order = order; } public static Group toGroup(Cursor cursor) { - String _id = cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbGroups.ID)); + int _id = cursor.getInt(cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbGroups.ID)); + String name = cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbGroups.NAME)); int order = cursor.getInt(cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbGroups.ORDER)); - return new Group(_id, order); + return new Group(_id, name, order); } @Override @@ -29,7 +32,7 @@ public boolean equals(@Nullable Object obj) { return false; } Group anotherGroup = (Group) obj; - return _id.equals(anotherGroup._id) && order == anotherGroup.order; + return _id == anotherGroup._id && order == anotherGroup.order; } @Override From 5fb95d9f81109765c5c442b3d0bbf1cc5756cb30 Mon Sep 17 00:00:00 2001 From: Altonss <_> Date: Tue, 27 Sep 2022 00:04:20 +0200 Subject: [PATCH 04/41] Fix group issues in importer --- .../protect/card_locker/importexport/CatimaImporter.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java b/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java index d83c25655a..8e6639da46 100644 --- a/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java +++ b/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java @@ -346,9 +346,9 @@ private void importLoyaltyCard(Context context, SQLiteDatabase database, CSVReco * session. */ private void importGroup(SQLiteDatabase database, CSVRecord record) throws FormatException { - String id = CSVHelpers.extractString(DBHelper.LoyaltyCardDbGroups.ID, record, null); + String name = CSVHelpers.extractString(DBHelper.LoyaltyCardDbGroups.NAME, record, null); - DBHelper.insertGroup(database, id); + DBHelper.insertGroup(database, name); } /** @@ -357,7 +357,7 @@ private void importGroup(SQLiteDatabase database, CSVRecord record) throws Forma */ private void importCardGroupMapping(SQLiteDatabase database, CSVRecord record) throws FormatException { Integer cardId = CSVHelpers.extractInt(DBHelper.LoyaltyCardDbIdsGroups.cardID, record, false); - String groupId = CSVHelpers.extractString(DBHelper.LoyaltyCardDbIdsGroups.groupID, record, null); + Integer groupId = CSVHelpers.extractInt(DBHelper.LoyaltyCardDbIdsGroups.groupID, record, false); List cardGroups = DBHelper.getLoyaltyCardGroups(database, cardId); cardGroups.add(DBHelper.getGroup(database, groupId)); From 2091d8cbcf3a22fedac8f963e80559a273ac9724 Mon Sep 17 00:00:00 2001 From: Altonss <_> Date: Tue, 27 Sep 2022 00:15:07 +0200 Subject: [PATCH 05/41] Update ManageGroupCursorAdapter to group id --- .../main/java/protect/card_locker/ManageGroupCursorAdapter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/protect/card_locker/ManageGroupCursorAdapter.java b/app/src/main/java/protect/card_locker/ManageGroupCursorAdapter.java index 9e198719b1..fdbfbadf11 100644 --- a/app/src/main/java/protect/card_locker/ManageGroupCursorAdapter.java +++ b/app/src/main/java/protect/card_locker/ManageGroupCursorAdapter.java @@ -18,7 +18,7 @@ public class ManageGroupCursorAdapter extends LoyaltyCardCursorAdapter { public ManageGroupCursorAdapter(Context inputContext, Cursor inputCursor, CardAdapterListener inputListener, Group group) { super(inputContext, inputCursor, inputListener); - mGroup = new Group(group._id, group.order); + mGroup = new Group(group._id, group.name, group.order); mInGroupOverlay = new HashMap<>(); mDatabase = new DBHelper(inputContext).getWritableDatabase(); } From 855912852b962adb143e496fb892ec9ef0bcfc4b Mon Sep 17 00:00:00 2001 From: Altonss <_> Date: Tue, 27 Sep 2022 00:36:33 +0200 Subject: [PATCH 06/41] Update CatimaExporter and LoyaltyCardViewActivity.java --- .../java/protect/card_locker/LoyaltyCardViewActivity.java | 2 +- .../protect/card_locker/importexport/CatimaExporter.java | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java b/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java index 8ae284413a..a2dcec209a 100644 --- a/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java +++ b/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java @@ -443,7 +443,7 @@ private void showInfoDialog() { if (loyaltyCardGroups.size() > 0) { List groupNames = new ArrayList<>(); for (Group group : loyaltyCardGroups) { - groupNames.add(group._id); + groupNames.add(group.name); } padSpannableString(infoText); 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 95a95decdf..f8a0a6adfc 100644 --- a/app/src/main/java/protect/card_locker/importexport/CatimaExporter.java +++ b/app/src/main/java/protect/card_locker/importexport/CatimaExporter.java @@ -104,14 +104,15 @@ private void writeCSV(SQLiteDatabase database, OutputStreamWriter output) throws printer.println(); // Print the header for groups - printer.printRecord(DBHelper.LoyaltyCardDbGroups.ID); + printer.printRecord(DBHelper.LoyaltyCardDbGroups.ID, + DBHelper.LoyaltyCardDbGroups.NAME); Cursor groupCursor = DBHelper.getGroupCursor(database); while (groupCursor.moveToNext()) { Group group = Group.toGroup(groupCursor); - printer.printRecord(group._id); + printer.printRecord(group._id, group.name); if (Thread.currentThread().isInterrupted()) { throw new InterruptedException(); From 6ab5959f9757727ac422c284ff377f276d38737d Mon Sep 17 00:00:00 2001 From: Altonss <_> Date: Fri, 30 Sep 2022 23:43:20 +0200 Subject: [PATCH 07/41] Further update to group id --- .../java/protect/card_locker/DBHelper.java | 14 +++++++++++++ .../card_locker/LoyaltyCardEditActivity.java | 6 +++--- .../card_locker/ManageGroupActivity.java | 20 +++++++++---------- .../card_locker/ManageGroupsActivity.java | 16 +++++++++++---- 4 files changed, 38 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/protect/card_locker/DBHelper.java b/app/src/main/java/protect/card_locker/DBHelper.java index 1f6f578e4c..91d1f84f22 100644 --- a/app/src/main/java/protect/card_locker/DBHelper.java +++ b/app/src/main/java/protect/card_locker/DBHelper.java @@ -819,6 +819,20 @@ public static Group getGroup(SQLiteDatabase database, final int groupId) { return group; } + public static Group getGroupByName(SQLiteDatabase database, String groupName) { + Cursor data = database.query(LoyaltyCardDbGroups.TABLE, null, + whereAttrs(LoyaltyCardDbGroups.NAME), withArgs(groupName), null, null, null); + + Group group = null; + if (data.getCount() == 1) { + data.moveToFirst(); + group = Group.toGroup(data); + } + data.close(); + + return group; + } + public static int getGroupCount(SQLiteDatabase database) { return (int) DatabaseUtils.queryNumEntries(database, LoyaltyCardDbGroups.TABLE); } diff --git a/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java b/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java index fcc3d11aed..bb1dc88d02 100644 --- a/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java +++ b/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java @@ -835,15 +835,15 @@ public void onResume() { for (Group group : DBHelper.getGroups(mDatabase)) { Chip chip = (Chip) getLayoutInflater().inflate(R.layout.layout_chip_choice, groupsChips, false); - chip.setText(group._id); + chip.setText(group.name); chip.setTag(group); - if (group._id.equals(addGroup)) { + if (group.name.equals(addGroup)) { chip.setChecked(true); } else { chip.setChecked(false); for (Group loyaltyCardGroup : loyaltyCardGroups) { - if (loyaltyCardGroup._id.equals(group._id)) { + if (loyaltyCardGroup._id == group._id) { chip.setChecked(true); break; } diff --git a/app/src/main/java/protect/card_locker/ManageGroupActivity.java b/app/src/main/java/protect/card_locker/ManageGroupActivity.java index 4b5174dc48..4351e9c0d0 100644 --- a/app/src/main/java/protect/card_locker/ManageGroupActivity.java +++ b/app/src/main/java/protect/card_locker/ManageGroupActivity.java @@ -73,8 +73,8 @@ public void afterTextChanged(Editable s) { mGroupNameText.setError(getResources().getText(R.string.group_name_is_empty)); return; } - if (!mGroup._id.equals(currentGroupName)) { - if (DBHelper.getGroup(mDatabase, currentGroupName) != null) { + if (!mGroup.name.equals(currentGroupName)) { + if (DBHelper.getGroupByName(mDatabase, currentGroupName) != null) { mGroupNameNotInUse = false; mGroupNameText.setError(getResources().getText(R.string.group_name_already_in_use)); } else { @@ -85,17 +85,15 @@ public void afterTextChanged(Editable s) { }); Intent intent = getIntent(); - String groupId = intent.getStringExtra("group"); - if (groupId == null) { - throw (new IllegalArgumentException("this activity expects a group loaded into it's intent")); - } + final Bundle b = intent.getExtras(); + Integer groupId = b.getInt("group"); Log.d("groupId", "groupId: " + groupId); mGroup = DBHelper.getGroup(mDatabase, groupId); if (mGroup == null) { throw (new IllegalArgumentException("cannot load group " + groupId + " from database")); } - mGroupNameText.setText(mGroup._id); - setTitle(getString(R.string.editGroup, mGroup._id)); + mGroupNameText.setText(mGroup.name); + setTitle(getString(R.string.editGroup, mGroup.name)); mAdapter = new ManageGroupCursorAdapter(this, null, this, mGroup); mCardList.setAdapter(mAdapter); registerForContextMenu(mCardList); @@ -114,7 +112,7 @@ public void afterTextChanged(Editable s) { saveButton.setOnClickListener(v -> { String currentGroupName = mGroupNameText.getText().toString().trim(); - if (!currentGroupName.equals(mGroup._id)) { + if (!currentGroupName.equals(mGroup.name)) { if (currentGroupName.length() == 0) { Toast.makeText(getApplicationContext(), R.string.group_name_is_empty, Toast.LENGTH_SHORT).show(); return; @@ -126,7 +124,7 @@ public void afterTextChanged(Editable s) { } mAdapter.commitToDatabase(); - if (!currentGroupName.equals(mGroup._id)) { + if (!currentGroupName.equals(mGroup.name)) { DBHelper.updateGroup(mDatabase, mGroup._id, currentGroupName); } Toast.makeText(getApplicationContext(), R.string.group_updated, Toast.LENGTH_SHORT).show(); @@ -224,7 +222,7 @@ public boolean onSupportNavigateUp() { } private boolean hasChanged() { - return mAdapter.hasChanged() || !mGroup._id.equals(mGroupNameText.getText().toString().trim()); + return mAdapter.hasChanged() || !mGroup.name.equals(mGroupNameText.getText().toString().trim()); } @Override diff --git a/app/src/main/java/protect/card_locker/ManageGroupsActivity.java b/app/src/main/java/protect/card_locker/ManageGroupsActivity.java index 07b92bd936..02cd9129ed 100644 --- a/app/src/main/java/protect/card_locker/ManageGroupsActivity.java +++ b/app/src/main/java/protect/card_locker/ManageGroupsActivity.java @@ -121,7 +121,7 @@ private void createGroup() { Toast.makeText(getApplicationContext(), R.string.group_name_is_empty, Toast.LENGTH_SHORT).show(); return; } - if (DBHelper.getGroup(mDatabase, inputString) != null) { + if (DBHelper.getGroupByName(mDatabase, inputString) != null) { Toast.makeText(getApplicationContext(), R.string.group_name_already_in_use, Toast.LENGTH_SHORT).show(); return; } @@ -140,11 +140,18 @@ private String getGroupName(View view) { return (String) groupNameTextView.getText(); } + private int getGroupId(View view) { + TextView groupNameTextView = view.findViewById(R.id.name); + String groupName = (String) groupNameTextView.getText(); + Group group = DBHelper.getGroupByName(mDatabase, groupName); + return group._id; + } + private void moveGroup(View view, boolean up) { List groups = DBHelper.getGroups(mDatabase); final String groupName = getGroupName(view); - int currentIndex = DBHelper.getGroup(mDatabase, groupName).order; + int currentIndex = DBHelper.getGroupByName(mDatabase, groupName).order; int newIndex; // Reinsert group in correct position @@ -185,12 +192,13 @@ public void onMoveUpButtonClicked(View view) { @Override public void onEditButtonClicked(View view) { Intent intent = new Intent(this, ManageGroupActivity.class); - intent.putExtra("group", getGroupName(view)); + intent.putExtra("group", getGroupId(view)); startActivity(intent); } @Override public void onDeleteButtonClicked(View view) { + final int groupId = getGroupId(view); final String groupName = getGroupName(view); AlertDialog.Builder builder = new AlertDialog.Builder(this); @@ -198,7 +206,7 @@ public void onDeleteButtonClicked(View view) { builder.setMessage(groupName); builder.setPositiveButton(getString(R.string.ok), (dialog, which) -> { - DBHelper.deleteGroup(mDatabase, groupName); + DBHelper.deleteGroup(mDatabase, groupId); updateGroupList(); // Delete may change ordering, so invalidate invalidateHomescreenActiveTab(); From cd22bcb9eca7144b36746cc4f1dbacfcc3afa83b Mon Sep 17 00:00:00 2001 From: Altonss <_> Date: Sat, 1 Oct 2022 00:52:33 +0200 Subject: [PATCH 08/41] Clean up ManageGroupsActivity.java and update tests to new group id --- .../card_locker/ManageGroupsActivity.java | 15 ++---- .../protect/card_locker/DatabaseTest.java | 42 +++++++++-------- .../protect/card_locker/ImportExportTest.java | 46 +++++++++---------- .../protect/card_locker/MainActivityTest.java | 8 ++-- 4 files changed, 55 insertions(+), 56 deletions(-) diff --git a/app/src/main/java/protect/card_locker/ManageGroupsActivity.java b/app/src/main/java/protect/card_locker/ManageGroupsActivity.java index 02cd9129ed..c1303ffbc5 100644 --- a/app/src/main/java/protect/card_locker/ManageGroupsActivity.java +++ b/app/src/main/java/protect/card_locker/ManageGroupsActivity.java @@ -140,13 +140,6 @@ private String getGroupName(View view) { return (String) groupNameTextView.getText(); } - private int getGroupId(View view) { - TextView groupNameTextView = view.findViewById(R.id.name); - String groupName = (String) groupNameTextView.getText(); - Group group = DBHelper.getGroupByName(mDatabase, groupName); - return group._id; - } - private void moveGroup(View view, boolean up) { List groups = DBHelper.getGroups(mDatabase); final String groupName = getGroupName(view); @@ -192,21 +185,23 @@ public void onMoveUpButtonClicked(View view) { @Override public void onEditButtonClicked(View view) { Intent intent = new Intent(this, ManageGroupActivity.class); - intent.putExtra("group", getGroupId(view)); + final String groupName = getGroupName(view); + Group group = DBHelper.getGroupByName(mDatabase, groupName); + intent.putExtra("group", group._id); startActivity(intent); } @Override public void onDeleteButtonClicked(View view) { - final int groupId = getGroupId(view); final String groupName = getGroupName(view); + Group group = DBHelper.getGroupByName(mDatabase, groupName); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle(R.string.deleteConfirmationGroup); builder.setMessage(groupName); builder.setPositiveButton(getString(R.string.ok), (dialog, which) -> { - DBHelper.deleteGroup(mDatabase, groupId); + DBHelper.deleteGroup(mDatabase, group._id); updateGroupList(); // Delete may change ordering, so invalidate invalidateHomescreenActiveTab(); diff --git a/app/src/test/java/protect/card_locker/DatabaseTest.java b/app/src/test/java/protect/card_locker/DatabaseTest.java index eef904d034..0e82123a05 100644 --- a/app/src/test/java/protect/card_locker/DatabaseTest.java +++ b/app/src/test/java/protect/card_locker/DatabaseTest.java @@ -291,14 +291,14 @@ public void addRemoveOneGroup() { assertTrue(result); assertEquals(1, DBHelper.getGroupCount(mDatabase)); - Group group = DBHelper.getGroup(mDatabase, "group one"); + Group group = DBHelper.getGroupByName(mDatabase, "group one"); assertNotNull(group); assertEquals("group one", group._id); - result = DBHelper.deleteGroup(mDatabase, "group one"); + result = DBHelper.deleteGroup(mDatabase, group._id); assertTrue(result); assertEquals(0, DBHelper.getGroupCount(mDatabase)); - assertNull(DBHelper.getGroup(mDatabase, "group one")); + assertNull(DBHelper.getGroup(mDatabase, group._id)); } @Test @@ -317,7 +317,7 @@ public void updateGroup() { assertEquals(1, DBHelper.getGroupCount(mDatabase)); // Add card to group - Group group = DBHelper.getGroup(mDatabase, "group one"); + Group group = DBHelper.getGroupByName(mDatabase, "group one"); List groupList1 = new ArrayList<>(); groupList1.add(group); DBHelper.setLoyaltyCardGroups(mDatabase, 1, groupList1); @@ -326,19 +326,19 @@ public void updateGroup() { List cardGroups = DBHelper.getLoyaltyCardGroups(mDatabase, (int) id); assertEquals(1, cardGroups.size()); assertEquals("group one", cardGroups.get(0)._id); - assertEquals(1, DBHelper.getGroupCardCount(mDatabase, "group one")); + assertEquals(1, DBHelper.getGroupCardCount(mDatabase, group._id)); // Rename group - result = DBHelper.updateGroup(mDatabase, "group one", "group one renamed"); + result = DBHelper.updateGroup(mDatabase, group._id, "group one renamed"); assertTrue(result); assertEquals(1, DBHelper.getGroupCount(mDatabase)); // Group one no longer exists - group = DBHelper.getGroup(mDatabase,"group one"); + group = DBHelper.getGroupByName(mDatabase,"group one"); assertNull(group); // But group one renamed does - Group group2 = DBHelper.getGroup(mDatabase, "group one renamed"); + Group group2 = DBHelper.getGroupByName(mDatabase, "group one renamed"); assertNotNull(group2); assertEquals("group one renamed", group2._id); @@ -347,14 +347,14 @@ public void updateGroup() { cardGroups = DBHelper.getLoyaltyCardGroups(mDatabase, (int) id); assertEquals(1, cardGroups.size()); assertEquals("group one renamed", cardGroups.get(0)._id); - assertEquals(1, DBHelper.getGroupCardCount(mDatabase, "group one renamed")); + assertEquals(1, DBHelper.getGroupCardCount(mDatabase, group._id)); } @Test public void updateMissingGroup() { assertEquals(0, DBHelper.getGroupCount(mDatabase)); - - boolean result = DBHelper.updateGroup(mDatabase, "group one", "new name"); + Group group1 = DBHelper.getGroupByName(mDatabase, "group one"); + boolean result = DBHelper.updateGroup(mDatabase, group1._id, "new name"); assertEquals(false, result); assertEquals(0, DBHelper.getGroupCount(mDatabase)); } @@ -375,7 +375,7 @@ public void duplicateGroupName() { assertTrue(result); assertEquals(1, DBHelper.getGroupCount(mDatabase)); - Group group = DBHelper.getGroup(mDatabase, "group one"); + Group group = DBHelper.getGroupByName(mDatabase, "group one"); assertNotNull(group); assertEquals("group one", group._id); @@ -399,16 +399,17 @@ public void updateGroupDuplicate() { assertEquals(2, DBHelper.getGroupCount(mDatabase)); // Should fail when trying to rename group two to one - boolean result3 = DBHelper.updateGroup(mDatabase, "group two", "group one"); + Group group2_init = DBHelper.getGroupByName(mDatabase, "group two"); + boolean result3 = DBHelper.updateGroup(mDatabase, group2_init._id, "group one"); assertFalse(result3); assertEquals(2, DBHelper.getGroupCount(mDatabase)); // Rename failed so both should still be the same - Group group = DBHelper.getGroup(mDatabase, "group one"); + Group group = DBHelper.getGroupByName(mDatabase, "group one"); assertNotNull(group); assertEquals("group one", group._id); - Group group2 = DBHelper.getGroup(mDatabase, "group two"); + Group group2 = DBHelper.getGroupByName(mDatabase, "group two"); assertNotNull(group2); assertEquals("group two", group2._id); } @@ -434,7 +435,8 @@ public void cardAddAndRemoveGroups() { assertEquals(2, DBHelper.getGroupCount(mDatabase)); - Group group1 = DBHelper.getGroup(mDatabase, "one"); + Group group1 = DBHelper.getGroupByName(mDatabase, "one"); + Group group2 = DBHelper.getGroupByName(mDatabase, "two"); // Card has no groups by default List cardGroups = DBHelper.getLoyaltyCardGroups(mDatabase, 1); @@ -448,15 +450,15 @@ public void cardAddAndRemoveGroups() { List cardGroups1 = DBHelper.getLoyaltyCardGroups(mDatabase, 1); assertEquals(1, cardGroups1.size()); assertEquals(cardGroups1.get(0)._id, group1._id); - assertEquals(1, DBHelper.getGroupCardCount(mDatabase, "one")); - assertEquals(0, DBHelper.getGroupCardCount(mDatabase, "two")); + assertEquals(1, DBHelper.getGroupCardCount(mDatabase, group1._id)); + assertEquals(0, DBHelper.getGroupCardCount(mDatabase, group2._id)); // Remove groups DBHelper.setLoyaltyCardGroups(mDatabase, 1, new ArrayList()); List cardGroups2 = DBHelper.getLoyaltyCardGroups(mDatabase, 1); assertEquals(0, cardGroups2.size()); - assertEquals(0, DBHelper.getGroupCardCount(mDatabase, "one")); - assertEquals(0, DBHelper.getGroupCardCount(mDatabase, "two")); + assertEquals(0, DBHelper.getGroupCardCount(mDatabase, group1._id)); + assertEquals(0, DBHelper.getGroupCardCount(mDatabase, group2._id)); } @Test diff --git a/app/src/test/java/protect/card_locker/ImportExportTest.java b/app/src/test/java/protect/card_locker/ImportExportTest.java index b8e5a20a0d..c8f9a940f8 100644 --- a/app/src/test/java/protect/card_locker/ImportExportTest.java +++ b/app/src/test/java/protect/card_locker/ImportExportTest.java @@ -298,7 +298,7 @@ private void checkGroups() { String expectedGroupName = String.format("group, \"%4d", index); - assertEquals(expectedGroupName, group._id); + assertEquals(expectedGroupName, group.name); index--; } @@ -401,7 +401,7 @@ private List groupsToGroupNames(List groups) { List groupNames = new ArrayList<>(); for (Group group : groups) { - groupNames.add(group._id); + groupNames.add(group.name); } return groupNames; @@ -418,25 +418,25 @@ public void multipleCardsExportImportWithGroups() throws IOException { List emptyGroup = new ArrayList<>(); List groupsForOne = new ArrayList<>(); - groupsForOne.add(DBHelper.getGroup(mDatabase, "group, \" 1")); + groupsForOne.add(DBHelper.getGroupByName(mDatabase, "group, \" 1")); List groupsForTwo = new ArrayList<>(); - groupsForTwo.add(DBHelper.getGroup(mDatabase, "group, \" 1")); - groupsForTwo.add(DBHelper.getGroup(mDatabase, "group, \" 2")); + groupsForTwo.add(DBHelper.getGroupByName(mDatabase, "group, \" 1")); + groupsForTwo.add(DBHelper.getGroupByName(mDatabase, "group, \" 2")); List groupsForThree = new ArrayList<>(); - groupsForThree.add(DBHelper.getGroup(mDatabase, "group, \" 1")); - groupsForThree.add(DBHelper.getGroup(mDatabase, "group, \" 2")); - groupsForThree.add(DBHelper.getGroup(mDatabase, "group, \" 3")); + groupsForThree.add(DBHelper.getGroupByName(mDatabase, "group, \" 1")); + groupsForThree.add(DBHelper.getGroupByName(mDatabase, "group, \" 2")); + groupsForThree.add(DBHelper.getGroupByName(mDatabase, "group, \" 3")); List groupsForFour = new ArrayList<>(); - groupsForFour.add(DBHelper.getGroup(mDatabase, "group, \" 1")); - groupsForFour.add(DBHelper.getGroup(mDatabase, "group, \" 2")); - groupsForFour.add(DBHelper.getGroup(mDatabase, "group, \" 3")); + groupsForFour.add(DBHelper.getGroupByName(mDatabase, "group, \" 1")); + groupsForFour.add(DBHelper.getGroupByName(mDatabase, "group, \" 2")); + groupsForFour.add(DBHelper.getGroupByName(mDatabase, "group, \" 3")); List groupsForFive = new ArrayList<>(); - groupsForFive.add(DBHelper.getGroup(mDatabase,"group, \" 1")); - groupsForFive.add(DBHelper.getGroup(mDatabase, "group, \" 3")); + groupsForFive.add(DBHelper.getGroupByName(mDatabase,"group, \" 1")); + groupsForFive.add(DBHelper.getGroupByName(mDatabase, "group, \" 3")); DBHelper.setLoyaltyCardGroups(mDatabase, 1, groupsForOne); DBHelper.setLoyaltyCardGroups(mDatabase, 2, groupsForTwo); @@ -892,7 +892,7 @@ public void exportImportV2Zip() throws FileNotFoundException { int loyaltyCardId = (int) DBHelper.insertLoyaltyCard(mDatabase, "Card 1", "Note 1", new Date(1618053234), new BigDecimal("100"), Currency.getInstance("USD"), "1234", "5432", CatimaBarcode.fromBarcode(BarcodeFormat.QR_CODE), 1, 0, null,0); loyaltyCardHashMap.put(loyaltyCardId, DBHelper.getLoyaltyCard(mDatabase, loyaltyCardId)); DBHelper.insertGroup(mDatabase, "One"); - List groups = Arrays.asList(DBHelper.getGroup(mDatabase, "One")); + List groups = Arrays.asList(DBHelper.getGroupByName(mDatabase, "One")); DBHelper.setLoyaltyCardGroups(mDatabase, loyaltyCardId, groups); loyaltyCardGroups.put(loyaltyCardId, groups); Utils.saveCardImage(activity.getApplicationContext(), launcherBitmap, loyaltyCardId, ImageLocationType.front); @@ -1017,20 +1017,20 @@ public void importV2CSV() { assertEquals(3, DBHelper.getGroupCount(mDatabase)); // Check all groups - Group healthGroup = DBHelper.getGroup(mDatabase, "Health"); + Group healthGroup = DBHelper.getGroupByName(mDatabase, "Health"); assertNotNull(healthGroup); - assertEquals(1, DBHelper.getGroupCardCount(mDatabase, "Health")); - assertEquals(Arrays.asList(4), DBHelper.getGroupCardIds(mDatabase, "Health")); + assertEquals(1, DBHelper.getGroupCardCount(mDatabase, healthGroup._id)); + assertEquals(Arrays.asList(4), DBHelper.getGroupCardIds(mDatabase, healthGroup._id)); - Group foodGroup = DBHelper.getGroup(mDatabase, "Food"); + Group foodGroup = DBHelper.getGroupByName(mDatabase, "Food"); assertNotNull(foodGroup); - assertEquals(2, DBHelper.getGroupCardCount(mDatabase, "Food")); - assertEquals(Arrays.asList(3, 5), DBHelper.getGroupCardIds(mDatabase, "Food")); + assertEquals(2, DBHelper.getGroupCardCount(mDatabase, foodGroup._id)); + assertEquals(Arrays.asList(3, 5), DBHelper.getGroupCardIds(mDatabase, foodGroup._id)); - Group fashionGroup = DBHelper.getGroup(mDatabase, "Fashion"); + Group fashionGroup = DBHelper.getGroupByName(mDatabase, "Fashion"); assertNotNull(fashionGroup); - assertEquals(2, DBHelper.getGroupCardCount(mDatabase, "Fashion")); - assertEquals(Arrays.asList(8, 6), DBHelper.getGroupCardIds(mDatabase, "Fashion")); + assertEquals(2, DBHelper.getGroupCardCount(mDatabase, fashionGroup._id)); + assertEquals(Arrays.asList(8, 6), DBHelper.getGroupCardIds(mDatabase, fashionGroup._id)); // Check all cards LoyaltyCard card1 = DBHelper.getLoyaltyCard(mDatabase, 1); diff --git a/app/src/test/java/protect/card_locker/MainActivityTest.java b/app/src/test/java/protect/card_locker/MainActivityTest.java index 4c3c12cb2c..ed4c9fbd56 100644 --- a/app/src/test/java/protect/card_locker/MainActivityTest.java +++ b/app/src/test/java/protect/card_locker/MainActivityTest.java @@ -195,7 +195,8 @@ public void testGroups() { assertEquals("Alphabetical two", groupTabs.getTabAt(2).getText().toString()); // Removing a group should also change the list - DBHelper.deleteGroup(database, "Alphabetical two"); + Group group2 = DBHelper.getGroupByName(database, "Alphabetical two"); + DBHelper.deleteGroup(database, group2._id); activityController.pause(); activityController.resume(); assertEquals(2, groupTabs.getTabCount()); @@ -203,7 +204,8 @@ public void testGroups() { assertEquals("One", groupTabs.getTabAt(1).getText().toString()); // Removing the last group should make the tabs disappear - DBHelper.deleteGroup(database, "One"); + Group group1 = DBHelper.getGroupByName(database, "One"); + DBHelper.deleteGroup(database, group1._id); activityController.pause(); activityController.resume(); assertEquals(0, groupTabs.getTabCount()); @@ -230,7 +232,7 @@ public void testFiltering() { DBHelper.insertGroup(database, "Group one"); List groups = new ArrayList<>(); - groups.add(DBHelper.getGroup(database, "Group one")); + groups.add(DBHelper.getGroupByName(database, "Group one")); DBHelper.setLoyaltyCardGroups(database, 1, groups); activityController.pause(); From 067333ea557859cf01b3615a909bd71144e8e7d9 Mon Sep 17 00:00:00 2001 From: Altonss <_> Date: Tue, 11 Oct 2022 18:45:30 +0200 Subject: [PATCH 09/41] Fix ManageGroupsActivity.java because of merge conflicts --- app/src/main/java/protect/card_locker/ManageGroupsActivity.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/protect/card_locker/ManageGroupsActivity.java b/app/src/main/java/protect/card_locker/ManageGroupsActivity.java index 0579f7d558..4649f24ec1 100644 --- a/app/src/main/java/protect/card_locker/ManageGroupsActivity.java +++ b/app/src/main/java/protect/card_locker/ManageGroupsActivity.java @@ -123,7 +123,7 @@ private void setGroupNameError(EditText input) { return; } - if (DBHelper.getGroup(mDatabase, string) != null) { + if (DBHelper.getGroupByName(mDatabase, string) != null) { input.setError(getString(R.string.group_name_already_in_use)); return; } From d9a2076cc031ea0d656e05d5ff9583fff59f4cc1 Mon Sep 17 00:00:00 2001 From: Altonss <_> Date: Tue, 11 Oct 2022 20:36:47 +0200 Subject: [PATCH 10/41] Migrate CatimaImporter to group id scheme --- .../importexport/CatimaImporter.java | 50 +++++++++++++------ 1 file changed, 36 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java b/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java index 8e6639da46..078eaec308 100644 --- a/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java +++ b/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java @@ -121,6 +121,9 @@ public void parseV2(Context context, SQLiteDatabase database, BufferedReader inp Integer part = 0; String stringPart = ""; + // store if export is already migrated to new group id scheme + boolean isGroupId = false; + try { while (true) { String tmp = input.readLine(); @@ -135,7 +138,7 @@ public void parseV2(Context context, SQLiteDatabase database, BufferedReader inp break; case 1: try { - parseV2Groups(database, stringPart); + isGroupId = parseV2Groups(database, stringPart); sectionParsed = true; } catch (FormatException e) { // We may have a multiline field, try again @@ -151,7 +154,7 @@ public void parseV2(Context context, SQLiteDatabase database, BufferedReader inp break; case 3: try { - parseV2CardGroups(database, stringPart); + parseV2CardGroups(database, stringPart, isGroupId); sectionParsed = true; } catch (FormatException e) { // We may have a multiline field, try again @@ -180,12 +183,15 @@ public void parseV2(Context context, SQLiteDatabase database, BufferedReader inp } } - public void parseV2Groups(SQLiteDatabase database, String data) throws IOException, FormatException, InterruptedException { + public boolean parseV2Groups(SQLiteDatabase database, String data) throws IOException, FormatException, InterruptedException { // Parse groups final CSVParser groupParser = new CSVParser(new StringReader(data), CSVFormat.RFC4180.builder().setHeader().build()); List records = new ArrayList<>(); + // store if export is already migrated to new group id scheme + boolean isGroupId = false; + try { for (CSVRecord record : groupParser) { records.add(record); @@ -201,8 +207,9 @@ public void parseV2Groups(SQLiteDatabase database, String data) throws IOExcepti } for (CSVRecord record : records) { - importGroup(database, record); + isGroupId = importGroup(database, record); } + return isGroupId; } public void parseV2Cards(Context context, SQLiteDatabase database, String data) throws IOException, FormatException, InterruptedException { @@ -230,7 +237,7 @@ public void parseV2Cards(Context context, SQLiteDatabase database, String data) } } - public void parseV2CardGroups(SQLiteDatabase database, String data) throws IOException, FormatException, InterruptedException { + public void parseV2CardGroups(SQLiteDatabase database, String data, boolean isGroupId) throws IOException, FormatException, InterruptedException { // Parse card group mappings final CSVParser cardGroupParser = new CSVParser(new StringReader(data), CSVFormat.RFC4180.builder().setHeader().build()); @@ -251,7 +258,7 @@ public void parseV2CardGroups(SQLiteDatabase database, String data) throws IOExc } for (CSVRecord record : records) { - importCardGroupMapping(database, record); + importCardGroupMapping(database, record, isGroupId); } } @@ -345,22 +352,37 @@ private void importLoyaltyCard(Context context, SQLiteDatabase database, CSVReco * Import a single group into the database using the given * session. */ - private void importGroup(SQLiteDatabase database, CSVRecord record) throws FormatException { - String name = CSVHelpers.extractString(DBHelper.LoyaltyCardDbGroups.NAME, record, null); - - DBHelper.insertGroup(database, name); + private boolean importGroup(SQLiteDatabase database, CSVRecord record) throws FormatException { + if (CSVHelpers.extractString(DBHelper.LoyaltyCardDbGroups.NAME, record, null) != null){ + String name = CSVHelpers.extractString(DBHelper.LoyaltyCardDbGroups.NAME, record, null); + DBHelper.insertGroup(database, name); + // return true if csv export is already migrated to the new group id scheme + return true; + } else { + String name = CSVHelpers.extractString(DBHelper.LoyaltyCardDbGroups.ID, record, null); + DBHelper.insertGroup(database, name); + return false; + } } /** * Import a single card to group mapping into the database using the given * session. */ - private void importCardGroupMapping(SQLiteDatabase database, CSVRecord record) throws FormatException { + private void importCardGroupMapping(SQLiteDatabase database, CSVRecord record, boolean isGroupId) throws FormatException { Integer cardId = CSVHelpers.extractInt(DBHelper.LoyaltyCardDbIdsGroups.cardID, record, false); - Integer groupId = CSVHelpers.extractInt(DBHelper.LoyaltyCardDbIdsGroups.groupID, record, false); - List cardGroups = DBHelper.getLoyaltyCardGroups(database, cardId); - cardGroups.add(DBHelper.getGroup(database, groupId)); + + if (isGroupId){ + Integer groupId = CSVHelpers.extractInt(DBHelper.LoyaltyCardDbIdsGroups.groupID, record, false); + cardGroups.add(DBHelper.getGroup(database, groupId)); + } + else{ + String groupName = CSVHelpers.extractString(DBHelper.LoyaltyCardDbIdsGroups.groupID, record, null); + cardGroups.add(DBHelper.getGroupByName(database, groupName)); + } + DBHelper.setLoyaltyCardGroups(database, cardId, cardGroups); } + } \ No newline at end of file From c3b5675f6e7814f7e6d65dd6ea72d128c7d0344b Mon Sep 17 00:00:00 2001 From: Altonss <_> Date: Tue, 11 Oct 2022 21:16:27 +0200 Subject: [PATCH 11/41] Migrate CatimaImporter to group id scheme --- .../card_locker/importexport/CatimaImporter.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java b/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java index 078eaec308..f384197275 100644 --- a/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java +++ b/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java @@ -353,15 +353,23 @@ private void importLoyaltyCard(Context context, SQLiteDatabase database, CSVReco * session. */ private boolean importGroup(SQLiteDatabase database, CSVRecord record) throws FormatException { - if (CSVHelpers.extractString(DBHelper.LoyaltyCardDbGroups.NAME, record, null) != null){ + boolean success = true; + try { String name = CSVHelpers.extractString(DBHelper.LoyaltyCardDbGroups.NAME, record, null); DBHelper.insertGroup(database, name); // return true if csv export is already migrated to the new group id scheme return true; - } else { + } catch (FormatException _e) { + success = false; + // This field did not exist before database version 16 + // We catch this exception so we can still import old backups + } + if (success) { String name = CSVHelpers.extractString(DBHelper.LoyaltyCardDbGroups.ID, record, null); DBHelper.insertGroup(database, name); return false; + } else { + return true; } } From 2dfe164c8830eb4aee9f03859835041c54719657 Mon Sep 17 00:00:00 2001 From: Altonss <_> Date: Tue, 11 Oct 2022 21:32:07 +0200 Subject: [PATCH 12/41] Fix updateMissingGroup test --- app/src/test/java/protect/card_locker/DatabaseTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/test/java/protect/card_locker/DatabaseTest.java b/app/src/test/java/protect/card_locker/DatabaseTest.java index 0e82123a05..8d540e6e0a 100644 --- a/app/src/test/java/protect/card_locker/DatabaseTest.java +++ b/app/src/test/java/protect/card_locker/DatabaseTest.java @@ -353,8 +353,8 @@ public void updateGroup() { @Test public void updateMissingGroup() { assertEquals(0, DBHelper.getGroupCount(mDatabase)); - Group group1 = DBHelper.getGroupByName(mDatabase, "group one"); - boolean result = DBHelper.updateGroup(mDatabase, group1._id, "new name"); + + boolean result = DBHelper.updateGroup(mDatabase, 1, "new name"); assertEquals(false, result); assertEquals(0, DBHelper.getGroupCount(mDatabase)); } From 220cf7dd959e32c07871df5969a76b41ddd3d1dc Mon Sep 17 00:00:00 2001 From: Altonss <_> Date: Tue, 11 Oct 2022 21:35:59 +0200 Subject: [PATCH 13/41] Fix addRemoveOneGroup test --- app/src/test/java/protect/card_locker/DatabaseTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/test/java/protect/card_locker/DatabaseTest.java b/app/src/test/java/protect/card_locker/DatabaseTest.java index 8d540e6e0a..a9060c56b4 100644 --- a/app/src/test/java/protect/card_locker/DatabaseTest.java +++ b/app/src/test/java/protect/card_locker/DatabaseTest.java @@ -293,7 +293,7 @@ public void addRemoveOneGroup() { Group group = DBHelper.getGroupByName(mDatabase, "group one"); assertNotNull(group); - assertEquals("group one", group._id); + assertEquals("group one", group.name); result = DBHelper.deleteGroup(mDatabase, group._id); assertTrue(result); From d6edde5b7b7ac92fa5a1b4f091b0059b776a4b78 Mon Sep 17 00:00:00 2001 From: Altonss <_> Date: Wed, 12 Oct 2022 12:13:54 +0200 Subject: [PATCH 14/41] Fix typo in DBHelper migration to v16 database --- app/src/main/java/protect/card_locker/DBHelper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/protect/card_locker/DBHelper.java b/app/src/main/java/protect/card_locker/DBHelper.java index 91d1f84f22..8da469da31 100644 --- a/app/src/main/java/protect/card_locker/DBHelper.java +++ b/app/src/main/java/protect/card_locker/DBHelper.java @@ -379,7 +379,7 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { LoyaltyCardDbIdsGroups.cardID + " ," + LoyaltyCardDbGroups.ID + " FROM tmpDbIdsGroups AS idsGroups JOIN tmpDbGroups AS groups ON " + - "tmp." + LoyaltyCardDbIdsGroups.groupID + "=" + "groups." + LoyaltyCardDbGroups.NAME); + "idsGroups." + LoyaltyCardDbIdsGroups.groupID + "=" + "groups." + LoyaltyCardDbGroups.NAME); db.execSQL("DROP TABLE tmpDbGroups"); db.execSQL("DROP TABLE tmpDbIdsGroups"); From 632b6d0f9c513b2d3654f9d086e922d1af7ac433 Mon Sep 17 00:00:00 2001 From: Altonss <_> Date: Wed, 12 Oct 2022 23:57:28 +0200 Subject: [PATCH 15/41] Fix the join for database version migration --- app/src/main/java/protect/card_locker/DBHelper.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/protect/card_locker/DBHelper.java b/app/src/main/java/protect/card_locker/DBHelper.java index 8da469da31..8c2f8c98bb 100644 --- a/app/src/main/java/protect/card_locker/DBHelper.java +++ b/app/src/main/java/protect/card_locker/DBHelper.java @@ -376,9 +376,9 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { LoyaltyCardDbIdsGroups.cardID + " ," + LoyaltyCardDbIdsGroups.groupID + ")" + " SELECT " + - LoyaltyCardDbIdsGroups.cardID + " ," + - LoyaltyCardDbGroups.ID + - " FROM tmpDbIdsGroups AS idsGroups JOIN tmpDbGroups AS groups ON " + + "idsGroups." + LoyaltyCardDbIdsGroups.cardID + " ," + + "groups." + LoyaltyCardDbGroups.ID + + " FROM tmpDbIdsGroups AS idsGroups JOIN " + LoyaltyCardDbGroups.TABLE + " AS groups ON " + "idsGroups." + LoyaltyCardDbIdsGroups.groupID + "=" + "groups." + LoyaltyCardDbGroups.NAME); db.execSQL("DROP TABLE tmpDbGroups"); From 86e160acbf1e89432aa30a138fd977b10b9071d6 Mon Sep 17 00:00:00 2001 From: Altonss <66519591+Altonss@users.noreply.github.com> Date: Thu, 13 Oct 2022 19:51:44 +0200 Subject: [PATCH 16/41] Update DBHelper.java Add ID column to tmpDbGroups --- app/src/main/java/protect/card_locker/DBHelper.java | 1 + 1 file changed, 1 insertion(+) diff --git a/app/src/main/java/protect/card_locker/DBHelper.java b/app/src/main/java/protect/card_locker/DBHelper.java index 8c2f8c98bb..96a591389b 100644 --- a/app/src/main/java/protect/card_locker/DBHelper.java +++ b/app/src/main/java/protect/card_locker/DBHelper.java @@ -324,6 +324,7 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.beginTransaction(); db.execSQL("CREATE TEMPORARY TABLE tmpDbGroups (" + + LoyaltyCardDbGroups.ID + " INTEGER primary key autoincrement," + LoyaltyCardDbGroups.NAME + " TEXT not null," + LoyaltyCardDbGroups.ORDER + " INTEGER DEFAULT '0' )"); From 97bf6afe24084e3b574ed5c6ce1141ac4d5a0e91 Mon Sep 17 00:00:00 2001 From: Altonss <66519591+Altonss@users.noreply.github.com> Date: Thu, 13 Oct 2022 19:56:21 +0200 Subject: [PATCH 17/41] Update DatabaseTest.java Move to group.name in DatabaseTest.java --- app/src/test/java/protect/card_locker/DatabaseTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/src/test/java/protect/card_locker/DatabaseTest.java b/app/src/test/java/protect/card_locker/DatabaseTest.java index a9060c56b4..1cb260649b 100644 --- a/app/src/test/java/protect/card_locker/DatabaseTest.java +++ b/app/src/test/java/protect/card_locker/DatabaseTest.java @@ -325,7 +325,7 @@ public void updateGroup() { // Ensure the card has one group and the group has one card List cardGroups = DBHelper.getLoyaltyCardGroups(mDatabase, (int) id); assertEquals(1, cardGroups.size()); - assertEquals("group one", cardGroups.get(0)._id); + assertEquals("group one", cardGroups.get(0).name); assertEquals(1, DBHelper.getGroupCardCount(mDatabase, group._id)); // Rename group @@ -377,7 +377,7 @@ public void duplicateGroupName() { Group group = DBHelper.getGroupByName(mDatabase, "group one"); assertNotNull(group); - assertEquals("group one", group._id); + assertEquals("group one", group.name); // Should fail on duplicate long id2 = DBHelper.insertGroup(mDatabase, "group one"); @@ -407,7 +407,7 @@ public void updateGroupDuplicate() { // Rename failed so both should still be the same Group group = DBHelper.getGroupByName(mDatabase, "group one"); assertNotNull(group); - assertEquals("group one", group._id); + assertEquals("group one", group.name); Group group2 = DBHelper.getGroupByName(mDatabase, "group two"); assertNotNull(group2); From 1117b05272150ca8b56e14d56f3bd3825db261b9 Mon Sep 17 00:00:00 2001 From: Altonss <66519591+Altonss@users.noreply.github.com> Date: Thu, 13 Oct 2022 20:05:43 +0200 Subject: [PATCH 18/41] Update DatabaseTest.java Migrate to group.name in DatabaseTest.java --- app/src/test/java/protect/card_locker/DatabaseTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/src/test/java/protect/card_locker/DatabaseTest.java b/app/src/test/java/protect/card_locker/DatabaseTest.java index 1cb260649b..ece778c69e 100644 --- a/app/src/test/java/protect/card_locker/DatabaseTest.java +++ b/app/src/test/java/protect/card_locker/DatabaseTest.java @@ -340,13 +340,13 @@ public void updateGroup() { // But group one renamed does Group group2 = DBHelper.getGroupByName(mDatabase, "group one renamed"); assertNotNull(group2); - assertEquals("group one renamed", group2._id); + assertEquals("group one renamed", group2.name); // And card is in "group one renamed" // Ensure the card has one group and the group has one card cardGroups = DBHelper.getLoyaltyCardGroups(mDatabase, (int) id); assertEquals(1, cardGroups.size()); - assertEquals("group one renamed", cardGroups.get(0)._id); + assertEquals("group one renamed", cardGroups.get(0).name); assertEquals(1, DBHelper.getGroupCardCount(mDatabase, group._id)); } @@ -411,7 +411,7 @@ public void updateGroupDuplicate() { Group group2 = DBHelper.getGroupByName(mDatabase, "group two"); assertNotNull(group2); - assertEquals("group two", group2._id); + assertEquals("group two", group2.name); } @Test From 7becd037c33f90521c3953a250ff18064a71e013 Mon Sep 17 00:00:00 2001 From: Altonss <_> Date: Thu, 13 Oct 2022 20:17:12 +0200 Subject: [PATCH 19/41] Fix DatabaseTest.java --- app/src/test/java/protect/card_locker/DatabaseTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/test/java/protect/card_locker/DatabaseTest.java b/app/src/test/java/protect/card_locker/DatabaseTest.java index ece778c69e..caa7460547 100644 --- a/app/src/test/java/protect/card_locker/DatabaseTest.java +++ b/app/src/test/java/protect/card_locker/DatabaseTest.java @@ -347,7 +347,7 @@ public void updateGroup() { cardGroups = DBHelper.getLoyaltyCardGroups(mDatabase, (int) id); assertEquals(1, cardGroups.size()); assertEquals("group one renamed", cardGroups.get(0).name); - assertEquals(1, DBHelper.getGroupCardCount(mDatabase, group._id)); + assertEquals(1, DBHelper.getGroupCardCount(mDatabase, group2._id)); } @Test From 1a760200afdadf030be3ba00ceeb6a61ae139ad9 Mon Sep 17 00:00:00 2001 From: Altonss <_> Date: Fri, 14 Oct 2022 11:38:40 +0200 Subject: [PATCH 20/41] Fix MainActivity.java : group._id was still used instead of group.name --- app/src/main/java/protect/card_locker/MainActivity.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/protect/card_locker/MainActivity.java b/app/src/main/java/protect/card_locker/MainActivity.java index a2835c7fec..a727f704a5 100644 --- a/app/src/main/java/protect/card_locker/MainActivity.java +++ b/app/src/main/java/protect/card_locker/MainActivity.java @@ -534,7 +534,7 @@ public void updateTabGroups(TabLayout groupsTabLayout) { for (Group group : newGroups) { TabLayout.Tab tab = groupsTabLayout.newTab(); - tab.setText(group._id); + tab.setText(group.name); tab.setTag(group); groupsTabLayout.addTab(tab, false); } From 99dc81bb6abc4c7bf2e67ca19e78933616d2563c Mon Sep 17 00:00:00 2001 From: Altonss <_> Date: Fri, 14 Oct 2022 11:54:17 +0200 Subject: [PATCH 21/41] Fix importGroup --- .../card_locker/importexport/CatimaImporter.java | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java b/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java index f384197275..353efc1e54 100644 --- a/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java +++ b/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java @@ -353,24 +353,18 @@ private void importLoyaltyCard(Context context, SQLiteDatabase database, CSVReco * session. */ private boolean importGroup(SQLiteDatabase database, CSVRecord record) throws FormatException { - boolean success = true; try { String name = CSVHelpers.extractString(DBHelper.LoyaltyCardDbGroups.NAME, record, null); DBHelper.insertGroup(database, name); // return true if csv export is already migrated to the new group id scheme return true; } catch (FormatException _e) { - success = false; // This field did not exist before database version 16 // We catch this exception so we can still import old backups } - if (success) { - String name = CSVHelpers.extractString(DBHelper.LoyaltyCardDbGroups.ID, record, null); - DBHelper.insertGroup(database, name); - return false; - } else { - return true; - } + String name = CSVHelpers.extractString(DBHelper.LoyaltyCardDbGroups.ID, record, null); + DBHelper.insertGroup(database, name); + return false; } /** From cbe778d9431c6cc91acbafcf8440a700c94822ec Mon Sep 17 00:00:00 2001 From: Altonss <_> Date: Fri, 14 Oct 2022 13:11:16 +0200 Subject: [PATCH 22/41] Add extra check to not insert new group with duplicate name --- app/src/main/java/protect/card_locker/DBHelper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/protect/card_locker/DBHelper.java b/app/src/main/java/protect/card_locker/DBHelper.java index 96a591389b..01d0d485fe 100644 --- a/app/src/main/java/protect/card_locker/DBHelper.java +++ b/app/src/main/java/protect/card_locker/DBHelper.java @@ -859,7 +859,7 @@ public static List getGroupCardIds(SQLiteDatabase database, final int g } public static long insertGroup(SQLiteDatabase database, final String name) { - if (name.isEmpty()) return -1; + if (name.isEmpty() || getGroupByName(database, name)!= null) return -1; ContentValues contentValues = new ContentValues(); contentValues.put(LoyaltyCardDbGroups.NAME, name); From 4d5104bb8882fe0fb094852ebc1eeedf31e03d7f Mon Sep 17 00:00:00 2001 From: Altonss <_> Date: Fri, 14 Oct 2022 15:14:59 +0200 Subject: [PATCH 23/41] Add extra check to not insert new group with duplicate name --- .../importexport/CatimaExporter.java | 2 +- .../importexport/CatimaImporter.java | 178 +++++++++++++++--- 2 files changed, 148 insertions(+), 32 deletions(-) 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 f8a0a6adfc..6831f52925 100644 --- a/app/src/main/java/protect/card_locker/importexport/CatimaExporter.java +++ b/app/src/main/java/protect/card_locker/importexport/CatimaExporter.java @@ -99,7 +99,7 @@ private void writeCSV(SQLiteDatabase database, OutputStreamWriter output) throws CSVPrinter printer = new CSVPrinter(output, CSVFormat.RFC4180); // Print the version - printer.printRecord("2"); + printer.printRecord("3"); printer.println(); diff --git a/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java b/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java index 353efc1e54..1f94a69236 100644 --- a/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java +++ b/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java @@ -92,6 +92,9 @@ public void importCSV(Context context, SQLiteDatabase database, InputStream inpu case 2: parseV2(context, database, bufferedReader); break; + case 3: + parseV3(context, database, bufferedReader); + break; default: throw new FormatException(String.format("No code to parse version %s", version)); } @@ -121,8 +124,68 @@ public void parseV2(Context context, SQLiteDatabase database, BufferedReader inp Integer part = 0; String stringPart = ""; - // store if export is already migrated to new group id scheme - boolean isGroupId = false; + try { + while (true) { + String tmp = input.readLine(); + + if (tmp == null || tmp.isEmpty()) { + boolean sectionParsed = false; + + switch (part) { + case 0: + // This is the version info, ignore + sectionParsed = true; + break; + case 1: + try { + parseV2Groups(database, stringPart); + sectionParsed = true; + } catch (FormatException e) { + // We may have a multiline field, try again + } + break; + case 2: + try { + parseV2Cards(context, database, stringPart); + sectionParsed = true; + } catch (FormatException e) { + // We may have a multiline field, try again + } + break; + case 3: + try { + parseV2CardGroups(database, stringPart); + sectionParsed = true; + } catch (FormatException e) { + // We may have a multiline field, try again + } + break; + default: + throw new FormatException("Issue parsing CSV data, too many parts for v2 parsing"); + } + + if (tmp == null) { + break; + } + + if (sectionParsed) { + part += 1; + stringPart = ""; + } else { + stringPart += tmp + "\n"; + } + } else { + stringPart += tmp + "\n"; + } + } + } catch (FormatException e) { + throw new FormatException("Issue parsing CSV data", e); + } + } + + public void parseV3(Context context, SQLiteDatabase database, BufferedReader input) throws IOException, FormatException, InterruptedException { + Integer part = 0; + String stringPart = ""; try { while (true) { @@ -138,7 +201,7 @@ public void parseV2(Context context, SQLiteDatabase database, BufferedReader inp break; case 1: try { - isGroupId = parseV2Groups(database, stringPart); + parseV3Groups(database, stringPart); sectionParsed = true; } catch (FormatException e) { // We may have a multiline field, try again @@ -154,7 +217,7 @@ public void parseV2(Context context, SQLiteDatabase database, BufferedReader inp break; case 3: try { - parseV2CardGroups(database, stringPart, isGroupId); + parseV3CardGroups(database, stringPart); sectionParsed = true; } catch (FormatException e) { // We may have a multiline field, try again @@ -183,14 +246,36 @@ public void parseV2(Context context, SQLiteDatabase database, BufferedReader inp } } - public boolean parseV2Groups(SQLiteDatabase database, String data) throws IOException, FormatException, InterruptedException { + public void parseV2Groups(SQLiteDatabase database, String data) throws IOException, FormatException, InterruptedException { // Parse groups final CSVParser groupParser = new CSVParser(new StringReader(data), CSVFormat.RFC4180.builder().setHeader().build()); List records = new ArrayList<>(); - // store if export is already migrated to new group id scheme - boolean isGroupId = false; + try { + for (CSVRecord record : groupParser) { + records.add(record); + + if (Thread.currentThread().isInterrupted()) { + throw new InterruptedException(); + } + } + } catch (IllegalArgumentException | IllegalStateException e) { + throw new FormatException("Issue parsing CSV data", e); + } finally { + groupParser.close(); + } + + for (CSVRecord record : records) { + importGroupV2(database, record); + } + } + + public void parseV3Groups(SQLiteDatabase database, String data) throws IOException, FormatException, InterruptedException { + // Parse groups + final CSVParser groupParser = new CSVParser(new StringReader(data), CSVFormat.RFC4180.builder().setHeader().build()); + + List records = new ArrayList<>(); try { for (CSVRecord record : groupParser) { @@ -207,9 +292,8 @@ public boolean parseV2Groups(SQLiteDatabase database, String data) throws IOExce } for (CSVRecord record : records) { - isGroupId = importGroup(database, record); + importGroupV3(database, record); } - return isGroupId; } public void parseV2Cards(Context context, SQLiteDatabase database, String data) throws IOException, FormatException, InterruptedException { @@ -237,7 +321,32 @@ public void parseV2Cards(Context context, SQLiteDatabase database, String data) } } - public void parseV2CardGroups(SQLiteDatabase database, String data, boolean isGroupId) throws IOException, FormatException, InterruptedException { + public void parseV2CardGroups(SQLiteDatabase database, String data) throws IOException, FormatException, InterruptedException { + // Parse card group mappings + final CSVParser cardGroupParser = new CSVParser(new StringReader(data), CSVFormat.RFC4180.builder().setHeader().build()); + + List records = new ArrayList<>(); + + try { + for (CSVRecord record : cardGroupParser) { + records.add(record); + + if (Thread.currentThread().isInterrupted()) { + throw new InterruptedException(); + } + } + } catch (IllegalArgumentException | IllegalStateException e) { + throw new FormatException("Issue parsing CSV data", e); + } finally { + cardGroupParser.close(); + } + + for (CSVRecord record : records) { + importCardGroupMappingV2(database, record); + } + } + + public void parseV3CardGroups(SQLiteDatabase database, String data) throws IOException, FormatException, InterruptedException { // Parse card group mappings final CSVParser cardGroupParser = new CSVParser(new StringReader(data), CSVFormat.RFC4180.builder().setHeader().build()); @@ -258,7 +367,7 @@ public void parseV2CardGroups(SQLiteDatabase database, String data, boolean isGr } for (CSVRecord record : records) { - importCardGroupMapping(database, record, isGroupId); + importCardGroupMappingV3(database, record); } } @@ -352,37 +461,44 @@ private void importLoyaltyCard(Context context, SQLiteDatabase database, CSVReco * Import a single group into the database using the given * session. */ - private boolean importGroup(SQLiteDatabase database, CSVRecord record) throws FormatException { - try { - String name = CSVHelpers.extractString(DBHelper.LoyaltyCardDbGroups.NAME, record, null); - DBHelper.insertGroup(database, name); - // return true if csv export is already migrated to the new group id scheme - return true; - } catch (FormatException _e) { - // This field did not exist before database version 16 - // We catch this exception so we can still import old backups - } + private void importGroupV2(SQLiteDatabase database, CSVRecord record) throws FormatException { String name = CSVHelpers.extractString(DBHelper.LoyaltyCardDbGroups.ID, record, null); DBHelper.insertGroup(database, name); - return false; + } + + /** + * Import a single group from the V3 scheme (database v16) into the database using the given + * session. + */ + private void importGroupV3(SQLiteDatabase database, CSVRecord record) throws FormatException { + String name = CSVHelpers.extractString(DBHelper.LoyaltyCardDbGroups.NAME, record, null); + DBHelper.insertGroup(database, name); } /** * Import a single card to group mapping into the database using the given * session. */ - private void importCardGroupMapping(SQLiteDatabase database, CSVRecord record, boolean isGroupId) throws FormatException { + private void importCardGroupMappingV2(SQLiteDatabase database, CSVRecord record) throws FormatException { Integer cardId = CSVHelpers.extractInt(DBHelper.LoyaltyCardDbIdsGroups.cardID, record, false); List cardGroups = DBHelper.getLoyaltyCardGroups(database, cardId); - if (isGroupId){ - Integer groupId = CSVHelpers.extractInt(DBHelper.LoyaltyCardDbIdsGroups.groupID, record, false); - cardGroups.add(DBHelper.getGroup(database, groupId)); - } - else{ - String groupName = CSVHelpers.extractString(DBHelper.LoyaltyCardDbIdsGroups.groupID, record, null); - cardGroups.add(DBHelper.getGroupByName(database, groupName)); - } + String groupName = CSVHelpers.extractString(DBHelper.LoyaltyCardDbIdsGroups.groupID, record, null); + cardGroups.add(DBHelper.getGroupByName(database, groupName)); + + DBHelper.setLoyaltyCardGroups(database, cardId, cardGroups); + } + + /** + * Import a single card to group mapping into the database using the given + * session. + */ + private void importCardGroupMappingV3(SQLiteDatabase database, CSVRecord record) throws FormatException { + Integer cardId = CSVHelpers.extractInt(DBHelper.LoyaltyCardDbIdsGroups.cardID, record, false); + List cardGroups = DBHelper.getLoyaltyCardGroups(database, cardId); + + Integer groupId = CSVHelpers.extractInt(DBHelper.LoyaltyCardDbIdsGroups.groupID, record, false); + cardGroups.add(DBHelper.getGroup(database, groupId)); DBHelper.setLoyaltyCardGroups(database, cardId, cardGroups); } From 1f8f7251c0804ff28ca8bc4fb8358e3d6c3ca07c Mon Sep 17 00:00:00 2001 From: Altonss <_> Date: Sat, 15 Oct 2022 01:42:25 +0200 Subject: [PATCH 24/41] Fix group id that was still used in GroupCursorAdapter instead of name --- app/src/main/java/protect/card_locker/GroupCursorAdapter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/protect/card_locker/GroupCursorAdapter.java b/app/src/main/java/protect/card_locker/GroupCursorAdapter.java index 96b350d08a..ed29fa89ec 100644 --- a/app/src/main/java/protect/card_locker/GroupCursorAdapter.java +++ b/app/src/main/java/protect/card_locker/GroupCursorAdapter.java @@ -48,7 +48,7 @@ public GroupCursorAdapter.GroupListItemViewHolder onCreateViewHolder(@NonNull Vi public void onBindViewHolder(GroupListItemViewHolder inputHolder, Cursor inputCursor) { Group group = Group.toGroup(inputCursor); - inputHolder.mName.setText(group._id); + inputHolder.mName.setText(group.name); int groupCardCount = DBHelper.getGroupCardCount(mDatabase, group._id); int archivedCardCount = DBHelper.getArchivedCardsCount(mDatabase, group._id); From 69dbf3b47624ca3b273c6f731e36fad4a7f0e094 Mon Sep 17 00:00:00 2001 From: Altonss <_> Date: Sat, 15 Oct 2022 02:46:02 +0200 Subject: [PATCH 25/41] Fix handling of group in importCardGroupMappingV3 --- .../importexport/CatimaImporter.java | 33 +++++++++++++------ 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java b/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java index 1f94a69236..c085786961 100644 --- a/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java +++ b/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java @@ -3,6 +3,7 @@ import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.net.Uri; +import android.util.Pair; import net.lingala.zip4j.io.inputstream.ZipInputStream; import net.lingala.zip4j.model.LocalFileHeader; @@ -23,6 +24,7 @@ import java.util.ArrayList; import java.util.Currency; import java.util.Date; +import java.util.Hashtable; import java.util.List; import protect.card_locker.CatimaBarcode; @@ -186,6 +188,7 @@ public void parseV2(Context context, SQLiteDatabase database, BufferedReader inp public void parseV3(Context context, SQLiteDatabase database, BufferedReader input) throws IOException, FormatException, InterruptedException { Integer part = 0; String stringPart = ""; + Hashtable groupsTable = null; try { while (true) { @@ -201,7 +204,7 @@ public void parseV3(Context context, SQLiteDatabase database, BufferedReader inp break; case 1: try { - parseV3Groups(database, stringPart); + groupsTable = parseV3Groups(database, stringPart); sectionParsed = true; } catch (FormatException e) { // We may have a multiline field, try again @@ -217,7 +220,7 @@ public void parseV3(Context context, SQLiteDatabase database, BufferedReader inp break; case 3: try { - parseV3CardGroups(database, stringPart); + parseV3CardGroups(database, stringPart, groupsTable); sectionParsed = true; } catch (FormatException e) { // We may have a multiline field, try again @@ -271,7 +274,7 @@ public void parseV2Groups(SQLiteDatabase database, String data) throws IOExcepti } } - public void parseV3Groups(SQLiteDatabase database, String data) throws IOException, FormatException, InterruptedException { + public Hashtable parseV3Groups(SQLiteDatabase database, String data) throws IOException, FormatException, InterruptedException { // Parse groups final CSVParser groupParser = new CSVParser(new StringReader(data), CSVFormat.RFC4180.builder().setHeader().build()); @@ -291,9 +294,15 @@ public void parseV3Groups(SQLiteDatabase database, String data) throws IOExcepti groupParser.close(); } + Hashtable groupsTable = new Hashtable(); + for (CSVRecord record : records) { - importGroupV3(database, record); + Pair group = importGroupV3(database, record); + Integer id = group.first; + String name = group.second; + groupsTable.put(id, name); } + return groupsTable; } public void parseV2Cards(Context context, SQLiteDatabase database, String data) throws IOException, FormatException, InterruptedException { @@ -346,7 +355,7 @@ public void parseV2CardGroups(SQLiteDatabase database, String data) throws IOExc } } - public void parseV3CardGroups(SQLiteDatabase database, String data) throws IOException, FormatException, InterruptedException { + public void parseV3CardGroups(SQLiteDatabase database, String data, Hashtable groupsTable) throws IOException, FormatException, InterruptedException { // Parse card group mappings final CSVParser cardGroupParser = new CSVParser(new StringReader(data), CSVFormat.RFC4180.builder().setHeader().build()); @@ -367,7 +376,7 @@ public void parseV3CardGroups(SQLiteDatabase database, String data) throws IOExc } for (CSVRecord record : records) { - importCardGroupMappingV3(database, record); + importCardGroupMappingV3(database, record, groupsTable); } } @@ -470,9 +479,12 @@ private void importGroupV2(SQLiteDatabase database, CSVRecord record) throws For * Import a single group from the V3 scheme (database v16) into the database using the given * session. */ - private void importGroupV3(SQLiteDatabase database, CSVRecord record) throws FormatException { + private Pair importGroupV3(SQLiteDatabase database, CSVRecord record) throws FormatException { + Integer id = CSVHelpers.extractInt(DBHelper.LoyaltyCardDbGroups.ID, record, false); String name = CSVHelpers.extractString(DBHelper.LoyaltyCardDbGroups.NAME, record, null); DBHelper.insertGroup(database, name); + Pair group = new Pair<>(id, name); + return group; } /** @@ -490,15 +502,16 @@ private void importCardGroupMappingV2(SQLiteDatabase database, CSVRecord record) } /** - * Import a single card to group mapping into the database using the given + * Import a single card to group mapping from V3 scheme (database v16) into the database using the given * session. */ - private void importCardGroupMappingV3(SQLiteDatabase database, CSVRecord record) throws FormatException { + private void importCardGroupMappingV3(SQLiteDatabase database, CSVRecord record, Hashtable groupsTable) throws FormatException { Integer cardId = CSVHelpers.extractInt(DBHelper.LoyaltyCardDbIdsGroups.cardID, record, false); List cardGroups = DBHelper.getLoyaltyCardGroups(database, cardId); Integer groupId = CSVHelpers.extractInt(DBHelper.LoyaltyCardDbIdsGroups.groupID, record, false); - cardGroups.add(DBHelper.getGroup(database, groupId)); + String groupName = groupsTable.get(groupId); + cardGroups.add(DBHelper.getGroupByName(database, groupName)); DBHelper.setLoyaltyCardGroups(database, cardId, cardGroups); } From ed724b81dc957e71d74cf7f2f9d4724498f97df1 Mon Sep 17 00:00:00 2001 From: Altonss <_> Date: Sat, 15 Oct 2022 03:27:32 +0200 Subject: [PATCH 26/41] Remove strangely failing test to see what happens during the unit test... --- app/src/test/java/protect/card_locker/ImportExportTest.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/src/test/java/protect/card_locker/ImportExportTest.java b/app/src/test/java/protect/card_locker/ImportExportTest.java index c8f9a940f8..7f2cefff67 100644 --- a/app/src/test/java/protect/card_locker/ImportExportTest.java +++ b/app/src/test/java/protect/card_locker/ImportExportTest.java @@ -467,7 +467,9 @@ public void multipleCardsExportImportWithGroups() throws IOException { checkGroups(); assertEquals(groupsToGroupNames(groupsForOne), groupsToGroupNames(DBHelper.getLoyaltyCardGroups(mDatabase, 1))); - assertEquals(groupsToGroupNames(groupsForTwo), groupsToGroupNames(DBHelper.getLoyaltyCardGroups(mDatabase, 2))); + System.out.println(groupsToGroupNames(groupsForTwo)); + System.out.println(groupsToGroupNames(DBHelper.getLoyaltyCardGroups(mDatabase, 2))); + // assertEquals(groupsToGroupNames(groupsForTwo), groupsToGroupNames(DBHelper.getLoyaltyCardGroups(mDatabase, 2))); assertEquals(groupsToGroupNames(groupsForThree), groupsToGroupNames(DBHelper.getLoyaltyCardGroups(mDatabase, 3))); assertEquals(groupsToGroupNames(groupsForFour), groupsToGroupNames(DBHelper.getLoyaltyCardGroups(mDatabase, 4))); assertEquals(groupsToGroupNames(groupsForFive), groupsToGroupNames(DBHelper.getLoyaltyCardGroups(mDatabase, 5))); From 7b28926826665c9970f5c8c444eaea2f2c932141 Mon Sep 17 00:00:00 2001 From: Altonss <_> Date: Sat, 15 Oct 2022 03:34:05 +0200 Subject: [PATCH 27/41] Revert "Remove strangely failing test to see what happens during the unit test..." This reverts commit ed724b81dc957e71d74cf7f2f9d4724498f97df1. --- app/src/test/java/protect/card_locker/ImportExportTest.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/app/src/test/java/protect/card_locker/ImportExportTest.java b/app/src/test/java/protect/card_locker/ImportExportTest.java index 7f2cefff67..c8f9a940f8 100644 --- a/app/src/test/java/protect/card_locker/ImportExportTest.java +++ b/app/src/test/java/protect/card_locker/ImportExportTest.java @@ -467,9 +467,7 @@ public void multipleCardsExportImportWithGroups() throws IOException { checkGroups(); assertEquals(groupsToGroupNames(groupsForOne), groupsToGroupNames(DBHelper.getLoyaltyCardGroups(mDatabase, 1))); - System.out.println(groupsToGroupNames(groupsForTwo)); - System.out.println(groupsToGroupNames(DBHelper.getLoyaltyCardGroups(mDatabase, 2))); - // assertEquals(groupsToGroupNames(groupsForTwo), groupsToGroupNames(DBHelper.getLoyaltyCardGroups(mDatabase, 2))); + assertEquals(groupsToGroupNames(groupsForTwo), groupsToGroupNames(DBHelper.getLoyaltyCardGroups(mDatabase, 2))); assertEquals(groupsToGroupNames(groupsForThree), groupsToGroupNames(DBHelper.getLoyaltyCardGroups(mDatabase, 3))); assertEquals(groupsToGroupNames(groupsForFour), groupsToGroupNames(DBHelper.getLoyaltyCardGroups(mDatabase, 4))); assertEquals(groupsToGroupNames(groupsForFive), groupsToGroupNames(DBHelper.getLoyaltyCardGroups(mDatabase, 5))); From 3ec1a47ceba80c81bbb333054638efbba2168d93 Mon Sep 17 00:00:00 2001 From: Altonss <_> Date: Sat, 15 Oct 2022 19:01:04 +0200 Subject: [PATCH 28/41] Rewrite some failing tests because of list order --- .../protect/card_locker/ImportExportTest.java | 38 ++++++++++++++++--- 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/app/src/test/java/protect/card_locker/ImportExportTest.java b/app/src/test/java/protect/card_locker/ImportExportTest.java index c8f9a940f8..35fe5151b2 100644 --- a/app/src/test/java/protect/card_locker/ImportExportTest.java +++ b/app/src/test/java/protect/card_locker/ImportExportTest.java @@ -39,6 +39,7 @@ import java.time.Duration; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.Currency; import java.util.Date; import java.util.HashMap; @@ -465,12 +466,37 @@ public void multipleCardsExportImportWithGroups() throws IOException { checkLoyaltyCards(); checkGroups(); - - assertEquals(groupsToGroupNames(groupsForOne), groupsToGroupNames(DBHelper.getLoyaltyCardGroups(mDatabase, 1))); - assertEquals(groupsToGroupNames(groupsForTwo), groupsToGroupNames(DBHelper.getLoyaltyCardGroups(mDatabase, 2))); - assertEquals(groupsToGroupNames(groupsForThree), groupsToGroupNames(DBHelper.getLoyaltyCardGroups(mDatabase, 3))); - assertEquals(groupsToGroupNames(groupsForFour), groupsToGroupNames(DBHelper.getLoyaltyCardGroups(mDatabase, 4))); - assertEquals(groupsToGroupNames(groupsForFive), groupsToGroupNames(DBHelper.getLoyaltyCardGroups(mDatabase, 5))); + // card 1 + List groupsForOneInitial = groupsToGroupNames(groupsForOne); + List groupsForOneImport = groupsToGroupNames(DBHelper.getLoyaltyCardGroups(mDatabase, 1)); + Collections.sort(groupsForOneInitial); + Collections.sort(groupsForOneImport); + assertEquals(groupsForOneInitial, groupsForOneImport); + // card 2 + List groupsForTwoInitial = groupsToGroupNames(groupsForTwo); + List groupsForTwoImport = groupsToGroupNames(DBHelper.getLoyaltyCardGroups(mDatabase, 2)); + Collections.sort(groupsForTwoInitial); + Collections.sort(groupsForTwoImport); + assertEquals(groupsForTwoInitial, groupsForTwoImport); + // card 3 + List groupsForThreeInitial = groupsToGroupNames(groupsForThree); + List groupsForThreeImport = groupsToGroupNames(DBHelper.getLoyaltyCardGroups(mDatabase, 3)); + Collections.sort(groupsForThreeInitial); + Collections.sort(groupsForThreeImport); + assertEquals(groupsForThreeInitial, groupsForThreeImport); + // card 4 + List groupsForFourInitial = groupsToGroupNames(groupsForFour); + List groupsForFourImport = groupsToGroupNames(DBHelper.getLoyaltyCardGroups(mDatabase, 4)); + Collections.sort(groupsForFourInitial); + Collections.sort(groupsForFourImport); + assertEquals(groupsForFourInitial, groupsForFourImport); + // card 5 + List groupsForFiveInitial = groupsToGroupNames(groupsForFive); + List groupsForFiveImport = groupsToGroupNames(DBHelper.getLoyaltyCardGroups(mDatabase, 5)); + Collections.sort(groupsForFiveInitial); + Collections.sort(groupsForFiveImport); + assertEquals(groupsForFiveInitial, groupsForFiveImport); + // cards 6 to 10 assertEquals(emptyGroup, DBHelper.getLoyaltyCardGroups(mDatabase, 6)); assertEquals(emptyGroup, DBHelper.getLoyaltyCardGroups(mDatabase, 7)); assertEquals(emptyGroup, DBHelper.getLoyaltyCardGroups(mDatabase, 8)); From 9831c9b35d1689fb9b1a090c9af938814293cf4e Mon Sep 17 00:00:00 2001 From: Altonss <_> Date: Sat, 15 Oct 2022 19:07:53 +0200 Subject: [PATCH 29/41] Remove not necessary order by group id --- app/src/main/java/protect/card_locker/DBHelper.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/src/main/java/protect/card_locker/DBHelper.java b/app/src/main/java/protect/card_locker/DBHelper.java index 01d0d485fe..2b9e97717b 100644 --- a/app/src/main/java/protect/card_locker/DBHelper.java +++ b/app/src/main/java/protect/card_locker/DBHelper.java @@ -586,8 +586,7 @@ public static LoyaltyCard getLoyaltyCard(SQLiteDatabase database, final int id) public static List getLoyaltyCardGroups(SQLiteDatabase database, final int id) { Cursor data = database.rawQuery("select * from " + LoyaltyCardDbGroups.TABLE + " g " + " LEFT JOIN " + LoyaltyCardDbIdsGroups.TABLE + " ig ON ig." + LoyaltyCardDbIdsGroups.groupID + " = g." + LoyaltyCardDbGroups.ID + - " where " + LoyaltyCardDbIdsGroups.cardID + "=?" + - " ORDER BY " + LoyaltyCardDbIdsGroups.groupID, withArgs(id)); + " where " + LoyaltyCardDbIdsGroups.cardID + "=?", withArgs(id)); List groups = new ArrayList<>(); From 8d0a50c4790d3646f1f8e7833335c6b795055042 Mon Sep 17 00:00:00 2001 From: Altonss <_> Date: Sat, 14 Jan 2023 21:48:42 +0100 Subject: [PATCH 30/41] Remove unused lines --- app/src/main/java/protect/card_locker/DBHelper.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/app/src/main/java/protect/card_locker/DBHelper.java b/app/src/main/java/protect/card_locker/DBHelper.java index 2b9e97717b..37c0e13904 100644 --- a/app/src/main/java/protect/card_locker/DBHelper.java +++ b/app/src/main/java/protect/card_locker/DBHelper.java @@ -874,9 +874,6 @@ public static boolean updateGroup(SQLiteDatabase database, final int groupId, fi ContentValues groupContentValues = new ContentValues(); groupContentValues.put(LoyaltyCardDbGroups.NAME, newName); - ContentValues lookupContentValues = new ContentValues(); - lookupContentValues.put(LoyaltyCardDbIdsGroups.groupID, newName); - database.beginTransaction(); try { // Update group name From 57c58160aafe64d097414bcd548d292c5be7c5df Mon Sep 17 00:00:00 2001 From: Altonss <_> Date: Sat, 14 Jan 2023 21:58:48 +0100 Subject: [PATCH 31/41] Fix version number in parseV3 error message --- .../java/protect/card_locker/importexport/CatimaImporter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java b/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java index 328cc5bb4b..8b070e818c 100644 --- a/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java +++ b/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java @@ -215,7 +215,7 @@ public void parseV3(Context context, SQLiteDatabase database, BufferedReader inp } break; default: - throw new FormatException("Issue parsing CSV data, too many parts for v2 parsing"); + throw new FormatException("Issue parsing CSV data, too many parts for v3 parsing"); } if (tmp == null) { From 132b9a9e95a8415e2ff37c790b35c5ebe6fb9b4f Mon Sep 17 00:00:00 2001 From: Altonss <_> Date: Sat, 14 Jan 2023 22:13:22 +0100 Subject: [PATCH 32/41] Improved importGroupV3 handling --- .../importexport/CatimaImporter.java | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java b/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java index 8b070e818c..babc92a315 100644 --- a/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java +++ b/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java @@ -282,14 +282,8 @@ public Hashtable parseV3Groups(SQLiteDatabase database, String d groupParser.close(); } - Hashtable groupsTable = new Hashtable(); + Hashtable groupsTable = importGroupV3(database, records); - for (CSVRecord record : records) { - Pair group = importGroupV3(database, record); - Integer id = group.first; - String name = group.second; - groupsTable.put(id, name); - } return groupsTable; } @@ -496,12 +490,15 @@ private void importGroupV2(SQLiteDatabase database, CSVRecord record) throws For * Import a single group from the V3 scheme (database v16) into the database using the given * session. */ - private Pair importGroupV3(SQLiteDatabase database, CSVRecord record) throws FormatException { - Integer id = CSVHelpers.extractInt(DBHelper.LoyaltyCardDbGroups.ID, record, false); - String name = CSVHelpers.extractString(DBHelper.LoyaltyCardDbGroups.NAME, record, null); - DBHelper.insertGroup(database, name); - Pair group = new Pair<>(id, name); - return group; + private Hashtable importGroupV3(SQLiteDatabase database, List records) throws FormatException { + Hashtable groupsTable = new Hashtable<>(); + for (CSVRecord record : records) { + Integer id = CSVHelpers.extractInt(DBHelper.LoyaltyCardDbGroups.ID, record, false); + String name = CSVHelpers.extractString(DBHelper.LoyaltyCardDbGroups.NAME, record, null); + DBHelper.insertGroup(database, name); + groupsTable.put(id, name); + } + return groupsTable; } /** From 5c26966584b57a8918c17a434c25b5dd9a2f94a6 Mon Sep 17 00:00:00 2001 From: Altonss <_> Date: Sat, 14 Jan 2023 22:44:35 +0100 Subject: [PATCH 33/41] Add tests by group id --- .../protect/card_locker/DatabaseTest.java | 67 +++++++++++++------ 1 file changed, 48 insertions(+), 19 deletions(-) diff --git a/app/src/test/java/protect/card_locker/DatabaseTest.java b/app/src/test/java/protect/card_locker/DatabaseTest.java index 407ea9c5bb..03df8e7d82 100644 --- a/app/src/test/java/protect/card_locker/DatabaseTest.java +++ b/app/src/test/java/protect/card_locker/DatabaseTest.java @@ -291,14 +291,17 @@ public void addRemoveOneGroup() { assertTrue(result); assertEquals(1, DBHelper.getGroupCount(mDatabase)); - Group group = DBHelper.getGroupByName(mDatabase, "group one"); - assertNotNull(group); - assertEquals("group one", group.name); - - result = DBHelper.deleteGroup(mDatabase, group._id); + Group groupByName = DBHelper.getGroupByName(mDatabase, "group one"); + assertNotNull(groupByName); + assertEquals("group one", groupByName.name); + Group groupByID = DBHelper.getGroup(mDatabase, groupByName._id); + assertNotNull(groupByID); + assertEquals("group one", groupByID.name); + + result = DBHelper.deleteGroup(mDatabase, groupByID._id); assertTrue(result); assertEquals(0, DBHelper.getGroupCount(mDatabase)); - assertNull(DBHelper.getGroup(mDatabase, group._id)); + assertNull(DBHelper.getGroup(mDatabase, groupByID._id)); } @Test @@ -317,7 +320,8 @@ public void updateGroup() { assertEquals(1, DBHelper.getGroupCount(mDatabase)); // Add card to group - Group group = DBHelper.getGroupByName(mDatabase, "group one"); + Group groupByName = DBHelper.getGroupByName(mDatabase, "group one"); + Group group = DBHelper.getGroup(mDatabase, groupByName._id); List groupList1 = new ArrayList<>(); groupList1.add(group); DBHelper.setLoyaltyCardGroups(mDatabase, 1, groupList1); @@ -334,20 +338,25 @@ public void updateGroup() { assertEquals(1, DBHelper.getGroupCount(mDatabase)); // Group one no longer exists - group = DBHelper.getGroupByName(mDatabase,"group one"); - assertNull(group); + Group groupByNameDeleted = DBHelper.getGroupByName(mDatabase,"group one"); + assertNull(groupByNameDeleted); + Group groupById = DBHelper.getGroup(mDatabase,group._id); + assertNotNull(groupById); // But group one renamed does - Group group2 = DBHelper.getGroupByName(mDatabase, "group one renamed"); - assertNotNull(group2); - assertEquals("group one renamed", group2.name); + Group groupRenamedByName = DBHelper.getGroupByName(mDatabase, "group one renamed"); + assertNotNull(groupRenamedByName); + assertEquals("group one renamed", groupRenamedByName.name); + Group groupRenamedById = DBHelper.getGroup(mDatabase, group._id); + assertNotNull(groupRenamedById); + assertEquals("group one renamed", groupRenamedById.name); // And card is in "group one renamed" // Ensure the card has one group and the group has one card cardGroups = DBHelper.getLoyaltyCardGroups(mDatabase, (int) id); assertEquals(1, cardGroups.size()); assertEquals("group one renamed", cardGroups.get(0).name); - assertEquals(1, DBHelper.getGroupCardCount(mDatabase, group2._id)); + assertEquals(1, DBHelper.getGroupCardCount(mDatabase, groupRenamedById._id)); } @Test @@ -375,7 +384,10 @@ public void duplicateGroupName() { assertTrue(result); assertEquals(1, DBHelper.getGroupCount(mDatabase)); - Group group = DBHelper.getGroupByName(mDatabase, "group one"); + Group groupByName = DBHelper.getGroupByName(mDatabase, "group one"); + assertNotNull(groupByName); + assertEquals("group one", groupByName.name); + Group group = DBHelper.getGroup(mDatabase, groupByName._id); assertNotNull(group); assertEquals("group one", group.name); @@ -399,17 +411,27 @@ public void updateGroupDuplicate() { assertEquals(2, DBHelper.getGroupCount(mDatabase)); // Should fail when trying to rename group two to one - Group group2_init = DBHelper.getGroupByName(mDatabase, "group two"); + Group group2_initByName = DBHelper.getGroupByName(mDatabase, "group two"); + boolean result3ByName = DBHelper.updateGroup(mDatabase, group2_initByName._id, "group one"); + assertFalse(result3ByName); + assertEquals(2, DBHelper.getGroupCount(mDatabase)); + Group group2_init = DBHelper.getGroup(mDatabase, group2_initByName._id); boolean result3 = DBHelper.updateGroup(mDatabase, group2_init._id, "group one"); assertFalse(result3); assertEquals(2, DBHelper.getGroupCount(mDatabase)); // Rename failed so both should still be the same - Group group = DBHelper.getGroupByName(mDatabase, "group one"); + Group groupByName = DBHelper.getGroupByName(mDatabase, "group one"); + assertNotNull(groupByName); + assertEquals("group one", groupByName.name); + Group group = DBHelper.getGroup(mDatabase, groupByName._id); assertNotNull(group); assertEquals("group one", group.name); - Group group2 = DBHelper.getGroupByName(mDatabase, "group two"); + Group group2ByName = DBHelper.getGroupByName(mDatabase, "group two"); + assertNotNull(group2ByName); + assertEquals("group two", group2ByName.name); + Group group2 = DBHelper.getGroup(mDatabase, group2ByName._id); assertNotNull(group2); assertEquals("group two", group2.name); } @@ -435,8 +457,10 @@ public void cardAddAndRemoveGroups() { assertEquals(2, DBHelper.getGroupCount(mDatabase)); - Group group1 = DBHelper.getGroupByName(mDatabase, "one"); - Group group2 = DBHelper.getGroupByName(mDatabase, "two"); + Group group1ByName = DBHelper.getGroupByName(mDatabase, "one"); + Group group2ByName = DBHelper.getGroupByName(mDatabase, "two"); + Group group1 = DBHelper.getGroup(mDatabase, group1ByName._id); + Group group2 = DBHelper.getGroup(mDatabase, group2ByName._id); // Card has no groups by default List cardGroups = DBHelper.getLoyaltyCardGroups(mDatabase, 1); @@ -449,15 +473,20 @@ public void cardAddAndRemoveGroups() { List cardGroups1 = DBHelper.getLoyaltyCardGroups(mDatabase, 1); assertEquals(1, cardGroups1.size()); + assertEquals(cardGroups1.get(0)._id, group1ByName._id); assertEquals(cardGroups1.get(0)._id, group1._id); + assertEquals(1, DBHelper.getGroupCardCount(mDatabase, group1ByName._id)); assertEquals(1, DBHelper.getGroupCardCount(mDatabase, group1._id)); + assertEquals(0, DBHelper.getGroupCardCount(mDatabase, group2ByName._id)); assertEquals(0, DBHelper.getGroupCardCount(mDatabase, group2._id)); // Remove groups DBHelper.setLoyaltyCardGroups(mDatabase, 1, new ArrayList()); List cardGroups2 = DBHelper.getLoyaltyCardGroups(mDatabase, 1); assertEquals(0, cardGroups2.size()); + assertEquals(0, DBHelper.getGroupCardCount(mDatabase, group1ByName._id)); assertEquals(0, DBHelper.getGroupCardCount(mDatabase, group1._id)); + assertEquals(0, DBHelper.getGroupCardCount(mDatabase, group2ByName._id)); assertEquals(0, DBHelper.getGroupCardCount(mDatabase, group2._id)); } From 4a241995382784a313fc8578b6e81db7b0f81bcd Mon Sep 17 00:00:00 2001 From: Altonss <_> Date: Sat, 14 Jan 2023 23:42:34 +0100 Subject: [PATCH 34/41] Remove useless groupName usage --- .../java/protect/card_locker/importexport/CatimaImporter.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java b/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java index babc92a315..4edf1b19df 100644 --- a/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java +++ b/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java @@ -524,8 +524,7 @@ private void importCardGroupMappingV3(SQLiteDatabase database, CSVRecord record, List cardGroups = DBHelper.getLoyaltyCardGroups(database, cardId); Integer groupId = CSVHelpers.extractInt(DBHelper.LoyaltyCardDbIdsGroups.groupID, record, false); - String groupName = groupsTable.get(groupId); - cardGroups.add(DBHelper.getGroupByName(database, groupName)); + cardGroups.add(DBHelper.getGroup(database, groupId)); DBHelper.setLoyaltyCardGroups(database, cardId, cardGroups); } From 3046d34a402829519ab54995b4fd41674a47d0ab Mon Sep 17 00:00:00 2001 From: Altonss <_> Date: Sun, 15 Jan 2023 00:15:33 +0100 Subject: [PATCH 35/41] Add group order to export and import --- .../protect/card_locker/importexport/CatimaExporter.java | 5 +++-- .../protect/card_locker/importexport/CatimaImporter.java | 6 +++++- 2 files changed, 8 insertions(+), 3 deletions(-) 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 6831f52925..ac569ae7c5 100644 --- a/app/src/main/java/protect/card_locker/importexport/CatimaExporter.java +++ b/app/src/main/java/protect/card_locker/importexport/CatimaExporter.java @@ -105,14 +105,15 @@ private void writeCSV(SQLiteDatabase database, OutputStreamWriter output) throws // Print the header for groups printer.printRecord(DBHelper.LoyaltyCardDbGroups.ID, - DBHelper.LoyaltyCardDbGroups.NAME); + DBHelper.LoyaltyCardDbGroups.NAME, + DBHelper.LoyaltyCardDbGroups.ORDER); Cursor groupCursor = DBHelper.getGroupCursor(database); while (groupCursor.moveToNext()) { Group group = Group.toGroup(groupCursor); - printer.printRecord(group._id, group.name); + printer.printRecord(group._id, group.name, group.order); if (Thread.currentThread().isInterrupted()) { throw new InterruptedException(); diff --git a/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java b/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java index 4edf1b19df..0afc17bfb0 100644 --- a/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java +++ b/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java @@ -3,7 +3,6 @@ import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.net.Uri; -import android.util.Pair; import net.lingala.zip4j.io.inputstream.ZipInputStream; import net.lingala.zip4j.model.LocalFileHeader; @@ -25,6 +24,7 @@ import java.util.Date; import java.util.Hashtable; import java.util.List; +import java.util.TreeMap; import protect.card_locker.CatimaBarcode; import protect.card_locker.DBHelper; @@ -492,12 +492,16 @@ private void importGroupV2(SQLiteDatabase database, CSVRecord record) throws For */ private Hashtable importGroupV3(SQLiteDatabase database, List records) throws FormatException { Hashtable groupsTable = new Hashtable<>(); + TreeMap sortedGroups = new TreeMap(); for (CSVRecord record : records) { Integer id = CSVHelpers.extractInt(DBHelper.LoyaltyCardDbGroups.ID, record, false); String name = CSVHelpers.extractString(DBHelper.LoyaltyCardDbGroups.NAME, record, null); + Integer order = CSVHelpers.extractInt(DBHelper.LoyaltyCardDbGroups.ORDER, record, false); DBHelper.insertGroup(database, name); + sortedGroups.put(order, DBHelper.getGroupByName(database, name)); groupsTable.put(id, name); } + DBHelper.reorderGroups(database, List.copyOf(sortedGroups.values())); return groupsTable; } From 01f7b93535f533c2ba799e06e4b93620cd3a710f Mon Sep 17 00:00:00 2001 From: Altonss <_> Date: Sun, 15 Jan 2023 00:26:26 +0100 Subject: [PATCH 36/41] Removed unused equals and hashCode methods in Group class --- .../main/java/protect/card_locker/Group.java | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/app/src/main/java/protect/card_locker/Group.java b/app/src/main/java/protect/card_locker/Group.java index c24b410e31..54953811c4 100644 --- a/app/src/main/java/protect/card_locker/Group.java +++ b/app/src/main/java/protect/card_locker/Group.java @@ -22,22 +22,4 @@ public static Group toGroup(Cursor cursor) { return new Group(_id, name, order); } - - @Override - public boolean equals(@Nullable Object obj) { - if (obj == null) { - return false; - } - if (!(obj instanceof Group)) { - return false; - } - Group anotherGroup = (Group) obj; - return _id == anotherGroup._id && order == anotherGroup.order; - } - - @Override - public int hashCode() { - String combined = _id + "_" + order; - return combined.hashCode(); - } } From 99a2632b82f01df728bf8179570074542aa97f90 Mon Sep 17 00:00:00 2001 From: Altonss <_> Date: Sun, 15 Jan 2023 00:32:45 +0100 Subject: [PATCH 37/41] Revert back to the use of whereAttrs, withArgs --- app/src/main/java/protect/card_locker/DBHelper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/protect/card_locker/DBHelper.java b/app/src/main/java/protect/card_locker/DBHelper.java index 00029cb5c0..c04be263a6 100644 --- a/app/src/main/java/protect/card_locker/DBHelper.java +++ b/app/src/main/java/protect/card_locker/DBHelper.java @@ -930,7 +930,7 @@ public static boolean deleteGroup(SQLiteDatabase database, final int groupId) { public static int getGroupCardCount(SQLiteDatabase database, final int groupId) { return (int) DatabaseUtils.queryNumEntries(database, LoyaltyCardDbIdsGroups.TABLE, - LoyaltyCardDbIdsGroups.groupID + "=" + groupId); + whereAttrs(LoyaltyCardDbIdsGroups.groupID),withArgs(groupId)); } static private String whereAttrs(String... attrs) { From e4363c91eb9ec607a1042ba9e79b56ac1fe9f0a2 Mon Sep 17 00:00:00 2001 From: Altonss <_> Date: Sun, 15 Jan 2023 00:38:23 +0100 Subject: [PATCH 38/41] Check if the new name already exists before updating group --- app/src/main/java/protect/card_locker/DBHelper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/protect/card_locker/DBHelper.java b/app/src/main/java/protect/card_locker/DBHelper.java index c04be263a6..7f9a0cc37c 100644 --- a/app/src/main/java/protect/card_locker/DBHelper.java +++ b/app/src/main/java/protect/card_locker/DBHelper.java @@ -876,7 +876,7 @@ public static long insertGroup(SQLiteDatabase database, final String name) { } public static boolean updateGroup(SQLiteDatabase database, final int groupId, final String newName) { - if (newName.isEmpty()) return false; + if (newName.isEmpty() || getGroupByName(database, newName)!= null) return false; boolean success = false; From 5aba54f1052ebb77ed11b40c5d4f5adeadf1370e Mon Sep 17 00:00:00 2001 From: Altonss <_> Date: Sun, 15 Jan 2023 00:47:22 +0100 Subject: [PATCH 39/41] Fix alphabetical ordering of groups --- app/src/main/java/protect/card_locker/DBHelper.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/protect/card_locker/DBHelper.java b/app/src/main/java/protect/card_locker/DBHelper.java index 7f9a0cc37c..2b35efa11e 100644 --- a/app/src/main/java/protect/card_locker/DBHelper.java +++ b/app/src/main/java/protect/card_locker/DBHelper.java @@ -595,7 +595,8 @@ public static LoyaltyCard getLoyaltyCard(SQLiteDatabase database, final int id) public static List getLoyaltyCardGroups(SQLiteDatabase database, final int id) { Cursor data = database.rawQuery("select * from " + LoyaltyCardDbGroups.TABLE + " g " + " LEFT JOIN " + LoyaltyCardDbIdsGroups.TABLE + " ig ON ig." + LoyaltyCardDbIdsGroups.groupID + " = g." + LoyaltyCardDbGroups.ID + - " where " + LoyaltyCardDbIdsGroups.cardID + "=?", withArgs(id)); + " where " + LoyaltyCardDbIdsGroups.cardID + "=?" + + " ORDER BY " + LoyaltyCardDbGroups.NAME, withArgs(id)); List groups = new ArrayList<>(); @@ -778,7 +779,7 @@ public static int getLoyaltyCardCount(SQLiteDatabase database) { */ public static Cursor getGroupCursor(SQLiteDatabase database) { return database.rawQuery("select * from " + LoyaltyCardDbGroups.TABLE + - " ORDER BY " + LoyaltyCardDbGroups.ORDER + " ASC," + LoyaltyCardDbGroups.ID + " ASC", null, null); + " ORDER BY " + LoyaltyCardDbGroups.ORDER + " ASC," + LoyaltyCardDbGroups.NAME + " ASC", null, null); } public static List getGroups(SQLiteDatabase database) { From a7bd6c050a3e7c0aa2792bec1eb6ad40d7bdaa13 Mon Sep 17 00:00:00 2001 From: Altonss <_> Date: Wed, 18 Jan 2023 22:14:45 +0100 Subject: [PATCH 40/41] Move addGroup to id --- .../card_locker/LoyaltyCardEditActivity.java | 6 +++--- .../java/protect/card_locker/MainActivity.java | 15 ++++++++------- .../java/protect/card_locker/ScanActivity.java | 8 ++++---- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java b/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java index 3ba237bba8..046d61c503 100644 --- a/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java +++ b/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java @@ -150,7 +150,7 @@ public class LoyaltyCardEditActivity extends CatimaAppCompatActivity { String cardId; String barcodeId; String barcodeType; - String addGroup; + int addGroup; Uri importLoyaltyCardUri = null; @@ -237,7 +237,7 @@ private void extractIntentFields(Intent intent) { 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; + addGroup = b != null ? b.getInt(BUNDLE_ADDGROUP) : null; importLoyaltyCardUri = intent.getData(); @@ -845,7 +845,7 @@ public void onResume() { chip.setText(group.name); chip.setTag(group); - if (group.name.equals(addGroup)) { + if (group._id == addGroup) { chip.setChecked(true); } else { chip.setChecked(false); diff --git a/app/src/main/java/protect/card_locker/MainActivity.java b/app/src/main/java/protect/card_locker/MainActivity.java index a635c7dbb0..0dd1e60797 100644 --- a/app/src/main/java/protect/card_locker/MainActivity.java +++ b/app/src/main/java/protect/card_locker/MainActivity.java @@ -347,8 +347,8 @@ public void onClick(DialogInterface dialog, int whichButton) { BarcodeValues barcodeValues = Utils.parseSetBarcodeActivityResult(Utils.BARCODE_SCAN, result.getResultCode(), intent, this); Bundle inputBundle = intent.getExtras(); - String group = inputBundle != null ? inputBundle.getString(LoyaltyCardEditActivity.BUNDLE_ADDGROUP) : null; - processBarcodeValues(barcodeValues, group); + int groupId = inputBundle != null ? inputBundle.getInt(LoyaltyCardEditActivity.BUNDLE_ADDGROUP) : null; + processBarcodeValues(barcodeValues, groupId); }); mSettingsLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> { @@ -414,8 +414,9 @@ protected void onResume() { addButton.setOnClickListener(v -> { Intent intent = new Intent(getApplicationContext(), ScanActivity.class); Bundle bundle = new Bundle(); - if (selectedTab != 0) { - bundle.putString(LoyaltyCardEditActivity.BUNDLE_ADDGROUP, groupsTabLayout.getTabAt(selectedTab).getText().toString()); + Group group = (Group) groupsTabLayout.getTabAt(selectedTab).getTag(); + if (group != null) { + bundle.putInt(LoyaltyCardEditActivity.BUNDLE_ADDGROUP, group._id); } intent.putExtras(bundle); mBarcodeScannerLauncher.launch(intent); @@ -500,7 +501,7 @@ private void updateLoyaltyCardList(boolean updateCount) { } } - private void processBarcodeValues(BarcodeValues barcodeValues, String group) { + private void processBarcodeValues(BarcodeValues barcodeValues, Integer groupId) { if (barcodeValues.isEmpty()) { throw new IllegalArgumentException("barcodesValues may not be empty"); } @@ -509,8 +510,8 @@ private void processBarcodeValues(BarcodeValues barcodeValues, String group) { Bundle newBundle = new Bundle(); newBundle.putString(LoyaltyCardEditActivity.BUNDLE_BARCODETYPE, barcodeValues.format()); newBundle.putString(LoyaltyCardEditActivity.BUNDLE_CARDID, barcodeValues.content()); - if (group != null) { - newBundle.putString(LoyaltyCardEditActivity.BUNDLE_ADDGROUP, group); + if (DBHelper.getGroup(mDatabase, groupId) != null) { + newBundle.putInt(LoyaltyCardEditActivity.BUNDLE_ADDGROUP, groupId); } newIntent.putExtras(newBundle); startActivity(newIntent); diff --git a/app/src/main/java/protect/card_locker/ScanActivity.java b/app/src/main/java/protect/card_locker/ScanActivity.java index 13119515e6..186896bf2c 100644 --- a/app/src/main/java/protect/card_locker/ScanActivity.java +++ b/app/src/main/java/protect/card_locker/ScanActivity.java @@ -53,7 +53,7 @@ public class ScanActivity extends CatimaAppCompatActivity { private DecoratedBarcodeView barcodeScannerView; private String cardId; - private String addGroup; + private Integer addGroup; private boolean torch = false; private ActivityResultLauncher manualAddLauncher; @@ -63,7 +63,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; - addGroup = b != null ? b.getString(LoyaltyCardEditActivity.BUNDLE_ADDGROUP) : null; + addGroup = b != null ? b.getInt(LoyaltyCardEditActivity.BUNDLE_ADDGROUP) : null; Log.d(TAG, "Scan activity: id=" + cardId); } @@ -107,7 +107,7 @@ public void barcodeResult(BarcodeResult result) { scanResultBundle.putString(BarcodeSelectorActivity.BARCODE_CONTENTS, result.getText()); scanResultBundle.putString(BarcodeSelectorActivity.BARCODE_FORMAT, result.getBarcodeFormat().name()); if (addGroup != null) { - scanResultBundle.putString(LoyaltyCardEditActivity.BUNDLE_ADDGROUP, addGroup); + scanResultBundle.putInt(LoyaltyCardEditActivity.BUNDLE_ADDGROUP, addGroup); } scanResult.putExtras(scanResultBundle); ScanActivity.this.setResult(RESULT_OK, scanResult); @@ -204,7 +204,7 @@ private void handleActivityResult(int requestCode, int resultCode, Intent intent manualResultBundle.putString(BarcodeSelectorActivity.BARCODE_CONTENTS, barcodeValues.content()); manualResultBundle.putString(BarcodeSelectorActivity.BARCODE_FORMAT, barcodeValues.format()); if (addGroup != null) { - manualResultBundle.putString(LoyaltyCardEditActivity.BUNDLE_ADDGROUP, addGroup); + manualResultBundle.putInt(LoyaltyCardEditActivity.BUNDLE_ADDGROUP, addGroup); } manualResult.putExtras(manualResultBundle); ScanActivity.this.setResult(RESULT_OK, manualResult); From 8b6030c5d243dd4803a60edbbea221b15898e912 Mon Sep 17 00:00:00 2001 From: Altonss <_> Date: Wed, 18 Jan 2023 22:23:00 +0100 Subject: [PATCH 41/41] Move addGroup to id --- app/src/main/java/protect/card_locker/MainActivity.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/protect/card_locker/MainActivity.java b/app/src/main/java/protect/card_locker/MainActivity.java index 0dd1e60797..d7bd55c386 100644 --- a/app/src/main/java/protect/card_locker/MainActivity.java +++ b/app/src/main/java/protect/card_locker/MainActivity.java @@ -510,7 +510,7 @@ private void processBarcodeValues(BarcodeValues barcodeValues, Integer groupId) Bundle newBundle = new Bundle(); newBundle.putString(LoyaltyCardEditActivity.BUNDLE_BARCODETYPE, barcodeValues.format()); newBundle.putString(LoyaltyCardEditActivity.BUNDLE_CARDID, barcodeValues.content()); - if (DBHelper.getGroup(mDatabase, groupId) != null) { + if (groupId != null) { newBundle.putInt(LoyaltyCardEditActivity.BUNDLE_ADDGROUP, groupId); } newIntent.putExtras(newBundle);