]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/CurrencySelectorRecyclerViewAdapter.java
more pronounced day/month delimiters in the transaction list
[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.annotation.NonNull;
26 import androidx.recyclerview.widget.DiffUtil;
27 import androidx.recyclerview.widget.ListAdapter;
28 import androidx.recyclerview.widget.RecyclerView;
29
30 import net.ktnx.mobileledger.R;
31 import net.ktnx.mobileledger.model.Currency;
32
33 import org.jetbrains.annotations.NotNull;
34
35 /**
36  * {@link RecyclerView.Adapter} that can display a {@link Currency} and makes a call to the
37  * specified {@link OnCurrencySelectedListener}.
38  */
39 public class CurrencySelectorRecyclerViewAdapter
40         extends ListAdapter<String, CurrencySelectorRecyclerViewAdapter.ViewHolder> {
41     private static final DiffUtil.ItemCallback<String> DIFF_CALLBACK =
42             new DiffUtil.ItemCallback<String>() {
43                 @Override
44                 public boolean areItemsTheSame(@NonNull String oldItem, @NonNull String newItem) {
45                     return oldItem.equals(newItem);
46                 }
47                 @Override
48                 public boolean areContentsTheSame(@NonNull String oldItem,
49                                                   @NonNull String newItem) {
50                     return true;
51                 }
52             };
53
54     private OnCurrencySelectedListener currencySelectedListener;
55     private OnCurrencyLongClickListener currencyLongClickListener;
56     public CurrencySelectorRecyclerViewAdapter() {
57         super(DIFF_CALLBACK);
58     }
59     @NotNull
60     @Override
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);
65     }
66
67     @Override
68     public void onBindViewHolder(final ViewHolder holder, int position) {
69         holder.bindTo(getItem(position));
70     }
71     public void setCurrencySelectedListener(OnCurrencySelectedListener listener) {
72         this.currencySelectedListener = listener;
73     }
74     public void resetCurrencySelectedListener() {
75         currencySelectedListener = null;
76     }
77     public void notifyCurrencySelected(String currency) {
78         if (null != currencySelectedListener)
79             currencySelectedListener.onCurrencySelected(currency);
80     }
81     public void setCurrencyLongClickListener(OnCurrencyLongClickListener listener) {
82         this.currencyLongClickListener = listener;
83     }
84     public void resetCurrencyLockClickListener() { currencyLongClickListener = null; }
85     private void notifyCurrencyLongClicked(String mItem) {
86         if (null != currencyLongClickListener)
87             currencyLongClickListener.onCurrencyLongClick(mItem);
88     }
89
90     public class ViewHolder extends RecyclerView.ViewHolder {
91         private final TextView mNameView;
92         private String mItem;
93
94         ViewHolder(View view) {
95             super(view);
96             mNameView = view.findViewById(R.id.content);
97
98             view.setOnClickListener(v -> notifyCurrencySelected(mItem));
99             view.setOnLongClickListener(v -> {
100                 notifyCurrencyLongClicked(mItem);
101                 return false;
102             });
103         }
104
105         @NotNull
106         @Override
107         public String toString() {
108             return super.toString() + " '" + mNameView.getText() + "'";
109         }
110         void bindTo(String item) {
111             mItem = item;
112             mNameView.setText(item);
113         }
114     }
115 }