+ mAccount.toggleAmountsExpanded();
+ if (mAccount.amountsExpanded()) {
+ tvAccountAmounts.setText(mAccount.getAmountsString());
+ amountExpanderContainer.setVisibility(View.GONE);
+ }
+ else {
+ tvAccountAmounts.setText(mAccount.getAmountsString(AMOUNT_LIMIT));
+ amountExpanderContainer.setVisibility(View.VISIBLE);
+ }
+
+ MobileLedgerProfile profile = mAccount.getProfile();
+ if (profile == null)
+ return;
+
+ DbOpQueue.add("update accounts set amounts_expanded=? where name=? and profile=?",
+ new Object[]{mAccount.amountsExpanded(), mAccount.getName(), profile.getUuid()
+ });
+
+ }
+ private boolean onItemLongClick(View v) {
+ MainActivity activity = (MainActivity) v.getContext();
+ AlertDialog.Builder builder = new AlertDialog.Builder(activity);
+ final String accountName = mAccount.getName();
+ builder.setTitle(accountName);
+ builder.setItems(R.array.acc_ctx_menu, (dialog, which) -> {
+ if (which == 0) {// show transactions
+ activity.showAccountTransactions(accountName);
+ }
+ else {
+ throw new RuntimeException(String.format("Unknown menu item id (%d)", which));
+ }
+ dialog.dismiss();
+ });
+ builder.show();
+ return true;
+ }
+ public void bindToAccount(AccountListItem item) {
+ final AccountListItem.Type newType = item.getType();
+ setType(newType);
+
+ switch (newType) {
+ case ACCOUNT:
+ LedgerAccount acc = item.getAccount();
+
+ debug("accounts", String.format(Locale.US, "Binding to '%s'", acc.getName()));
+ Context ctx = row.getContext();
+ Resources rm = ctx.getResources();
+ mAccount = acc;
+
+ row.setTag(acc);
+
+ tvAccountName.setText(acc.getShortName());
+
+ ConstraintLayout.LayoutParams lp =
+ (ConstraintLayout.LayoutParams) tvAccountName.getLayoutParams();
+ lp.setMarginStart(
+ acc.getLevel() * rm.getDimensionPixelSize(R.dimen.thumb_row_height) /
+ 3);
+
+ if (acc.hasSubAccounts()) {
+ expanderContainer.setVisibility(View.VISIBLE);
+ expanderContainer.setRotation(acc.isExpanded() ? 0 : 180);
+ }
+ else {
+ expanderContainer.setVisibility(View.GONE);
+ }
+
+ int amounts = acc.getAmountCount();
+ if ((amounts > AMOUNT_LIMIT) && !acc.amountsExpanded()) {
+ tvAccountAmounts.setText(acc.getAmountsString(AMOUNT_LIMIT));
+ amountExpanderContainer.setVisibility(View.VISIBLE);
+ }
+ else {
+ tvAccountAmounts.setText(acc.getAmountsString());
+ amountExpanderContainer.setVisibility(View.GONE);
+ }
+
+ break;
+ case HEADER:
+ setLastUpdateText(Data.lastAccountsUpdateText.get());
+ break;
+ default:
+ throw new IllegalStateException("Unexpected value: " + newType);
+ }
+
+ }
+ void setLastUpdateText(String text) {
+ tvLastUpdate.setText(text);
+ }
+ private void initLastUpdateObserver() {
+ if (lastUpdateObserver != null)
+ return;
+
+ lastUpdateObserver = (o, arg) -> setLastUpdateText(Data.lastAccountsUpdateText.get());
+
+ Data.lastAccountsUpdateText.addObserver(lastUpdateObserver);
+ }
+ private void dropLastUpdateObserver() {
+ if (lastUpdateObserver == null)
+ return;
+
+ Data.lastAccountsUpdateText.deleteObserver(lastUpdateObserver);
+ lastUpdateObserver = null;
+ }
+ private void setType(AccountListItem.Type newType) {
+ if (newType == lastType)
+ return;
+
+ switch (newType) {
+ case ACCOUNT:
+ row.setLongClickable(true);
+ amountExpanderContainer.setVisibility(View.VISIBLE);
+ vAccountNameLayout.setVisibility(View.VISIBLE);
+ tvAccountAmounts.setVisibility(View.VISIBLE);
+ lLastUpdate.setVisibility(View.GONE);
+ dropLastUpdateObserver();
+ break;
+ case HEADER:
+ row.setLongClickable(false);
+ tvAccountAmounts.setVisibility(View.GONE);
+ amountExpanderContainer.setVisibility(View.GONE);
+ vAccountNameLayout.setVisibility(View.GONE);
+ lLastUpdate.setVisibility(View.VISIBLE);
+ initLastUpdateObserver();
+ break;
+ default:
+ throw new IllegalStateException("Unexpected value: " + newType);
+ }
+
+ lastType = newType;