diff --git a/app/src/main/java/protect/card_locker/DBHelper.java b/app/src/main/java/protect/card_locker/DBHelper.java index 53ec2ea5ca..ee3dfe899e 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 = 16; + public static final int DATABASE_VERSION = 17; 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"; } @@ -87,7 +88,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 unique not null," + LoyaltyCardDbGroups.ORDER + " INTEGER DEFAULT '0')"); // create table for cards @@ -112,7 +114,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 @@ -321,6 +323,78 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("ALTER TABLE " + LoyaltyCardDbIds.TABLE + " ADD COLUMN " + LoyaltyCardDbIds.VALID_FROM + " INTEGER"); } + + if (oldVersion < 17 && newVersion >= 17) { + // 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.ID + " INTEGER primary key autoincrement," + + 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 " + + "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"); + db.execSQL("DROP TABLE tmpDbIdsGroups"); + + db.setTransactionSuccessful(); + db.endTransaction(); + } } private static ContentValues generateFTSContentValues(final int id, final String store, final String note) { @@ -534,7 +608,7 @@ public static List getLoyaltyCardGroups(SQLiteDatabase database, final in 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)); + " ORDER BY " + LoyaltyCardDbGroups.NAME, withArgs(id)); List groups = new ArrayList<>(); @@ -602,14 +676,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(); @@ -717,7 +791,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.NAME + " ASC", null, null); } public static List getGroups(SQLiteDatabase database) { @@ -747,16 +821,29 @@ 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)); + whereAttrs(LoyaltyCardDbGroups.ID), withArgs(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); + whereAttrs(LoyaltyCardDbGroups.ID), withArgs(groupId), null, null, null); + + Group group = null; + if (data.getCount() == 1) { + data.moveToFirst(); + group = Group.toGroup(data); + } + data.close(); + + 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) { @@ -772,9 +859,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); + whereAttrs(LoyaltyCardDbIdsGroups.groupID), withArgs(groupId), null, null, null); List cardIds = new ArrayList<>(); if (!data.moveToFirst()) { @@ -793,36 +880,27 @@ public static List getGroupCardIds(SQLiteDatabase database, final Strin } 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.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) { - if (newName.isEmpty()) return false; + public static boolean updateGroup(SQLiteDatabase database, final int groupId, final String newName) { + if (newName.isEmpty() || getGroupByName(database, newName)!= null) return false; boolean success = false; ContentValues groupContentValues = new ContentValues(); - groupContentValues.put(LoyaltyCardDbGroups.ID, newName); - - ContentValues lookupContentValues = new ContentValues(); - lookupContentValues.put(LoyaltyCardDbIdsGroups.groupID, newName); + groupContentValues.put(LoyaltyCardDbGroups.NAME, newName); database.beginTransaction(); 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)); + whereAttrs(LoyaltyCardDbGroups.ID), withArgs(groupId)); if (groupsChanged == 1) { database.setTransactionSuccessful(); @@ -836,20 +914,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)); + whereAttrs(LoyaltyCardDbGroups.ID), withArgs(groupId)); // And delete lookup table entries associated with this group database.delete(LoyaltyCardDbIdsGroups.TABLE, - whereAttrs(LoyaltyCardDbIdsGroups.groupID), - withArgs(groupName)); + whereAttrs(LoyaltyCardDbIdsGroups.groupID), withArgs(groupId)); if (groupsDeleted == 1) { database.setTransactionSuccessful(); @@ -865,9 +941,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)); + whereAttrs(LoyaltyCardDbIdsGroups.groupID),withArgs(groupId)); } static private String whereAttrs(String... attrs) { diff --git a/app/src/main/java/protect/card_locker/Group.java b/app/src/main/java/protect/card_locker/Group.java index 90d8da69d6..54953811c4 100644 --- a/app/src/main/java/protect/card_locker/Group.java +++ b/app/src/main/java/protect/card_locker/Group.java @@ -5,36 +5,21 @@ 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); - } - - @Override - public boolean equals(@Nullable Object obj) { - if (obj == null) { - return false; - } - if (!(obj instanceof Group)) { - return false; - } - Group anotherGroup = (Group) obj; - return _id.equals(anotherGroup._id) && order == anotherGroup.order; - } - - @Override - public int hashCode() { - String combined = _id + "_" + order; - return combined.hashCode(); + return new Group(_id, name, order); } } 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); diff --git a/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java b/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java index a2e7b30986..ffd490c0bf 100644 --- a/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java +++ b/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java @@ -157,7 +157,7 @@ public class LoyaltyCardEditActivity extends CatimaAppCompatActivity { String cardId; String barcodeId; String barcodeType; - String addGroup; + Integer addGroup; Uri importLoyaltyCardUri = null; @@ -245,7 +245,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(); @@ -820,15 +820,15 @@ public void onResume() { LayoutChipChoiceBinding chipChoiceBinding = LayoutChipChoiceBinding .inflate(LayoutInflater.from(groupsChips.getContext()), groupsChips, false); Chip chip = chipChoiceBinding.getRoot(); - chip.setText(group._id); + chip.setText(group.name); chip.setTag(group); - if (group._id.equals(addGroup)) { + if (group._id == 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/LoyaltyCardViewActivity.java b/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java index e04aaedde5..25baac37da 100644 --- a/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java +++ b/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java @@ -516,7 +516,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/MainActivity.java b/app/src/main/java/protect/card_locker/MainActivity.java index 2546d2ae10..49a7222bd9 100644 --- a/app/src/main/java/protect/card_locker/MainActivity.java +++ b/app/src/main/java/protect/card_locker/MainActivity.java @@ -343,8 +343,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 -> { @@ -413,8 +413,12 @@ 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()); + TabLayout.Tab tab = groupsTabLayout.getTabAt(selectedTab); + if (tab != null) { + Group group = (Group) groupsTabLayout.getTabAt(selectedTab).getTag(); + if (group != null) { + bundle.putInt(LoyaltyCardEditActivity.BUNDLE_ADDGROUP, group._id); + } } intent.putExtras(bundle); mBarcodeScannerLauncher.launch(intent); @@ -498,7 +502,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"); } @@ -507,8 +511,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 (groupId != null) { + newBundle.putInt(LoyaltyCardEditActivity.BUNDLE_ADDGROUP, groupId); } newIntent.putExtras(newBundle); startActivity(newIntent); @@ -579,7 +583,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); } diff --git a/app/src/main/java/protect/card_locker/ManageGroupActivity.java b/app/src/main/java/protect/card_locker/ManageGroupActivity.java index a161bf922a..13beee2c56 100644 --- a/app/src/main/java/protect/card_locker/ManageGroupActivity.java +++ b/app/src/main/java/protect/card_locker/ManageGroupActivity.java @@ -78,8 +78,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 { @@ -90,17 +90,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/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(); } diff --git a/app/src/main/java/protect/card_locker/ManageGroupsActivity.java b/app/src/main/java/protect/card_locker/ManageGroupsActivity.java index 592c670062..5c20b24cf5 100644 --- a/app/src/main/java/protect/card_locker/ManageGroupsActivity.java +++ b/app/src/main/java/protect/card_locker/ManageGroupsActivity.java @@ -120,7 +120,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; } @@ -184,7 +184,7 @@ 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 @@ -225,20 +225,23 @@ public void onMoveUpButtonClicked(View view) { @Override public void onEditButtonClicked(View view) { Intent intent = new Intent(this, ManageGroupActivity.class); - intent.putExtra("group", getGroupName(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 String groupName = getGroupName(view); + Group group = DBHelper.getGroupByName(mDatabase, groupName); AlertDialog.Builder builder = new MaterialAlertDialogBuilder(this); builder.setTitle(R.string.deleteConfirmationGroup); builder.setMessage(groupName); builder.setPositiveButton(getString(R.string.ok), (dialog, which) -> { - DBHelper.deleteGroup(mDatabase, groupName); + DBHelper.deleteGroup(mDatabase, group._id); updateGroupList(); // Delete may change ordering, so invalidate invalidateHomescreenActiveTab(); diff --git a/app/src/main/java/protect/card_locker/ScanActivity.java b/app/src/main/java/protect/card_locker/ScanActivity.java index 4e6f17762e..f4b6eacacf 100644 --- a/app/src/main/java/protect/card_locker/ScanActivity.java +++ b/app/src/main/java/protect/card_locker/ScanActivity.java @@ -59,7 +59,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; @@ -69,7 +69,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); } @@ -110,7 +110,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); @@ -209,7 +209,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); diff --git a/app/src/main/java/protect/card_locker/importexport/CatimaExporter.java b/app/src/main/java/protect/card_locker/importexport/CatimaExporter.java index 263782d8df..bcc02f267b 100644 --- a/app/src/main/java/protect/card_locker/importexport/CatimaExporter.java +++ b/app/src/main/java/protect/card_locker/importexport/CatimaExporter.java @@ -99,19 +99,21 @@ 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(); // Print the header for groups - printer.printRecord(DBHelper.LoyaltyCardDbGroups.ID); + printer.printRecord(DBHelper.LoyaltyCardDbGroups.ID, + DBHelper.LoyaltyCardDbGroups.NAME, + DBHelper.LoyaltyCardDbGroups.ORDER); Cursor groupCursor = DBHelper.getGroupCursor(database); while (groupCursor.moveToNext()) { Group group = Group.toGroup(groupCursor); - printer.printRecord(group._id); + 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 dbd35162c3..3b184d13ce 100644 --- a/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java +++ b/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java @@ -22,7 +22,9 @@ import java.util.ArrayList; import java.util.Currency; 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; @@ -82,6 +84,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)); } @@ -123,7 +128,7 @@ public void parseV2(Context context, SQLiteDatabase database, BufferedReader inp break; case 1: try { - parseV2Groups(database, stringPart.toString()); + parseV2Groups(database, stringPart.toString()); sectionParsed = true; } catch (FormatException e) { // We may have a multiline field, try again @@ -168,6 +173,70 @@ public void parseV2(Context context, SQLiteDatabase database, BufferedReader inp } } + public void parseV3(Context context, SQLiteDatabase database, BufferedReader input) throws IOException, FormatException, InterruptedException { + int part = 0; + StringBuilder stringPart = new StringBuilder(); + Hashtable groupsTable = null; + + 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 { + groupsTable = parseV3Groups(database, stringPart.toString()); + sectionParsed = true; + } catch (FormatException e) { + // We may have a multiline field, try again + } + break; + case 2: + try { + parseV2Cards(context, database, stringPart.toString()); + sectionParsed = true; + } catch (FormatException e) { + // We may have a multiline field, try again + } + break; + case 3: + try { + parseV3CardGroups(database, stringPart.toString(), groupsTable); + 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 v3 parsing"); + } + + if (tmp == null) { + break; + } + + if (sectionParsed) { + part += 1; + stringPart = new StringBuilder(); + } else { + stringPart.append(tmp).append('\n'); + } + } else { + stringPart.append(tmp).append('\n'); + } + } + } catch (FormatException e) { + throw new FormatException("Issue parsing CSV data", e); + } + } + 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()); @@ -189,10 +258,35 @@ public void parseV2Groups(SQLiteDatabase database, String data) throws IOExcepti } for (CSVRecord record : records) { - importGroup(database, record); + importGroupV2(database, record); } } + 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()); + + List records = new ArrayList<>(); + + 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(); + } + + Hashtable groupsTable = importGroupV3(database, records); + + return groupsTable; + } + public void parseV2Cards(Context context, SQLiteDatabase database, String data) throws IOException, FormatException, InterruptedException { // Parse cards final CSVParser cardParser = new CSVParser(new StringReader(data), CSVFormat.RFC4180.builder().setHeader().build()); @@ -239,7 +333,32 @@ public void parseV2CardGroups(SQLiteDatabase database, String data) throws IOExc } for (CSVRecord record : records) { - importCardGroupMapping(database, record); + importCardGroupMappingV2(database, record); + } + } + + 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()); + + 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) { + importCardGroupMappingV3(database, record, groupsTable); } } @@ -368,22 +487,57 @@ 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 id = CSVHelpers.extractString(DBHelper.LoyaltyCardDbGroups.ID, record, null); + private void importGroupV2(SQLiteDatabase database, CSVRecord record) throws FormatException { + String name = CSVHelpers.extractString(DBHelper.LoyaltyCardDbGroups.ID, record, null); + DBHelper.insertGroup(database, name); + } - DBHelper.insertGroup(database, id); + /** + * Import a single group from the V3 scheme (database v16) into the database using the given + * session. + */ + 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); + } + List sortedGroupList = new ArrayList<>(sortedGroups.values()); + DBHelper.reorderGroups(database, sortedGroupList); + return groupsTable; } /** * 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 importCardGroupMappingV2(SQLiteDatabase database, CSVRecord record) throws FormatException { Integer cardId = CSVHelpers.extractInt(DBHelper.LoyaltyCardDbIdsGroups.cardID, record, false); - String groupId = CSVHelpers.extractString(DBHelper.LoyaltyCardDbIdsGroups.groupID, record, null); + List cardGroups = DBHelper.getLoyaltyCardGroups(database, cardId); + + 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 from V3 scheme (database v16) into the database using the given + * session. + */ + 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)); + DBHelper.setLoyaltyCardGroups(database, cardId, cardGroups); } + } diff --git a/app/src/test/java/protect/card_locker/DatabaseTest.java b/app/src/test/java/protect/card_locker/DatabaseTest.java index b35f3bbad9..e5ca0d13c9 100644 --- a/app/src/test/java/protect/card_locker/DatabaseTest.java +++ b/app/src/test/java/protect/card_locker/DatabaseTest.java @@ -298,14 +298,17 @@ public void addRemoveOneGroup() { assertTrue(result); assertEquals(1, DBHelper.getGroupCount(mDatabase)); - Group group = DBHelper.getGroup(mDatabase, "group one"); - assertNotNull(group); - assertEquals("group one", group._id); - - result = DBHelper.deleteGroup(mDatabase, "group one"); + 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 one")); + assertNull(DBHelper.getGroup(mDatabase, groupByID._id)); } @Test @@ -324,7 +327,8 @@ public void updateGroup() { assertEquals(1, DBHelper.getGroupCount(mDatabase)); // Add card to group - Group group = DBHelper.getGroup(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); @@ -332,36 +336,41 @@ 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(1, DBHelper.getGroupCardCount(mDatabase, "group one")); + assertEquals("group one", cardGroups.get(0).name); + 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"); - 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.getGroup(mDatabase, "group one renamed"); - assertNotNull(group2); - assertEquals("group one renamed", group2._id); + 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)._id); - assertEquals(1, DBHelper.getGroupCardCount(mDatabase, "group one renamed")); + assertEquals("group one renamed", cardGroups.get(0).name); + assertEquals(1, DBHelper.getGroupCardCount(mDatabase, groupRenamedById._id)); } @Test public void updateMissingGroup() { assertEquals(0, DBHelper.getGroupCount(mDatabase)); - boolean result = DBHelper.updateGroup(mDatabase, "group one", "new name"); + boolean result = DBHelper.updateGroup(mDatabase, 1, "new name"); assertEquals(false, result); assertEquals(0, DBHelper.getGroupCount(mDatabase)); } @@ -382,9 +391,12 @@ public void duplicateGroupName() { assertTrue(result); assertEquals(1, DBHelper.getGroupCount(mDatabase)); - Group group = DBHelper.getGroup(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._id); + assertEquals("group one", group.name); // Should fail on duplicate long id2 = DBHelper.insertGroup(mDatabase, "group one"); @@ -406,18 +418,29 @@ 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_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.getGroup(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._id); + assertEquals("group one", group.name); - Group group2 = DBHelper.getGroup(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._id); + assertEquals("group two", group2.name); } @Test @@ -441,7 +464,10 @@ public void cardAddAndRemoveGroups() { assertEquals(2, DBHelper.getGroupCount(mDatabase)); - Group group1 = DBHelper.getGroup(mDatabase, "one"); + 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); @@ -454,16 +480,21 @@ 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, "one")); - assertEquals(0, DBHelper.getGroupCardCount(mDatabase, "two")); + 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, "one")); - assertEquals(0, DBHelper.getGroupCardCount(mDatabase, "two")); + 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)); } @Test diff --git a/app/src/test/java/protect/card_locker/ImportExportTest.java b/app/src/test/java/protect/card_locker/ImportExportTest.java index f71db4713d..fe10e59709 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; @@ -305,7 +306,7 @@ private void checkGroups() { String expectedGroupName = String.format("group, \"%4d", index); - assertEquals(expectedGroupName, group._id); + assertEquals(expectedGroupName, group.name); index--; } @@ -408,7 +409,7 @@ private List groupsToGroupNames(List groups) { List groupNames = new ArrayList<>(); for (Group group : groups) { - groupNames.add(group._id); + groupNames.add(group.name); } return groupNames; @@ -425,25 +426,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); @@ -472,12 +473,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)); @@ -905,7 +931,7 @@ public void exportImportV2Zip() throws FileNotFoundException { int loyaltyCardId = (int) DBHelper.insertLoyaltyCard(mDatabase, "Card 1", "Note 1", new Date(1601510400), 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); @@ -1031,20 +1057,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 51497f5494..0bc44bdccc 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();