]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListAdapter.java
fix comparison of transaction date delimiter contents
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / transaction_list / TransactionListAdapter.java
1 /*
2  * Copyright © 2021 Damyan Ivanov.
3  * This file is part of MoLe.
4  * MoLe is free software: you can distribute it and/or modify it
5  * under the term of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your opinion), any later version.
8  *
9  * MoLe is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License terms for details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
16  */
17
18 package net.ktnx.mobileledger.ui.transaction_list;
19
20 import android.view.LayoutInflater;
21 import android.view.ViewGroup;
22
23 import androidx.annotation.NonNull;
24 import androidx.recyclerview.widget.AsyncListDiffer;
25 import androidx.recyclerview.widget.DiffUtil;
26 import androidx.recyclerview.widget.RecyclerView;
27
28 import net.ktnx.mobileledger.databinding.LastUpdateLayoutBinding;
29 import net.ktnx.mobileledger.databinding.TransactionDelimiterBinding;
30 import net.ktnx.mobileledger.databinding.TransactionListRowBinding;
31 import net.ktnx.mobileledger.model.LedgerTransaction;
32 import net.ktnx.mobileledger.model.TransactionListItem;
33 import net.ktnx.mobileledger.utils.Logger;
34 import net.ktnx.mobileledger.utils.Misc;
35
36 import java.util.List;
37 import java.util.Locale;
38
39 public class TransactionListAdapter extends RecyclerView.Adapter<TransactionRowHolderBase> {
40     private final AsyncListDiffer<TransactionListItem> listDiffer;
41     public TransactionListAdapter() {
42         super();
43
44         listDiffer = new AsyncListDiffer<>(this, new DiffUtil.ItemCallback<TransactionListItem>() {
45             @Override
46             public boolean areItemsTheSame(@NonNull TransactionListItem oldItem,
47                                            @NonNull TransactionListItem newItem) {
48                 if (oldItem.getType() != newItem.getType())
49                     return false;
50                 switch (oldItem.getType()) {
51                     case DELIMITER:
52                         return (oldItem.getDate()
53                                        .equals(newItem.getDate()));
54                     case TRANSACTION:
55                         return oldItem.getTransaction()
56                                       .getLedgerId() == newItem.getTransaction()
57                                                                .getLedgerId();
58                     case HEADER:
59                         return true;    // there can be only one header
60                     default:
61                         throw new IllegalStateException(
62                                 String.format(Locale.US, "Unexpected transaction item type %s",
63                                         oldItem.getType()));
64                 }
65             }
66             @Override
67             public boolean areContentsTheSame(@NonNull TransactionListItem oldItem,
68                                               @NonNull TransactionListItem newItem) {
69                 switch (oldItem.getType()) {
70                     case DELIMITER:
71                         return oldItem.isMonthShown() == newItem.isMonthShown();
72                     case TRANSACTION:
73                         return oldItem.getTransaction()
74                                       .equals(newItem.getTransaction()) &&
75                                Misc.equalStrings(oldItem.getBoldAccountName(),
76                                        newItem.getBoldAccountName());
77                     case HEADER:
78                         // headers don't differ in their contents. they observe the last update
79                         // date and react to its changes
80                         return true;
81                     default:
82                         throw new IllegalStateException(
83                                 String.format(Locale.US, "Unexpected transaction item type %s",
84                                         oldItem.getType()));
85
86                 }
87             }
88         });
89     }
90     @Override
91     public int getItemViewType(int position) {
92         return listDiffer.getCurrentList()
93                          .get(position)
94                          .getType()
95                          .ordinal();
96     }
97     public void onBindViewHolder(@NonNull TransactionRowHolderBase holder, int position) {
98         TransactionListItem item = listDiffer.getCurrentList()
99                                              .get(position);
100
101         // in a race when transaction value is reduced, but the model hasn't been notified yet
102         // the view will disappear when the notifications reaches the model, so by simply omitting
103         // the out-of-range get() call nothing bad happens - just a to-be-deleted view remains
104         // a bit longer
105         if (item == null)
106             return;
107
108         final TransactionListItem.Type newType = item.getType();
109
110         switch (newType) {
111             case TRANSACTION:
112                 LedgerTransaction tr = item.getTransaction();
113                 holder.asTransaction()
114                       .bind(tr, item.getBoldAccountName());
115
116                 break;
117             case DELIMITER:
118                 holder.asDelimiter()
119                       .bind(item);
120                 break;
121             case HEADER:
122                 holder.asHeader()
123                       .bind();
124
125                 break;
126             default:
127                 throw new IllegalStateException("Unexpected value: " + newType);
128         }
129     }
130     @NonNull
131     @Override
132     public TransactionRowHolderBase onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
133 //        debug("perf", "onCreateViewHolder called");
134         final LayoutInflater inflater = LayoutInflater.from(parent.getContext());
135
136         switch (TransactionListItem.Type.valueOf(viewType)) {
137             case TRANSACTION:
138                 return new TransactionRowHolder(
139                         TransactionListRowBinding.inflate(inflater, parent, false));
140             case DELIMITER:
141                 return new TransactionListDelimiterRowHolder(
142                         TransactionDelimiterBinding.inflate(inflater, parent, false));
143             case HEADER:
144                 return new TransactionListLastUpdateRowHolder(
145                         LastUpdateLayoutBinding.inflate(inflater, parent, false));
146             default:
147                 throw new IllegalStateException("Unexpected value: " + viewType);
148         }
149     }
150
151     @Override
152     public int getItemCount() {
153         return listDiffer.getCurrentList()
154                          .size();
155     }
156     public void setTransactions(List<TransactionListItem> newList) {
157         Logger.debug("transactions",
158                 String.format(Locale.US, "Got new transaction list (%d items)", newList.size()));
159         listDiffer.submitList(newList);
160     }
161 }