]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/CurrencySelectorRecyclerViewAdapter.java
make transaction header row always have Id of 0
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / CurrencySelectorRecyclerViewAdapter.java
1 /*
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.
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.View;
22 import android.view.ViewGroup;
23 import android.widget.TextView;
24
25 import androidx.recyclerview.widget.ListAdapter;
26 import androidx.recyclerview.widget.RecyclerView;
27
28 import net.ktnx.mobileledger.R;
29 import net.ktnx.mobileledger.model.Currency;
30
31 import org.jetbrains.annotations.NotNull;
32
33 /**
34  * {@link RecyclerView.Adapter} that can display a {@link Currency} and makes a call to the
35  * specified {@link OnCurrencySelectedListener}.
36  */
37 public class CurrencySelectorRecyclerViewAdapter
38         extends ListAdapter<Currency, CurrencySelectorRecyclerViewAdapter.ViewHolder> {
39
40     private OnCurrencySelectedListener currencySelectedListener;
41     private OnCurrencyLongClickListener currencyLongClickListener;
42     public CurrencySelectorRecyclerViewAdapter() {
43         super(Currency.DIFF_CALLBACK);
44     }
45     @NotNull
46     @Override
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);
51     }
52
53     @Override
54     public void onBindViewHolder(final ViewHolder holder, int position) {
55         holder.bindTo(getItem(position));
56     }
57     public void setCurrencySelectedListener(OnCurrencySelectedListener listener) {
58         this.currencySelectedListener = listener;
59     }
60     public void resetCurrencySelectedListener() {
61         currencySelectedListener = null;
62     }
63     public void notifyCurrencySelected(Currency currency) {
64         if (null != currencySelectedListener)
65             currencySelectedListener.onCurrencySelected(currency);
66     }
67     public void setCurrencyLongClickListener(OnCurrencyLongClickListener listener) {
68         this.currencyLongClickListener = listener;
69     }
70     public void resetCurrencyLockClickListener() { currencyLongClickListener = null; }
71     private void notifyCurrencyLongClicked(Currency mItem) {
72         if (null != currencyLongClickListener)
73             currencyLongClickListener.onCurrencyLongClick(mItem);
74     }
75
76     public class ViewHolder extends RecyclerView.ViewHolder {
77         private final TextView mNameView;
78         private Currency mItem;
79
80         ViewHolder(View view) {
81             super(view);
82             mNameView = view.findViewById(R.id.content);
83
84             view.setOnClickListener(v -> notifyCurrencySelected(mItem));
85             view.setOnLongClickListener(v -> {
86                 notifyCurrencyLongClicked(mItem);
87                 return false;
88             });
89         }
90
91         @NotNull
92         @Override
93         public String toString() {
94             return super.toString() + " '" + mNameView.getText() + "'";
95         }
96         void bindTo(Currency item) {
97             mItem = item;
98             mNameView.setText(item.getName());
99         }
100     }
101 }