]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListAdapter.java
more pronounced day/month delimiters in the transaction list
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / transaction_list / TransactionListAdapter.java
index 599a9f8c8024e39e020a63485be749e1f5e0f27d..bc463ed564661bb6d3da11155158322bbb2b5b29 100644 (file)
@@ -28,7 +28,6 @@ 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.LedgerTransaction;
 import net.ktnx.mobileledger.model.TransactionListItem;
 import net.ktnx.mobileledger.utils.Logger;
 import net.ktnx.mobileledger.utils.Misc;
@@ -41,6 +40,8 @@ public class TransactionListAdapter extends RecyclerView.Adapter<TransactionRowH
     public TransactionListAdapter() {
         super();
 
+        setHasStableIds(true);
+
         listDiffer = new AsyncListDiffer<>(this, new DiffUtil.ItemCallback<TransactionListItem>() {
             @Override
             public boolean areItemsTheSame(@NonNull TransactionListItem oldItem,
@@ -68,13 +69,14 @@ public class TransactionListAdapter extends RecyclerView.Adapter<TransactionRowH
                                               @NonNull TransactionListItem newItem) {
                 switch (oldItem.getType()) {
                     case DELIMITER:
-                        // Delimiters items are "same" for same dates and the contents are the date
-                        return true;
+                        return oldItem.isMonthShown() == newItem.isMonthShown();
                     case TRANSACTION:
                         return oldItem.getTransaction()
                                       .equals(newItem.getTransaction()) &&
                                Misc.equalStrings(oldItem.getBoldAccountName(),
-                                       newItem.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
@@ -89,6 +91,24 @@ public class TransactionListAdapter extends RecyclerView.Adapter<TransactionRowH
         });
     }
     @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)
@@ -110,9 +130,8 @@ public class TransactionListAdapter extends RecyclerView.Adapter<TransactionRowH
 
         switch (newType) {
             case TRANSACTION:
-                LedgerTransaction tr = item.getTransaction();
                 holder.asTransaction()
-                      .bind(tr, item.getBoldAccountName());
+                      .bind(item, item.getBoldAccountName());
 
                 break;
             case DELIMITER: