]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListAdapter.java
rework transaction list with proper view holders and no background load
[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                         // Delimiters items are "same" for same dates and the contents are the date
72                         return true;
73                     case TRANSACTION:
74                         return oldItem.getTransaction()
75                                       .equals(newItem.getTransaction()) &&
76                                Misc.equalStrings(oldItem.getBoldAccountName(),
77                                        newItem.getBoldAccountName());
78                     case HEADER:
79                         // headers don't differ in their contents. they observe the last update
80                         // date and react to its changes
81                         return true;
82                     default:
83                         throw new IllegalStateException(
84                                 String.format(Locale.US, "Unexpected transaction item type %s",
85                                         oldItem.getType()));
86
87                 }
88             }
89         });
90     }
91     @Override
92     public int getItemViewType(int position) {
93         return listDiffer.getCurrentList()
94                          .get(position)
95                          .getType()
96                          .ordinal();
97     }
98     public void onBindViewHolder(@NonNull TransactionRowHolderBase holder, int position) {
99         TransactionListItem item = listDiffer.getCurrentList()
100                                              .get(position);
101
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
105         // a bit longer
106         if (item == null)
107             return;
108
109         final TransactionListItem.Type newType = item.getType();
110
111         switch (newType) {
112             case TRANSACTION:
113                 LedgerTransaction tr = item.getTransaction();
114                 holder.asTransaction()
115                       .bind(tr, item.getBoldAccountName());
116
117                 break;
118             case DELIMITER:
119                 holder.asDelimiter()
120                       .bind(item);
121                 break;
122             case HEADER:
123                 holder.asHeader()
124                       .bind();
125
126                 break;
127             default:
128                 throw new IllegalStateException("Unexpected value: " + newType);
129         }
130     }
131     @NonNull
132     @Override
133     public TransactionRowHolderBase onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
134 //        debug("perf", "onCreateViewHolder called");
135         final LayoutInflater inflater = LayoutInflater.from(parent.getContext());
136
137         switch (TransactionListItem.Type.valueOf(viewType)) {
138             case TRANSACTION:
139                 return new TransactionRowHolder(
140                         TransactionListRowBinding.inflate(inflater, parent, false));
141             case DELIMITER:
142                 return new TransactionListDelimiterRowHolder(
143                         TransactionDelimiterBinding.inflate(inflater, parent, false));
144             case HEADER:
145                 return new TransactionListLastUpdateRowHolder(
146                         LastUpdateLayoutBinding.inflate(inflater, parent, false));
147             default:
148                 throw new IllegalStateException("Unexpected value: " + viewType);
149         }
150     }
151
152     @Override
153     public int getItemCount() {
154         return listDiffer.getCurrentList()
155                          .size();
156     }
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);
161     }
162 }