]> git.ktnx.net Git - mobile-ledger.git/commitdiff
do not force transaction list reload upon main activity start
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Fri, 23 Apr 2021 20:32:43 +0000 (23:32 +0300)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Sun, 25 Apr 2021 17:07:58 +0000 (17:07 +0000)
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

app/src/main/java/net/ktnx/mobileledger/ui/MainModel.java
app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java
app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java

index a620255dff7fcea330ce35dffe43e0f2c21ada52..9aafedd50e7e8e8b448cf1030cde8853ee9bde68 100644 (file)
@@ -41,7 +41,7 @@ import java.util.Locale;
 public class MainModel extends ViewModel {
     public final MutableLiveData<Integer> foundTransactionItemIndex = new MutableLiveData<>(null);
     private final MutableLiveData<Boolean> updatingFlag = new MutableLiveData<>(false);
-    private final MutableLiveData<String> accountFilter = new MutableLiveData<>();
+    private final MutableLiveData<String> accountFilter = new MutableLiveData<>(null);
     private final MutableLiveData<List<TransactionListItem>> displayedTransactions =
             new MutableLiveData<>(new ArrayList<>());
     private final MutableLiveData<String> updateError = new MutableLiveData<>();
index 04aed0424d9d68f4b7745b72d2c23ca809812bce..46cc410e18d401eb1da86bdcb21c950d79081dff 100644 (file)
@@ -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
index 56d5032f90a06d12bcc41e4b10196c50eafaecf9..e15349803dc1bee40f94c6a3003d42830c8b56d9 100644 (file)
@@ -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);