Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to new group id #1046

Closed
wants to merge 44 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
b8823ff
First changes to DBHelper to adapt to the new group id
Altonss Sep 23, 2022
1c16bd8
Update DBHelper.java
Altonss Sep 23, 2022
a89ccff
Improve DBHelper and adapt Group.java to id changes
Sep 26, 2022
5fb95d9
Fix group issues in importer
Sep 26, 2022
2091d8c
Update ManageGroupCursorAdapter to group id
Sep 26, 2022
8559128
Update CatimaExporter and LoyaltyCardViewActivity.java
Sep 26, 2022
6ab5959
Further update to group id
Sep 30, 2022
cd22bcb
Clean up ManageGroupsActivity.java and update tests to new group id
Sep 30, 2022
dc9719d
Merge branch 'master' into patch-2
Altonss Oct 11, 2022
067333e
Fix ManageGroupsActivity.java because of merge conflicts
Oct 11, 2022
d9a2076
Migrate CatimaImporter to group id scheme
Oct 11, 2022
c3b5675
Migrate CatimaImporter to group id scheme
Oct 11, 2022
2dfe164
Fix updateMissingGroup test
Oct 11, 2022
220cf7d
Fix addRemoveOneGroup test
Oct 11, 2022
d6edde5
Fix typo in DBHelper migration to v16 database
Oct 12, 2022
632b6d0
Fix the join for database version migration
Oct 12, 2022
86e160a
Update DBHelper.java
Altonss Oct 13, 2022
97bf6af
Update DatabaseTest.java
Altonss Oct 13, 2022
1117b05
Update DatabaseTest.java
Altonss Oct 13, 2022
7becd03
Fix DatabaseTest.java
Oct 13, 2022
1a76020
Fix MainActivity.java : group._id was still used instead of group.name
Oct 14, 2022
99dc81b
Fix importGroup
Oct 14, 2022
cbe778d
Add extra check to not insert new group with duplicate name
Oct 14, 2022
4d5104b
Add extra check to not insert new group with duplicate name
Oct 14, 2022
1f8f725
Fix group id that was still used in GroupCursorAdapter instead of name
Oct 14, 2022
69dbf3b
Fix handling of group in importCardGroupMappingV3
Oct 15, 2022
ed724b8
Remove strangely failing test to see what happens during the unit tes…
Oct 15, 2022
7b28926
Revert "Remove strangely failing test to see what happens during the …
Oct 15, 2022
3ec1a47
Rewrite some failing tests because of list order
Oct 15, 2022
9831c9b
Remove not necessary order by group id
Oct 15, 2022
29a8838
Merge branch 'master' into patch-2
Altonss Oct 22, 2022
8d0a50c
Remove unused lines
Jan 14, 2023
6e76d56
Merge remote-tracking branch 'my-repo/patch-2' into patch-2
Jan 14, 2023
57c5816
Fix version number in parseV3 error message
Jan 14, 2023
132b9a9
Improved importGroupV3 handling
Jan 14, 2023
5c26966
Add tests by group id
Jan 14, 2023
4a24199
Remove useless groupName usage
Jan 14, 2023
3046d34
Add group order to export and import
Jan 14, 2023
01f7b93
Removed unused equals and hashCode methods in Group class
Jan 14, 2023
99a2632
Revert back to the use of whereAttrs, withArgs
Jan 14, 2023
e4363c9
Check if the new name already exists before updating group
Jan 14, 2023
5aba54f
Fix alphabetical ordering of groups
Jan 14, 2023
a7bd6c0
Move addGroup to id
Jan 18, 2023
8b6030c
Move addGroup to id
Jan 18, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 111 additions & 35 deletions app/src/main/java/protect/card_locker/DBHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}

Expand Down Expand Up @@ -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 unique not null," +
LoyaltyCardDbGroups.ORDER + " INTEGER DEFAULT '0')");

// create table for cards
Expand All @@ -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
Expand Down Expand Up @@ -314,6 +316,78 @@ 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.ID + " INTEGER primary key autoincrement," +
LoyaltyCardDbGroups.NAME + " TEXT not null," +
LoyaltyCardDbGroups.ORDER + " INTEGER DEFAULT '0' )");
TheLastProject marked this conversation as resolved.
Show resolved Hide resolved

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 + " ," +
Altonss marked this conversation as resolved.
Show resolved Hide resolved
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);
TheLastProject marked this conversation as resolved.
Show resolved Hide resolved

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) {
Expand Down Expand Up @@ -522,7 +596,7 @@ public static List<Group> 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<Group> groups = new ArrayList<>();

Expand Down Expand Up @@ -590,14 +664,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();
Expand Down Expand Up @@ -705,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 + " COLLATE NOCASE ASC", null, null);
" ORDER BY " + LoyaltyCardDbGroups.ORDER + " ASC," + LoyaltyCardDbGroups.NAME + " ASC", null, null);
}

public static List<Group> getGroups(SQLiteDatabase database) {
Expand Down Expand Up @@ -735,16 +809,29 @@ public static void reorderGroups(SQLiteDatabase database, final List<Group> 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(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.ID), withArgs(groupName), null, null, null);
whereAttrs(LoyaltyCardDbGroups.NAME), withArgs(groupName), null, null, null);

Group group = null;
if (data.getCount() == 1) {
Expand All @@ -760,9 +847,9 @@ public static int getGroupCount(SQLiteDatabase database) {
return (int) DatabaseUtils.queryNumEntries(database, LoyaltyCardDbGroups.TABLE);
}

public static List<Integer> getGroupCardIds(SQLiteDatabase database, final String groupName) {
public static List<Integer> 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<Integer> cardIds = new ArrayList<>();

if (!data.moveToFirst()) {
Expand All @@ -781,36 +868,27 @@ public static List<Integer> 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();
Expand All @@ -824,20 +902,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();
Expand All @@ -853,9 +929,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) {
Expand Down
29 changes: 7 additions & 22 deletions app/src/main/java/protect/card_locker/Group.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public class LoyaltyCardEditActivity extends CatimaAppCompatActivity {
String cardId;
String barcodeId;
String barcodeType;
String addGroup;
int addGroup;

Uri importLoyaltyCardUri = null;

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -842,15 +842,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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ private void showInfoDialog() {
if (loyaltyCardGroups.size() > 0) {
List<String> groupNames = new ArrayList<>();
for (Group group : loyaltyCardGroups) {
groupNames.add(group._id);
groupNames.add(group.name);
}

padSpannableString(infoText);
Expand Down
Loading