Added Android code
[wl-app.git] / Android / app / src / main / java / com / moiseum / wolnelektury / view / search / components / FiltersProgressFlowLayout.java
1 package com.moiseum.wolnelektury.view.search.components;
2
3 import android.content.Context;
4 import android.content.res.TypedArray;
5 import android.graphics.Color;
6 import android.graphics.PorterDuff;
7 import android.support.annotation.NonNull;
8 import android.support.annotation.Nullable;
9 import android.support.v7.widget.AppCompatCheckBox;
10 import android.util.AttributeSet;
11 import android.view.LayoutInflater;
12 import android.view.View;
13 import android.widget.CompoundButton;
14 import android.widget.ImageButton;
15 import android.widget.ProgressBar;
16 import android.widget.RelativeLayout;
17 import android.widget.TextView;
18
19 import com.moiseum.wolnelektury.R;
20 import com.moiseum.wolnelektury.connection.models.CategoryModel;
21 import com.nex3z.flowlayout.FlowLayout;
22
23 import java.lang.ref.WeakReference;
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import butterknife.BindView;
28 import butterknife.ButterKnife;
29 import butterknife.OnClick;
30
31 /**
32  * Created by piotrostrowski on 26.11.2017.
33  */
34
35 public class FiltersProgressFlowLayout extends RelativeLayout {
36
37         public interface FiltersProgressFlowLayoutRetryListener {
38                 void onRetryClicked();
39         }
40
41         @BindView(R.id.flList)
42         FlowLayout flList;
43         @BindView(R.id.tvEmpty)
44         TextView tvEmpty;
45         @BindView(R.id.pbLoading)
46         ProgressBar pbLoading;
47         @BindView(R.id.ibRetry)
48         ImageButton ibRetry;
49
50         private List<CategoryModel> categories = new ArrayList<>();
51         private CompoundButton.OnCheckedChangeListener checkChangeListener = new CompoundButton.OnCheckedChangeListener() {
52                 @Override
53                 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
54                         for (CategoryModel category : categories) {
55                                 String slug = (String) buttonView.getTag();
56                                 if (slug.equals(category.getSlug())) {
57                                         category.setChecked(isChecked);
58                                         return;
59                                 }
60                         }
61                 }
62         };
63         private FiltersProgressFlowLayoutRetryListener listener;
64
65         public FiltersProgressFlowLayout(@NonNull Context context) {
66                 this(context, null);
67         }
68
69         public FiltersProgressFlowLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
70                 this(context, attrs, 0);
71         }
72
73         public FiltersProgressFlowLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
74                 super(context, attrs, defStyleAttr);
75                 init(attrs);
76         }
77
78         private void init(AttributeSet attrs) {
79                 View view = LayoutInflater.from(getContext()).inflate(R.layout.progress_flowlayout, this, true);
80                 ButterKnife.bind(this, view);
81
82                 TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.FiltersProgressFlowLayout);
83                 try {
84                         if (a.hasValue(R.styleable.FiltersProgressFlowLayout_emptyLayoutText)) {
85                                 tvEmpty.setText(a.getString(R.styleable.FiltersProgressFlowLayout_emptyLayoutText));
86                         }
87                 } finally {
88                         a.recycle();
89                 }
90         }
91
92         public void addCategories(List<CategoryModel> categories) {
93                 this.categories = categories;
94                 LayoutInflater inflater = LayoutInflater.from(getContext());
95                 for (CategoryModel categoryModel : categories) {
96                         AppCompatCheckBox checkBox = (AppCompatCheckBox) inflater.inflate(R.layout.checkbox, flList, false);
97                         checkBox.setText(categoryModel.getName());
98                         checkBox.setTag(categoryModel.getSlug());
99                         checkBox.setChecked(categoryModel.isChecked());
100                         checkBox.setOnCheckedChangeListener(checkChangeListener);
101                         flList.addView(checkBox);
102                 }
103                 pbLoading.setVisibility(GONE);
104                 flList.setVisibility(VISIBLE);
105         }
106
107         public List<CategoryModel> getSelectedCategories() {
108                 List<CategoryModel> selectedCategories = new ArrayList<>();
109                 for (CategoryModel categoryModel : categories) {
110                         if (categoryModel.isChecked()) {
111                                 selectedCategories.add(categoryModel);
112                         }
113                 }
114                 return selectedCategories;
115         }
116
117         public void showRetryButton(FiltersProgressFlowLayoutRetryListener listener) {
118                 this.listener = listener;
119                 tvEmpty.setVisibility(GONE);
120                 pbLoading.setVisibility(GONE);
121                 ibRetry.setVisibility(VISIBLE);
122         }
123
124         @OnClick(R.id.ibRetry)
125         public void retryButtonClick() {
126                 if (listener != null) {
127                         listener.onRetryClicked();
128                 }
129                 ibRetry.setVisibility(GONE);
130                 pbLoading.setVisibility(VISIBLE);
131         }
132 }