Added Android code
[wl-app.git] / Android / app / src / main / java / com / moiseum / wolnelektury / components / recycler / RecyclerAdapter.java
1 package com.moiseum.wolnelektury.components.recycler;
2
3 import android.content.Context;
4 import android.support.v7.widget.RecyclerView;
5 import android.view.LayoutInflater;
6 import android.view.View;
7 import android.view.ViewGroup;
8
9 import com.moiseum.wolnelektury.connection.models.CategoryModel;
10
11 import java.util.Collections;
12 import java.util.List;
13
14 /**
15  * @author golonkos
16  */
17
18 public abstract class RecyclerAdapter<T, VH extends ViewHolder> extends RecyclerView.Adapter<VH> {
19
20         /**
21          * On click listener.
22          */
23         public interface OnItemClickListener<T> {
24                 /**
25                  * @param item clicked item
26                  * @param view clicked view
27                  */
28                 void onItemClicked(T item, View view, int position);
29         }
30
31         public enum Selection {
32                 NONE, SINGLE
33         }
34
35         private static final int NO_POSITION = -1;
36
37         private LayoutInflater layoutInflater;
38         private OnItemClickListener<T> onItemClickListener;
39
40         private List<T> items = Collections.emptyList();
41
42         private T selectedItem;
43         private int selectedItemPosition = NO_POSITION;
44
45         private Selection selection = Selection.NONE;
46
47         private View.OnClickListener onClickListener = new View.OnClickListener() {
48                 @Override
49                 public void onClick(View v) {
50                         int position = (int) v.getTag();
51                         T item = getItem(position);
52                         onItemClicked(v, item, position);
53                 }
54         };
55
56         public RecyclerAdapter(Context context, Selection selection) {
57                 layoutInflater = LayoutInflater.from(context);
58                 this.selection = selection;
59         }
60
61         protected void onItemClicked(View view, T item, int position) {
62                 selectItemAndNotify(item, position);
63                 if (onItemClickListener != null) {
64                         onItemClickListener.onItemClicked(item, view, position);
65                 }
66         }
67
68         @Override
69         public void onBindViewHolder(VH viewHolder, final int position) {
70                 viewHolder.itemView.setTag(position);
71                 viewHolder.itemView.setOnClickListener(onClickListener);
72                 T item = getItem(position);
73                 viewHolder.itemView.setSelected(isSelected(getItem(position)));
74                 viewHolder.bind(item, isSelected(item));
75         }
76
77         @Override
78         public int getItemCount() {
79                 return items.size();
80         }
81
82
83         /**
84          * @param onItemClickListener item click listener
85          */
86         public void setOnItemClickListener(OnItemClickListener<T> onItemClickListener) {
87                 this.onItemClickListener = onItemClickListener;
88         }
89
90
91         public OnItemClickListener<T> getOnItemClickListener() {
92                 return onItemClickListener;
93         }
94
95         public List<T> getItems() {
96                 return items;
97         }
98
99         /**
100          * @param items new items
101          */
102         public void setItems(List<T> items) {
103                 this.items = items;
104                 notifyDataSetChanged();
105         }
106
107         public void addItems(List<T> items) {
108                 this.items.addAll(items);
109                 notifyDataSetChanged();
110         }
111
112         /**
113          * @param position position of element to remove
114          * @return removed item or null if element not found
115          */
116         public T removeItem(int position) {
117                 if (position >= 0 && position < items.size()) {
118                         T item = items.remove(position);
119                         notifyItemRemoved(position);
120                         notifyItemRangeChanged(position, items.size());
121                         return item;
122                 }
123                 return null;
124         }
125
126         public void clear() {
127                 this.items.clear();
128                 notifyDataSetChanged();
129         }
130
131         protected void addItem(int position, T item) {
132                 items.add(position, item);
133         }
134
135         protected View inflate(int layoutResId, ViewGroup parent) {
136                 return layoutInflater.inflate(layoutResId, parent, false);
137         }
138
139         /**
140          * @param position position of element
141          * @return item from specific position
142          */
143         public T getItem(int position) {
144                 return this.items.get(position);
145         }
146
147         protected abstract String getItemId(T item);
148
149         private boolean isSelected(T item) {
150                 return selectedItem != null && getItemId(selectedItem).contains(getItemId(item));
151         }
152
153         public void selectItem(T item) {
154                 int position = NO_POSITION;
155                 for (int i = 0; i < items.size(); i++) {
156                         if (getItemId(item).equals(getItemId(items.get(i)))) {
157                                 position = i;
158                                 break;
159                         }
160                 }
161                 selectItemAndNotify(item, position);
162         }
163
164         private void selectItemAndNotify(T item, int position) {
165                 int selectedPositionToNotify = selectedItemPosition;
166                 setSelectedItem(item, position);
167
168                 if (selectedPositionToNotify != NO_POSITION) {
169                         notifyItemChanged(selectedPositionToNotify);
170                 }
171                 notifyItemChanged(position);
172         }
173
174         private void setSelectedItem(T item, int position) {
175                 switch (selection) {
176                         case SINGLE:
177                                 selectedItem = item;
178                                 selectedItemPosition = position;
179                                 break;
180                         case NONE:
181                                 break;
182                 }
183         }
184
185
186 }
187