+ super.onCreateOptionsMenu(menu, inflater);
+ }
+ private void onProfileChanged(Profile profile, boolean showZeroBalanceAccounts) {
+ if (profile == null)
+ return;
+
+ DB.get()
+ .getAccountDAO()
+ .getAllWithAmounts(profile.getId(), showZeroBalanceAccounts)
+ .observe(getViewLifecycleOwner(), list -> GeneralBackgroundTasks.run(() -> {
+ List<AccountListItem> adapterList = new ArrayList<>();
+ adapterList.add(new AccountListItem.Header(Data.lastAccountsUpdateText));
+ HashMap<String, LedgerAccount> 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);
+ }
+
+ if (!showZeroBalanceAccounts) {
+ removeZeroAccounts(adapterList);
+ }
+ modelAdapter.setAccounts(adapterList);
+ Data.lastUpdateAccountCount.postValue(adapterList.size() - 1);
+ }));
+ }
+ private void removeZeroAccounts(List<AccountListItem> list) {
+ boolean removed = true;
+
+ while (removed) {
+ AccountListItem last = null;
+ removed = false;
+ List<AccountListItem> newList = new ArrayList<>();
+
+ for (AccountListItem item : list) {
+ if (last == null) {
+ last = item;
+ continue;
+ }
+
+ if (!last.isAccount() || !last.toAccount()
+ .allAmountsAreZero() || last.toAccount()
+ .getAccount()
+ .isParentOf(
+ item.toAccount()
+ .getAccount()))
+ {
+ newList.add(last);
+ }
+ else {
+ removed = true;
+ }
+
+ last = item;
+ }
+
+ if (last != null) {
+ if (!last.isAccount() || !last.toAccount()
+ .allAmountsAreZero())
+ {
+ newList.add(last);
+ }
+ else {
+ removed = true;
+ }
+ }
+
+ list.clear();
+ list.addAll(newList);
+ }