2 * Copyright © 2019 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.View;
22 import android.view.ViewGroup;
23 import android.widget.TextView;
25 import androidx.recyclerview.widget.ListAdapter;
26 import androidx.recyclerview.widget.RecyclerView;
28 import net.ktnx.mobileledger.R;
29 import net.ktnx.mobileledger.model.Currency;
31 import org.jetbrains.annotations.NotNull;
34 * {@link RecyclerView.Adapter} that can display a {@link Currency} and makes a call to the
35 * specified {@link OnCurrencySelectedListener}.
37 public class CurrencySelectorRecyclerViewAdapter
38 extends ListAdapter<Currency, CurrencySelectorRecyclerViewAdapter.ViewHolder> {
40 private OnCurrencySelectedListener currencySelectedListener;
41 private OnCurrencyLongClickListener currencyLongClickListener;
42 public CurrencySelectorRecyclerViewAdapter() {
43 super(Currency.DIFF_CALLBACK);
47 public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
48 View view = LayoutInflater.from(parent.getContext())
49 .inflate(R.layout.fragment_currency_selector, parent, false);
50 return new ViewHolder(view);
54 public void onBindViewHolder(final ViewHolder holder, int position) {
55 holder.bindTo(getItem(position));
57 public void setCurrencySelectedListener(OnCurrencySelectedListener listener) {
58 this.currencySelectedListener = listener;
60 public void resetCurrencySelectedListener() {
61 currencySelectedListener = null;
63 public void notifyCurrencySelected(Currency currency) {
64 if (null != currencySelectedListener)
65 currencySelectedListener.onCurrencySelected(currency);
67 public void setCurrencyLongClickListener(OnCurrencyLongClickListener listener) {
68 this.currencyLongClickListener = listener;
70 public void resetCurrencyLockClickListener() { currencyLongClickListener = null; }
71 private void notifyCurrencyLongClicked(Currency mItem) {
72 if (null != currencyLongClickListener)
73 currencyLongClickListener.onCurrencyLongClick(mItem);
76 public class ViewHolder extends RecyclerView.ViewHolder {
77 private final TextView mNameView;
78 private Currency mItem;
80 ViewHolder(View view) {
82 mNameView = view.findViewById(R.id.content);
84 view.setOnClickListener(v -> notifyCurrencySelected(mItem));
85 view.setOnLongClickListener(v -> {
86 notifyCurrencyLongClicked(mItem);
93 public String toString() {
94 return super.toString() + " '" + mNameView.getText() + "'";
96 void bindTo(Currency item) {
98 mNameView.setText(item.getName());