summaryrefslogtreecommitdiffstats
path: root/app/src/main/java/fr/gouv/etalab/mastodon/drawers/EmojisSearchAdapter.java
blob: cfcc83fc5137a2cf0648d15806445568deac9c72 (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
135
136
137
138
139
140
141
142
143
144
145
146
package fr.gouv.etalab.mastodon.drawers;
/* Copyright 2017 Thomas Schneider
 *
 * This file is a part of Mastalab
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 *
 * Mastalab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with Mastalab; if not,
 * see <http://www.gnu.org/licenses>. */


import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;


import com.bumptech.glide.Glide;

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

import fr.gouv.etalab.mastodon.R;
import fr.gouv.etalab.mastodon.client.Entities.Emojis;



/**
 * Created by Thomas on 01/11/2017.
 * Adapter for emojis when searching
 */
public class EmojisSearchAdapter extends ArrayAdapter<Emojis> implements Filterable {

    private List<Emojis> emojis, tempEmojis, suggestions ;
    private LayoutInflater layoutInflater;

    public EmojisSearchAdapter(Context context, List<Emojis> emojis){
        super(context, android.R.layout.simple_list_item_1, emojis);
        this.emojis = emojis;
        this.tempEmojis = new ArrayList<>(emojis);
        this.suggestions = new ArrayList<>(emojis);
        layoutInflater = LayoutInflater.from(context);
    }


    @Override
    public int getCount() {
        return emojis.size();
    }

    @Override
    public Emojis getItem(int position) {
        return emojis.get(position);
    }

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


    @NonNull
    @Override
    public View getView(final int position, View convertView, @NonNull ViewGroup parent) {

        final Emojis emoji = emojis.get(position);
        final ViewHolder holder;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.drawer_emoji_search, parent, false);
            holder = new ViewHolder();
            holder.emoji_icon = convertView.findViewById(R.id.emoji_icon);
            holder.emoji_shortcode = convertView.findViewById(R.id.emoji_shortcode);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.emoji_shortcode.setText(String.format("%s", emoji.getShortcode()));
        //Profile picture
        Glide.with(holder.emoji_icon.getContext())
                .load(emoji.getUrl())
                .into(holder.emoji_icon);
        return convertView;
    }

    @NonNull
    @Override
    public Filter getFilter() {
        return emojiFilter;
    }


    private Filter emojiFilter = new Filter() {
        @Override
        public CharSequence convertResultToString(Object resultValue) {
            Emojis emoji = (Emojis) resultValue;
            return emoji.getShortcode();
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            if (constraint != null) {
                suggestions.clear();
                suggestions.addAll(tempEmojis);
                FilterResults filterResults = new FilterResults();
                filterResults.values = suggestions;
                filterResults.count = suggestions.size();
                return filterResults;
            } else {
                return new FilterResults();
            }
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            ArrayList<Emojis> c = (ArrayList<Emojis>) results.values;
            if (results.count > 0) {
                clear();
                addAll(c);
                notifyDataSetChanged();
            } else{
                clear();
                notifyDataSetChanged();
            }
        }
    };

    private class ViewHolder {
        ImageView emoji_icon;
        TextView emoji_shortcode;
    }


}