|
| 1 | +package fr.free.nrw.commons.category; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.view.LayoutInflater; |
| 5 | +import android.view.View; |
| 6 | +import android.view.ViewGroup; |
| 7 | +import android.widget.CheckBox; |
| 8 | +import android.widget.CompoundButton; |
| 9 | +import android.widget.CompoundButton.OnCheckedChangeListener; |
| 10 | +import android.widget.Filter; |
| 11 | +import android.widget.Filterable; |
| 12 | +import android.widget.TextView; |
| 13 | +import androidx.annotation.NonNull; |
| 14 | +import androidx.recyclerview.widget.RecyclerView; |
| 15 | +import fr.free.nrw.commons.R; |
| 16 | +import fr.free.nrw.commons.category.CategoryEditSearchRecyclerViewAdapter.RecyclerViewHolder; |
| 17 | +import fr.free.nrw.commons.nearby.Label; |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +public class CategoryEditSearchRecyclerViewAdapter |
| 22 | + extends RecyclerView.Adapter<RecyclerViewHolder> |
| 23 | + implements Filterable { |
| 24 | + |
| 25 | + private List<String> displayedCategories; |
| 26 | + private List<String> categories = new ArrayList<>(); |
| 27 | + private List<String> newCategories = new ArrayList<>(); |
| 28 | + private final LayoutInflater inflater; |
| 29 | + private CategoryClient categoryClient; |
| 30 | + private Context context; |
| 31 | + |
| 32 | + private Callback callback; |
| 33 | + |
| 34 | + public CategoryEditSearchRecyclerViewAdapter(Context context, ArrayList<Label> labels, |
| 35 | + RecyclerView categoryRecyclerView, CategoryClient categoryClient, Callback callback) { |
| 36 | + this.context = context; |
| 37 | + inflater = LayoutInflater.from(context); |
| 38 | + this.categoryClient = categoryClient; |
| 39 | + this.callback = callback; |
| 40 | + } |
| 41 | + |
| 42 | + public void addToCategories(List<String> categories) { |
| 43 | + for(String category : categories) { |
| 44 | + if (!this.categories.contains(category)) { |
| 45 | + this.categories.add(category); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + public void addToCategories(String categoryToBeAdded) { |
| 51 | + if (!categories.contains(categoryToBeAdded)) { |
| 52 | + categories.add(categoryToBeAdded); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + public void removeFromCategories(String categoryToBeRemoved) { |
| 57 | + if (categories.contains(categoryToBeRemoved)) { |
| 58 | + categories.remove(categoryToBeRemoved); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + public void removeFromNewCategories(String categoryToBeRemoved) { |
| 63 | + if (newCategories.contains(categoryToBeRemoved)) { |
| 64 | + newCategories.remove(categoryToBeRemoved); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + public void addToNewCategories(List<String> newCategories) { |
| 69 | + for(String category : newCategories) { |
| 70 | + if (!this.newCategories.contains(category)) { |
| 71 | + this.newCategories.add(category); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + public void addToNewCategories(String addedCategory) { |
| 77 | + if (!newCategories.contains(addedCategory)) { |
| 78 | + newCategories.add(addedCategory); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + public List<String> getCategories() { |
| 83 | + return categories; |
| 84 | + } |
| 85 | + |
| 86 | + public List<String> getNewCategories() { |
| 87 | + return newCategories; |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public Filter getFilter() { |
| 92 | + return new Filter() { |
| 93 | + |
| 94 | + @Override |
| 95 | + protected FilterResults performFiltering(CharSequence constraint) { |
| 96 | + FilterResults results = new FilterResults(); |
| 97 | + List<String> resultCategories = categoryClient.searchCategories(constraint.toString(), 10).blockingGet(); |
| 98 | + results.values = resultCategories; |
| 99 | + results.count = resultCategories.size(); |
| 100 | + return results; |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + protected void publishResults(CharSequence constraint, FilterResults results) { |
| 105 | + List<String> resultList = (List<String>)results.values; |
| 106 | + // Do not re-add already added categories |
| 107 | + for (String category : categories) { |
| 108 | + if (resultList.contains(category)) { |
| 109 | + resultList.remove(category); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + displayedCategories = resultList; |
| 114 | + notifyDataSetChanged(); |
| 115 | + if (displayedCategories.size()==0) { |
| 116 | + callback.noResultsFound(); |
| 117 | + } else { |
| 118 | + callback.someResultsFound(); |
| 119 | + } |
| 120 | + } |
| 121 | + }; |
| 122 | + } |
| 123 | + |
| 124 | + public class RecyclerViewHolder extends RecyclerView.ViewHolder { |
| 125 | + public CheckBox categoryCheckBox; |
| 126 | + public TextView categoryTextView; |
| 127 | + |
| 128 | + public RecyclerViewHolder(View view) { |
| 129 | + super(view); |
| 130 | + categoryCheckBox = view.findViewById(R.id.category_checkbox); |
| 131 | + categoryTextView = view.findViewById(R.id.category_text); |
| 132 | + categoryCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() { |
| 133 | + @Override |
| 134 | + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { |
| 135 | + if (isChecked) { |
| 136 | + addToNewCategories(categoryTextView.getText().toString()); |
| 137 | + } else { |
| 138 | + removeFromNewCategories(categoryTextView.getText().toString()); |
| 139 | + } |
| 140 | + List<String> allCategories = new ArrayList<>(); |
| 141 | + allCategories.addAll(categories); |
| 142 | + allCategories.addAll(newCategories); |
| 143 | + callback.updateSelectedCategoriesTextView(allCategories); |
| 144 | + } |
| 145 | + }); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + @NonNull |
| 150 | + @Override |
| 151 | + public RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { |
| 152 | + View itemView = inflater.inflate(R.layout.layout_edit_category_item , parent, false); |
| 153 | + return new RecyclerViewHolder(itemView); |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public void onBindViewHolder(@NonNull RecyclerViewHolder holder, int position) { |
| 158 | + holder.categoryTextView.setText(displayedCategories.get(position)); |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public long getItemId(int position) { |
| 163 | + return displayedCategories.get(position).hashCode(); |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public int getItemCount() { |
| 168 | + return (displayedCategories == null) ? 0 : displayedCategories.size(); |
| 169 | + } |
| 170 | + |
| 171 | + public interface Callback { |
| 172 | + void updateSelectedCategoriesTextView(List<String> selectedCategories); |
| 173 | + void noResultsFound(); |
| 174 | + void someResultsFound(); |
| 175 | + } |
| 176 | +} |
0 commit comments