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.App;
40 import net.ktnx.mobileledger.R;
41 import net.ktnx.mobileledger.async.GeneralBackgroundTasks;
42 import net.ktnx.mobileledger.databinding.AccountSummaryFragmentBinding;
43 import net.ktnx.mobileledger.db.AccountWithAmounts;
44 import net.ktnx.mobileledger.db.DB;
45 import net.ktnx.mobileledger.db.Profile;
46 import net.ktnx.mobileledger.model.AccountListItem;
47 import net.ktnx.mobileledger.model.Data;
48 import net.ktnx.mobileledger.model.LedgerAccount;
49 import net.ktnx.mobileledger.ui.FabManager;
50 import net.ktnx.mobileledger.ui.MainModel;
51 import net.ktnx.mobileledger.ui.MobileLedgerListFragment;
52 import net.ktnx.mobileledger.ui.activity.MainActivity;
53 import net.ktnx.mobileledger.utils.Colors;
55 import org.jetbrains.annotations.NotNull;
57 import java.util.ArrayList;
58 import java.util.HashMap;
59 import java.util.List;
61 public class AccountSummaryFragment extends MobileLedgerListFragment {
62 public AccountSummaryAdapter modelAdapter;
63 private AccountSummaryFragmentBinding b;
64 private MenuItem menuShowZeroBalances;
65 private MainModel model;
67 public void onCreate(@Nullable Bundle savedInstanceState) {
68 super.onCreate(savedInstanceState);
69 debug("flow", "AccountSummaryFragment.onCreate()");
70 setHasOptionsMenu(true);
72 public void onAttach(@NotNull Context context) {
73 super.onAttach(context);
74 debug("flow", "AccountSummaryFragment.onAttach()");
77 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
78 @Nullable Bundle savedInstanceState) {
79 debug("flow", "AccountSummaryFragment.onCreateView()");
80 b = AccountSummaryFragmentBinding.inflate(inflater, container, false);
84 public SwipeRefreshLayout getRefreshLayout() {
85 return b.accountSwipeRefreshLayout;
88 public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
89 debug("flow", "AccountSummaryFragment.onActivityCreated()");
90 super.onViewCreated(view, savedInstanceState);
92 model = new ViewModelProvider(requireActivity()).get(MainModel.class);
94 Data.backgroundTasksRunning.observe(this.getViewLifecycleOwner(),
95 this::onBackgroundTaskRunningChanged);
97 modelAdapter = new AccountSummaryAdapter();
98 MainActivity mainActivity = getMainActivity();
100 LinearLayoutManager llm = new LinearLayoutManager(mainActivity);
101 llm.setOrientation(RecyclerView.VERTICAL);
102 b.accountRoot.setLayoutManager(llm);
103 b.accountRoot.setAdapter(modelAdapter);
104 DividerItemDecoration did =
105 new DividerItemDecoration(mainActivity, DividerItemDecoration.VERTICAL);
106 b.accountRoot.addItemDecoration(did);
108 mainActivity.fabShouldShow();
110 if (mainActivity instanceof FabManager.FabHandler)
111 FabManager.handle(mainActivity, b.accountRoot);
113 Colors.themeWatch.observe(getViewLifecycleOwner(), this::themeChanged);
114 b.accountSwipeRefreshLayout.setOnRefreshListener(() -> {
115 debug("ui", "refreshing accounts via swipe");
116 model.scheduleTransactionListRetrieval();
119 Data.observeProfile(this, profile -> onProfileChanged(profile, Boolean.TRUE.equals(
120 model.getShowZeroBalanceAccounts()
122 model.getShowZeroBalanceAccounts()
123 .setValue(App.getShowZeroBalanceAccounts());
126 public void onCreateOptionsMenu(@NotNull Menu menu, @NotNull MenuInflater inflater) {
127 inflater.inflate(R.menu.account_list, menu);
129 menuShowZeroBalances = menu.findItem(R.id.menu_account_list_show_zero_balances);
130 if ((menuShowZeroBalances == null))
131 throw new AssertionError();
133 menuShowZeroBalances.setOnMenuItemClickListener(menuItem -> {
134 model.getShowZeroBalanceAccounts()
135 .setValue(Boolean.FALSE.equals(model.getShowZeroBalanceAccounts()
140 model.getShowZeroBalanceAccounts()
141 .observe(this, v -> {
142 menuShowZeroBalances.setChecked(v);
143 onProfileChanged(Data.getProfile(), v);
144 App.storeShowZeroBalanceAccounts(v);
147 super.onCreateOptionsMenu(menu, inflater);
149 private void onProfileChanged(Profile profile, boolean showZeroBalanceAccounts) {
155 .getAllWithAmounts(profile.getId(), showZeroBalanceAccounts)
156 .observe(getViewLifecycleOwner(), list -> GeneralBackgroundTasks.run(() -> {
157 List<AccountListItem> adapterList = new ArrayList<>();
158 adapterList.add(new AccountListItem.Header(Data.lastAccountsUpdateText));
159 HashMap<String, LedgerAccount> accMap = new HashMap<>();
160 for (AccountWithAmounts dbAcc : list) {
161 LedgerAccount parent = null;
162 String parentName = dbAcc.account.getParentName();
163 if (parentName != null)
164 parent = accMap.get(parentName);
166 parent.setHasSubAccounts(true);
167 final LedgerAccount account = LedgerAccount.fromDBO(dbAcc, parent);
168 if (account.isVisible())
169 adapterList.add(new AccountListItem.Account(account));
170 accMap.put(dbAcc.account.getName(), account);
173 if (!showZeroBalanceAccounts) {
174 removeZeroAccounts(adapterList);
176 modelAdapter.setAccounts(adapterList);
177 Data.lastUpdateAccountCount.postValue(adapterList.size() - 1);
180 private void removeZeroAccounts(List<AccountListItem> list) {
181 boolean removed = true;
184 AccountListItem last = null;
186 List<AccountListItem> newList = new ArrayList<>();
188 for (AccountListItem item : list) {
194 if (!last.isAccount() || !last.toAccount()
195 .allAmountsAreZero() || last.toAccount()
211 if (!last.isAccount() || !last.toAccount()
212 .allAmountsAreZero())
222 list.addAll(newList);