hide accounts, shown when a global preference is checked
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Mon, 10 Dec 2018 21:35:22 +0000 (21:35 +0000)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Mon, 10 Dec 2018 21:35:22 +0000 (21:35 +0000)
app/src/main/java/net/ktnx/mobileledger/AccountRowLayout.java [new file with mode: 0644]
app/src/main/java/net/ktnx/mobileledger/AccountSummary.java
app/src/main/java/net/ktnx/mobileledger/SettingsActivity.java
app/src/main/res/menu/account_summary.xml
app/src/main/res/raw/sql_4.sql [new file with mode: 0644]
app/src/main/res/values/strings.xml
app/src/main/res/xml/pref_headers.xml
app/src/main/res/xml/pref_interface.xml [new file with mode: 0644]

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 (file)
index 0000000..57ca623
--- /dev/null
@@ -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;
+    }
+}
index d653ced44ed798f4984bafbd59b0668d76226477..411d96197e5e7627a5c7fe7cc453a897243eeeee 100644 (file)
@@ -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);
 
index 7d84fe0c2e03ce972a45f7006dc05393b61b3bc7..0f8246a2075008390e92e5c41ab79c1b4088cb14 100644 (file)
@@ -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.
index d0d2f137671c1560a14be5bd7ace78e062575aa0..17f8525846a6414d6380ccce92bd81cc4c709c19 100644 (file)
@@ -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" />
 </menu>
\ 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 (file)
index 0000000..d1a14d7
--- /dev/null
@@ -0,0 +1,2 @@
+alter table accounts add hidden boolean default 0;
+update accounts set hidden = 0;
\ No newline at end of file
index b7501a08fc226b9da62d992aa275df577d55e184..903582cf5d386396e176ff4b0cbb2c598bec2b61 100644 (file)
@@ -76,4 +76,7 @@
     <string name="err_bad_auth">Invalid username or password</string>
     <string name="action_reset_new_transaction_activity_title">Reset</string>
     <string name="ime_action_next_title">Next</string>
+    <string name="interface_pref_header_title">Interface</string>
+    <string name="pref_show_hidden_accounts_off_summary">Hidden accounts are not visible in the account list</string>
+    <string name="pref_show_hidden_accounts_on_summary">Hidden accounts are shown in the account list</string>
 </resources>
index 3fa08f1d43a12c0ee6180e8cb701add9c5950c77..c8c21f1725e82baf54a0579a7a7c0d8bf10e7422 100644 (file)
@@ -6,6 +6,10 @@
         android:fragment="net.ktnx.mobileledger.SettingsActivity$BackendPreferenceFragment"
         android:icon="@drawable/ic_info_black_24dp"
         android:title="@string/pref_header_backend" />
+    <header
+        android:fragment="net.ktnx.mobileledger.SettingsActivity$InterfacePreferenceFragment"
+        android:icon="@drawable/ic_info_black_24dp"
+        android:title="@string/interface_pref_header_title" />
 <!--
     <header
         android:fragment="net.ktnx.mobileledger.SettingsActivity$NotificationPreferenceFragment"
diff --git a/app/src/main/res/xml/pref_interface.xml b/app/src/main/res/xml/pref_interface.xml
new file mode 100644 (file)
index 0000000..218ab0e
--- /dev/null
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="utf-8"?>
+<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
+
+    <SwitchPreference
+        android:id="@+id/pref_show_hidden_accounts"
+        android:defaultValue="false"
+        android:key="show_hidden_accounts"
+        android:summaryOff="@string/pref_show_hidden_accounts_off_summary"
+        android:summaryOn="@string/pref_show_hidden_accounts_on_summary"
+        android:title="@string/menu_acc_summary_show_hidden_accounts_title" />
+</PreferenceScreen>
\ No newline at end of file