]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java
more pronounced day/month delimiters in the transaction list
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / account_summary / AccountSummaryFragment.java
1 /*
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.ktnx.mobileledger.ui.account_summary;
19
20 import static net.ktnx.mobileledger.utils.Logger.debug;
21
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;
30
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;
38
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;
53
54 import org.jetbrains.annotations.NotNull;
55
56 import java.util.ArrayList;
57 import java.util.HashMap;
58 import java.util.List;
59
60 public class AccountSummaryFragment extends MobileLedgerListFragment {
61     public AccountSummaryAdapter modelAdapter;
62     private AccountSummaryFragmentBinding b;
63     private MenuItem menuShowZeroBalances;
64     private MainModel model;
65     @Override
66     public void onCreate(@Nullable Bundle savedInstanceState) {
67         super.onCreate(savedInstanceState);
68         debug("flow", "AccountSummaryFragment.onCreate()");
69         setHasOptionsMenu(true);
70     }
71     public void onAttach(@NotNull Context context) {
72         super.onAttach(context);
73         debug("flow", "AccountSummaryFragment.onAttach()");
74     }
75     @Override
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);
80         return b.getRoot();
81     }
82     @Override
83     public SwipeRefreshLayout getRefreshLayout() {
84         return b.accountSwipeRefreshLayout;
85     }
86     @Override
87     public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
88         debug("flow", "AccountSummaryFragment.onActivityCreated()");
89         super.onViewCreated(view, savedInstanceState);
90
91         model = new ViewModelProvider(requireActivity()).get(MainModel.class);
92
93         Data.backgroundTasksRunning.observe(this.getViewLifecycleOwner(),
94                 this::onBackgroundTaskRunningChanged);
95
96         modelAdapter = new AccountSummaryAdapter();
97         MainActivity mainActivity = getMainActivity();
98
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);
106
107         mainActivity.fabShouldShow();
108
109         if (mainActivity instanceof FabManager.FabHandler)
110             FabManager.handle(mainActivity, b.accountRoot);
111
112         Colors.themeWatch.observe(getViewLifecycleOwner(), this::themeChanged);
113         b.accountSwipeRefreshLayout.setOnRefreshListener(() -> {
114             debug("ui", "refreshing accounts via swipe");
115             model.scheduleTransactionListRetrieval();
116         });
117
118         Data.observeProfile(this, profile -> onProfileChanged(profile, Boolean.TRUE.equals(
119                 model.getShowZeroBalanceAccounts()
120                      .getValue())));
121     }
122     @Override
123     public void onCreateOptionsMenu(@NotNull Menu menu, @NotNull MenuInflater inflater) {
124         inflater.inflate(R.menu.account_list, menu);
125
126         menuShowZeroBalances = menu.findItem(R.id.menu_account_list_show_zero_balances);
127         if ((menuShowZeroBalances == null))
128             throw new AssertionError();
129
130         menuShowZeroBalances.setOnMenuItemClickListener(menuItem -> {
131             model.getShowZeroBalanceAccounts()
132                  .setValue(Boolean.FALSE.equals(model.getShowZeroBalanceAccounts()
133                                                      .getValue()));
134             return true;
135         });
136
137         model.getShowZeroBalanceAccounts()
138              .observe(this, v -> {
139                  menuShowZeroBalances.setChecked(v);
140                  onProfileChanged(Data.getProfile(), v);
141              });
142
143         super.onCreateOptionsMenu(menu, inflater);
144     }
145     private void onProfileChanged(Profile profile, boolean showZeroBalanceAccounts) {
146         if (profile == null)
147             return;
148
149         DB.get()
150           .getAccountDAO()
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);
161                   if (parent != null)
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);
167               }
168
169               if (!showZeroBalanceAccounts) {
170                   removeZeroAccounts(adapterList);
171               }
172               modelAdapter.setAccounts(adapterList);
173               Data.lastUpdateAccountCount.postValue(adapterList.size() - 1);
174           }));
175     }
176     private void removeZeroAccounts(List<AccountListItem> list) {
177         boolean removed = true;
178
179         while (removed) {
180             AccountListItem last = null;
181             removed = false;
182             List<AccountListItem> newList = new ArrayList<>();
183
184             for (AccountListItem item : list) {
185                 if (last == null) {
186                     last = item;
187                     continue;
188                 }
189
190                 if (!last.isAccount() || !last.toAccount()
191                                               .allAmountsAreZero() || last.toAccount()
192                                                                           .getAccount()
193                                                                           .isParentOf(
194                                                                                   item.toAccount()
195                                                                                       .getAccount()))
196                 {
197                     newList.add(last);
198                 }
199                 else {
200                     removed = true;
201                 }
202
203                 last = item;
204             }
205
206             if (last != null) {
207                 if (!last.isAccount() || !last.toAccount()
208                                               .allAmountsAreZero())
209                 {
210                     newList.add(last);
211                 }
212                 else {
213                     removed = true;
214                 }
215             }
216
217             list.clear();
218             list.addAll(newList);
219         }
220     }
221 }