1/*
2 * Copyright (c) 2018, Daniel Gultsch All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation and/or
12 * other materials provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its contributors
15 * may be used to endorse or promote products derived from this software without
16 * specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30package eu.siacs.conversations.ui.adapter;
31
32import android.content.Context;
33import androidx.annotation.NonNull;
34import android.widget.ArrayAdapter;
35import android.widget.Filter;
36
37import java.util.ArrayList;
38import java.util.List;
39import java.util.Locale;
40
41import eu.siacs.conversations.entities.PresenceTemplate;
42
43
44public class PresenceTemplateAdapter extends ArrayAdapter<PresenceTemplate> {
45
46 private final List<PresenceTemplate> templates;
47
48 private final Filter filter = new Filter() {
49
50 @Override
51 protected FilterResults performFiltering(CharSequence constraint) {
52 FilterResults results = new FilterResults();
53 if (constraint == null || constraint.length() == 0) {
54 results.values = new ArrayList<>(templates);
55 results.count = templates.size();
56 } else {
57 ArrayList<PresenceTemplate> suggestions = new ArrayList<>();
58 final String needle = constraint.toString().trim().toLowerCase(Locale.getDefault());
59 for(PresenceTemplate template : templates) {
60 final String lc = template.getStatusMessage().toLowerCase(Locale.getDefault());
61 if (needle.isEmpty() || lc.contains(needle)) {
62 suggestions.add(template);
63 }
64 }
65 results.values = suggestions;
66 results.count = suggestions.size();
67 }
68 return results;
69 }
70
71 @Override
72 protected void publishResults(CharSequence constraint, FilterResults results) {
73 ArrayList filteredList = (ArrayList) results.values;
74 clear();
75 for (Object c : filteredList) {
76 add((PresenceTemplate) c);
77 }
78 notifyDataSetChanged();
79 }
80 };
81
82 public PresenceTemplateAdapter(@NonNull Context context, int resource, @NonNull List<PresenceTemplate> templates) {
83 super(context, resource, new ArrayList<>());
84 this.templates = new ArrayList<>(templates);
85 }
86
87 @Override
88 @NonNull
89 public Filter getFilter() {
90 return this.filter;
91 }
92}