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 return oldItem.isMonthShown() == newItem.isMonthShown();
73 return oldItem.getTransaction()
74 .equals(newItem.getTransaction()) &&
75 Misc.equalStrings(oldItem.getBoldAccountName(),
76 newItem.getBoldAccountName());
78 // headers don't differ in their contents. they observe the last update
79 // date and react to its changes
82 throw new IllegalStateException(
83 String.format(Locale.US, "Unexpected transaction item type %s",
91 public int getItemViewType(int position) {
92 return listDiffer.getCurrentList()
97 public void onBindViewHolder(@NonNull TransactionRowHolderBase holder, int position) {
98 TransactionListItem item = listDiffer.getCurrentList()
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
108 final TransactionListItem.Type newType = item.getType();
112 LedgerTransaction tr = item.getTransaction();
113 holder.asTransaction()
114 .bind(tr, item.getBoldAccountName());
127 throw new IllegalStateException("Unexpected value: " + newType);
132 public TransactionRowHolderBase onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
133 // debug("perf", "onCreateViewHolder called");
134 final LayoutInflater inflater = LayoutInflater.from(parent.getContext());
136 switch (TransactionListItem.Type.valueOf(viewType)) {
138 return new TransactionRowHolder(
139 TransactionListRowBinding.inflate(inflater, parent, false));
141 return new TransactionListDelimiterRowHolder(
142 TransactionDelimiterBinding.inflate(inflater, parent, false));
144 return new TransactionListLastUpdateRowHolder(
145 LastUpdateLayoutBinding.inflate(inflater, parent, false));
147 throw new IllegalStateException("Unexpected value: " + viewType);
152 public int getItemCount() {
153 return listDiffer.getCurrentList()
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);