|
| 1 | +package it.niedermann.owncloud.notes.edit.details; |
| 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.TextView; |
| 8 | + |
| 9 | +import androidx.annotation.NonNull; |
| 10 | +import androidx.annotation.Nullable; |
| 11 | +import androidx.appcompat.widget.AppCompatImageView; |
| 12 | +import androidx.core.content.ContextCompat; |
| 13 | +import androidx.recyclerview.widget.RecyclerView; |
| 14 | + |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.List; |
| 17 | + |
| 18 | +import it.niedermann.owncloud.notes.R; |
| 19 | +import it.niedermann.owncloud.notes.databinding.ItemCategoryBinding; |
| 20 | +import it.niedermann.owncloud.notes.main.navigation.NavigationItem; |
| 21 | +import it.niedermann.owncloud.notes.shared.util.NoteUtil; |
| 22 | + |
| 23 | +public class CategoryAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { |
| 24 | + |
| 25 | + @NonNull |
| 26 | + private final List<NavigationItem> categories = new ArrayList<>(); |
| 27 | + @NonNull |
| 28 | + private final CategoryListener listener; |
| 29 | + private final Context context; |
| 30 | + |
| 31 | + |
| 32 | + public CategoryAdapter(@NonNull Context context, @NonNull CategoryListener categoryListener) { |
| 33 | + this.context = context; |
| 34 | + this.listener = categoryListener; |
| 35 | + } |
| 36 | + |
| 37 | + @NonNull |
| 38 | + @Override |
| 39 | + public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { |
| 40 | + View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_category, parent, false); |
| 41 | + return new CategoryViewHolder(v); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { |
| 46 | + final NavigationItem category = categories.get(position); |
| 47 | + final CategoryViewHolder categoryViewHolder = (CategoryViewHolder) holder; |
| 48 | + categoryViewHolder.getIcon().setImageDrawable(ContextCompat.getDrawable(context, category.icon)); |
| 49 | + categoryViewHolder.getCategoryWrapper().setOnClickListener((v) -> listener.onCategoryChosen(category.label)); |
| 50 | + categoryViewHolder.getCategory().setText(NoteUtil.extendCategory(category.label)); |
| 51 | + if (category.count != null && category.count > 0) { |
| 52 | + categoryViewHolder.getCount().setText(String.valueOf(category.count)); |
| 53 | + } else { |
| 54 | + categoryViewHolder.getCount().setVisibility(View.GONE); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public int getItemCount() { |
| 60 | + return categories.size(); |
| 61 | + } |
| 62 | + |
| 63 | + static class CategoryViewHolder extends RecyclerView.ViewHolder { |
| 64 | + private final ItemCategoryBinding binding; |
| 65 | + |
| 66 | + private CategoryViewHolder(View view) { |
| 67 | + super(view); |
| 68 | + binding = ItemCategoryBinding.bind(view); |
| 69 | + } |
| 70 | + |
| 71 | + private View getCategoryWrapper() { |
| 72 | + return binding.categoryWrapper; |
| 73 | + } |
| 74 | + |
| 75 | + private AppCompatImageView getIcon() { |
| 76 | + return binding.icon; |
| 77 | + } |
| 78 | + |
| 79 | + private TextView getCategory() { |
| 80 | + return binding.category; |
| 81 | + } |
| 82 | + |
| 83 | + private TextView getCount() { |
| 84 | + return binding.count; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * @deprecated use {@link #setCategoryList(List)} |
| 90 | + */ |
| 91 | + public void setCategoryList(List<NavigationItem.CategoryNavigationItem> categories, @Nullable String currentSearchString) { |
| 92 | + setCategoryList(categories); |
| 93 | + } |
| 94 | + |
| 95 | + public void setCategoryList(@NonNull List<NavigationItem.CategoryNavigationItem> categories) { |
| 96 | + this.categories.clear(); |
| 97 | + this.categories.addAll(categories); |
| 98 | + notifyDataSetChanged(); |
| 99 | + } |
| 100 | + |
| 101 | + public interface CategoryListener { |
| 102 | + void onCategoryChosen(String category); |
| 103 | + |
| 104 | + default void onCategoryAdded() { |
| 105 | + } |
| 106 | + |
| 107 | + default void onCategoryCleared() { |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments