]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/TemplateDetailSourceSelectorRecyclerViewAdapter.java
fix displayed transactions count
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / TemplateDetailSourceSelectorRecyclerViewAdapter.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;
19
20 import android.view.LayoutInflater;
21 import android.view.ViewGroup;
22
23 import androidx.recyclerview.widget.ListAdapter;
24 import androidx.recyclerview.widget.RecyclerView;
25
26 import net.ktnx.mobileledger.databinding.FragmentTemplateDetailSourceSelectorBinding;
27 import net.ktnx.mobileledger.model.TemplateDetailSource;
28
29 import org.jetbrains.annotations.NotNull;
30
31 /**
32  * {@link RecyclerView.Adapter} that can display a {@link TemplateDetailSource} and makes a call
33  * to the
34  * specified {@link OnSourceSelectedListener}.
35  */
36 public class TemplateDetailSourceSelectorRecyclerViewAdapter extends
37         ListAdapter<TemplateDetailSource,
38                 TemplateDetailSourceSelectorRecyclerViewAdapter.ViewHolder> {
39
40     private OnSourceSelectedListener sourceSelectedListener;
41     public TemplateDetailSourceSelectorRecyclerViewAdapter() {
42         super(TemplateDetailSource.DIFF_CALLBACK);
43     }
44     @NotNull
45     @Override
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);
51     }
52
53     @Override
54     public void onBindViewHolder(final ViewHolder holder, int position) {
55         holder.bindTo(getItem(position));
56     }
57     public void setSourceSelectedListener(OnSourceSelectedListener listener) {
58         this.sourceSelectedListener = listener;
59     }
60     public void resetSourceSelectedListener() {
61         sourceSelectedListener = null;
62     }
63     public void notifySourceSelected(TemplateDetailSource item) {
64         if (null != sourceSelectedListener)
65             sourceSelectedListener.onSourceSelected(false, item.getGroupNumber());
66     }
67     public void notifyLiteralSelected() {
68         if (null != sourceSelectedListener)
69             sourceSelectedListener.onSourceSelected(true, (short) -1);
70     }
71     public class ViewHolder extends RecyclerView.ViewHolder {
72         private final FragmentTemplateDetailSourceSelectorBinding b;
73         private TemplateDetailSource mItem;
74
75         ViewHolder(FragmentTemplateDetailSourceSelectorBinding binding) {
76             super(binding.getRoot());
77             b = binding;
78
79             b.getRoot()
80              .setOnClickListener(v -> notifySourceSelected(mItem));
81         }
82
83         @NotNull
84         @Override
85         public String toString() {
86             return super.toString() + " " + b.groupNumber.getText() + ": '" +
87                    b.matchedText.getText() + "'";
88         }
89         void bindTo(TemplateDetailSource item) {
90             mItem = item;
91             b.groupNumber.setText(String.valueOf(item.getGroupNumber()));
92             b.matchedText.setText(item.getMatchedText());
93         }
94     }
95 }