From: Damyan Ivanov Date: Wed, 9 Jan 2019 20:16:45 +0000 (+0000) Subject: Show only starred menu now works X-Git-Tag: v0.3~91 X-Git-Url: https://git.ktnx.net/?a=commitdiff_plain;h=0167d1f5e98944aab435727e06ff74ccf9913908;p=mobile-ledger.git Show only starred menu now works --- diff --git a/app/src/main/java/net/ktnx/mobileledger/MobileLedgerApplication.java b/app/src/main/java/net/ktnx/mobileledger/MobileLedgerApplication.java index 20762c07..ea4a3d66 100644 --- a/app/src/main/java/net/ktnx/mobileledger/MobileLedgerApplication.java +++ b/app/src/main/java/net/ktnx/mobileledger/MobileLedgerApplication.java @@ -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 @@ -18,13 +18,18 @@ 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() { diff --git a/app/src/main/java/net/ktnx/mobileledger/model/Data.java b/app/src/main/java/net/ktnx/mobileledger/model/Data.java index 0deda2f4..d8b3b190 100644 --- a/app/src/main/java/net/ktnx/mobileledger/model/Data.java +++ b/app/src/main/java/net/ktnx/mobileledger/model/Data.java @@ -33,4 +33,5 @@ public final class Data { public static ObservableValue profile = new ObservableValue<>(); public static ObservableList profiles = new ObservableList<>(new ArrayList<>()); + public static ObservableValue optShowOnlyStarred = new ObservableValue<>(); } 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 1f43c61e..7218b15e 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 @@ -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; + }); } } diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java b/app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java index 760f6aac..e762133c 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java @@ -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: diff --git a/app/src/main/res/menu/account_summary.xml b/app/src/main/res/menu/account_summary.xml index d2bd884a..93052102 100644 --- a/app/src/main/res/menu/account_summary.xml +++ b/app/src/main/res/menu/account_summary.xml @@ -1,6 +1,6 @@