]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java
separate FAB management in a helper class
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / account_summary / AccountSummaryFragment.java
index dbc200eb8f6e8ff592ae02048c9682efd8326877..d1aca96d8f48907d21b409ac24fbc83f1a6ee162 100644 (file)
@@ -25,14 +25,16 @@ 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;
 
 import net.ktnx.mobileledger.R;
+import net.ktnx.mobileledger.model.AccountListItem;
 import net.ktnx.mobileledger.model.Data;
-import net.ktnx.mobileledger.model.LedgerAccount;
-import net.ktnx.mobileledger.model.MobileLedgerProfile;
+import net.ktnx.mobileledger.ui.FabManager;
+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;
@@ -65,18 +67,19 @@ public class AccountSummaryFragment extends MobileLedgerListFragment {
     }
 
     @Override
-
-    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
+    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
         debug("flow", "AccountSummaryFragment.onActivityCreated()");
-        super.onActivityCreated(savedInstanceState);
+        super.onViewCreated(view, 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);
+        root = view.findViewById(R.id.account_root);
         LinearLayoutManager llm = new LinearLayoutManager(mainActivity);
         llm.setOrientation(RecyclerView.VERTICAL);
         root.setLayoutManager(llm);
@@ -87,26 +90,23 @@ public class AccountSummaryFragment extends MobileLedgerListFragment {
 
         mainActivity.fabShouldShow();
 
-        manageFabOnScroll();
+        if (mainActivity instanceof FabManager.FabHandler)
+            FabManager.handle((FabManager.FabHandler) mainActivity, root);
 
-        swiper = mainActivity.findViewById(R.id.account_swiper);
+        refreshLayout = view.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();
         });
 
-        MobileLedgerProfile profile = Data.profile.getValue();
-        if (profile != null) {
-            profile.getDisplayedAccounts()
-                   .observe(getViewLifecycleOwner(),
-                           (accounts) -> onAccountsChanged(profile, accounts));
-        }
+        model.getDisplayedAccounts()
+             .observe(getViewLifecycleOwner(), this::onAccountsChanged);
     }
-    private void onAccountsChanged(MobileLedgerProfile profile, List<LedgerAccount> accounts) {
+    private void onAccountsChanged(List<AccountListItem> accounts) {
         Logger.debug("async-acc",
                 String.format(Locale.US, "fragment: got new account list (%d items)",
                         accounts.size()));
-        modelAdapter.setAccounts(profile, accounts);
+        modelAdapter.setAccounts(accounts);
     }
 }