Show only starred menu now works
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Wed, 9 Jan 2019 20:16:45 +0000 (20:16 +0000)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Wed, 9 Jan 2019 20:16:45 +0000 (20:16 +0000)
app/src/main/java/net/ktnx/mobileledger/MobileLedgerApplication.java
app/src/main/java/net/ktnx/mobileledger/model/Data.java
app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java
app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java
app/src/main/res/menu/account_summary.xml

index 20762c07a9ee76b5c66db1d953ca8c5cf9f8f6eb..ea4a3d663126c7a65de8cd8ea3d02b632f54b7a4 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2018 Damyan Ivanov.
+ * 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
 package net.ktnx.mobileledger;
 
 import android.app.Application;
+import android.content.SharedPreferences;
 import android.content.res.Configuration;
 import android.content.res.Resources;
 import android.os.Build;
+import android.preference.PreferenceManager;
 
+import net.ktnx.mobileledger.model.Data;
 import net.ktnx.mobileledger.utils.Globals;
 import net.ktnx.mobileledger.utils.MLDB;
 
+import static net.ktnx.mobileledger.ui.activity.SettingsActivity.PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS;
+
 public class MobileLedgerApplication extends Application {
 
     @Override
@@ -32,6 +37,13 @@ public class MobileLedgerApplication extends Application {
         super.onCreate();
         updateColorValues();
         MLDB.init(this);
+        SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(this);
+        Data.optShowOnlyStarred.set(p.getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false));
+        SharedPreferences.OnSharedPreferenceChangeListener handler = (preference, value) -> {
+            Data.optShowOnlyStarred
+                    .set(preference.getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false));
+        };
+        p.registerOnSharedPreferenceChangeListener(handler);
     }
     @Override
     public void onTerminate() {
index 0deda2f4592020ddbad2b3435149068f0ea1e00b..d8b3b19011dc636107e640d73fcfd1c372b88db5 100644 (file)
@@ -33,4 +33,5 @@ public final class Data {
     public static ObservableValue<MobileLedgerProfile> profile = new ObservableValue<>();
     public static ObservableList<MobileLedgerProfile> profiles =
             new ObservableList<>(new ArrayList<>());
+    public static ObservableValue<Boolean> optShowOnlyStarred = new ObservableValue<>();
 }
index 1f43c61ed5c0fabd8cddd5cdf8824dc2655e9959..7218b15e728c7c89d4fc99a9e5a497e900a79a57 100644 (file)
@@ -50,9 +50,7 @@ import static net.ktnx.mobileledger.ui.activity.SettingsActivity.PREF_KEY_SHOW_O
 
 public class AccountSummaryFragment extends MobileLedgerListFragment {
 
-    private static long account_list_last_updated;
-    private static boolean account_list_needs_update = true;
-    MenuItem mShowHiddenAccounts;
+    MenuItem mShowOnlyStarred;
     SharedPreferences.OnSharedPreferenceChangeListener sBindPreferenceSummaryToValueListener;
     private AccountSummaryViewModel model;
     private AccountSummaryAdapter modelAdapter;
@@ -61,7 +59,7 @@ public class AccountSummaryFragment extends MobileLedgerListFragment {
     private Observer backgroundTaskCountObserver;
     @Override
     public void onDestroy() {
-        if(backgroundTaskCountObserver!= null) {
+        if (backgroundTaskCountObserver != null) {
             Log.d("acc", "destroying background task count observer");
             Data.backgroundTaskCount.deleteObserver(backgroundTaskCountObserver);
         }
@@ -187,18 +185,6 @@ public class AccountSummaryFragment extends MobileLedgerListFragment {
 
         model.scheduleAccountListReload(this.getContext());
     }
-    public void onShowOnlyStarredClicked(MenuItem mi) {
-        SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(mActivity);
-        boolean flag = pref.getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false);
-
-        SharedPreferences.Editor editor = pref.edit();
-        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();
-    }
-
     void stopSelection() {
         modelAdapter.stopSelection();
         if (optMenu != null) {
@@ -223,16 +209,34 @@ public class AccountSummaryFragment extends MobileLedgerListFragment {
         inflater.inflate(R.menu.account_summary, menu);
         optMenu = menu;
 
-        mShowHiddenAccounts = menu.findItem(R.id.menu_acc_summary_only_starred);
-        if (mShowHiddenAccounts == null) throw new AssertionError();
+        mShowOnlyStarred = menu.findItem(R.id.menu_acc_summary_only_starred);
+        if (mShowOnlyStarred == null) throw new AssertionError();
 
-        sBindPreferenceSummaryToValueListener = (preference, value) -> mShowHiddenAccounts
-                .setChecked(preference.getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false));
-        SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(mActivity);
-        pref.registerOnSharedPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
+        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();
+            }
+        });
+
+        mShowOnlyStarred.setChecked(Data.optShowOnlyStarred.get());
 
-        mShowHiddenAccounts.setChecked(pref.getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false));
+        Log.d("menu", "Accounts: onCreateOptionsMenu called");
 
-        Log.d("menu", "MainActivity: onCreateOptionsMenu called");
+        mShowOnlyStarred.setOnMenuItemClickListener(item -> {
+            SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(mActivity);
+            SharedPreferences.Editor editor = pref.edit();
+            boolean flag = item.isChecked();
+            editor.putBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, !flag);
+            Log.d("pref",
+                    "Setting show only starred accounts pref to " + (flag ? "false" : "true"));
+            editor.apply();
+
+
+            return true;
+        });
     }
 }
