- public void onConfirmAccSelection(MenuItem item) {
- AccountSummaryViewModel.commitSelections(mActivity);
- stopSelection();
- }
- @Override
- public void onCreateOptionsMenu(@NotNull Menu menu, @NotNull MenuInflater inflater) {
- // Inflate the menu; this adds items to the action bar if it is present.
- inflater.inflate(R.menu.account_summary, menu);
- optMenu = menu;
-
- mShowOnlyStarred = menu.findItem(R.id.menu_acc_summary_only_starred);
- if (mShowOnlyStarred == null) throw new AssertionError();
- MenuItem mCancelSelection = menu.findItem(R.id.menu_acc_summary_cancel_selection);
- if (mCancelSelection == null) throw new AssertionError();
- MenuItem mConfirmSelection = menu.findItem(R.id.menu_acc_summary_confirm_selection);
- if (mConfirmSelection == null) throw new AssertionError();
-
- Data.optShowOnlyStarred.addObserver((o, arg) -> {
- boolean newValue = Data.optShowOnlyStarred.get();
- debug("pref", String.format("pref change came (%s)", newValue ? "true" : "false"));
- mShowOnlyStarred.setChecked(newValue);
- update_account_table();
- });
-
- mShowOnlyStarred.setChecked(Data.optShowOnlyStarred.get());
-
- debug("menu", "Accounts: onCreateOptionsMenu called");
-
- mShowOnlyStarred.setOnMenuItemClickListener(item -> {
- SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(mActivity);
- SharedPreferences.Editor editor = pref.edit();
- boolean flag = item.isChecked();
- editor.putBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, !flag);
- debug("pref",
- "Setting show only starred accounts pref to " + (flag ? "false" : "true"));
- editor.apply();
-
- return true;
- });
-
- mCancelSelection.setOnMenuItemClickListener(item -> {
- stopSelection();
- return true;
- });
-
- mConfirmSelection.setOnMenuItemClickListener(item -> {
- AccountSummaryViewModel.commitSelections(mActivity);
- stopSelection();
-
- return true;
- });
+ 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);
+ }));