]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/patterns/PatternsRecyclerViewAdapter.java
first step towards pattern-assisted auto-filling of transactions
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / patterns / PatternsRecyclerViewAdapter.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.patterns;
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.PatternLayoutBinding;
29 import net.ktnx.mobileledger.model.PatternEntry;
30
31 import org.jetbrains.annotations.NotNull;
32
33 import java.util.List;
34 import java.util.Objects;
35
36 public class PatternsRecyclerViewAdapter extends RecyclerView.Adapter<PatternViewHolder> {
37     private final AsyncListDiffer<PatternEntry> listDiffer;
38     public PatternsRecyclerViewAdapter() {
39         listDiffer = new AsyncListDiffer<>(this, new DiffUtil.ItemCallback<PatternEntry>() {
40             @Override
41             public boolean areItemsTheSame(@NotNull PatternEntry oldItem,
42                                            @NotNull PatternEntry newItem) {
43                 return oldItem.getId() == newItem.getId();
44             }
45             @Override
46             public boolean areContentsTheSame(@NotNull PatternEntry oldItem,
47                                               @NotNull PatternEntry newItem) {
48                 return Objects.equals(oldItem.getName(), newItem.getName());
49             }
50         });
51     }
52     @NonNull
53     @Override
54     public PatternViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
55         PatternLayoutBinding b =
56                 PatternLayoutBinding.inflate(LayoutInflater.from(parent.getContext()), parent,
57                         false);
58
59         return new PatternViewHolder(b);
60     }
61     @Override
62     public void onBindViewHolder(@NonNull PatternViewHolder holder, int position) {
63         holder.bindToItem(listDiffer.getCurrentList()
64                                     .get(position));
65     }
66     @Override
67     public int getItemCount() {
68         return listDiffer.getCurrentList()
69                          .size();
70     }
71     public void setPatterns(List<PatternEntry> newList) {
72         listDiffer.submitList(newList);
73     }
74 }