From ffbfe16379b73aaf0bc68b4251c2acb930a19d26 Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Sat, 5 Jan 2019 08:26:15 +0000 Subject: [PATCH] async account list --- .../async/CommitAccountsTask.java | 61 ++++++++++ .../async/CommitAccountsTaskParams.java | 31 +++++ .../async/UpdateAccountsTask.java | 67 +++++++++++ .../net/ktnx/mobileledger/model/Data.java | 4 +- .../AccountSummaryFragment.java | 11 +- .../AccountSummaryViewModel.java | 112 +++++++----------- 6 files changed, 206 insertions(+), 80 deletions(-) create mode 100644 app/src/main/java/net/ktnx/mobileledger/async/CommitAccountsTask.java create mode 100644 app/src/main/java/net/ktnx/mobileledger/async/CommitAccountsTaskParams.java create mode 100644 app/src/main/java/net/ktnx/mobileledger/async/UpdateAccountsTask.java diff --git a/app/src/main/java/net/ktnx/mobileledger/async/CommitAccountsTask.java b/app/src/main/java/net/ktnx/mobileledger/async/CommitAccountsTask.java new file mode 100644 index 00000000..7fb96b9d --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/async/CommitAccountsTask.java @@ -0,0 +1,61 @@ +/* + * Copyright © 2019 Damyan Ivanov. + * This file is part of Mobile-Ledger. + * Mobile-Ledger is free software: you can distribute it and/or modify it + * under the term of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your opinion), any later version. + * + * Mobile-Ledger is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License terms for details. + * + * You should have received a copy of the GNU General Public License + * along with Mobile-Ledger. If not, see . + */ + +package net.ktnx.mobileledger.async; + +import android.database.sqlite.SQLiteDatabase; +import android.os.AsyncTask; +import android.util.Log; + +import net.ktnx.mobileledger.model.Data; +import net.ktnx.mobileledger.model.LedgerAccount; +import net.ktnx.mobileledger.utils.MLDB; + +import java.util.ArrayList; + +public class CommitAccountsTask + extends AsyncTask> { + protected ArrayList doInBackground(CommitAccountsTaskParams... params) { + Data.backgroundTaskCount.incrementAndGet(); + ArrayList newList = new ArrayList<>(); + try { + + SQLiteDatabase db = MLDB.getWritableDatabase(); + db.beginTransaction(); + try { + for (LedgerAccount acc : params[0].accountList) { + Log.d("db", String.format("Setting %s to %s", acc.getName(), + acc.isHidden() ? "hidden" : "starred")); + db.execSQL("UPDATE accounts SET hidden=? WHERE name=?", + new Object[]{acc.isHiddenToBe() ? 1 : 0, acc.getName()}); + + acc.setHidden(acc.isHiddenToBe()); + if (!params[0].showOnlyStarred || !acc.isHidden()) newList.add(acc); + } + db.setTransactionSuccessful(); + } + finally { + db.endTransaction(); + } + } + finally { + Data.backgroundTaskCount.decrementAndGet(); + } + + return newList; + } +} diff --git a/app/src/main/java/net/ktnx/mobileledger/async/CommitAccountsTaskParams.java b/app/src/main/java/net/ktnx/mobileledger/async/CommitAccountsTaskParams.java new file mode 100644 index 00000000..c3de7f20 --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/async/CommitAccountsTaskParams.java @@ -0,0 +1,31 @@ +/* + * Copyright © 2019 Damyan Ivanov. + * This file is part of Mobile-Ledger. + * Mobile-Ledger is free software: you can distribute it and/or modify it + * under the term of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your opinion), any later version. + * + * Mobile-Ledger is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License terms for details. + * + * You should have received a copy of the GNU General Public License + * along with Mobile-Ledger. If not, see . + */ + +package net.ktnx.mobileledger.async; + +import net.ktnx.mobileledger.model.LedgerAccount; + +import java.util.List; + +public class CommitAccountsTaskParams { + List accountList; + boolean showOnlyStarred; + public CommitAccountsTaskParams(List accountList, boolean showOnlyStarred) { + this.accountList = accountList; + this.showOnlyStarred = showOnlyStarred; + } +} diff --git a/app/src/main/java/net/ktnx/mobileledger/async/UpdateAccountsTask.java b/app/src/main/java/net/ktnx/mobileledger/async/UpdateAccountsTask.java new file mode 100644 index 00000000..458c1af6 --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/async/UpdateAccountsTask.java @@ -0,0 +1,67 @@ +/* + * Copyright © 2019 Damyan Ivanov. + * This file is part of Mobile-Ledger. + * Mobile-Ledger is free software: you can distribute it and/or modify it + * under the term of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your opinion), any later version. + * + * Mobile-Ledger is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License terms for details. + * + * You should have received a copy of the GNU General Public License + * along with Mobile-Ledger. If not, see . + */ + +package net.ktnx.mobileledger.async; + +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; +import android.os.AsyncTask; +import android.util.Log; + +import net.ktnx.mobileledger.model.Data; +import net.ktnx.mobileledger.model.LedgerAccount; +import net.ktnx.mobileledger.utils.MLDB; + +import java.util.ArrayList; + +public class UpdateAccountsTask extends AsyncTask> { + protected ArrayList doInBackground(Boolean[] onlyStarred) { + Data.backgroundTaskCount.incrementAndGet(); + try { + ArrayList newList = new ArrayList<>(); + + String sql = "SELECT name, hidden FROM accounts"; + if (onlyStarred[0]) sql += " WHERE hidden = 0"; + sql += " ORDER BY name"; + + SQLiteDatabase db = MLDB.getReadableDatabase(); + try (Cursor cursor = db.rawQuery(sql, null)) { + while (cursor.moveToNext()) { + LedgerAccount acc = new LedgerAccount(cursor.getString(0)); + acc.setHidden(cursor.getInt(1) == 1); + try (Cursor c2 = db.rawQuery( + "SELECT value, currency FROM account_values " + "WHERE account = ?", + new String[]{acc.getName()})) + { + while (c2.moveToNext()) { + acc.addAmount(c2.getFloat(0), c2.getString(1)); + } + } + newList.add(acc); + } + } + + Data.accounts.set(newList); + + return newList; + } + finally { + Log.d("UAT", "decrementing background task count"); + Data.backgroundTaskCount.decrementAndGet(); + } + } +} diff --git a/app/src/main/java/net/ktnx/mobileledger/model/Data.java b/app/src/main/java/net/ktnx/mobileledger/model/Data.java index 89d29c4d..e492e142 100644 --- a/app/src/main/java/net/ktnx/mobileledger/model/Data.java +++ b/app/src/main/java/net/ktnx/mobileledger/model/Data.java @@ -17,11 +17,13 @@ package net.ktnx.mobileledger.model; +import java.util.ArrayList; import java.util.List; public final class Data { public static TransactionList transactions = new TransactionList(); - public static ObservableValue> accounts = new ObservableValue<>(); + public static ObservableValue> accounts = + new ObservableValue<>(new ArrayList()); public static ObservableValue> descriptions = new ObservableValue<>(); public static ObservableAtomicInteger backgroundTaskCount = new ObservableAtomicInteger(0); } diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java b/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java index 99ce8bde..9214e794 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java @@ -42,7 +42,6 @@ import net.ktnx.mobileledger.model.LedgerAccount; import net.ktnx.mobileledger.ui.MobileLedgerListFragment; import net.ktnx.mobileledger.ui.RecyclerItemListener; import net.ktnx.mobileledger.ui.activity.MainActivity; -import net.ktnx.mobileledger.utils.MLDB; import java.lang.ref.WeakReference; import java.util.Date; @@ -112,8 +111,7 @@ public class AccountSummaryFragment extends MobileLedgerListFragment { mActivity.markDrawerItemCurrent(R.id.nav_account_summary); model = ViewModelProviders.of(this).get(AccountSummaryViewModel.class); - List accounts = model.getAccounts(this.getContext()); - modelAdapter = new AccountSummaryAdapter(accounts); + modelAdapter = new AccountSummaryAdapter(); RecyclerView root = mActivity.findViewById(R.id.account_root); root.setAdapter(modelAdapter); @@ -133,7 +131,7 @@ public class AccountSummaryFragment extends MobileLedgerListFragment { modelAdapter.selectItem(position); } else { - List accounts = model.getAccounts(mActivity); + List accounts = Data.accounts.get(); if (accounts != null) { LedgerAccount account = accounts.get(position); @@ -210,8 +208,7 @@ public class AccountSummaryFragment extends MobileLedgerListFragment { private void update_account_table() { if (this.getContext() == null) return; - model.reloadAccounts(this.getContext()); - modelAdapter.notifyDataSetChanged(); + model.scheduleAccountListReload(this.getContext()); } public void onShowOnlyStarredClicked(MenuItem mi) { SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(mActivity); @@ -240,7 +237,7 @@ public class AccountSummaryFragment extends MobileLedgerListFragment { stopSelection(); } public void onConfirmAccSelection(MenuItem item) { - model.commitSelections(mActivity); + AccountSummaryViewModel.commitSelections(mActivity); stopSelection(); } @Override diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryViewModel.java b/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryViewModel.java index 26d97c53..1afced05 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryViewModel.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryViewModel.java @@ -17,12 +17,9 @@ package net.ktnx.mobileledger.ui.account_summary; -import android.app.Application; -import android.arch.lifecycle.AndroidViewModel; +import android.arch.lifecycle.ViewModel; import android.content.Context; import android.content.res.Resources; -import android.database.Cursor; -import android.database.sqlite.SQLiteDatabase; import android.graphics.Typeface; import android.os.Build; import android.preference.PreferenceManager; @@ -37,97 +34,67 @@ import android.widget.LinearLayout; import android.widget.TextView; import net.ktnx.mobileledger.R; +import net.ktnx.mobileledger.async.CommitAccountsTask; +import net.ktnx.mobileledger.async.CommitAccountsTaskParams; +import net.ktnx.mobileledger.async.UpdateAccountsTask; +import net.ktnx.mobileledger.model.Data; import net.ktnx.mobileledger.model.LedgerAccount; -import net.ktnx.mobileledger.utils.MLDB; import java.util.ArrayList; -import java.util.List; import static net.ktnx.mobileledger.ui.activity.SettingsActivity.PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS; -class AccountSummaryViewModel extends AndroidViewModel { - private List accounts; +class AccountSummaryViewModel extends ViewModel { + void scheduleAccountListReload(Context context) { + boolean showingOnlyStarred = PreferenceManager.getDefaultSharedPreferences(context) + .getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false); - public AccountSummaryViewModel(@NonNull Application application) { - super(application); - } - - List getAccounts(Context context) { - if (accounts == null) { - accounts = new ArrayList<>(); - reloadAccounts(context); - } + UAT task = new UAT(); + task.execute(showingOnlyStarred); - return accounts; } - - void reloadAccounts(Context context) { - accounts.clear(); - boolean showingOnlyStarred = - PreferenceManager.getDefaultSharedPreferences(getApplication()) - .getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false); - String sql = "SELECT name, hidden FROM accounts"; - if (showingOnlyStarred) sql += " WHERE hidden = 0"; - sql += " ORDER BY name"; - - try (SQLiteDatabase db = MLDB.getReadableDatabase()) { - try (Cursor cursor = db - .rawQuery(sql,null)) - { - while (cursor.moveToNext()) { - LedgerAccount acc = new LedgerAccount(cursor.getString(0)); - acc.setHidden(cursor.getInt(1) == 1); - try (Cursor c2 = db.rawQuery( - "SELECT value, currency FROM account_values " + "WHERE account = ?", - new String[]{acc.getName()})) - { - while (c2.moveToNext()) { - acc.addAmount(c2.getFloat(0), c2.getString(1)); - } - } - accounts.add(acc); - } - } + static void commitSelections(Context context) { + boolean showingOnlyStarred = PreferenceManager.getDefaultSharedPreferences(context) + .getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false); + CAT task = new CAT(); + //noinspection unchecked + task.execute(new CommitAccountsTaskParams(Data.accounts.get(), showingOnlyStarred)); + } + private static class UAT extends UpdateAccountsTask { + @Override + protected void onPostExecute(ArrayList list) { + super.onPostExecute(list); + if (list != null) Data.accounts.set(list); } } - void commitSelections(Context context) { - try (SQLiteDatabase db = MLDB.getWritableDatabase()) { - db.beginTransaction(); - try { - for (LedgerAccount acc : accounts) { - Log.d("db", String.format("Setting %s to %s", acc.getName(), - acc.isHidden() ? "hidden" : "starred")); - db.execSQL("UPDATE accounts SET hidden=? WHERE name=?", - new Object[]{acc.isHiddenToBe() ? 1 : 0, acc.getName()}); - } - db.setTransactionSuccessful(); - for (LedgerAccount acc : accounts ) { acc.setHidden(acc.isHiddenToBe()); } + private static class CAT extends CommitAccountsTask { + @Override + protected void onPostExecute(ArrayList list) { + super.onPostExecute(list); + if (list != null) { + Log.d("acc", "setting new account list"); + Data.accounts.set(list); } - finally { db.endTransaction(); } } } } -class AccountSummaryAdapter extends RecyclerView.Adapter { - private List accounts; +class AccountSummaryAdapter extends RecyclerView.Adapter { private boolean selectionActive; - AccountSummaryAdapter(List accounts) { - this.accounts = accounts; + AccountSummaryAdapter() { this.selectionActive = false; } public void onBindViewHolder(@NonNull LedgerRowHolder holder, int position) { - LedgerAccount acc = accounts.get(position); + LedgerAccount acc = Data.accounts.get().get(position); Context ctx = holder.row.getContext(); Resources rm = ctx.getResources(); holder.tvAccountName.setText(acc.getShortName()); holder.tvAccountName.setPadding( acc.getLevel() * rm.getDimensionPixelSize(R.dimen.activity_horizontal_margin) / 2, - 0, 0, - 0); + 0, 0, 0); holder.tvAccountAmounts.setText(acc.getAmountsString()); if (acc.isHidden()) { @@ -150,7 +117,7 @@ class AccountSummaryAdapter extends RecyclerView.Adapter