2 * Copyright © 2024 Damyan Ivanov.
3 * This file is part of MoLe.
4 * MoLe is free software: you can distribute it and/or modify it
5 * under the term of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your opinion), any later version.
9 * MoLe is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License terms for details.
14 * You should have received a copy of the GNU General Public License
15 * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
18 package net.ktnx.mobileledger.ui.account_summary;
20 import static net.ktnx.mobileledger.utils.Logger.debug;
22 import android.content.Context;
23 import android.os.Bundle;
24 import android.view.LayoutInflater;
25 import android.view.Menu;
26 import android.view.MenuInflater;
27 import android.view.MenuItem;
28 import android.view.View;
29 import android.view.ViewGroup;
31 import androidx.annotation.NonNull;
32 import androidx.annotation.Nullable;
33 import androidx.lifecycle.ViewModelProvider;
34 import androidx.recyclerview.widget.DividerItemDecoration;
35 import androidx.recyclerview.widget.LinearLayoutManager;
36 import androidx.recyclerview.widget.RecyclerView;
37 import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
39 import net.ktnx.mobileledger.R;
40 import net.ktnx.mobileledger.async.GeneralBackgroundTasks;
41 import net.ktnx.mobileledger.databinding.AccountSummaryFragmentBinding;
42 import net.ktnx.mobileledger.db.AccountWithAmounts;
43 import net.ktnx.mobileledger.db.DB;
44 import net.ktnx.mobileledger.db.Profile;
45 import net.ktnx.mobileledger.model.AccountListItem;
46 import net.ktnx.mobileledger.model.Data;
47 import net.ktnx.mobileledger.model.LedgerAccount;
48 import net.ktnx.mobileledger.ui.FabManager;
49 import net.ktnx.mobileledger.ui.MainModel;
50 import net.ktnx.mobileledger.ui.MobileLedgerListFragment;
51 import net.ktnx.mobileledger.ui.activity.MainActivity;
52 import net.ktnx.mobileledger.utils.Colors;
54 import org.jetbrains.annotations.NotNull;
56 import java.util.ArrayList;
57 import java.util.HashMap;
58 import java.util.List;
60 public class AccountSummaryFragment extends MobileLedgerListFragment {
61 public AccountSummaryAdapter modelAdapter;
62 private AccountSummaryFragmentBinding b;
63 private MenuItem menuShowZeroBalances;
64 private MainModel model;
66 public void onCreate(@Nullable Bundle savedInstanceState) {
67 super.onCreate(savedInstanceState);
68 debug("flow", "AccountSummaryFragment.onCreate()");
69 setHasOptionsMenu(true);
71 public void onAttach(@NotNull Context context) {
72 super.onAttach(context);
73 debug("flow", "AccountSummaryFragment.onAttach()");
76 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
77 @Nullable Bundle savedInstanceState) {
78 debug("flow", "AccountSummaryFragment.onCreateView()");
79 b = AccountSummaryFragmentBinding.inflate(inflater, container, false);
83 public SwipeRefreshLayout getRefreshLayout() {
84 return b.accountSwipeRefreshLayout;
87 public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
88 debug("flow", "AccountSummaryFragment.onActivityCreated()");
89 super.onViewCreated(view, savedInstanceState);
91 model = new ViewModelProvider(requireActivity()).get(MainModel.class);
93 Data.backgroundTasksRunning.observe(this.getViewLifecycleOwner(),
94 this::onBackgroundTaskRunningChanged);
96 modelAdapter = new AccountSummaryAdapter();
97 MainActivity mainActivity = getMainActivity();
99 LinearLayoutManager llm = new LinearLayoutManager(mainActivity);
100 llm.setOrientation(RecyclerView.VERTICAL);
101 b.accountRoot.setLayoutManager(llm);
102 b.accountRoot.setAdapter(modelAdapter);
103 DividerItemDecoration did =
104 new DividerItemDecoration(mainActivity, DividerItemDecoration.VERTICAL);
105 b.accountRoot.addItemDecoration(did);
107 mainActivity.fabShouldShow();
109 if (mainActivity instanceof FabManager.FabHandler)
110 FabManager.handle(mainActivity, b.accountRoot);
112 Colors.themeWatch.observe(getViewLifecycleOwner(), this::themeChanged);
113 b.accountSwipeRefreshLayout.setOnRefreshListener(() -> {
114 debug("ui", "refreshing accounts via swipe");
115 model.scheduleTransactionListRetrieval();
118 Data.observeProfile(this, profile -> onProfileChanged(profile, Boolean.TRUE.equals(
119 model.getShowZeroBalanceAccounts()
123 public void onCreateOptionsMenu(@NotNull Menu menu, @NotNull MenuInflater inflater) {
124 inflater.inflate(R.menu.account_list, menu);
126 menuShowZeroBalances = menu.findItem(R.id.menu_account_list_show_zero_balances);
127 if ((menuShowZeroBalances == null))
128 throw new AssertionError();
130 menuShowZeroBalances.setOnMenuItemClickListener(menuItem -> {
131 model.getShowZeroBalanceAccounts()
132 .setValue(Boolean.FALSE.equals(model.getShowZeroBalanceAccounts()
137 model.getShowZeroBalanceAccounts()
138 .observe(this, v -> {
139 menuShowZeroBalances.setChecked(v);
140 onProfileChanged(Data.getProfile(), v);
143 super.onCreateOptionsMenu(menu, inflater);
145 private void onProfileChanged(Profile profile, boolean showZeroBalanceAccounts) {
151 .getAllWithAmounts(profile.getId(), showZeroBalanceAccounts)
152 .observe(getViewLifecycleOwner(), list -> GeneralBackgroundTasks.run(() -> {
153 List<AccountListItem> adapterList = new ArrayList<>();
154 adapterList.add(new AccountListItem.Header(Data.lastAccountsUpdateText));
155 HashMap<String, LedgerAccount> accMap = new HashMap<>();
156 for (AccountWithAmounts dbAcc : list) {
157 LedgerAccount parent = null;
158 String parentName = dbAcc.account.getParentName();
159 if (parentName != null)
160 parent = accMap.get(parentName);
162 parent.setHasSubAccounts(true);
163 final LedgerAccount account = LedgerAccount.fromDBO(dbAcc, parent);
164 if (account.isVisible())
165 adapterList.add(new AccountListItem.Account(account));
166 accMap.put(dbAcc.account.getName(), account);
169 if (!showZeroBalanceAccounts) {
170 removeZeroAccounts(adapterList);
172 modelAdapter.setAccounts(adapterList);
173 Data.lastUpdateAccountCount.postValue(adapterList.size() - 1);
176 private void removeZeroAccounts(List<AccountListItem> list) {
177 boolean removed = true;
180 AccountListItem last = null;
182 List<AccountListItem> newList = new ArrayList<>();
184 for (AccountListItem item : list) {
190 if (!last.isAccount() || !last.toAccount()
191 .allAmountsAreZero() || last.toAccount()
207 if (!last.isAccount() || !last.toAccount()
208 .allAmountsAreZero())
218 list.addAll(newList);