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

CategoryAdapter.java « category « edit « notes « owncloud « niedermann « it « java « main « src « app - github.com/stefan-niedermann/nextcloud-notes.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4b3bb9ba8ffeb46463f9c7f91cbf01051e62b477 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package it.niedermann.owncloud.notes.edit.category;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.databinding.ItemCategoryBinding;
import it.niedermann.owncloud.notes.main.navigation.NavigationItem;
import it.niedermann.owncloud.notes.shared.util.NoteUtil;

public class CategoryAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final String clearItemId = "clear_item";
    private static final String addItemId = "add_item";
    @NonNull
    private List<NavigationItem> categories = new ArrayList<>();
    @NonNull
    private final CategoryListener listener;
    private final Context context;

    CategoryAdapter(@NonNull Context context, @NonNull CategoryListener categoryListener) {
        this.context = context;
        this.listener = categoryListener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_category, parent, false);
        return new CategoryViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        NavigationItem category = categories.get(position);
        CategoryViewHolder categoryViewHolder = (CategoryViewHolder) holder;

        switch (category.id) {
            case addItemId:
                Drawable wrapDrawable = DrawableCompat.wrap(ContextCompat.getDrawable(context, category.icon));
                DrawableCompat.setTint(wrapDrawable, ContextCompat.getColor(context, R.color.icon_color_default));
                categoryViewHolder.getIcon().setImageDrawable(wrapDrawable);
                categoryViewHolder.getCategoryWrapper().setOnClickListener((v) -> listener.onCategoryAdded());
                break;
            case clearItemId:
                categoryViewHolder.getIcon().setImageDrawable(ContextCompat.getDrawable(context, category.icon));
                categoryViewHolder.getCategoryWrapper().setOnClickListener((v) -> listener.onCategoryCleared());
                break;
            default:
                categoryViewHolder.getIcon().setImageDrawable(ContextCompat.getDrawable(context, category.icon));
                categoryViewHolder.getCategoryWrapper().setOnClickListener((v) -> listener.onCategoryChosen(category.label));
                break;
        }
        categoryViewHolder.getCategory().setText(NoteUtil.extendCategory(category.label));
        if (category.count != null && category.count > 0) {
            categoryViewHolder.getCount().setText(String.valueOf(category.count));
        } else {
            categoryViewHolder.getCount().setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return categories.size();
    }

    static class CategoryViewHolder extends RecyclerView.ViewHolder {
        private final ItemCategoryBinding binding;

        private CategoryViewHolder(View view) {
            super(view);
            binding = ItemCategoryBinding.bind(view);
        }

        private View getCategoryWrapper() {
            return binding.categoryWrapper;
        }

        private AppCompatImageView getIcon() {
            return binding.icon;
        }

        private TextView getCategory() {
            return binding.category;
        }

        private TextView getCount() {
            return binding.count;
        }
    }

    void setCategoryList(List<NavigationItem.CategoryNavigationItem> categories, @Nullable String currentSearchString) {
        this.categories.clear();
        this.categories.addAll(categories);
        final NavigationItem clearItem = new NavigationItem(clearItemId, context.getString(R.string.no_category), 0, R.drawable.ic_clear_grey_24dp);
        this.categories.add(0, clearItem);
        if (currentSearchString != null && currentSearchString.trim().length() > 0) {
            boolean currentSearchStringIsInCategories = false;
            for (NavigationItem category : categories) {
                if (currentSearchString.equals(category.label)) {
                    currentSearchStringIsInCategories = true;
                    break;
                }
            }
            if (!currentSearchStringIsInCategories) {
                NavigationItem addItem = new NavigationItem(addItemId, context.getString(R.string.add_category, currentSearchString.trim()), 0, R.drawable.ic_add_blue_24dp);
                this.categories.add(addItem);
            }
        }
        notifyDataSetChanged();
    }

    public interface CategoryListener {
        void onCategoryChosen(String category);

        void onCategoryAdded();

        void onCategoryCleared();
    }
}