From c1606fef628bfc9d97cd0902de2c38e6f7522b99 Mon Sep 17 00:00:00 2001 From: Sylvia van Os Date: Fri, 27 Nov 2020 22:33:59 +0100 Subject: [PATCH] Manual group ordering --- app/build.gradle | 1 + .../java/protect/card_locker/DBHelper.java | 37 ++++++- .../card_locker/GroupCursorAdapter.java | 4 +- .../card_locker/ManageGroupsActivity.java | 78 ++++++++++++--- .../ic_baseline_arrow_drop_down_24.xml | 5 + .../drawable/ic_baseline_arrow_drop_up_24.xml | 5 + app/src/main/res/layout/group_layout.xml | 98 +++++++++++-------- app/src/main/res/values/strings.xml | 2 + .../protect/card_locker/ImportExportTest.java | 4 +- .../protect/card_locker/MainActivityTest.java | 6 +- 10 files changed, 175 insertions(+), 65 deletions(-) create mode 100644 app/src/main/res/drawable/ic_baseline_arrow_drop_down_24.xml create mode 100644 app/src/main/res/drawable/ic_baseline_arrow_drop_up_24.xml diff --git a/app/build.gradle b/app/build.gradle index cfcd3df69..2863bdc03 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -15,6 +15,7 @@ android { targetSdkVersion 29 versionCode 47 versionName "1.3.0" + vectorDrawables.useSupportLibrary = true } buildTypes { release { diff --git a/app/src/main/java/protect/card_locker/DBHelper.java b/app/src/main/java/protect/card_locker/DBHelper.java index 4894d8674..ce5ea4035 100644 --- a/app/src/main/java/protect/card_locker/DBHelper.java +++ b/app/src/main/java/protect/card_locker/DBHelper.java @@ -14,12 +14,13 @@ 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 = 5; + public static final int DATABASE_VERSION = 6; static class LoyaltyCardDbGroups { public static final String TABLE = "groups"; public static final String ID = "_id"; + public static final String ORDER = "orderId"; } static class LoyaltyCardDbIds @@ -52,7 +53,8 @@ public class DBHelper extends SQLiteOpenHelper { // create table for card groups db.execSQL("create table " + LoyaltyCardDbGroups.TABLE + "(" + - LoyaltyCardDbGroups.ID + " TEXT primary key not null)"); + LoyaltyCardDbGroups.ID + " TEXT primary key not null," + + LoyaltyCardDbGroups.ORDER + " INTEGER DEFAULT '0')"); // create table for cards db.execSQL("create table " + LoyaltyCardDbIds.TABLE + "(" + @@ -109,6 +111,13 @@ public class DBHelper extends SQLiteOpenHelper LoyaltyCardDbIdsGroups.groupID + " TEXT," + "primary key (" + LoyaltyCardDbIdsGroups.cardID + "," + LoyaltyCardDbIdsGroups.groupID +"))"); } + + // Upgrade from version 5 to 6 + if(oldVersion < 6 && newVersion >= 6) + { + db.execSQL("ALTER TABLE " + LoyaltyCardDbGroups.TABLE + + " ADD COLUMN " + LoyaltyCardDbGroups.ORDER + " INTEGER DEFAULT '0'"); + } } public long insertLoyaltyCard(final String store, final String note, final String cardId, @@ -376,7 +385,7 @@ public class DBHelper extends SQLiteOpenHelper SQLiteDatabase db = getReadableDatabase(); Cursor res = db.rawQuery("select * from " + LoyaltyCardDbGroups.TABLE + - " ORDER BY " + LoyaltyCardDbGroups.ID + " COLLATE NOCASE ASC", null, null); + " ORDER BY " + LoyaltyCardDbGroups.ORDER + " ASC," + LoyaltyCardDbGroups.ID + " COLLATE NOCASE ASC", null, null); return res; } @@ -398,6 +407,26 @@ public class DBHelper extends SQLiteOpenHelper return groups; } + public void reorderGroups(final List groups) + { + Integer order = 0; + + SQLiteDatabase db = getWritableDatabase(); + ContentValues contentValues; + + for (Group group : groups) + { + contentValues = new ContentValues(); + contentValues.put(LoyaltyCardDbGroups.ORDER, order); + + db.update(LoyaltyCardDbGroups.TABLE, contentValues, + LoyaltyCardDbGroups.ID + "=?", + new String[]{group._id}); + + order++; + } + } + public Group getGroup(final String groupName) { SQLiteDatabase db = getReadableDatabase(); @@ -467,6 +496,7 @@ public class DBHelper extends SQLiteOpenHelper SQLiteDatabase db = getWritableDatabase(); ContentValues contentValues = new ContentValues(); contentValues.put(LoyaltyCardDbGroups.ID, name); + contentValues.put(LoyaltyCardDbGroups.ORDER, getGroupCount()); final long newId = db.insert(LoyaltyCardDbGroups.TABLE, null, contentValues); return newId; } @@ -475,6 +505,7 @@ public class DBHelper extends SQLiteOpenHelper { ContentValues contentValues = new ContentValues(); contentValues.put(LoyaltyCardDbGroups.ID, name); + contentValues.put(LoyaltyCardDbGroups.ORDER, getGroupCount()); final long newId = db.insert(LoyaltyCardDbGroups.TABLE, null, contentValues); return (newId != -1); } diff --git a/app/src/main/java/protect/card_locker/GroupCursorAdapter.java b/app/src/main/java/protect/card_locker/GroupCursorAdapter.java index e06ea7ad6..536ac643b 100644 --- a/app/src/main/java/protect/card_locker/GroupCursorAdapter.java +++ b/app/src/main/java/protect/card_locker/GroupCursorAdapter.java @@ -37,8 +37,8 @@ class GroupCursorAdapter extends CursorAdapter public void bindView(View view, Context context, Cursor cursor) { // Find fields to populate in inflated template - TextView nameField = (TextView) view.findViewById(R.id.name); - TextView countField = (TextView) view.findViewById(R.id.cardCount); + TextView nameField = view.findViewById(R.id.name); + TextView countField = view.findViewById(R.id.cardCount); // Extract properties from cursor Group group = Group.toGroup(cursor); diff --git a/app/src/main/java/protect/card_locker/ManageGroupsActivity.java b/app/src/main/java/protect/card_locker/ManageGroupsActivity.java index 435ade395..aa162286e 100644 --- a/app/src/main/java/protect/card_locker/ManageGroupsActivity.java +++ b/app/src/main/java/protect/card_locker/ManageGroupsActivity.java @@ -15,6 +15,8 @@ import android.widget.TextView; import com.google.android.material.floatingactionbutton.FloatingActionButton; +import java.util.List; + import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; @@ -112,12 +114,16 @@ public class ManageGroupsActivity extends AppCompatActivity return super.onOptionsItemSelected(item); } + public void moveGroupUp(View view) { + moveGroup(view, true); + } + + public void moveGroupDown(View view) { + moveGroup(view, false); + } + public void editGroup(View view) { - LinearLayout parentRow = (LinearLayout) view.getParent(); - - TextView groupNameTextView = (TextView) parentRow.findViewById(R.id.name); - - final String groupName = (String) groupNameTextView.getText(); + final String groupName = getGroupname(view); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle(R.string.enter_group_name); @@ -131,8 +137,6 @@ public class ManageGroupsActivity extends AppCompatActivity public void onClick(DialogInterface dialog, int which) { db.updateGroup(groupName, input.getText().toString()); updateGroupList(); - // Rename may change ordering, so invalidate - invalidateHomescreenActiveTab(); } }); builder.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() { @@ -146,11 +150,7 @@ public class ManageGroupsActivity extends AppCompatActivity } public void deleteGroup(View view) { - LinearLayout parentRow = (LinearLayout) view.getParent(); - - TextView groupNameTextView = (TextView) parentRow.findViewById(R.id.name); - - final String groupName = (String) groupNameTextView.getText(); + final String groupName = getGroupname(view); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle(R.string.deleteConfirmationGroup); @@ -187,8 +187,6 @@ public class ManageGroupsActivity extends AppCompatActivity public void onClick(DialogInterface dialog, int which) { db.insertGroup(input.getText().toString()); updateGroupList(); - // Create may change ordering, so invalidate - invalidateHomescreenActiveTab(); } }); builder.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() { @@ -201,4 +199,56 @@ public class ManageGroupsActivity extends AppCompatActivity return dialog; } + + private String getGroupname(View view) { + LinearLayout parentRow = (LinearLayout) view.getParent().getParent(); + TextView groupNameTextView = parentRow.findViewById(R.id.name); + return (String) groupNameTextView.getText(); + } + + private void moveGroup(View view, boolean up) { + final String groupName = getGroupname(view); + + List groups = db.getGroups(); + + int currentIndex = -1; + Integer newIndex; + + // Get current index in group list + for (int i = 0; i < groups.size(); i++) { + if (groups.get(i)._id.equals(groupName)) { + currentIndex = i; + + break; + } + } + + if (currentIndex == -1) { + throw new IndexOutOfBoundsException(); + } + + // Reinsert group in correct position + if (up) { + newIndex = currentIndex - 1; + } else { + newIndex = currentIndex + 1; + } + + // Don't try to move out of bounds + if (newIndex < 0 || newIndex >= groups.size()) { + return; + } + + Group group = groups.remove(currentIndex); + groups.add(newIndex, group); + + // Update database + db.reorderGroups(groups); + + // Update UI + updateGroupList(); + + // Ordering may have changed, so invalidate + invalidateHomescreenActiveTab(); + } } \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_baseline_arrow_drop_down_24.xml b/app/src/main/res/drawable/ic_baseline_arrow_drop_down_24.xml new file mode 100644 index 000000000..3dbfedba2 --- /dev/null +++ b/app/src/main/res/drawable/ic_baseline_arrow_drop_down_24.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/drawable/ic_baseline_arrow_drop_up_24.xml b/app/src/main/res/drawable/ic_baseline_arrow_drop_up_24.xml new file mode 100644 index 000000000..ba9e5039a --- /dev/null +++ b/app/src/main/res/drawable/ic_baseline_arrow_drop_up_24.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/layout/group_layout.xml b/app/src/main/res/layout/group_layout.xml index 787cd38d4..36e7e4fb3 100644 --- a/app/src/main/res/layout/group_layout.xml +++ b/app/src/main/res/layout/group_layout.xml @@ -2,7 +2,7 @@ - - - - - - - - - + - + - + + + + + + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 6d7cf116c..7d98919a4 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -134,6 +134,8 @@ All Please confirm you want to delete this group Failed opening a file manager. Please make sure one is installed. + Move up in list + Move down in list Leave without saving Are you sure you want to leave this screen? Changed made will not be saved. Scan the barcode with your camera diff --git a/app/src/test/java/protect/card_locker/ImportExportTest.java b/app/src/test/java/protect/card_locker/ImportExportTest.java index 3d019d7c5..64c13bd80 100644 --- a/app/src/test/java/protect/card_locker/ImportExportTest.java +++ b/app/src/test/java/protect/card_locker/ImportExportTest.java @@ -202,7 +202,7 @@ public class ImportExportTest private void checkGroups() { Cursor cursor = db.getGroupCursor(); - int index = 1; + int index = db.getGroupCount(); while(cursor.moveToNext()) { @@ -212,7 +212,7 @@ public class ImportExportTest assertEquals(expectedGroupName, group._id); - index++; + index--; } cursor.close(); } diff --git a/app/src/test/java/protect/card_locker/MainActivityTest.java b/app/src/test/java/protect/card_locker/MainActivityTest.java index 0090ee359..f9a606d95 100644 --- a/app/src/test/java/protect/card_locker/MainActivityTest.java +++ b/app/src/test/java/protect/card_locker/MainActivityTest.java @@ -193,14 +193,14 @@ public class MainActivityTest assertEquals("All", groupTabs.getTabAt(0).getText().toString()); assertEquals("One", groupTabs.getTabAt(1).getText().toString()); - // Adding another group should have them sorted alphabetically + // Adding another group should have it added to the end db.insertGroup("Alphabetical two"); activityController.pause(); activityController.resume(); assertEquals(3, groupTabs.getTabCount()); assertEquals("All", groupTabs.getTabAt(0).getText().toString()); - assertEquals("Alphabetical two", groupTabs.getTabAt(1).getText().toString()); - assertEquals("One", groupTabs.getTabAt(2).getText().toString()); + assertEquals("One", groupTabs.getTabAt(1).getText().toString()); + assertEquals("Alphabetical two", groupTabs.getTabAt(2).getText().toString()); // Removing a group should also change the list db.deleteGroup("Alphabetical two");