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.patterns;
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.PatternLayoutBinding;
29 import net.ktnx.mobileledger.model.PatternEntry;
31 import org.jetbrains.annotations.NotNull;
33 import java.util.List;
34 import java.util.Objects;
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>() {
41 public boolean areItemsTheSame(@NotNull PatternEntry oldItem,
42 @NotNull PatternEntry newItem) {
43 return oldItem.getId() == newItem.getId();
46 public boolean areContentsTheSame(@NotNull PatternEntry oldItem,
47 @NotNull PatternEntry newItem) {
48 return Objects.equals(oldItem.getName(), newItem.getName());
54 public PatternViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
55 PatternLayoutBinding b =
56 PatternLayoutBinding.inflate(LayoutInflater.from(parent.getContext()), parent,
59 return new PatternViewHolder(b);
62 public void onBindViewHolder(@NonNull PatternViewHolder holder, int position) {
63 holder.bindToItem(listDiffer.getCurrentList()
67 public int getItemCount() {
68 return listDiffer.getCurrentList()
71 public void setPatterns(List<PatternEntry> newList) {
72 listDiffer.submitList(newList);