From ab390e82c5d6c9924d3fac5edbc808fb491fbc8d Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Thu, 10 Jan 2019 17:38:04 +0000 Subject: [PATCH] persistent profile list order --- .../mobileledger/model/MobileLedgerProfile.java | 16 ++++++++++++++++ .../ui/activity/ProfileListActivity.java | 1 + .../java/net/ktnx/mobileledger/utils/MLDB.java | 3 ++- app/src/main/res/raw/sql_16.sql | 1 + 4 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 app/src/main/res/raw/sql_16.sql diff --git a/app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java b/app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java index 038515bf..96fa9b21 100644 --- a/app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java +++ b/app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java @@ -65,6 +65,22 @@ public final class MobileLedgerProfile { } return result; } + public static void storeProfilesOrder() { + SQLiteDatabase db = MLDB.getWritableDatabase(); + db.beginTransaction(); + try { + int orderNo = 0; + for (MobileLedgerProfile p : Data.profiles.getList()) { + db.execSQL("update profiles set order_no=? where uuid=?", + new Object[]{orderNo, p.getUuid()}); + orderNo++; + } + db.setTransactionSuccessful(); + } + finally { + db.endTransaction(); + } + } public static List createInitialProfileList() { List result = new ArrayList<>(); MobileLedgerProfile first = diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/activity/ProfileListActivity.java b/app/src/main/java/net/ktnx/mobileledger/ui/activity/ProfileListActivity.java index b61f567a..3e4f14b2 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/activity/ProfileListActivity.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/activity/ProfileListActivity.java @@ -127,6 +127,7 @@ public class ProfileListActivity extends AppCompatActivity { @NonNull RecyclerView.ViewHolder target) { Collections.swap(Data.profiles.getList(), viewHolder.getAdapterPosition(), target.getAdapterPosition()); + MobileLedgerProfile.storeProfilesOrder(); adapter.notifyItemMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition()); return true; diff --git a/app/src/main/java/net/ktnx/mobileledger/utils/MLDB.java b/app/src/main/java/net/ktnx/mobileledger/utils/MLDB.java index b863cf6e..f4cfa619 100644 --- a/app/src/main/java/net/ktnx/mobileledger/utils/MLDB.java +++ b/app/src/main/java/net/ktnx/mobileledger/utils/MLDB.java @@ -200,12 +200,13 @@ public final class MLDB { if ((helperForWriting != helperForReading) && (helperForWriting != null)) helperForWriting.close(); } + public enum DatabaseMode {READ, WRITE} } class MobileLedgerDatabase extends SQLiteOpenHelper implements AutoCloseable { public static final String DB_NAME = "mobile-ledger.db"; - public static final int LATEST_REVISION = 15; + public static final int LATEST_REVISION = 16; private final Application mContext; diff --git a/app/src/main/res/raw/sql_16.sql b/app/src/main/res/raw/sql_16.sql new file mode 100644 index 00000000..235f6559 --- /dev/null +++ b/app/src/main/res/raw/sql_16.sql @@ -0,0 +1 @@ +alter table profiles add order_no integer; \ No newline at end of file -- 2.39.2