-import android.widget.LinearLayout;
-import android.widget.TextView;
-
-import net.ktnx.mobileledger.R;
-import net.ktnx.mobileledger.model.LedgerTransaction;
-import net.ktnx.mobileledger.model.LedgerTransactionAccount;
-import net.ktnx.mobileledger.utils.Globals;
-import net.ktnx.mobileledger.utils.MLDB;
-
-import static net.ktnx.mobileledger.utils.DimensionUtils.dp2px;
-
-public class TransactionListAdapter
- extends RecyclerView.Adapter<TransactionListAdapter.TransactionRowHolder> {
- TransactionListViewModel model;
- private String boldAccountName;
- public TransactionListAdapter(TransactionListViewModel model) {
- this.model = model;
+
+import androidx.annotation.NonNull;
+import androidx.recyclerview.widget.AsyncListDiffer;
+import androidx.recyclerview.widget.DiffUtil;
+import androidx.recyclerview.widget.RecyclerView;
+
+import net.ktnx.mobileledger.databinding.LastUpdateLayoutBinding;
+import net.ktnx.mobileledger.databinding.TransactionDelimiterBinding;
+import net.ktnx.mobileledger.databinding.TransactionListRowBinding;
+import net.ktnx.mobileledger.model.TransactionListItem;
+import net.ktnx.mobileledger.utils.Logger;
+import net.ktnx.mobileledger.utils.Misc;
+
+import java.util.List;
+import java.util.Locale;
+
+public class TransactionListAdapter extends RecyclerView.Adapter<TransactionRowHolderBase> {
+ private final AsyncListDiffer<TransactionListItem> listDiffer;
+ public TransactionListAdapter() {
+ super();
+
+ setHasStableIds(true);
+
+ listDiffer = new AsyncListDiffer<>(this, new DiffUtil.ItemCallback<TransactionListItem>() {
+ @Override
+ public boolean areItemsTheSame(@NonNull TransactionListItem oldItem,
+ @NonNull TransactionListItem newItem) {
+ if (oldItem.getType() != newItem.getType())
+ return false;
+ switch (oldItem.getType()) {
+ case DELIMITER:
+ return (oldItem.getDate()
+ .equals(newItem.getDate()));
+ case TRANSACTION:
+ return oldItem.getTransaction()
+ .getLedgerId() == newItem.getTransaction()
+ .getLedgerId();
+ case HEADER:
+ return true; // there can be only one header
+ default:
+ throw new IllegalStateException(
+ String.format(Locale.US, "Unexpected transaction item type %s",
+ oldItem.getType()));
+ }
+ }
+ @Override
+ public boolean areContentsTheSame(@NonNull TransactionListItem oldItem,
+ @NonNull TransactionListItem newItem) {
+ switch (oldItem.getType()) {
+ case DELIMITER:
+ return oldItem.isMonthShown() == newItem.isMonthShown();
+ case TRANSACTION:
+ return oldItem.getTransaction()
+ .equals(newItem.getTransaction()) &&
+ Misc.equalStrings(oldItem.getBoldAccountName(),
+ newItem.getBoldAccountName()) &&
+ Misc.equalStrings(oldItem.getRunningTotal(),
+ newItem.getRunningTotal());
+ case HEADER:
+ // headers don't differ in their contents. they observe the last update
+ // date and react to its changes
+ return true;
+ default:
+ throw new IllegalStateException(
+ String.format(Locale.US, "Unexpected transaction item type %s",
+ oldItem.getType()));
+
+ }
+ }
+ });
+ }
+ @Override
+ public long getItemId(int position) {
+ TransactionListItem item = listDiffer.getCurrentList()
+ .get(position);
+ switch (item.getType()) {
+ case HEADER:
+ return -1;
+ case TRANSACTION:
+ return item.getTransaction()
+ .getLedgerId();
+ case DELIMITER:
+ return -item.getDate()
+ .toDate()
+ .getTime();
+ default:
+ throw new IllegalStateException("Unexpected value: " + item.getType());
+ }
+ }
+ @Override
+ public int getItemViewType(int position) {
+ return listDiffer.getCurrentList()
+ .get(position)
+ .getType()
+ .ordinal();