/*
- * Copyright © 2020 Damyan Ivanov.
+ * Copyright © 2021 Damyan Ivanov.
* This file is part of MoLe.
* MoLe is free software: you can distribute it and/or modify it
* under the term of the GNU General Public License as published by
package net.ktnx.mobileledger.ui.account_summary;
-import android.content.Context;
import android.content.res.Resources;
-import android.text.TextUtils;
-import android.util.Log;
+import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
-import android.widget.ImageView;
-import android.widget.TextView;
import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.constraintlayout.widget.ConstraintLayout;
+import androidx.lifecycle.LifecycleOwner;
import androidx.recyclerview.widget.AsyncListDiffer;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;
import net.ktnx.mobileledger.R;
-import net.ktnx.mobileledger.async.DbOpQueue;
+import net.ktnx.mobileledger.databinding.AccountListRowBinding;
+import net.ktnx.mobileledger.databinding.AccountListSummaryRowBinding;
+import net.ktnx.mobileledger.db.Account;
+import net.ktnx.mobileledger.db.DB;
+import net.ktnx.mobileledger.model.AccountListItem;
import net.ktnx.mobileledger.model.LedgerAccount;
-import net.ktnx.mobileledger.model.MobileLedgerProfile;
import net.ktnx.mobileledger.ui.activity.MainActivity;
+import net.ktnx.mobileledger.utils.Logger;
+import net.ktnx.mobileledger.utils.Misc;
import org.jetbrains.annotations.NotNull;
-import java.util.ArrayList;
+import java.util.List;
+import java.util.Locale;
import static net.ktnx.mobileledger.utils.Logger.debug;
-public class AccountSummaryAdapter
- extends RecyclerView.Adapter<AccountSummaryAdapter.LedgerRowHolder> {
+public class AccountSummaryAdapter extends RecyclerView.Adapter<AccountSummaryAdapter.RowHolder> {
public static final int AMOUNT_LIMIT = 3;
- private MobileLedgerProfile profile;
- private AsyncListDiffer<LedgerAccount> listDiffer;
+ private static final int ITEM_TYPE_HEADER = 1;
+ private static final int ITEM_TYPE_ACCOUNT = 2;
+ private final AsyncListDiffer<AccountListItem> listDiffer;
+
AccountSummaryAdapter() {
- listDiffer = new AsyncListDiffer<>(this, new DiffUtil.ItemCallback<LedgerAccount>() {
+ setHasStableIds(true);
+
+ listDiffer = new AsyncListDiffer<>(this, new DiffUtil.ItemCallback<AccountListItem>() {
+ @Nullable
@Override
- public boolean areItemsTheSame(@NotNull LedgerAccount oldItem,
- @NotNull LedgerAccount newItem) {
- return TextUtils.equals(oldItem.getName(), newItem.getName());
+ public Object getChangePayload(@NonNull AccountListItem oldItem,
+ @NonNull AccountListItem newItem) {
+ Change changes = new Change();
+
+ final LedgerAccount oldAcc = oldItem.getAccount();
+ final LedgerAccount newAcc = newItem.getAccount();
+
+ if (!Misc.equalStrings(oldAcc.getName(), newAcc.getName()))
+ changes.add(Change.NAME);
+
+ if (oldAcc.getLevel() != newAcc.getLevel())
+ changes.add(Change.LEVEL);
+
+ if (oldAcc.isExpanded() != newAcc.isExpanded())
+ changes.add(Change.EXPANDED);
+
+ if (oldAcc.amountsExpanded() != newAcc.amountsExpanded())
+ changes.add(Change.EXPANDED_AMOUNTS);
+
+ if (!oldAcc.getAmountsString()
+ .equals(newAcc.getAmountsString()))
+ changes.add(Change.AMOUNTS);
+
+ return changes.toPayload();
}
@Override
- public boolean areContentsTheSame(@NotNull LedgerAccount oldItem,
- @NotNull LedgerAccount newItem) {
- return (oldItem.isExpanded() == newItem.isExpanded()) &&
- (oldItem.amountsExpanded() == newItem.amountsExpanded() &&
- TextUtils.equals(oldItem.getAmountsString(), newItem.getAmountsString()));
+ public boolean areItemsTheSame(@NotNull AccountListItem oldItem,
+ @NotNull AccountListItem newItem) {
+ final AccountListItem.Type oldType = oldItem.getType();
+ final AccountListItem.Type newType = newItem.getType();
+ if (oldType != newType)
+ return false;
+ if (oldType == AccountListItem.Type.HEADER)
+ return true;
+
+ return oldItem.getAccount()
+ .getId() == newItem.getAccount()
+ .getId();
+ }
+ @Override
+ public boolean areContentsTheSame(@NotNull AccountListItem oldItem,
+ @NotNull AccountListItem newItem) {
+ return oldItem.sameContent(newItem);
}
});
}
-
- public void onBindViewHolder(@NonNull LedgerRowHolder holder, int position) {
- holder.bindToAccount(listDiffer.getCurrentList().get(position));
+ @Override
+ public long getItemId(int position) {
+ if (position == 0)
+ return 0;
+ return listDiffer.getCurrentList()
+ .get(position)
+ .getAccount()
+ .getId();
}
-
- @NonNull
@Override
- public LedgerRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
- View row = LayoutInflater.from(parent.getContext())
- .inflate(R.layout.account_summary_row, parent, false);
- return new LedgerRowHolder(row);
+ public void onBindViewHolder(@NonNull RowHolder holder, int position,
+ @NonNull List<Object> payloads) {
+ holder.bind(listDiffer.getCurrentList()
+ .get(position), payloads);
+ super.onBindViewHolder(holder, position, payloads);
}
+ public void onBindViewHolder(@NonNull RowHolder holder, int position) {
+ holder.bind(listDiffer.getCurrentList()
+ .get(position), null);
+ }
+ @NonNull
+ @Override
+ public RowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
+ final LayoutInflater inflater = LayoutInflater.from(parent.getContext());
+
+ final RowHolder result;
+ switch (viewType) {
+ case ITEM_TYPE_HEADER:
+ result = new HeaderRowHolder(
+ AccountListSummaryRowBinding.inflate(inflater, parent, false));
+ break;
+ case ITEM_TYPE_ACCOUNT:
+ result = new AccountRowHolder(
+ AccountListRowBinding.inflate(inflater, parent, false));
+ break;
+ default:
+ throw new IllegalStateException("Unexpected value: " + viewType);
+ }
+// Logger.debug("acc-ui", "Creating " + result);
+ return result;
+ }
@Override
public int getItemCount() {
- return listDiffer.getCurrentList().size();
+ return listDiffer.getCurrentList()
+ .size();
+ }
+ @Override
+ public int getItemViewType(int position) {
+ return (position == 0) ? ITEM_TYPE_HEADER : ITEM_TYPE_ACCOUNT;
}
- public void setAccounts(MobileLedgerProfile profile, ArrayList<LedgerAccount> newList) {
- this.profile = profile;
- listDiffer.submitList(newList);
+ public void setAccounts(List<AccountListItem> newList) {
+ Misc.onMainThread(() -> listDiffer.submitList(newList));
+ }
+ static class Change {
+ static final int NAME = 1;
+ static final int EXPANDED = 1 << 1;
+ static final int LEVEL = 1 << 2;
+ static final int EXPANDED_AMOUNTS = 1 << 3;
+ static final int AMOUNTS = 1 << 4;
+ private int value = 0;
+ public Change() {
+ }
+ public Change(int initialValue) {
+ value = initialValue;
+ }
+ public void add(int bits) {
+ value = value | bits;
+ }
+ public void add(Change change) {
+ value = value | change.value;
+ }
+ public void remove(int bits) {
+ value = value & (~bits);
+ }
+ public void remove(Change change) {
+ value = value & (~change.value);
+ }
+ public Change toPayload() {
+ if (value == 0)
+ return null;
+ return this;
+ }
+ public boolean has(int bits) {
+ return value == 0 || (value & bits) == bits;
+ }
}
- class LedgerRowHolder extends RecyclerView.ViewHolder {
- TextView tvAccountName, tvAccountAmounts;
- ConstraintLayout row;
- View expanderContainer;
- ImageView expander;
- View accountExpanderContainer;
- public LedgerRowHolder(@NonNull View itemView) {
+
+ static abstract class RowHolder extends RecyclerView.ViewHolder {
+ public RowHolder(@NonNull View itemView) {
super(itemView);
- row = itemView.findViewById(R.id.account_summary_row);
- tvAccountName = itemView.findViewById(R.id.account_row_acc_name);
- tvAccountAmounts = itemView.findViewById(R.id.account_row_acc_amounts);
- expanderContainer = itemView.findViewById(R.id.account_expander_container);
- expander = itemView.findViewById(R.id.account_expander);
- accountExpanderContainer =
- itemView.findViewById(R.id.account_row_amounts_expander_container);
+ }
+ public abstract void bind(AccountListItem accountListItem, @Nullable List<Object> payloads);
+ }
+
+ static class HeaderRowHolder extends RowHolder {
+ private final AccountListSummaryRowBinding b;
+ public HeaderRowHolder(@NonNull AccountListSummaryRowBinding binding) {
+ super(binding.getRoot());
+ b = binding;
+ }
+ @Override
+ public void bind(AccountListItem item, @Nullable List<Object> payloads) {
+ Resources r = itemView.getResources();
+// Logger.debug("acc", itemView.getContext()
+// .toString());
+ ((AccountListItem.Header) item).getText()
+ .observe((LifecycleOwner) itemView.getContext(),
+ b.lastUpdateText::setText);
+ }
+ }
+
+ class AccountRowHolder extends AccountSummaryAdapter.RowHolder {
+ private final AccountListRowBinding b;
+ public AccountRowHolder(@NonNull AccountListRowBinding binding) {
+ super(binding.getRoot());
+ b = binding;
itemView.setOnLongClickListener(this::onItemLongClick);
- tvAccountName.setOnLongClickListener(this::onItemLongClick);
- tvAccountAmounts.setOnLongClickListener(this::onItemLongClick);
- expanderContainer.setOnLongClickListener(this::onItemLongClick);
- expander.setOnLongClickListener(this::onItemLongClick);
- row.setOnLongClickListener(this::onItemLongClick);
-
- tvAccountName.setOnClickListener(v -> toggleAccountExpanded());
- expanderContainer.setOnClickListener(v -> toggleAccountExpanded());
- expander.setOnClickListener(v -> toggleAccountExpanded());
- tvAccountAmounts.setOnClickListener(v -> toggleAmountsExpanded());
- }
- private @NonNull
- LedgerAccount getAccount() {
- final ArrayList<LedgerAccount> accountList = profile.getAccounts()
- .getValue();
- if (accountList == null)
- throw new IllegalStateException("No account list");
-
- return accountList.get(getAdapterPosition());
+ b.accountRowAccName.setOnLongClickListener(this::onItemLongClick);
+ b.accountRowAccAmounts.setOnLongClickListener(this::onItemLongClick);
+ b.accountExpanderContainer.setOnLongClickListener(this::onItemLongClick);
+ b.accountExpander.setOnLongClickListener(this::onItemLongClick);
+
+ b.accountRowAccName.setOnClickListener(v -> toggleAccountExpanded());
+ b.accountExpanderContainer.setOnClickListener(v -> toggleAccountExpanded());
+ b.accountExpander.setOnClickListener(v -> toggleAccountExpanded());
+ b.accountRowAccAmounts.setOnClickListener(v -> toggleAmountsExpanded());
}
private void toggleAccountExpanded() {
- LedgerAccount acc = getAccount();
- if (!acc.hasSubAccounts())
+ LedgerAccount account = getAccount();
+ if (!account.hasSubAccounts())
return;
debug("accounts", "Account expander clicked");
- acc.toggleExpanded();
- expanderContainer.animate()
- .rotation(acc.isExpanded() ? 0 : 180);
-
- MobileLedgerProfile profile = acc.getProfile();
- if (profile == null)
- return;
-
- DbOpQueue.add("update accounts set expanded=? where name=? and profile=?",
- new Object[]{acc.isExpanded(), acc.getName(), profile.getUuid()
- }, profile::scheduleAccountListReload);
-
+ AsyncTask.execute(() -> {
+ Account dbo = account.toDBO();
+ dbo.setExpanded(!dbo.isExpanded());
+ Logger.debug("accounts",
+ String.format(Locale.ROOT, "%s (%d) → %s", account.getName(), dbo.getId(),
+ dbo.isExpanded() ? "expanded" : "collapsed"));
+ DB.get()
+ .getAccountDAO()
+ .updateSync(dbo);
+ });
+ }
+ @NotNull
+ private LedgerAccount getAccount() {
+ return listDiffer.getCurrentList()
+ .get(getBindingAdapterPosition())
+ .getAccount();
}
private void toggleAmountsExpanded() {
- LedgerAccount acc = getAccount();
- if (acc.getAmountCount() <= AMOUNT_LIMIT)
+ LedgerAccount account = getAccount();
+ if (account.getAmountCount() <= AMOUNT_LIMIT)
return;
- acc.toggleAmountsExpanded();
- if (acc.amountsExpanded()) {
- tvAccountAmounts.setText(acc.getAmountsString());
- accountExpanderContainer.setVisibility(View.GONE);
+ account.toggleAmountsExpanded();
+ if (account.amountsExpanded()) {
+ b.accountRowAccAmounts.setText(account.getAmountsString());
+ b.accountRowAmountsExpanderContainer.setVisibility(View.GONE);
}
else {
- tvAccountAmounts.setText(acc.getAmountsString(AMOUNT_LIMIT));
- accountExpanderContainer.setVisibility(View.VISIBLE);
+ b.accountRowAccAmounts.setText(account.getAmountsString(AMOUNT_LIMIT));
+ b.accountRowAmountsExpanderContainer.setVisibility(View.VISIBLE);
}
- MobileLedgerProfile profile = acc.getProfile();
- if (profile == null)
- return;
-
- DbOpQueue.add("update accounts set amounts_expanded=? where name=? and profile=?",
- new Object[]{acc.amountsExpanded(), acc.getName(), profile.getUuid()
- });
-
+ AsyncTask.execute(() -> {
+ Account dbo = account.toDBO();
+ DB.get()
+ .getAccountDAO()
+ .updateSync(dbo);
+ });
}
private boolean onItemLongClick(View v) {
MainActivity activity = (MainActivity) v.getContext();
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
- View row;
- int id = v.getId();
- switch (id) {
- case R.id.account_summary_row:
- row = v;
- break;
- case R.id.account_row_acc_amounts:
- case R.id.account_row_amounts_expander_container:
- row = (View) v.getParent();
- break;
- case R.id.account_row_acc_name:
- case R.id.account_expander_container:
- row = (View) v.getParent()
- .getParent();
- break;
- case R.id.account_expander:
- row = (View) v.getParent()
- .getParent()
- .getParent();
- break;
- default:
- Log.e("error",
- String.format("Don't know how to handle long click on id %d", id));
- return false;
- }
- LedgerAccount acc = getAccount();
- builder.setTitle(acc.getName());
+ final String accountName = getAccount().getName();
+ builder.setTitle(accountName);
builder.setItems(R.array.acc_ctx_menu, (dialog, which) -> {
- switch (which) {
- case 0:
- // show transactions
- activity.showAccountTransactions(acc.getName());
- break;
+ 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(LedgerAccount acc) {
- Context ctx = row.getContext();
- Resources rm = ctx.getResources();
+ @Override
+ public void bind(AccountListItem item, @Nullable List<Object> payloads) {
+ LedgerAccount acc = item.getAccount();
+
+ Change changes = new Change();
+ if (payloads != null) {
+ for (Object p : payloads) {
+ if (p instanceof Change)
+ changes.add((Change) p);
+ }
+ }
+// debug("accounts",
+// String.format(Locale.US, "Binding '%s' to %s", acc.getName(), this));
- row.setTag(acc);
+ Resources rm = b.getRoot()
+ .getContext()
+ .getResources();
- tvAccountName.setText(acc.getShortName());
+ if (changes.has(Change.NAME))
+ b.accountRowAccName.setText(acc.getShortName());
- ConstraintLayout.LayoutParams lp =
- (ConstraintLayout.LayoutParams) tvAccountName.getLayoutParams();
- lp.setMarginStart(
- acc.getLevel() * rm.getDimensionPixelSize(R.dimen.thumb_row_height) / 3);
+ if (changes.has(Change.LEVEL)) {
+ ConstraintLayout.LayoutParams lp =
+ (ConstraintLayout.LayoutParams) b.accountNameLayout.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);
+ b.accountExpanderContainer.setVisibility(View.VISIBLE);
+
+ if (changes.has(Change.EXPANDED)) {
+ int wantedRotation = acc.isExpanded() ? 0 : 180;
+ if (b.accountExpanderContainer.getRotation() != wantedRotation) {
+// Logger.debug("acc-ui",
+// String.format(Locale.ROOT, "Rotating %s to %d", acc.getName(),
+// wantedRotation));
+ b.accountExpanderContainer.animate()
+ .rotation(wantedRotation);
+ }
+ }
}
else {
- expanderContainer.setVisibility(View.GONE);
+ b.accountExpanderContainer.setVisibility(View.GONE);
}
- int amounts = acc.getAmountCount();
- if ((amounts > AMOUNT_LIMIT) && !acc.amountsExpanded()) {
- tvAccountAmounts.setText(acc.getAmountsString(AMOUNT_LIMIT));
- accountExpanderContainer.setVisibility(View.VISIBLE);
- }
- else {
- tvAccountAmounts.setText(acc.getAmountsString());
- accountExpanderContainer.setVisibility(View.GONE);
+ if (changes.has(Change.EXPANDED_AMOUNTS)) {
+ int amounts = acc.getAmountCount();
+ if ((amounts > AMOUNT_LIMIT) && !acc.amountsExpanded()) {
+ b.accountRowAccAmounts.setText(acc.getAmountsString(AMOUNT_LIMIT));
+ b.accountRowAmountsExpanderContainer.setVisibility(View.VISIBLE);
+ }
+ else {
+ b.accountRowAccAmounts.setText(acc.getAmountsString());
+ b.accountRowAmountsExpanderContainer.setVisibility(View.GONE);
+ }
}
}
}