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;
20 import android.view.LayoutInflater;
21 import android.view.ViewGroup;
23 import androidx.recyclerview.widget.ListAdapter;
24 import androidx.recyclerview.widget.RecyclerView;
26 import net.ktnx.mobileledger.databinding.FragmentTemplateDetailSourceSelectorBinding;
27 import net.ktnx.mobileledger.model.TemplateDetailSource;
29 import org.jetbrains.annotations.NotNull;
32 * {@link RecyclerView.Adapter} that can display a {@link TemplateDetailSource} and makes a call
34 * specified {@link OnSourceSelectedListener}.
36 public class TemplateDetailSourceSelectorRecyclerViewAdapter extends
37 ListAdapter<TemplateDetailSource,
38 TemplateDetailSourceSelectorRecyclerViewAdapter.ViewHolder> {
40 private OnSourceSelectedListener sourceSelectedListener;
41 public TemplateDetailSourceSelectorRecyclerViewAdapter() {
42 super(TemplateDetailSource.DIFF_CALLBACK);
46 public ViewHolder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {
47 FragmentTemplateDetailSourceSelectorBinding b =
48 FragmentTemplateDetailSourceSelectorBinding.inflate(
49 LayoutInflater.from(parent.getContext()), parent, false);
50 return new ViewHolder(b);
54 public void onBindViewHolder(final ViewHolder holder, int position) {
55 holder.bindTo(getItem(position));
57 public void setSourceSelectedListener(OnSourceSelectedListener listener) {
58 this.sourceSelectedListener = listener;
60 public void resetSourceSelectedListener() {
61 sourceSelectedListener = null;
63 public void notifySourceSelected(TemplateDetailSource item) {
64 if (null != sourceSelectedListener)
65 sourceSelectedListener.onSourceSelected(false, item.getGroupNumber());
67 public void notifyLiteralSelected() {
68 if (null != sourceSelectedListener)
69 sourceSelectedListener.onSourceSelected(true, (short) -1);
71 public class ViewHolder extends RecyclerView.ViewHolder {
72 private final FragmentTemplateDetailSourceSelectorBinding b;
73 private TemplateDetailSource mItem;
75 ViewHolder(FragmentTemplateDetailSourceSelectorBinding binding) {
76 super(binding.getRoot());
80 .setOnClickListener(v -> notifySourceSelected(mItem));
85 public String toString() {
86 return super.toString() + " " + b.groupNumber.getText() + ": '" +
87 b.matchedText.getText() + "'";
89 void bindTo(TemplateDetailSource item) {
91 b.groupNumber.setText(String.valueOf(item.getGroupNumber()));
92 b.matchedText.setText(item.getMatchedText());