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.
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.
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/>.
18 package net.ktnx.mobileledger.ui.transaction_list;
20 import android.view.LayoutInflater;
21 import android.view.ViewGroup;
23 import androidx.annotation.NonNull;
24 import androidx.recyclerview.widget.AsyncListDiffer;
25 import androidx.recyclerview.widget.DiffUtil;
26 import androidx.recyclerview.widget.RecyclerView;
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;
36 import java.util.List;
37 import java.util.Locale;
39 public class TransactionListAdapter extends RecyclerView.Adapter<TransactionRowHolderBase> {
40 private final AsyncListDiffer<TransactionListItem> listDiffer;
41 public TransactionListAdapter() {
44 listDiffer = new AsyncListDiffer<>(this, new DiffUtil.ItemCallback<TransactionListItem>() {
46 public boolean areItemsTheSame(@NonNull TransactionListItem oldItem,
47 @NonNull TransactionListItem newItem) {
48 if (oldItem.getType() != newItem.getType())
50 switch (oldItem.getType()) {
52 return (oldItem.getDate()
53 .equals(newItem.getDate()));
55 return oldItem.getTransaction()
56 .getLedgerId() == newItem.getTransaction()
59 return true; // there can be only one header
61 throw new IllegalStateException(
62 String.format(Locale.US, "Unexpected transaction item type %s",
67 public boolean areContentsTheSame(@NonNull TransactionListItem oldItem,
68 @NonNull TransactionListItem newItem) {
69 switch (oldItem.getType()) {
71 // Delimiters items are "same" for same dates and the contents are the date
74 return oldItem.getTransaction()
75 .equals(newItem.getTransaction()) &&
76 Misc.equalStrings(oldItem.getBoldAccountName(),
77 newItem.getBoldAccountName());
79 // headers don't differ in their contents. they observe the last update
80 // date and react to its changes
83 throw new IllegalStateException(
84 String.format(Locale.US, "Unexpected transaction item type %s",
92 public int getItemViewType(int position) {
93 return listDiffer.getCurrentList()
98 public void onBindViewHolder(@NonNull TransactionRowHolderBase holder, int position) {
99 TransactionListItem item = listDiffer.getCurrentList()
102 // in a race when transaction value is reduced, but the model hasn't been notified yet
103 // the view will disappear when the notifications reaches the model, so by simply omitting
104 // the out-of-range get() call nothing bad happens - just a to-be-deleted view remains
109 final TransactionListItem.Type newType = item.getType();
113 LedgerTransaction tr = item.getTransaction();
114 holder.asTransaction()
115 .bind(tr, item.getBoldAccountName());
128 throw new IllegalStateException("Unexpected value: " + newType);
133 public TransactionRowHolderBase onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
134 // debug("perf", "onCreateViewHolder called");
135 final LayoutInflater inflater = LayoutInflater.from(parent.getContext());
137 switch (TransactionListItem.Type.valueOf(viewType)) {
139 return new TransactionRowHolder(
140 TransactionListRowBinding.inflate(inflater, parent, false));
142 return new TransactionListDelimiterRowHolder(
143 TransactionDelimiterBinding.inflate(inflater, parent, false));
145 return new TransactionListLastUpdateRowHolder(
146 LastUpdateLayoutBinding.inflate(inflater, parent, false));
148 throw new IllegalStateException("Unexpected value: " + viewType);
153 public int getItemCount() {
154 return listDiffer.getCurrentList()
157 public void setTransactions(List<TransactionListItem> newList) {
158 Logger.debug("transactions",
159 String.format(Locale.US, "Got new transaction list (%d items)", newList.size()));
160 listDiffer.submitList(newList);