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.annotation.NonNull;
26 import androidx.recyclerview.widget.DiffUtil;
27 import androidx.recyclerview.widget.ListAdapter;
28 import androidx.recyclerview.widget.RecyclerView;
30 import net.ktnx.mobileledger.R;
31 import net.ktnx.mobileledger.model.Currency;
33 import org.jetbrains.annotations.NotNull;
36 * {@link RecyclerView.Adapter} that can display a {@link Currency} and makes a call to the
37 * specified {@link OnCurrencySelectedListener}.
39 public class CurrencySelectorRecyclerViewAdapter
40 extends ListAdapter<String, CurrencySelectorRecyclerViewAdapter.ViewHolder> {
41 private static final DiffUtil.ItemCallback<String> DIFF_CALLBACK =
42 new DiffUtil.ItemCallback<String>() {
44 public boolean areItemsTheSame(@NonNull String oldItem, @NonNull String newItem) {
45 return oldItem.equals(newItem);
48 public boolean areContentsTheSame(@NonNull String oldItem,
49 @NonNull String newItem) {
54 private OnCurrencySelectedListener currencySelectedListener;
55 private OnCurrencyLongClickListener currencyLongClickListener;
56 public CurrencySelectorRecyclerViewAdapter() {
61 public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
62 View view = LayoutInflater.from(parent.getContext())
63 .inflate(R.layout.fragment_currency_selector, parent, false);
64 return new ViewHolder(view);
68 public void onBindViewHolder(final ViewHolder holder, int position) {
69 holder.bindTo(getItem(position));
71 public void setCurrencySelectedListener(OnCurrencySelectedListener listener) {
72 this.currencySelectedListener = listener;
74 public void resetCurrencySelectedListener() {
75 currencySelectedListener = null;
77 public void notifyCurrencySelected(String currency) {
78 if (null != currencySelectedListener)
79 currencySelectedListener.onCurrencySelected(currency);
81 public void setCurrencyLongClickListener(OnCurrencyLongClickListener listener) {
82 this.currencyLongClickListener = listener;
84 public void resetCurrencyLockClickListener() { currencyLongClickListener = null; }
85 private void notifyCurrencyLongClicked(String mItem) {
86 if (null != currencyLongClickListener)
87 currencyLongClickListener.onCurrencyLongClick(mItem);
90 public class ViewHolder extends RecyclerView.ViewHolder {
91 private final TextView mNameView;
94 ViewHolder(View view) {
96 mNameView = view.findViewById(R.id.content);
98 view.setOnClickListener(v -> notifyCurrencySelected(mItem));
99 view.setOnLongClickListener(v -> {
100 notifyCurrencyLongClicked(mItem);
107 public String toString() {
108 return super.toString() + " '" + mNameView.getText() + "'";
110 void bindTo(String item) {
112 mNameView.setText(item);