+ Data.observeProfile(this, profile -> onProfileChanged(profile, Boolean.TRUE.equals(
+ model.getShowZeroBalanceAccounts()
+ .getValue())));
+ model.getShowZeroBalanceAccounts()
+ .setValue(App.getShowZeroBalanceAccounts());
+ }
+ @Override
+ public void onCreateOptionsMenu(@NotNull Menu menu, @NotNull MenuInflater inflater) {
+ inflater.inflate(R.menu.account_list, menu);
+
+ menuShowZeroBalances = menu.findItem(R.id.menu_account_list_show_zero_balances);
+ if ((menuShowZeroBalances == null))
+ throw new AssertionError();
+
+ menuShowZeroBalances.setOnMenuItemClickListener(menuItem -> {
+ model.getShowZeroBalanceAccounts()
+ .setValue(Boolean.FALSE.equals(model.getShowZeroBalanceAccounts()
+ .getValue()));
+ return true;
+ });
+
+ model.getShowZeroBalanceAccounts()
+ .observe(this, v -> {
+ menuShowZeroBalances.setChecked(v);
+ onProfileChanged(Data.getProfile(), v);
+ App.storeShowZeroBalanceAccounts(v);
+ });
+
+ 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);
+ }));