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.templates;
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.TemplateListTemplateItemBinding;
29 import net.ktnx.mobileledger.db.TemplateHeader;
31 import org.jetbrains.annotations.NotNull;
33 import java.util.List;
35 public class TemplatesRecyclerViewAdapter extends RecyclerView.Adapter<TemplateViewHolder> {
36 private final AsyncListDiffer<TemplateHeader> listDiffer;
37 public TemplatesRecyclerViewAdapter() {
38 listDiffer = new AsyncListDiffer<>(this, new DiffUtil.ItemCallback<TemplateHeader>() {
40 public boolean areItemsTheSame(@NotNull TemplateHeader oldItem,
41 @NotNull TemplateHeader newItem) {
42 return oldItem.getId()
43 .equals(newItem.getId());
46 public boolean areContentsTheSame(@NotNull TemplateHeader oldItem,
47 @NotNull TemplateHeader newItem) {
48 return oldItem.equals(newItem);
54 public TemplateViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
55 TemplateListTemplateItemBinding b =
56 TemplateListTemplateItemBinding.inflate(LayoutInflater.from(parent.getContext()),
59 return new TemplateViewHolder(b);
62 public void onBindViewHolder(@NonNull TemplateViewHolder holder, int position) {
63 holder.bindToItem(listDiffer.getCurrentList()
67 public int getItemCount() {
68 return listDiffer.getCurrentList()
71 public void setTemplates(List<TemplateHeader> newList) {
72 listDiffer.submitList(newList);