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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
package com.mapswithme.maps.search;
import android.graphics.Typeface;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.mapswithme.maps.R;
import com.mapswithme.util.UiUtils;
class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.BaseViewHolder>
{
private static final int TYPE_POPULATE_BUTTON = 0;
private static final int TYPE_SUGGEST = 1;
private static final int TYPE_RESULT = 2;
private SearchResult[] mResults;
private final SearchFragment mSearchFragment;
protected static abstract class BaseViewHolder extends RecyclerView.ViewHolder
{
SearchResult mResult;
// Position within search results
int mOrder;
BaseViewHolder(View view)
{
super(view);
}
void bind(@NonNull SearchResult result, int order)
{
mResult = result;
mOrder = order;
}
}
private class PopulateResultsViewHolder extends BaseViewHolder
{
PopulateResultsViewHolder(View view)
{
super(view);
view.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
mSearchFragment.showAllResultsOnMap();
}
});
}
}
private static abstract class BaseResultViewHolder extends BaseViewHolder
{
BaseResultViewHolder(View view)
{
super(view);
view.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
processClick(mResult, mOrder);
}
});
}
@Override
void bind(@NonNull SearchResult result, int order)
{
super.bind(result, order);
SpannableStringBuilder builder = new SpannableStringBuilder(result.name);
if (result.highlightRanges != null)
{
final int size = result.highlightRanges.length / 2;
int index = 0;
for (int i = 0; i < size; i++)
{
final int start = result.highlightRanges[index++];
final int len = result.highlightRanges[index++];
builder.setSpan(new StyleSpan(Typeface.BOLD), start, start + len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
getTitleView().setText(builder);
}
abstract TextView getTitleView();
abstract void processClick(SearchResult result, int order);
}
private class SuggestViewHolder extends BaseResultViewHolder
{
SuggestViewHolder(View view)
{
super(view);
}
@Override
TextView getTitleView()
{
return (TextView) itemView;
}
@Override
void processClick(SearchResult result, int order)
{
mSearchFragment.setSearchQuery(result.suggestion);
}
}
private class ResultViewHolder extends BaseResultViewHolder
{
final TextView mName;
final View mClosedMarker;
final TextView mDescription;
final TextView mRegion;
final TextView mDistance;
// FIXME: Better format based on result type
private CharSequence formatDescription(SearchResult result)
{
final SpannableStringBuilder res = new SpannableStringBuilder(result.description.featureType);
final SpannableStringBuilder tail = new SpannableStringBuilder();
final int stars = Math.min(result.description.stars, 5);
if (stars > 0)
{
// Colorize last dimmed stars
final SpannableStringBuilder sb = new SpannableStringBuilder("★ ★ ★ ★ ★");
if (stars < 5)
{
final int start = sb.length() - ((5 - stars) * 2 - 1);
sb.setSpan(new ForegroundColorSpan(itemView.getResources().getColor(R.color.search_star_dimmed)),
start, sb.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
tail.append(sb);
}
else if (!TextUtils.isEmpty(result.description.cuisine))
tail.append(result.description.cuisine);
if (!TextUtils.isEmpty(tail))
res.append(" • ")
.append(tail);
return res;
}
ResultViewHolder(View view)
{
super(view);
mName = (TextView) view.findViewById(R.id.title);
mClosedMarker = view.findViewById(R.id.closed);
mDescription = (TextView) view.findViewById(R.id.description);
mRegion = (TextView) view.findViewById(R.id.region);
mDistance = (TextView) view.findViewById(R.id.distance);
}
@Override
TextView getTitleView()
{
return mName;
}
@Override
void bind(@NonNull SearchResult result, int order)
{
super.bind(result, order);
UiUtils.showIf(result.description.closedNow, mClosedMarker);
UiUtils.setTextAndHideIfEmpty(mDescription, formatDescription(result));
UiUtils.setTextAndHideIfEmpty(mRegion, result.description.region);
UiUtils.setTextAndHideIfEmpty(mDistance, result.description.distance);
}
@Override
void processClick(SearchResult result, int order)
{
mSearchFragment.showSingleResultOnMap(order);
notifyDataSetChanged();
}
}
public SearchAdapter(SearchFragment fragment)
{
mSearchFragment = fragment;
}
@Override
public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
final LayoutInflater inflater = LayoutInflater.from(parent.getContext());
switch (viewType)
{
case TYPE_POPULATE_BUTTON:
return new PopulateResultsViewHolder(inflater.inflate(R.layout.item_search_populate, parent, false));
case TYPE_SUGGEST:
return new SuggestViewHolder(inflater.inflate(R.layout.item_search_suggest, parent, false));
case TYPE_RESULT:
return new ResultViewHolder(inflater.inflate(R.layout.item_search_result, parent, false));
default:
throw new IllegalArgumentException("Unhandled view type given");
}
}
@Override
public void onBindViewHolder(BaseViewHolder holder, int position)
{
if (showPopulateButton())
{
if (position == 0)
return;
position--;
}
holder.bind(mResults[position], position);
}
@Override
public int getItemViewType(int position)
{
if (showPopulateButton())
{
if (position == 0)
return TYPE_POPULATE_BUTTON;
position--;
}
switch (mResults[position].type)
{
case SearchResult.TYPE_SUGGEST:
return TYPE_SUGGEST;
case SearchResult.TYPE_RESULT:
return TYPE_RESULT;
default:
throw new IllegalArgumentException("Unhandled SearchResult type");
}
}
boolean showPopulateButton()
{
return (mResults != null && mResults.length > 0);
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public int getItemCount()
{
if (mResults == null)
return 0;
if (showPopulateButton())
return mResults.length + 1;
return mResults.length;
}
public void clear()
{
refreshData(null);
}
public void refreshData(SearchResult[] results)
{
mResults = results;
notifyDataSetChanged();
}
}
|