- private void onAccountsChanged(MobileLedgerProfile profile, List<LedgerAccount> accounts) {
- Logger.debug("async-acc",
- String.format(Locale.US, "fragment: got new account list (%d items)",
- accounts.size()));
- modelAdapter.setAccounts(profile, accounts);
+ private void onProfileChanged(Profile profile) {
+ if (profile == null)
+ return;
+
+ DB.get()
+ .getAccountDAO()
+ .getAllWithAmounts(profile.getId())
+ .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);
+ }
+ modelAdapter.setAccounts(adapterList);
+ Data.lastUpdateAccountCount.postValue(adapterList.size() - 1);
+ }));