From: Damyan Ivanov Date: Fri, 14 Dec 2018 05:58:00 +0000 (+0000) Subject: better account starr-ing control X-Git-Tag: v0.3~246 X-Git-Url: https://git.ktnx.net/?a=commitdiff_plain;h=1b884be8a22a3cd1460da24f7378e06cc6f1c51a;p=mobile-ledger.git better account starr-ing control --- diff --git a/app/src/main/java/net/ktnx/mobileledger/AccountSummary.java b/app/src/main/java/net/ktnx/mobileledger/AccountSummary.java index 77a60074..e34aa6b7 100644 --- a/app/src/main/java/net/ktnx/mobileledger/AccountSummary.java +++ b/app/src/main/java/net/ktnx/mobileledger/AccountSummary.java @@ -28,6 +28,8 @@ import java.lang.ref.WeakReference; import java.util.Date; import java.util.List; +import static net.ktnx.mobileledger.SettingsActivity.PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS; + public class AccountSummary extends AppCompatActivity { DrawerLayout drawer; @@ -94,7 +96,12 @@ public class AccountSummary extends AppCompatActivity { modelAdapter.startSelection(); if (optMenu != null) { optMenu.findItem(R.id.menu_acc_summary_cancel_selection).setVisible(true); - optMenu.findItem(R.id.menu_acc_summary_hide_selected).setVisible(true); + optMenu.findItem(R.id.menu_acc_summary_confirm_selection).setVisible(true); + optMenu.findItem(R.id.menu_acc_summary_only_starred).setVisible(false); + } + { + FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.btn_add_transaction); + if (fab != null) fab.hide(); } } })); @@ -175,11 +182,11 @@ public class AccountSummary extends AppCompatActivity { if (mShowHiddenAccounts == null) throw new AssertionError(); sBindPreferenceSummaryToValueListener = (preference, value) -> mShowHiddenAccounts - .setChecked(preference.getBoolean("show_hidden_accounts", false)); + .setChecked(preference.getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false)); SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this); pref.registerOnSharedPreferenceChangeListener(sBindPreferenceSummaryToValueListener); - mShowHiddenAccounts.setChecked(pref.getBoolean("show_hidden_accounts", false)); + mShowHiddenAccounts.setChecked(pref.getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false)); return true; } @@ -206,11 +213,11 @@ public class AccountSummary extends AppCompatActivity { public void onShowOnlyStarredClicked(MenuItem mi) { SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this); - boolean flag = pref.getBoolean("show_hidden_accounts", false); + boolean flag = pref.getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false); SharedPreferences.Editor editor = pref.edit(); - editor.putBoolean("show_hidden_accounts", !flag); - Log.d("pref", "Setting show_hidden_accounts to " + (flag ? "false" : "true")); + editor.putBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, !flag); + Log.d("pref", "Setting show only starred accounts pref to " + (flag ? "false" : "true")); editor.apply(); update_account_table(); @@ -252,11 +259,23 @@ public class AccountSummary extends AppCompatActivity { model.reloadAccounts(); modelAdapter.notifyDataSetChanged(); } - public void onCancelAccSelection(MenuItem item) { + void stopSelection() { modelAdapter.stopSelection(); if (optMenu != null) { optMenu.findItem(R.id.menu_acc_summary_cancel_selection).setVisible(false); - optMenu.findItem(R.id.menu_acc_summary_hide_selected).setVisible(false); + optMenu.findItem(R.id.menu_acc_summary_confirm_selection).setVisible(false); + optMenu.findItem(R.id.menu_acc_summary_only_starred).setVisible(true); + } + { + FloatingActionButton fab = findViewById(R.id.btn_add_transaction); + if (fab != null) fab.show(); } } + public void onCancelAccSelection(MenuItem item) { + stopSelection(); + } + public void onConfirmAccSelection(MenuItem item) { + model.commitSelections(); + stopSelection(); + } } diff --git a/app/src/main/java/net/ktnx/mobileledger/AccountSummaryViewModel.java b/app/src/main/java/net/ktnx/mobileledger/AccountSummaryViewModel.java index c1f16258..b283d755 100644 --- a/app/src/main/java/net/ktnx/mobileledger/AccountSummaryViewModel.java +++ b/app/src/main/java/net/ktnx/mobileledger/AccountSummaryViewModel.java @@ -11,6 +11,7 @@ import android.os.Build; import android.preference.PreferenceManager; import android.support.annotation.NonNull; import android.support.v7.widget.RecyclerView; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -21,6 +22,8 @@ import android.widget.TextView; import java.util.ArrayList; import java.util.List; +import static net.ktnx.mobileledger.SettingsActivity.PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS; + class AccountSummaryViewModel extends AndroidViewModel { private MobileLedgerDatabase dbh; private List accounts; @@ -41,11 +44,11 @@ class AccountSummaryViewModel extends AndroidViewModel { void reloadAccounts() { accounts.clear(); - boolean showingHiddenAccounts = + boolean showingOnlyStarred = PreferenceManager.getDefaultSharedPreferences(getApplication()) - .getBoolean("show_hidden_accounts", false); + .getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false); String sql = "SELECT name, hidden FROM accounts"; - if (!showingHiddenAccounts) sql += " WHERE hidden = 0"; + if (showingOnlyStarred) sql += " WHERE hidden = 0"; sql += " ORDER BY name"; try (SQLiteDatabase db = dbh.getReadableDatabase()) { @@ -68,6 +71,22 @@ class AccountSummaryViewModel extends AndroidViewModel { } } } + void commitSelections() { + try(SQLiteDatabase db = dbh.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()); } + } + finally { db.endTransaction(); } + } + } } class AccountSummaryAdapter extends RecyclerView.Adapter { @@ -112,7 +131,7 @@ class AccountSummaryAdapter extends RecyclerView.Adapter amounts; - private boolean selected; static Pattern higher_account = Pattern.compile("^[^:]+:"); LedgerAccount(String name) { this.setName(name); hidden = false; - selected = false; } public boolean isHidden() { @@ -98,16 +97,17 @@ class LedgerAccount { public String getParentName() { return parentName; } - - public boolean isSelected() { - return selected; + public void togglehidden() { + hidden = !hidden; } - public void setSelected(boolean selected) { - this.selected = selected; + public boolean isHiddenToBe() { + return hiddenToBe; } - - public void toggleSelected() { - selected = !selected; + public void setHiddenToBe(boolean hiddenToBe) { + this.hiddenToBe = hiddenToBe; + } + public void toggleHiddenToBe() { + setHiddenToBe(!hiddenToBe); } } diff --git a/app/src/main/java/net/ktnx/mobileledger/SettingsActivity.java b/app/src/main/java/net/ktnx/mobileledger/SettingsActivity.java index 0f8246a2..ca70fe0b 100644 --- a/app/src/main/java/net/ktnx/mobileledger/SettingsActivity.java +++ b/app/src/main/java/net/ktnx/mobileledger/SettingsActivity.java @@ -34,6 +34,7 @@ import java.util.List; * API Guide for more information on developing a Settings UI. */ public class SettingsActivity extends AppCompatPreferenceActivity { + public static String PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS = "pref_show_only_starred_accounts"; /** * A preference value change listener that updates the preference's summary @@ -224,7 +225,6 @@ public class SettingsActivity extends AppCompatPreferenceActivity { // to their values. When their values change, their summaries are // updated to reflect the new value, per the Android Design // guidelines. -// bindPreferenceSummaryToValue(findPreference("show_hidden_accounts")); } diff --git a/app/src/main/res/drawable/checkbox_star_black.xml b/app/src/main/res/drawable/checkbox_star_black.xml new file mode 100644 index 00000000..92eace41 --- /dev/null +++ b/app/src/main/res/drawable/checkbox_star_black.xml @@ -0,0 +1,25 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/checkbox_star_white.xml b/app/src/main/res/drawable/checkbox_star_white.xml new file mode 100644 index 00000000..6e2dcc15 --- /dev/null +++ b/app/src/main/res/drawable/checkbox_star_white.xml @@ -0,0 +1,25 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_star_black_24dp.xml b/app/src/main/res/drawable/ic_star_black_24dp.xml new file mode 100644 index 00000000..742bacdc --- /dev/null +++ b/app/src/main/res/drawable/ic_star_black_24dp.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/drawable/ic_star_border_black_24dp.xml b/app/src/main/res/drawable/ic_star_border_black_24dp.xml new file mode 100644 index 00000000..c5822f94 --- /dev/null +++ b/app/src/main/res/drawable/ic_star_border_black_24dp.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/drawable/ic_star_border_white_24dp.xml b/app/src/main/res/drawable/ic_star_border_white_24dp.xml new file mode 100644 index 00000000..818483a9 --- /dev/null +++ b/app/src/main/res/drawable/ic_star_border_white_24dp.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/layout/account_summary_row.xml b/app/src/main/res/layout/account_summary_row.xml index 9617db65..478b6e6d 100644 --- a/app/src/main/res/layout/account_summary_row.xml +++ b/app/src/main/res/layout/account_summary_row.xml @@ -16,7 +16,8 @@ + android:layout_height="match_parent" + android:button="@drawable/checkbox_star_black" /> + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a57104ea..4b9a8ef3 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -81,4 +81,5 @@ Only starred accounts are shown Hide selected accounts Cancel selection + Confirm selectin diff --git a/app/src/main/res/xml/pref_interface.xml b/app/src/main/res/xml/pref_interface.xml index bc832f88..62ed9c69 100644 --- a/app/src/main/res/xml/pref_interface.xml +++ b/app/src/main/res/xml/pref_interface.xml @@ -2,9 +2,9 @@