X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fui%2Faccount_summary%2FAccountSummaryFragment.java;h=35a9edb7205c84bd16889468d90416b6018d547b;hb=2e58db0c8edde65561f40cdd386cac74ce6fbd67;hp=9654f140f5739cdfe800e939e0eef4f535e24590;hpb=32479d6cca6ffb6b74edbd4b884b9062d42fcbff;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 9654f140..35a9edb7 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 @@ -29,25 +29,33 @@ import androidx.lifecycle.ViewModelProvider; import androidx.recyclerview.widget.DividerItemDecoration; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; +import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; -import net.ktnx.mobileledger.R; +import net.ktnx.mobileledger.async.GeneralBackgroundTasks; +import net.ktnx.mobileledger.databinding.AccountSummaryFragmentBinding; +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; public class AccountSummaryFragment extends MobileLedgerListFragment { public AccountSummaryAdapter modelAdapter; + private AccountSummaryFragmentBinding b; @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -62,12 +70,15 @@ public class AccountSummaryFragment extends MobileLedgerListFragment { public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { debug("flow", "AccountSummaryFragment.onCreateView()"); - return inflater.inflate(R.layout.account_summary_fragment, container, false); + b = AccountSummaryFragmentBinding.inflate(inflater, container, false); + return b.getRoot(); } - @Override - - public void onViewCreated(@Nullable View view, @Nullable Bundle savedInstanceState) { + public SwipeRefreshLayout getRefreshLayout() { + return b.accountSwipeRefreshLayout; + } + @Override + public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { debug("flow", "AccountSummaryFragment.onActivityCreated()"); super.onViewCreated(view, savedInstanceState); @@ -76,36 +87,55 @@ public class AccountSummaryFragment extends MobileLedgerListFragment { Data.backgroundTasksRunning.observe(this.getViewLifecycleOwner(), this::onBackgroundTaskRunningChanged); - modelAdapter = new AccountSummaryAdapter(model); + modelAdapter = new AccountSummaryAdapter(); MainActivity mainActivity = getMainActivity(); - root = mainActivity.findViewById(R.id.account_root); LinearLayoutManager llm = new LinearLayoutManager(mainActivity); llm.setOrientation(RecyclerView.VERTICAL); - root.setLayoutManager(llm); - root.setAdapter(modelAdapter); + b.accountRoot.setLayoutManager(llm); + b.accountRoot.setAdapter(modelAdapter); DividerItemDecoration did = new DividerItemDecoration(mainActivity, DividerItemDecoration.VERTICAL); - root.addItemDecoration(did); + b.accountRoot.addItemDecoration(did); mainActivity.fabShouldShow(); - manageFabOnScroll(); + if (mainActivity instanceof FabManager.FabHandler) + FabManager.handle(mainActivity, b.accountRoot); - refreshLayout = mainActivity.findViewById(R.id.account_swipe_refresh_layout); Colors.themeWatch.observe(getViewLifecycleOwner(), this::themeChanged); - refreshLayout.setOnRefreshListener(() -> { + b.accountSwipeRefreshLayout.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 -> GeneralBackgroundTasks.run(() -> { + 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); + })); } }