From 1796886d67b4d62c9fad1ea0980f38f53b8ce3b7 Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Fri, 23 Apr 2021 23:32:43 +0300 Subject: [PATCH] do not force transaction list reload upon main activity start instead, let the account filter observer trigger it (account filter live data is initialized with a value) this drops the reloading upon profile change, but profile changes restart the activity anyway, as do theme changes also, more precise reaction on account filter changes in the fragment -- no need to reload transactions there now that the main activity does it --- .../net/ktnx/mobileledger/ui/MainModel.java | 2 +- .../ui/activity/MainActivity.java | 12 +++---- .../TransactionListFragment.java | 33 +++++++------------ 3 files changed, 19 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/MainModel.java b/app/src/main/java/net/ktnx/mobileledger/ui/MainModel.java index a620255d..9aafedd5 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/MainModel.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/MainModel.java @@ -41,7 +41,7 @@ import java.util.Locale; public class MainModel extends ViewModel { public final MutableLiveData foundTransactionItemIndex = new MutableLiveData<>(null); private final MutableLiveData updatingFlag = new MutableLiveData<>(false); - private final MutableLiveData accountFilter = new MutableLiveData<>(); + private final MutableLiveData accountFilter = new MutableLiveData<>(null); private final MutableLiveData> displayedTransactions = new MutableLiveData<>(new ArrayList<>()); private final MutableLiveData updateError = new MutableLiveData<>(); 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 04aed042..46cc410e 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 @@ -456,14 +456,14 @@ public class MainActivity extends ProfileThemedActivity implements FabManager.Fa return; } - mainModel.stopTransactionsRetrieval(); + mainModel.getAccountFilter() + .observe(this, v -> { + Logger.debug(TAG, "account filter changed, reloading transactions"); + mainModel.scheduleTransactionListReload(); + }); + mainModel.stopTransactionsRetrieval(); mainModel.clearTransactions(); - - if (haveProfile) { - Logger.debug("transactions", "requesting list reload"); - mainModel.scheduleTransactionListReload(); - } } private void profileThemeChanged() { // un-hook all observed LiveData diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java b/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java index 56d5032f..e1534980 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java @@ -129,8 +129,8 @@ public class TransactionListFragment extends MobileLedgerListFragment vAccountFilter = view.findViewById(R.id.transaction_list_account_name_filter); accNameFilter = view.findViewById(R.id.transaction_filter_account_name); - Profile profile = Data.getProfile(); - accNameFilter.setAdapter(new AccountAutocompleteAdapter(mainActivity, profile)); + Data.observeProfile(getViewLifecycleOwner(), this::onProfileChanged); + accNameFilter.setOnItemClickListener((parent, v, position, id) -> { // debug("tmp", "direct onItemClick"); model.getAccountFilter() @@ -151,8 +151,6 @@ public class TransactionListFragment extends MobileLedgerListFragment .setOnClickListener(v -> { model.getAccountFilter() .setValue(null); - vAccountFilter.setVisibility(View.GONE); - menuTransactionListFilter.setVisible(true); Globals.hideSoftKeyboard(mainActivity); }); @@ -165,24 +163,21 @@ public class TransactionListFragment extends MobileLedgerListFragment } }); } - private void onAccountNameFilterChanged(String accName) { - final String fieldText = accNameFilter.getText() - .toString(); - if ((accName == null) && (fieldText.equals(""))) + private void onProfileChanged(Profile profile) { + if (profile == null) return; - if (accNameFilter != null) { - accNameFilter.setText(accName, false); - } + accNameFilter.setAdapter(new AccountAutocompleteAdapter(getContext(), profile)); + } + private void onAccountNameFilterChanged(String accName) { + accNameFilter.setText(accName, false); + final boolean filterActive = (accName != null) && !accName.isEmpty(); if (vAccountFilter != null) { vAccountFilter.setVisibility(filterActive ? View.VISIBLE : View.GONE); } if (menuTransactionListFilter != null) menuTransactionListFilter.setVisible(!filterActive); - - model.scheduleTransactionListReload(); - } @Override public void onCreateOptionsMenu(@NotNull Menu menu, @NotNull MenuInflater inflater) { @@ -192,18 +187,14 @@ public class TransactionListFragment extends MobileLedgerListFragment if ((menuTransactionListFilter == null)) throw new AssertionError(); - if ((model.getAccountFilter() - .getValue() != null) || (vAccountFilter.getVisibility() == View.VISIBLE)) - { - menuTransactionListFilter.setVisible(false); - } + model.getAccountFilter() + .observe(this, v -> menuTransactionListFilter.setVisible(v == null)); super.onCreateOptionsMenu(menu, inflater); menuTransactionListFilter.setOnMenuItemClickListener(item -> { vAccountFilter.setVisibility(View.VISIBLE); - if (menuTransactionListFilter != null) - menuTransactionListFilter.setVisible(false); + menuTransactionListFilter.setVisible(false); accNameFilter.requestFocus(); InputMethodManager imm = (InputMethodManager) getMainActivity().getSystemService(INPUT_METHOD_SERVICE); -- 2.39.5