diff options
author | Stefan Niedermann <info@niedermann.it> | 2020-05-05 15:49:38 +0300 |
---|---|---|
committer | Stefan Niedermann <info@niedermann.it> | 2020-05-05 15:49:38 +0300 |
commit | 7c341a632b9ce75b2d635cabcbb2628ce6bd2fbf (patch) | |
tree | 9492c6f4cb6f8039909d73c509fdce774e9af412 /app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/FilterLabelsAdapter.java | |
parent | ea53ba5cfbd2aa7cfafee00c8b0800cd8ac021c4 (diff) |
Renaming
Diffstat (limited to 'app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/FilterLabelsAdapter.java')
-rw-r--r-- | app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/FilterLabelsAdapter.java | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/FilterLabelsAdapter.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/FilterLabelsAdapter.java new file mode 100644 index 000000000..096f0db9c --- /dev/null +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/filter/FilterLabelsAdapter.java @@ -0,0 +1,95 @@ +package it.niedermann.nextcloud.deck.ui.filter; + +import android.content.res.ColorStateList; +import android.graphics.Color; +import android.view.LayoutInflater; +import android.view.ViewGroup; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.recyclerview.widget.RecyclerView; + +import java.util.ArrayList; +import java.util.List; + +import it.niedermann.nextcloud.deck.databinding.ItemFilterLabelBinding; +import it.niedermann.nextcloud.deck.model.Label; +import it.niedermann.nextcloud.deck.util.ColorUtil; + +@SuppressWarnings("WeakerAccess") +public class FilterLabelsAdapter extends RecyclerView.Adapter<FilterLabelsAdapter.LabelViewHolder> { + @NonNull + private final List<Label> labels = new ArrayList<>(); + @NonNull + private final List<Label> selectedLabels = new ArrayList<>(); + @Nullable + private final SelectionListener<Label> selectionListener; + + public FilterLabelsAdapter(@NonNull List<Label> labels, @NonNull List<Label> selectedLabels, @Nullable SelectionListener<Label> selectionListener) { + super(); + this.labels.addAll(labels); + this.selectedLabels.addAll(selectedLabels); + this.selectionListener = selectionListener; + setHasStableIds(true); + notifyDataSetChanged(); + } + + @Override + public long getItemId(int position) { + return labels.get(position).getLocalId(); + } + + @NonNull + @Override + public LabelViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + return new LabelViewHolder(ItemFilterLabelBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false)); + } + + @Override + public void onBindViewHolder(@NonNull LabelViewHolder viewHolder, int position) { + viewHolder.bind(labels.get(position)); + } + + @Override + public int getItemCount() { + return labels.size(); + } + + public List<Label> getSelected() { + return selectedLabels; + } + + class LabelViewHolder extends RecyclerView.ViewHolder { + private ItemFilterLabelBinding binding; + + LabelViewHolder(@NonNull ItemFilterLabelBinding binding) { + super(binding.getRoot()); + this.binding = binding; + } + + void bind(final Label label) { + binding.label.setText(label.getTitle()); + final int labelColor = Color.parseColor("#" + label.getColor()); + binding.label.setChipBackgroundColor(ColorStateList.valueOf(labelColor)); + final int color = ColorUtil.getForegroundColorForBackgroundColor(labelColor); + binding.label.setTextColor(color); + itemView.setSelected(selectedLabels.contains(label)); + + itemView.setOnClickListener(view -> { + if (selectedLabels.contains(label)) { + selectedLabels.remove(label); + itemView.setSelected(false); + if (selectionListener != null) { + selectionListener.onItemDeselected(label); + } + } else { + selectedLabels.add(label); + itemView.setSelected(true); + if (selectionListener != null) { + selectionListener.onItemSelected(label); + } + } + }); + } + } +} |