Welcome to mirror list, hosted at ThFree Co, Russian Federation.

FilterDueTypeAdapter.java « filter « ui « deck « nextcloud « niedermann « it « java « main « src « app - github.com/stefan-niedermann/nextcloud-deck.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: dab5bb9f7e3bba244dfc096fec413a39c5da36d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package it.niedermann.nextcloud.deck.ui.filter;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import java.util.Arrays;

import it.niedermann.nextcloud.deck.databinding.ItemFilterDuetypeBinding;
import it.niedermann.nextcloud.deck.model.enums.EDueType;

public class FilterDueTypeAdapter extends RecyclerView.Adapter<FilterDueTypeAdapter.DueTypeViewHolder> {
    @NonNull
    private final EDueType[] dueTypes = EDueType.values();
    private int selectedDueTypePosition;
    @Nullable
    private final SelectionListener<EDueType> selectionListener;

    @SuppressWarnings("WeakerAccess")
    public FilterDueTypeAdapter(@NonNull EDueType selectedDueType, @Nullable SelectionListener<EDueType> selectionListener) {
        super();
        this.selectedDueTypePosition = Arrays.binarySearch(dueTypes, selectedDueType);
        this.selectionListener = selectionListener;
        setHasStableIds(true);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public DueTypeViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new DueTypeViewHolder(ItemFilterDuetypeBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull DueTypeViewHolder viewHolder, int position) {
        viewHolder.bind(dueTypes[position]);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return dueTypes.length;
    }

    class DueTypeViewHolder extends RecyclerView.ViewHolder {
        private ItemFilterDuetypeBinding binding;

        DueTypeViewHolder(@NonNull ItemFilterDuetypeBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        void bind(final EDueType dueType) {
            binding.dueType.setText(dueType.toString(binding.dueType.getContext()));
            itemView.setSelected(dueTypes[selectedDueTypePosition].equals(dueType));

            itemView.setOnClickListener(view -> {
                final int oldSelection = selectedDueTypePosition;
                if (dueTypes[selectedDueTypePosition].equals(dueType)) {
                    selectedDueTypePosition = Arrays.binarySearch(dueTypes, EDueType.NO_FILTER);
                    itemView.setSelected(false);
                    if (selectionListener != null) {
                        selectionListener.onItemSelected(EDueType.NO_FILTER);
                    }
                    notifyItemChanged(selectedDueTypePosition);
                } else {
                    selectedDueTypePosition = Arrays.binarySearch(dueTypes, dueType);
                    itemView.setSelected(true);
                    if (selectionListener != null) {
                        selectionListener.onItemSelected(dueType);
                    }
                }
                notifyItemChanged(oldSelection);
            });
        }
    }
}