]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java
major rework of parsed transaction/descriptions/accounts storage
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / account_summary / AccountSummaryFragment.java
index 9f2344fb605bbad3c2a09cc3a4110b9a64108592..29003b820f484ea75c06bb24a15a578bc83eddd7 100644 (file)
@@ -25,6 +25,7 @@ import android.view.ViewGroup;
 
 import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
+import androidx.lifecycle.ViewModelProvider;
 import androidx.recyclerview.widget.DividerItemDecoration;
 import androidx.recyclerview.widget.LinearLayoutManager;
 import androidx.recyclerview.widget.RecyclerView;
@@ -32,7 +33,7 @@ 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.MainModel;
 import net.ktnx.mobileledger.ui.MobileLedgerListFragment;
 import net.ktnx.mobileledger.ui.activity.MainActivity;
 import net.ktnx.mobileledger.utils.Colors;
@@ -40,7 +41,8 @@ import net.ktnx.mobileledger.utils.Logger;
 
 import org.jetbrains.annotations.NotNull;
 
-import java.util.ArrayList;
+import java.util.List;
+import java.util.Locale;
 
 import static net.ktnx.mobileledger.utils.Logger.debug;
 
@@ -69,10 +71,12 @@ public class AccountSummaryFragment extends MobileLedgerListFragment {
         debug("flow", "AccountSummaryFragment.onActivityCreated()");
         super.onActivityCreated(savedInstanceState);
 
+        MainModel model = new ViewModelProvider(requireActivity()).get(MainModel.class);
+
         Data.backgroundTasksRunning.observe(this.getViewLifecycleOwner(),
                 this::onBackgroundTaskRunningChanged);
 
-        modelAdapter = new AccountSummaryAdapter();
+        modelAdapter = new AccountSummaryAdapter(model);
         MainActivity mainActivity = getMainActivity();
 
         root = mainActivity.findViewById(R.id.account_root);
@@ -88,22 +92,20 @@ public class AccountSummaryFragment extends MobileLedgerListFragment {
 
         manageFabOnScroll();
 
-        swiper = mainActivity.findViewById(R.id.account_swiper);
+        refreshLayout = mainActivity.findViewById(R.id.account_swipe_refresh_layout);
         Colors.themeWatch.observe(getViewLifecycleOwner(), this::themeChanged);
-        swiper.setOnRefreshListener(() -> {
+        refreshLayout.setOnRefreshListener(() -> {
             debug("ui", "refreshing accounts via swipe");
-            Data.scheduleTransactionListRetrieval(mainActivity);
+            model.scheduleTransactionListRetrieval();
         });
 
-        Data.profile.observe(getViewLifecycleOwner(), profile -> profile.getAccounts()
-                                                                        .observe(
-                                                                                getViewLifecycleOwner(),
-                                                                                (accounts) -> onAccountsChanged(
-                                                                                        profile,
-                                                                                        accounts)));
+        model.getDisplayedAccounts()
+             .observe(getViewLifecycleOwner(), this::onAccountsChanged);
     }
-    private void onAccountsChanged(MobileLedgerProfile profile, ArrayList<LedgerAccount> accounts) {
-        Logger.debug("async-acc", "fragment: got new account list");
-        modelAdapter.setAccounts(profile, accounts);
+    private void onAccountsChanged(List<LedgerAccount> accounts) {
+        Logger.debug("async-acc",
+                String.format(Locale.US, "fragment: got new account list (%d items)",
+                        accounts.size()));
+        modelAdapter.setAccounts(accounts);
     }
 }