]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java
rework account list management to be fully asynchronous
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / account_summary / AccountSummaryFragment.java
index 8ee910b54fe33b4c33655cc771fc18cd8d0fb167..dbc200eb8f6e8ff592ae02048c9682efd8326877 100644 (file)
@@ -31,12 +31,18 @@ import androidx.recyclerview.widget.RecyclerView;
 
 import net.ktnx.mobileledger.R;
 import net.ktnx.mobileledger.model.Data;
+import net.ktnx.mobileledger.model.LedgerAccount;
+import net.ktnx.mobileledger.model.MobileLedgerProfile;
 import net.ktnx.mobileledger.ui.MobileLedgerListFragment;
 import net.ktnx.mobileledger.ui.activity.MainActivity;
 import net.ktnx.mobileledger.utils.Colors;
+import net.ktnx.mobileledger.utils.Logger;
 
 import org.jetbrains.annotations.NotNull;
 
+import java.util.List;
+import java.util.Locale;
+
 import static net.ktnx.mobileledger.utils.Logger.debug;
 
 public class AccountSummaryFragment extends MobileLedgerListFragment {
@@ -50,7 +56,6 @@ public class AccountSummaryFragment extends MobileLedgerListFragment {
     public void onAttach(@NotNull Context context) {
         super.onAttach(context);
         debug("flow", "AccountSummaryFragment.onAttach()");
-        mActivity = (MainActivity) context;
     }
     @Override
     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@@ -69,28 +74,39 @@ public class AccountSummaryFragment extends MobileLedgerListFragment {
                 this::onBackgroundTaskRunningChanged);
 
         modelAdapter = new AccountSummaryAdapter();
+        MainActivity mainActivity = getMainActivity();
 
-        root = mActivity.findViewById(R.id.account_root);
-        LinearLayoutManager llm = new LinearLayoutManager(mActivity);
+        root = mainActivity.findViewById(R.id.account_root);
+        LinearLayoutManager llm = new LinearLayoutManager(mainActivity);
         llm.setOrientation(RecyclerView.VERTICAL);
         root.setLayoutManager(llm);
         root.setAdapter(modelAdapter);
         DividerItemDecoration did =
-                new DividerItemDecoration(mActivity, DividerItemDecoration.VERTICAL);
+                new DividerItemDecoration(mainActivity, DividerItemDecoration.VERTICAL);
         root.addItemDecoration(did);
 
-        mActivity.fabShouldShow();
+        mainActivity.fabShouldShow();
 
         manageFabOnScroll();
 
-        swiper = mActivity.findViewById(R.id.account_swiper);
+        swiper = mainActivity.findViewById(R.id.account_swiper);
         Colors.themeWatch.observe(getViewLifecycleOwner(), this::themeChanged);
         swiper.setOnRefreshListener(() -> {
             debug("ui", "refreshing accounts via swipe");
-            Data.scheduleTransactionListRetrieval(mActivity);
+            Data.scheduleTransactionListRetrieval(mainActivity);
         });
 
-        Data.accounts.addObserver(
-                (o, arg) -> mActivity.runOnUiThread(() -> modelAdapter.notifyDataSetChanged()));
+        MobileLedgerProfile profile = Data.profile.getValue();
+        if (profile != null) {
+            profile.getDisplayedAccounts()
+                   .observe(getViewLifecycleOwner(),
+                           (accounts) -> onAccountsChanged(profile, accounts));
+        }
+    }
+    private void onAccountsChanged(MobileLedgerProfile profile, List<LedgerAccount> accounts) {
+        Logger.debug("async-acc",
+                String.format(Locale.US, "fragment: got new account list (%d items)",
+                        accounts.size()));
+        modelAdapter.setAccounts(profile, accounts);
     }
 }