From b4175624de863c7343de10db2436722e78e51cb1 Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Wed, 9 Jan 2019 20:33:27 +0000 Subject: [PATCH] get back the account starring functionality --- .../async/CommitAccountsTask.java | 5 +-- .../async/UpdateAccountsTask.java | 7 ++-- .../AccountSummaryFragment.java | 34 ++++++++++++------ .../AccountSummaryViewModel.java | 35 +++++++++---------- 4 files changed, 46 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/net/ktnx/mobileledger/async/CommitAccountsTask.java b/app/src/main/java/net/ktnx/mobileledger/async/CommitAccountsTask.java index 7fb96b9d..fea120f3 100644 --- a/app/src/main/java/net/ktnx/mobileledger/async/CommitAccountsTask.java +++ b/app/src/main/java/net/ktnx/mobileledger/async/CommitAccountsTask.java @@ -32,6 +32,7 @@ public class CommitAccountsTask protected ArrayList doInBackground(CommitAccountsTaskParams... params) { Data.backgroundTaskCount.incrementAndGet(); ArrayList newList = new ArrayList<>(); + String profile = Data.profile.get().getUuid(); try { SQLiteDatabase db = MLDB.getWritableDatabase(); @@ -40,8 +41,8 @@ public class CommitAccountsTask 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()}); + db.execSQL("UPDATE accounts SET hidden=? WHERE profile=? AND name=?", + new Object[]{acc.isHiddenToBe() ? 1 : 0, profile, acc.getName()}); acc.setHidden(acc.isHiddenToBe()); if (!params[0].showOnlyStarred || !acc.isHidden()) newList.add(acc); diff --git a/app/src/main/java/net/ktnx/mobileledger/async/UpdateAccountsTask.java b/app/src/main/java/net/ktnx/mobileledger/async/UpdateAccountsTask.java index 1ade3d53..250214d6 100644 --- a/app/src/main/java/net/ktnx/mobileledger/async/UpdateAccountsTask.java +++ b/app/src/main/java/net/ktnx/mobileledger/async/UpdateAccountsTask.java @@ -28,15 +28,16 @@ import net.ktnx.mobileledger.utils.MLDB; import java.util.ArrayList; -public class UpdateAccountsTask extends AsyncTask> { - protected ArrayList doInBackground(Boolean[] onlyStarred) { +public class UpdateAccountsTask extends AsyncTask> { + protected ArrayList doInBackground(Void... params) { Data.backgroundTaskCount.incrementAndGet(); String profileUUID = Data.profile.get().getUuid(); + boolean onlyStarred = Data.optShowOnlyStarred.get(); try { ArrayList newList = new ArrayList<>(); String sql = "SELECT name, hidden FROM accounts WHERE profile = ?"; - if (onlyStarred[0]) sql += " AND hidden = 0"; + if (onlyStarred) sql += " AND hidden = 0"; sql += " ORDER BY name"; SQLiteDatabase db = MLDB.getReadableDatabase(); 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 7218b15e..d14cf418 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 @@ -175,7 +175,7 @@ public class AccountSummaryFragment extends MobileLedgerListFragment { Data.profile.addObserver(new Observer() { @Override public void update(Observable o, Object arg) { - mActivity.runOnUiThread(() -> model.scheduleAccountListReload(mActivity)); + mActivity.runOnUiThread(() -> model.scheduleAccountListReload()); } }); update_account_table(); @@ -183,7 +183,7 @@ public class AccountSummaryFragment extends MobileLedgerListFragment { private void update_account_table() { if (this.getContext() == null) return; - model.scheduleAccountListReload(this.getContext()); + model.scheduleAccountListReload(); } void stopSelection() { modelAdapter.stopSelection(); @@ -211,15 +211,16 @@ public class AccountSummaryFragment extends MobileLedgerListFragment { mShowOnlyStarred = menu.findItem(R.id.menu_acc_summary_only_starred); if (mShowOnlyStarred == null) throw new AssertionError(); - - Data.optShowOnlyStarred.addObserver(new Observer() { - @Override - public void update(Observable o, Object arg) { - boolean newValue = Data.optShowOnlyStarred.get(); - Log.d("pref", String.format("pref change came (%s)", newValue ? "true" : "false")); - mShowOnlyStarred.setChecked(newValue); - update_account_table(); - } + MenuItem mCancelSelection = menu.findItem(R.id.menu_acc_summary_cancel_selection); + if (mCancelSelection == null) throw new AssertionError(); + MenuItem mConfirmSelection = menu.findItem(R.id.menu_acc_summary_confirm_selection); + if (mConfirmSelection == null) throw new AssertionError(); + + Data.optShowOnlyStarred.addObserver((o, arg) -> { + boolean newValue = Data.optShowOnlyStarred.get(); + Log.d("pref", String.format("pref change came (%s)", newValue ? "true" : "false")); + mShowOnlyStarred.setChecked(newValue); + update_account_table(); }); mShowOnlyStarred.setChecked(Data.optShowOnlyStarred.get()); @@ -235,6 +236,17 @@ public class AccountSummaryFragment extends MobileLedgerListFragment { "Setting show only starred accounts pref to " + (flag ? "false" : "true")); editor.apply(); + return true; + }); + + mCancelSelection.setOnMenuItemClickListener(item -> { + stopSelection(); + return true; + }); + + mConfirmSelection.setOnMenuItemClickListener(item -> { + AccountSummaryViewModel.commitSelections(mActivity); + stopSelection(); return true; }); 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 ce5f68e2..dd7f0327 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 @@ -22,7 +22,6 @@ import android.content.Context; import android.content.res.Resources; import android.graphics.Typeface; import android.os.Build; -import android.preference.PreferenceManager; import android.support.annotation.NonNull; import android.support.v7.widget.RecyclerView; import android.util.Log; @@ -42,24 +41,18 @@ import net.ktnx.mobileledger.model.LedgerAccount; import java.util.ArrayList; -import static net.ktnx.mobileledger.ui.activity.SettingsActivity.PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS; - class AccountSummaryViewModel extends ViewModel { - void scheduleAccountListReload(Context context) { - boolean showingOnlyStarred = PreferenceManager.getDefaultSharedPreferences(context) - .getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false); - - UAT task = new UAT(); - task.execute(showingOnlyStarred); - - } 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)); + task.execute( + new CommitAccountsTaskParams(Data.accounts.get(), Data.optShowOnlyStarred.get())); } + void scheduleAccountListReload() { + UAT task = new UAT(); + task.execute(); + + } + private static class UAT extends UpdateAccountsTask { @Override protected void onPostExecute(ArrayList list) { @@ -70,6 +63,7 @@ class AccountSummaryViewModel extends ViewModel { } } } + private static class CAT extends CommitAccountsTask { @Override protected void onPostExecute(ArrayList list) { @@ -156,15 +150,18 @@ class AccountSummaryAdapter extends RecyclerView.Adapter