From 7f557f4dc1d24ff77f148bf64f12ccd27a57a1ae Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Mon, 10 Dec 2018 21:35:22 +0000 Subject: [PATCH] hide accounts, shown when a global preference is checked --- .../ktnx/mobileledger/AccountRowLayout.java | 39 ++++++++++ .../net/ktnx/mobileledger/AccountSummary.java | 78 ++++++++++++++++--- .../ktnx/mobileledger/SettingsActivity.java | 38 ++++++++- app/src/main/res/menu/account_summary.xml | 1 + app/src/main/res/raw/sql_4.sql | 2 + app/src/main/res/values/strings.xml | 3 + app/src/main/res/xml/pref_headers.xml | 4 + app/src/main/res/xml/pref_interface.xml | 11 +++ 8 files changed, 164 insertions(+), 12 deletions(-) create mode 100644 app/src/main/java/net/ktnx/mobileledger/AccountRowLayout.java create mode 100644 app/src/main/res/raw/sql_4.sql create mode 100644 app/src/main/res/xml/pref_interface.xml diff --git a/app/src/main/java/net/ktnx/mobileledger/AccountRowLayout.java b/app/src/main/java/net/ktnx/mobileledger/AccountRowLayout.java new file mode 100644 index 00000000..57ca6232 --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/AccountRowLayout.java @@ -0,0 +1,39 @@ +package net.ktnx.mobileledger; + +import android.content.Context; +import android.util.AttributeSet; +import android.widget.LinearLayout; + +class AccountRowLayout extends LinearLayout { + private String accountName; + + public + AccountRowLayout(Context context, String accountName) { + super(context); + this.accountName = accountName; + } + + public + AccountRowLayout(Context context, AttributeSet attrs, String accountName) { + super(context, attrs); + this.accountName = accountName; + } + + public + AccountRowLayout(Context context, AttributeSet attrs, int defStyleAttr, String accountName) { + super(context, attrs, defStyleAttr); + this.accountName = accountName; + } + + public + AccountRowLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes, + String accountName) { + super(context, attrs, defStyleAttr, defStyleRes); + this.accountName = accountName; + } + + public + String getAccountName() { + return accountName; + } +} diff --git a/app/src/main/java/net/ktnx/mobileledger/AccountSummary.java b/app/src/main/java/net/ktnx/mobileledger/AccountSummary.java index d653ced4..411d9619 100644 --- a/app/src/main/java/net/ktnx/mobileledger/AccountSummary.java +++ b/app/src/main/java/net/ktnx/mobileledger/AccountSummary.java @@ -2,9 +2,11 @@ package net.ktnx.mobileledger; import android.annotation.SuppressLint; import android.content.Intent; +import android.content.SharedPreferences; import android.content.pm.PackageInfo; import android.content.res.Resources; import android.database.Cursor; +import android.graphics.Typeface; import android.os.Build; import android.os.Bundle; import android.preference.PreferenceManager; @@ -26,7 +28,6 @@ import android.widget.EditText; import android.widget.LinearLayout; import android.widget.ProgressBar; import android.widget.TextView; -import android.widget.Toast; import java.util.Date; import java.util.regex.Matcher; @@ -41,7 +42,9 @@ public class AccountSummary extends AppCompatActivity { private static long account_list_last_updated; private static boolean account_list_needs_update = true; - private LinearLayout clickedAccountRow; + MenuItem mShowHiddenAccounts; + SharedPreferences.OnSharedPreferenceChangeListener sBindPreferenceSummaryToValueListener; + private AccountRowLayout clickedAccountRow; public static void preferences_changed() { account_list_needs_update = true; @@ -131,6 +134,24 @@ public class AccountSummary extends AppCompatActivity { getMenuInflater().inflate(R.menu.account_summary, menu); mRefresh = menu.findItem(R.id.menu_acc_summary_refresh); if (mRefresh == null) throw new AssertionError(); + + mShowHiddenAccounts = menu.findItem(R.id.menu_acc_summary_show_hidden); + if (mShowHiddenAccounts == null) throw new AssertionError(); + + sBindPreferenceSummaryToValueListener = + new SharedPreferences.OnSharedPreferenceChangeListener() { + @Override + public + void onSharedPreferenceChanged(SharedPreferences preference, String value) { + mShowHiddenAccounts + .setChecked(preference.getBoolean("show_hidden_accounts", false)); + } + }; + SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this); + pref.registerOnSharedPreferenceChangeListener(sBindPreferenceSummaryToValueListener); + + mShowHiddenAccounts.setChecked(pref.getBoolean("show_hidden_accounts", false)); + return true; } @@ -153,6 +174,19 @@ public class AccountSummary extends AppCompatActivity { update_accounts(true); } + public + void onShowHiddenAccountsClicked(MenuItem mi) { + SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this); + boolean flag = pref.getBoolean("show_hidden_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.apply(); + + update_account_table(); + } + private void prepare_db() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { MobileLedgerDB.setDb_filename(this.getApplicationInfo().deviceProtectedDataDir + "/" + MobileLedgerDB.DATABASE_NAME); @@ -237,8 +271,9 @@ public class AccountSummary extends AppCompatActivity { } public void hideAccountClicked(MenuItem item) { - TextView textView = (TextView) clickedAccountRow.getChildAt(0); - Toast.makeText(this, textView.getText(), Toast.LENGTH_SHORT).show(); + db.execSQL("update accounts set hidden=1 where name=?", + new Object[]{clickedAccountRow.getAccountName()}); + update_account_table(); } @SuppressLint("DefaultLocale") @@ -249,25 +284,44 @@ public class AccountSummary extends AppCompatActivity { View.OnCreateContextMenuListener ccml = new View.OnCreateContextMenuListener() { @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { - clickedAccountRow = (LinearLayout) v; + clickedAccountRow = (AccountRowLayout) v; getMenuInflater().inflate(R.menu.account_summary_account_menu, menu); } }; - int actionBarHeight = - getTheme().obtainStyledAttributes(new int[]{android.R.attr.actionBarSize}) - .getDimensionPixelSize(0, dp2px(56)); + int rowHeight = + (int) (getTheme().obtainStyledAttributes(new int[]{android.R.attr.actionBarSize}) + .getDimensionPixelSize(0, dp2px(56)) * 0.75); - try (Cursor cursor = db.rawQuery("SELECT name FROM accounts ORDER BY name;", null)) { + boolean showingHiddenAccounts = PreferenceManager.getDefaultSharedPreferences(this) + .getBoolean("show_hidden_accounts", false); + Log.d("pref", "show_hidden_accounts is " + (showingHiddenAccounts ? "true" : "false")); + + try (Cursor cursor = db + .rawQuery("SELECT name, hidden FROM accounts ORDER BY name;", null)) + { boolean even = false; + String skippingAccountName = null; while (cursor.moveToNext()) { String acc_name = cursor.getString(0); + if (skippingAccountName != null) { + if (acc_name.startsWith(skippingAccountName + ":")) continue; + + skippingAccountName = null; + } + + boolean is_hidden = cursor.getInt(1) == 1; + + if (!showingHiddenAccounts && is_hidden) { + skippingAccountName = acc_name; + continue; + } - LinearLayout r = new LinearLayout(this); + LinearLayout r = new AccountRowLayout(this, acc_name); r.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); r.setGravity(Gravity.CENTER_VERTICAL); r.setPadding(getResources().getDimensionPixelSize(R.dimen.activity_horizontal_margin), dp2px(3), getResources().getDimensionPixelSize(R.dimen.activity_horizontal_margin), dp2px(4)); - r.setMinimumHeight(actionBarHeight); + r.setMinimumHeight(rowHeight); if (even) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { @@ -292,6 +346,7 @@ public class AccountSummary extends AppCompatActivity { String short_acc_name = strip_higher_accounts(acc_name, indent_level); acc_tv.setPadding(indent_level[0] * getResources().getDimensionPixelSize(R.dimen.activity_horizontal_margin) / 2, 0, 0, 0); acc_tv.setText(short_acc_name); + if (is_hidden) acc_tv.setTypeface(null, Typeface.ITALIC); r.addView(acc_tv); TextView amt_tv = new TextView(this, null, R.style.account_summary_amounts); @@ -310,6 +365,7 @@ public class AccountSummary extends AppCompatActivity { } } amt_tv.setText(amt_text.toString()); + if (is_hidden) amt_tv.setTypeface(null, Typeface.ITALIC); r.addView(amt_tv); diff --git a/app/src/main/java/net/ktnx/mobileledger/SettingsActivity.java b/app/src/main/java/net/ktnx/mobileledger/SettingsActivity.java index 7d84fe0c..0f8246a2 100644 --- a/app/src/main/java/net/ktnx/mobileledger/SettingsActivity.java +++ b/app/src/main/java/net/ktnx/mobileledger/SettingsActivity.java @@ -82,6 +82,7 @@ public class SettingsActivity extends AppCompatPreferenceActivity { // simple string representation. preference.setSummary(stringValue); } + return true; }; @@ -169,7 +170,8 @@ public class SettingsActivity extends AppCompatPreferenceActivity { return PreferenceFragment.class.getName().equals(fragmentName) || BackendPreferenceFragment.class.getName().equals(fragmentName) || DataSyncPreferenceFragment.class.getName().equals(fragmentName) - || NotificationPreferenceFragment.class.getName().equals(fragmentName); + || NotificationPreferenceFragment.class.getName().equals(fragmentName) + || InterfacePreferenceFragment.class.getName().equals(fragmentName); } /** @@ -204,6 +206,40 @@ public class SettingsActivity extends AppCompatPreferenceActivity { } } + /** + * This fragment shows general preferences only. It is used when the + * activity is showing a two-pane settings UI. + */ + @TargetApi(Build.VERSION_CODES.HONEYCOMB) + public static + class InterfacePreferenceFragment extends PreferenceFragment { + @Override + public + void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + addPreferencesFromResource(R.xml.pref_interface); + setHasOptionsMenu(true); + + // Bind the summaries of EditText/List/Dialog/Ringtone preferences + // 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")); + + } + + @Override + public + boolean onOptionsItemSelected(MenuItem item) { + int id = item.getItemId(); + if (id == android.R.id.home) { + startActivity(new Intent(getActivity(), SettingsActivity.class)); + return true; + } + return super.onOptionsItemSelected(item); + } + } + /** * This fragment shows notification preferences only. It is used when the * activity is showing a two-pane settings UI. diff --git a/app/src/main/res/menu/account_summary.xml b/app/src/main/res/menu/account_summary.xml index d0d2f137..17f85258 100644 --- a/app/src/main/res/menu/account_summary.xml +++ b/app/src/main/res/menu/account_summary.xml @@ -14,5 +14,6 @@ android:checked="false" android:title="@string/menu_acc_summary_show_hidden_accounts_title" app:actionLayout="@layout/switch_item" + android:onClick="onShowHiddenAccountsClicked" app:showAsAction="never" /> \ No newline at end of file diff --git a/app/src/main/res/raw/sql_4.sql b/app/src/main/res/raw/sql_4.sql new file mode 100644 index 00000000..d1a14d75 --- /dev/null +++ b/app/src/main/res/raw/sql_4.sql @@ -0,0 +1,2 @@ +alter table accounts add hidden boolean default 0; +update accounts set hidden = 0; \ 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 b7501a08..903582cf 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -76,4 +76,7 @@ Invalid username or password Reset Next + Interface + Hidden accounts are not visible in the account list + Hidden accounts are shown in the account list diff --git a/app/src/main/res/xml/pref_headers.xml b/app/src/main/res/xml/pref_headers.xml index 3fa08f1d..c8c21f17 100644 --- a/app/src/main/res/xml/pref_headers.xml +++ b/app/src/main/res/xml/pref_headers.xml @@ -6,6 +6,10 @@ android:fragment="net.ktnx.mobileledger.SettingsActivity$BackendPreferenceFragment" android:icon="@drawable/ic_info_black_24dp" android:title="@string/pref_header_backend" /> +