+ private final MainModel model;
+ private final AsyncListDiffer<TransactionListItem> listDiffer;
+ public TransactionListAdapter(MainModel model) {
+ super();
+ this.model = model;
+
+ 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()
+ .getId() == newItem.getTransaction()
+ .getId();
+ 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:
+ // Delimiters items are "same" for same dates and the contents are the date
+ return true;
+ case TRANSACTION:
+ return oldItem.getTransaction()
+ .equals(newItem.getTransaction());
+ 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()));
+
+ }
+ }
+ });
+ }