index 760f6aacfbbdfffda4d69ede20cf59dcd4a5f9be..e762133c53da44d5edf3ef331372129fd731698e 100644 (file)
@@ -33,8 +33,6 @@ import android.support.v7.app.ActionBarDrawerToggle;
 import android.support.v7.app.AppCompatActivity;
 import android.support.v7.widget.Toolbar;
 import android.util.Log;
-import android.view.ContextMenu;
-import android.view.MenuItem;
 import android.view.View;
 import android.widget.LinearLayout;
 import android.widget.ProgressBar;
@@ -265,29 +263,6 @@ public class MainActivity extends AppCompatActivity {
             }
         }
     }
-    public void onOptionsMenuClicked(MenuItem menuItem) {
-        ContextMenu.ContextMenuInfo info = menuItem.getMenuInfo();
-        switch (menuItem.getItemId()) {
-            case R.id.menu_acc_summary_cancel_selection:
-                if (accountSummaryFragment != null)
-                    accountSummaryFragment.onCancelAccSelection(menuItem);
-                break;
-            case R.id.menu_acc_summary_confirm_selection:
-                if (accountSummaryFragment != null)
-                    accountSummaryFragment.onConfirmAccSelection(menuItem);
-                break;
-            case R.id.menu_acc_summary_only_starred:
-                if (accountSummaryFragment != null)
-                    accountSummaryFragment.onShowOnlyStarredClicked(menuItem);
-                break;
-            case R.id.menu_transaction_list_filter:
-                if (transactionListFragment != null)
-                    transactionListFragment.onShowFilterClick(menuItem);
-                break;
-            default:
-                Log.e("menu", String.format("Menu item %d not handled", menuItem.getItemId()));
-        }
-    }
     public void onViewClicked(View view) {
         switch (view.getId()) {
             case R.id.clearAccountNameFilter:
index d2bd884a8bfe87b788fb8e5b4b549d379a53c47e..93052102f3575cae2831abbc5604878557864ead 100644 (file)
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="utf-8"?>
 <!--
-  ~ Copyright © 2018 Damyan Ivanov.
+  ~ 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
@@ -25,7 +25,6 @@
         android:id="@+id/menu_acc_summary_only_starred"
         android:checkable="true"
         android:checked="false"
-        android:onClick="onOptionsMenuClicked"
         android:title="@string/menu_acc_summary_show_only_starred_title"
         app:actionLayout="@layout/switch_item"
         app:showAsAction="never" />
         android:title="@string/menu_acc_summary_hide_selected_title"
         app:showAsAction="always"
         android:visible="false"
-        android:onClick="onOptionsMenuClicked"
         />
     <item android:id="@+id/menu_acc_summary_cancel_selection"
         android:title="@string/menu_acc_summary_cancel_selection_title"
         app:showAsAction="always"
         android:visible="false"
-        android:onClick="onOptionsMenuClicked"
         android:icon="@drawable/ic_cancel_white_24dp" />
     <item android:id="@+id/menu_acc_summary_confirm_selection"
         android:title="@string/menu_acc_summary_confirm_selection_title"
         app:showAsAction="always"
         android:visible="false"
-        android:onClick="onOptionsMenuClicked"
         android:icon="@drawable/ic_check_white_24dp" />
 </menu>
\ No newline at end of file