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

FilterDoneTypeAdapter.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: d194f78abaea5f5d9666f7b0b9923178c22e61ad (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
85
86
87
88
89
90
91
92
93
94
95
96
package it.niedermann.nextcloud.deck.ui.filter;

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

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

import java.util.Arrays;

import it.niedermann.nextcloud.deck.databinding.ItemFilterDonetypeBinding;
import it.niedermann.nextcloud.deck.model.enums.EDoneType;
import it.niedermann.nextcloud.deck.ui.theme.ThemeUtils;
import it.niedermann.nextcloud.deck.ui.theme.Themed;

public class FilterDoneTypeAdapter extends RecyclerView.Adapter<FilterDoneTypeAdapter.DoneTypeViewHolder> {
    @NonNull
    private final EDoneType[] doneTypes = EDoneType.values();
    private int selectedDoneTypePosition;
    @Nullable
    private final SelectionListener<EDoneType> selectionListener;
    @ColorInt
    private final int color;

    @SuppressWarnings("WeakerAccess")
    public FilterDoneTypeAdapter(@NonNull EDoneType selectedDoneType, @Nullable SelectionListener<EDoneType> selectionListener, @ColorInt int color) {
        super();
        this.selectedDoneTypePosition = Arrays.binarySearch(doneTypes, selectedDoneType);
        this.selectionListener = selectionListener;
        this.color = color;
        setHasStableIds(true);
    }

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

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

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

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

    class DoneTypeViewHolder extends RecyclerView.ViewHolder implements Themed {
        private final ItemFilterDonetypeBinding binding;

        DoneTypeViewHolder(@NonNull ItemFilterDonetypeBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        void bind(final EDoneType doneType) {
            binding.doneType.setText(doneType.toString(binding.doneType.getContext()));
            itemView.setSelected(doneTypes[selectedDoneTypePosition].equals(doneType));
            applyTheme(color);

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

        @Override
        public void applyTheme(int color) {
            final var utils = ThemeUtils.of(color, itemView.getContext());
            utils.deck.themeSelectedCheck(binding.selectedCheck.getContext(), binding.selectedCheck.getDrawable());
        }
    }
}