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.databinding.TemplatesFallbackDividerBinding;
30 import net.ktnx.mobileledger.db.TemplateHeader;
32 import org.jetbrains.annotations.NotNull;
34 import java.util.ArrayList;
35 import java.util.List;
37 public class TemplatesRecyclerViewAdapter extends RecyclerView.Adapter<BaseTemplateViewHolder> {
38 static final int ITEM_TYPE_TEMPLATE = 1;
39 static final int ITEM_TYPE_DIVIDER = 2;
40 private final AsyncListDiffer<BaseTemplateItem> listDiffer;
42 public TemplatesRecyclerViewAdapter() {
43 listDiffer = new AsyncListDiffer<>(this, new DiffUtil.ItemCallback<BaseTemplateItem>() {
45 public boolean areItemsTheSame(
46 @NotNull TemplatesRecyclerViewAdapter.BaseTemplateItem oldItem,
47 @NotNull TemplatesRecyclerViewAdapter.BaseTemplateItem newItem) {
48 return oldItem.getId() == newItem.getId();
51 public boolean areContentsTheSame(
52 @NotNull TemplatesRecyclerViewAdapter.BaseTemplateItem oldItem,
53 @NotNull TemplatesRecyclerViewAdapter.BaseTemplateItem newItem) {
54 return oldItem.equals(newItem);
60 public BaseTemplateViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
61 final LayoutInflater inflater = LayoutInflater.from(parent.getContext());
63 case ITEM_TYPE_TEMPLATE:
64 TemplateListTemplateItemBinding b =
65 TemplateListTemplateItemBinding.inflate(inflater, parent, false);
66 return new BaseTemplateViewHolder.TemplateViewHolder(b);
67 case ITEM_TYPE_DIVIDER:
68 return new BaseTemplateViewHolder.TemplateDividerViewHolder(
69 TemplatesFallbackDividerBinding.inflate(inflater, parent, false));
71 throw new RuntimeException("Can't handle " + viewType);
75 public void onBindViewHolder(@NonNull BaseTemplateViewHolder holder, int position) {
76 holder.bindToItem(listDiffer.getCurrentList()
80 public int getItemViewType(int position) {
81 BaseTemplateItem item = getItem(position);
82 if (item instanceof TemplateItem)
83 return ITEM_TYPE_TEMPLATE;
84 if (item instanceof TemplateDivider)
85 return ITEM_TYPE_DIVIDER;
87 throw new RuntimeException("Can't handle " + item);
90 public int getItemCount() {
91 return listDiffer.getCurrentList()
94 public void setTemplates(List<TemplateHeader> newList) {
95 List<BaseTemplateItem> itemList = new ArrayList<>();
97 boolean reachedFallbackItems = false;
99 for (TemplateHeader item : newList) {
100 if (!reachedFallbackItems && item.isFallback()) {
101 itemList.add(new TemplateDivider());
102 reachedFallbackItems = true;
104 itemList.add(new TemplateItem(item));
107 listDiffer.submitList(itemList);
109 public BaseTemplateItem getItem(int position) {
110 return listDiffer.getCurrentList()
114 static abstract class BaseTemplateItem {
115 abstract long getId();
117 abstract boolean equals(BaseTemplateItem other);
120 static class TemplateItem extends BaseTemplateItem {
121 final TemplateHeader template;
122 TemplateItem(TemplateHeader template) {this.template = template;}
125 return template.getId();
128 boolean equals(BaseTemplateItem other) {
129 return template.equals(((TemplateItem) other).template);
133 static class TemplateDivider extends BaseTemplateItem {
139 boolean equals(BaseTemplateItem other) {