X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fui%2Faccount_summary%2FAccountSummaryFragment.java;h=8ad6da6ff0fa7233613d1ffaa53fd694e99d805e;hb=faa01b22a0f23efe6302d4787c244bb4f241b8da;hp=29003b820f484ea75c06bb24a15a578bc83eddd7;hpb=5bba2c06a81c87327fdcf3f2a85c3206d932c2f9;p=mobile-ledger.git diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java b/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java index 29003b82..8ad6da6f 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java @@ -1,5 +1,5 @@ /* - * Copyright © 2020 Damyan Ivanov. + * Copyright © 2021 Damyan Ivanov. * This file is part of MoLe. * MoLe is free software: you can distribute it and/or modify it * under the term of the GNU General Public License as published by @@ -18,6 +18,7 @@ package net.ktnx.mobileledger.ui.account_summary; import android.content.Context; +import android.os.AsyncTask; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; @@ -31,18 +32,23 @@ import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import net.ktnx.mobileledger.R; +import net.ktnx.mobileledger.db.AccountWithAmounts; +import net.ktnx.mobileledger.db.DB; +import net.ktnx.mobileledger.db.Profile; +import net.ktnx.mobileledger.model.AccountListItem; import net.ktnx.mobileledger.model.Data; import net.ktnx.mobileledger.model.LedgerAccount; +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; -import net.ktnx.mobileledger.utils.Logger; import org.jetbrains.annotations.NotNull; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; -import java.util.Locale; import static net.ktnx.mobileledger.utils.Logger.debug; @@ -66,20 +72,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(model); + modelAdapter = new AccountSummaryAdapter(); 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); @@ -90,22 +95,43 @@ public class AccountSummaryFragment extends MobileLedgerListFragment { mainActivity.fabShouldShow(); - manageFabOnScroll(); + if (mainActivity instanceof FabManager.FabHandler) + FabManager.handle(mainActivity, root); - refreshLayout = mainActivity.findViewById(R.id.account_swipe_refresh_layout); + refreshLayout = view.findViewById(R.id.account_swipe_refresh_layout); Colors.themeWatch.observe(getViewLifecycleOwner(), this::themeChanged); refreshLayout.setOnRefreshListener(() -> { debug("ui", "refreshing accounts via swipe"); model.scheduleTransactionListRetrieval(); }); - model.getDisplayedAccounts() - .observe(getViewLifecycleOwner(), this::onAccountsChanged); + Data.observeProfile(this, this::onProfileChanged); } - private void onAccountsChanged(List accounts) { - Logger.debug("async-acc", - String.format(Locale.US, "fragment: got new account list (%d items)", - accounts.size())); - modelAdapter.setAccounts(accounts); + private void onProfileChanged(Profile profile) { + if (profile == null) + return; + + DB.get() + .getAccountDAO() + .getAllWithAmounts(profile.getId()) + .observe(getViewLifecycleOwner(), list -> AsyncTask.execute(() -> { + List adapterList = new ArrayList<>(); + adapterList.add(new AccountListItem.Header(Data.lastAccountsUpdateText)); + HashMap accMap = new HashMap<>(); + for (AccountWithAmounts dbAcc : list) { + LedgerAccount parent = null; + String parentName = dbAcc.account.getParentName(); + if (parentName != null) + parent = accMap.get(parentName); + if (parent != null) + parent.setHasSubAccounts(true); + final LedgerAccount account = LedgerAccount.fromDBO(dbAcc, parent); + if (account.isVisible()) + adapterList.add(new AccountListItem.Account(account)); + accMap.put(dbAcc.account.getName(), account); + } + modelAdapter.setAccounts(adapterList); + Data.lastUpdateAccountCount.postValue(adapterList.size() - 1); + })); } }