Skip to content

Commit 0ed395e

Browse files
committed
Async data handling fixed
1 parent aaf127c commit 0ed395e

File tree

1 file changed

+25
-33
lines changed
  • portable/comp-android/diacomp/src/main/java/org/bosik/diacomp/android/frontend/views/fdpicker

1 file changed

+25
-33
lines changed

portable/comp-android/diacomp/src/main/java/org/bosik/diacomp/android/frontend/views/fdpicker/FoodDishPicker.java

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@
4747
import java.util.ArrayList;
4848
import java.util.List;
4949
import java.util.Locale;
50+
import java.util.stream.Collectors;
5051

5152
class ItemAdapter extends ArrayAdapter<Versioned<? extends Named>>
5253
{
5354
private final List<Versioned<? extends Named>> itemsAll;
54-
private final List<Versioned<? extends Named>> suggestions;
5555
private final int viewResourceId;
5656
private List<String> tokens;
5757

@@ -60,44 +60,41 @@ public ItemAdapter(Context context, int viewResourceId, List<Versioned<? extends
6060
super(context, viewResourceId, items);
6161

6262
this.itemsAll = new ArrayList<>(items);
63-
this.suggestions = new ArrayList<>();
6463
this.viewResourceId = viewResourceId;
6564
}
6665

6766
@Override
6867
public View getView(int position, View convertView, ViewGroup parent)
6968
{
7069
View v = convertView;
70+
7171
if (v == null)
7272
{
7373
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
7474
v = vi.inflate(viewResourceId, null);
7575
}
7676

77-
if (position < suggestions.size())
78-
{
79-
Versioned<? extends Named> item = suggestions.get(position);
77+
final Versioned<? extends Named> item = getItem(position);
8078

81-
TextView itemCaption = v.findViewById(R.id.itemDescription);
82-
itemCaption.setText(highlightOccurrences(
83-
item.getData().getName(),
84-
tokens,
85-
getContext().getResources().getColor(R.color.search_highlight, null)
86-
));
79+
TextView itemCaption = v.findViewById(R.id.itemDescription);
80+
itemCaption.setText(highlightOccurrences(
81+
item.getData().getName(),
82+
tokens,
83+
getContext().getResources().getColor(R.color.search_highlight, null)
84+
));
8785

88-
// FIXME: use separated resources (not button's)
89-
if (item.getData() instanceof FoodItem)
90-
{
91-
itemCaption.setCompoundDrawablesWithIntrinsicBounds(R.drawable.button_foodbase, 0, 0, 0);
92-
}
93-
else if (item.getData() instanceof DishItem)
94-
{
95-
itemCaption.setCompoundDrawablesWithIntrinsicBounds(R.drawable.button_dishbase, 0, 0, 0);
96-
}
97-
else
98-
{
99-
throw new IllegalArgumentException("Invalid item type: " + item.getClass().getName());
100-
}
86+
// FIXME: use separated resources (not button's)
87+
if (item.getData() instanceof FoodItem)
88+
{
89+
itemCaption.setCompoundDrawablesWithIntrinsicBounds(R.drawable.button_foodbase, 0, 0, 0);
90+
}
91+
else if (item.getData() instanceof DishItem)
92+
{
93+
itemCaption.setCompoundDrawablesWithIntrinsicBounds(R.drawable.button_dishbase, 0, 0, 0);
94+
}
95+
else
96+
{
97+
throw new IllegalArgumentException("Invalid item type: " + item.getClass().getName());
10198
}
10299

103100
return v;
@@ -146,17 +143,12 @@ protected FilterResults performFiltering(CharSequence constraint)
146143
if (constraint != null)
147144
{
148145
tokens = Utils.parseTokens(constraint.toString());
149-
suggestions.clear();
150146

151-
for (Versioned<? extends Named> item : itemsAll)
152-
{
153-
if (Utils.matchesTokens(item.getData().getName(), tokens))
154-
{
155-
suggestions.add(item);
156-
}
157-
}
147+
final List<Versioned<? extends Named>> suggestions = itemsAll.stream()
148+
.filter(e -> Utils.matchesTokens(e.getData().getName(), tokens))
149+
.collect(Collectors.toList());
158150

159-
FilterResults filterResults = new FilterResults();
151+
final FilterResults filterResults = new FilterResults();
160152
filterResults.values = suggestions;
161153
filterResults.count = suggestions.size();
162154

0 commit comments

Comments
 (0)