forked from barros-felipe/Atlas-Android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAtlasAddressBar.java
542 lines (460 loc) · 22.5 KB
/
AtlasAddressBar.java
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
package com.layer.atlas;
import android.content.Context;
import android.content.res.Resources;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.layer.atlas.provider.Participant;
import com.layer.atlas.provider.ParticipantProvider;
import com.layer.atlas.util.views.EmptyDelEditText;
import com.layer.atlas.util.views.FlowLayout;
import com.layer.atlas.util.views.MaxHeightScrollView;
import com.layer.sdk.LayerClient;
import com.layer.sdk.messaging.Conversation;
import com.layer.sdk.query.CompoundPredicate;
import com.layer.sdk.query.Predicate;
import com.layer.sdk.query.Query;
import com.layer.sdk.query.RecyclerViewController;
import com.layer.sdk.query.SortDescriptor;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class AtlasAddressBar extends LinearLayout {
private LayerClient mLayerClient;
private ParticipantProvider mParticipantProvider;
private Picasso mPicasso;
private OnConversationClickListener mOnConversationClickListener;
private OnParticipantSelectionChangeListener mOnParticipantSelectionChangeListener;
private FlowLayout mSelectedParticipantLayout;
private EmptyDelEditText mFilter;
private RecyclerView mParticipantList;
private AvailableConversationAdapter mAvailableConversationAdapter;
private final Set<String> mSelectedParticipantIds = new LinkedHashSet<String>();
public AtlasAddressBar(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public AtlasAddressBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
LayoutInflater inflater = LayoutInflater.from(context);
inflater.inflate(R.layout.atlas_address_bar, this, true);
mSelectedParticipantLayout = (FlowLayout) findViewById(R.id.selected_participant_group);
mFilter = (EmptyDelEditText) findViewById(R.id.filter);
mSelectedParticipantLayout.setStretchChild(mFilter);
mParticipantList = (RecyclerView) findViewById(R.id.participant_list);
((MaxHeightScrollView) findViewById(R.id.selected_participant_scroll)).setMaximumHeight(getResources().getDimensionPixelSize(R.dimen.atlas_selected_participant_group_max_height));
setOrientation(VERTICAL);
}
public AtlasAddressBar init(LayerClient layerClient, ParticipantProvider participantProvider, Picasso picasso) {
mLayerClient = layerClient;
mParticipantProvider = participantProvider;
mPicasso = picasso;
RecyclerView.LayoutManager manager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
mParticipantList.setLayoutManager(manager);
mAvailableConversationAdapter = new AvailableConversationAdapter(mLayerClient, mParticipantProvider, mPicasso);
mParticipantList.setAdapter(mAvailableConversationAdapter);
// Hitting backspace with an empty search string deletes the last selected participant
mFilter.setOnEmptyDelListener(new EmptyDelEditText.OnEmptyDelListener() {
@Override
public boolean onEmptyDel(EmptyDelEditText editText) {
removeLastSelectedParticipant();
return true;
}
});
// Refresh available participants and conversations with every search string change
mFilter.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable e) {
refresh();
}
});
return this;
}
public AtlasAddressBar addTextChangedListener(TextWatcher textWatcher) {
mFilter.addTextChangedListener(textWatcher);
return this;
}
public AtlasAddressBar removeTextChangedListener(TextWatcher textWatcher) {
mFilter.removeTextChangedListener(textWatcher);
return this;
}
public AtlasAddressBar setOnEditorActionListener(TextView.OnEditorActionListener listener) {
mFilter.setOnEditorActionListener(listener);
return this;
}
public AtlasAddressBar setOnConversationClickListener(OnConversationClickListener onConversationClickListener) {
mOnConversationClickListener = onConversationClickListener;
return this;
}
public AtlasAddressBar setOnParticipantSelectionChangeListener(OnParticipantSelectionChangeListener onParticipantSelectionChangeListener) {
mOnParticipantSelectionChangeListener = onParticipantSelectionChangeListener;
return this;
}
public AtlasAddressBar setSuggestionsVisibility(int visibility) {
mParticipantList.setVisibility(visibility);
return this;
}
public Set<String> getSelectedParticipanIds() {
return new LinkedHashSet<String>(mSelectedParticipantIds);
}
public AtlasAddressBar refresh() {
if (mAvailableConversationAdapter == null) return this;
mAvailableConversationAdapter.refresh(getSearchFilter(), mSelectedParticipantIds);
return this;
}
private boolean selectParticipant(String participantId) {
if (mSelectedParticipantIds.contains(participantId)) return true;
if (mSelectedParticipantIds.size() >= 24) return false;
mSelectedParticipantIds.add(participantId);
ParticipantChip chip = new ParticipantChip(getContext(), mParticipantProvider, participantId, mPicasso);
mSelectedParticipantLayout.addView(chip, mSelectedParticipantLayout.getChildCount() - 1);
mFilter.setText(null);
refresh();
if (mOnParticipantSelectionChangeListener != null) {
mOnParticipantSelectionChangeListener.onParticipantSelectionChanged(this, new ArrayList<String>(mSelectedParticipantIds));
}
return true;
}
private void unselectParticipant(ParticipantChip chip) {
if (!mSelectedParticipantIds.contains(chip.mParticipantId)) return;
mSelectedParticipantIds.remove(chip.mParticipantId);
mSelectedParticipantLayout.removeView(chip);
refresh();
if (mOnParticipantSelectionChangeListener != null) {
mOnParticipantSelectionChangeListener.onParticipantSelectionChanged(this, new ArrayList<String>(mSelectedParticipantIds));
}
}
private String getSearchFilter() {
String s = mFilter.getText().toString();
return s.trim().isEmpty() ? null : s;
}
private String removeLastSelectedParticipant() {
ParticipantChip lastChip = null;
for (int i = 0; i < mSelectedParticipantLayout.getChildCount(); i++) {
View v = mSelectedParticipantLayout.getChildAt(i);
if (v instanceof ParticipantChip) lastChip = (ParticipantChip) v;
}
if (lastChip == null) return null;
unselectParticipant(lastChip);
return lastChip.mParticipantId;
}
/**
* Automatically refresh on resume
*/
@Override
protected void onVisibilityChanged(View changedView, int visibility) {
super.onVisibilityChanged(changedView, visibility);
if (visibility != View.VISIBLE) return;
refresh();
}
/**
* Save selected participants
*/
@Override
protected Parcelable onSaveInstanceState() {
Parcelable superState = super.onSaveInstanceState();
if (mSelectedParticipantIds.isEmpty()) return superState;
SavedState savedState = new SavedState(superState);
savedState.mSelectedParticipantIds = new ArrayList<String>(mSelectedParticipantIds);
return savedState;
}
/**
* Restore selected participants
*/
@Override
protected void onRestoreInstanceState(Parcelable state) {
if (!(state instanceof SavedState)) {
super.onRestoreInstanceState(state);
return;
}
SavedState savedState = (SavedState) state;
super.onRestoreInstanceState(savedState.getSuperState());
if (savedState.mSelectedParticipantIds != null) {
mSelectedParticipantIds.clear();
for (String participantId : savedState.mSelectedParticipantIds) {
selectParticipant(participantId);
}
}
}
private static class SavedState extends BaseSavedState {
List<String> mSelectedParticipantIds;
public SavedState(Parcelable superState) {
super(superState);
}
@Override
public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
dest.writeStringList(mSelectedParticipantIds);
}
public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<SavedState>() {
public SavedState createFromParcel(Parcel in) {
return new SavedState(in);
}
public SavedState[] newArray(int size) {
return new SavedState[size];
}
};
private SavedState(Parcel in) {
super(in);
mSelectedParticipantIds = in.createStringArrayList();
}
}
/**
* ParticipantChip implements the View used to populate the selected participant FlowLayout.
*/
private class ParticipantChip extends LinearLayout {
private String mParticipantId;
private AtlasAvatar mAvatar;
private TextView mName;
private ImageView mRemove;
public ParticipantChip(Context context, ParticipantProvider participantProvider, String participantId, Picasso picasso) {
super(context);
LayoutInflater inflater = LayoutInflater.from(context);
Resources r = getContext().getResources();
mParticipantId = participantId;
// Inflate and cache views
inflater.inflate(R.layout.atlas_participant_chip, this, true);
mAvatar = (AtlasAvatar) findViewById(R.id.avatar);
mName = (TextView) findViewById(R.id.name);
mRemove = (ImageView) findViewById(R.id.remove);
// Set layout
int height = r.getDimensionPixelSize(R.dimen.atlas_chip_height);
int margin = r.getDimensionPixelSize(R.dimen.atlas_chip_margin);
FlowLayout.LayoutParams p = new FlowLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, height);
p.setMargins(margin, margin, margin, margin);
setLayoutParams(p);
setOrientation(HORIZONTAL);
setBackgroundDrawable(r.getDrawable(R.drawable.atlas_participant_chip_background));
// Initialize participant data
Participant participant = participantProvider.getParticipant(participantId);
mName.setText(participant.getName());
mAvatar.init(participantProvider, picasso).setParticipants(participantId);
setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
unselectParticipant(ParticipantChip.this);
}
});
}
}
private enum Type {
PARTICIPANT,
CONVERSATION
}
/**
* AvailableConversationAdapter provides items for individual Participants and existing
* Conversations. Items are filtered by a participant filter string and by a set of selected
* Participants.
*/
private class AvailableConversationAdapter extends RecyclerView.Adapter<AvailableConversationAdapter.ViewHolder> implements RecyclerViewController.Callback {
protected final LayerClient mLayerClient;
protected final ParticipantProvider mParticipantProvider;
protected final Picasso mPicasso;
private final RecyclerViewController<Conversation> mQueryController;
private final ArrayList<String> mParticipantIds = new ArrayList<String>();
private final ArrayList<Participant> mParticipants = new ArrayList<Participant>();
private final Map<String, Participant> mParticipantMap = new HashMap<String, Participant>();
public AvailableConversationAdapter(LayerClient client, ParticipantProvider participantProvider, Picasso picasso) {
this(client, participantProvider, picasso, null);
}
public AvailableConversationAdapter(LayerClient client, ParticipantProvider participantProvider, Picasso picasso, Collection<String> updateAttributes) {
mQueryController = client.newRecyclerViewController(null, updateAttributes, this);
mLayerClient = client;
mParticipantProvider = participantProvider;
mPicasso = picasso;
setHasStableIds(false);
}
/**
* Refreshes this adapter by re-querying the ParticipantProvider and filtering Conversations
* to return only those Conversations with the given set of selected Participants.
*/
public void refresh(String filter, Set<String> selectedParticipantIds) {
// Apply text search filter to available participants
String userId = mLayerClient.getAuthenticatedUserId();
synchronized (mParticipantIds) {
mParticipantProvider.getMatchingParticipants(filter, mParticipantMap);
mParticipants.clear();
for (Map.Entry<String, Participant> entry : mParticipantMap.entrySet()) {
// Don't show participants we've already selected
if (selectedParticipantIds.contains(entry.getKey())) continue;
if (entry.getKey().equals(userId)) continue;
mParticipants.add(entry.getValue());
}
Collections.sort(mParticipants);
mParticipantIds.clear();
for (Participant p : mParticipants) {
mParticipantIds.add(p.getId());
}
}
// TODO: compute add/remove/move and notify those instead of complete notify
notifyDataSetChanged();
// Filter down to only those conversations including the selected participants, hiding one-on-one conversations
Query.Builder<Conversation> builder = Query.builder(Conversation.class)
.sortDescriptor(new SortDescriptor(Conversation.Property.LAST_MESSAGE_SENT_AT, SortDescriptor.Order.DESCENDING));
if (selectedParticipantIds.isEmpty()) {
builder.predicate(new Predicate(Conversation.Property.PARTICIPANT_COUNT, Predicate.Operator.GREATER_THAN, 2));
} else {
builder.predicate(new CompoundPredicate(CompoundPredicate.Type.AND,
new Predicate(Conversation.Property.PARTICIPANT_COUNT, Predicate.Operator.GREATER_THAN, 2),
new Predicate(Conversation.Property.PARTICIPANTS, Predicate.Operator.IN, selectedParticipantIds)));
}
mQueryController.setQuery(builder.build()).execute();
}
//==============================================================================================
// Adapter
//==============================================================================================
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
ViewHolder viewHolder = new ViewHolder(parent);
viewHolder.mAvatar.init(mParticipantProvider, mPicasso);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
synchronized (mParticipantIds) {
switch (getType(position)) {
case PARTICIPANT: {
position = adapterPositionToParticipantPosition(position);
String participantId = mParticipantIds.get(position);
Participant participant = mParticipants.get(position);
viewHolder.mTitle.setText(participant.getName());
viewHolder.itemView.setTag(participantId);
viewHolder.itemView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
selectParticipant((String) v.getTag());
}
});
viewHolder.mAvatar.setParticipants(participantId);
}
break;
case CONVERSATION: {
position = adapterPositionToConversationPosition(position);
Conversation conversation = mQueryController.getItem(position);
String userId = mLayerClient.getAuthenticatedUserId();
List<String> names = new ArrayList<String>();
Set<String> ids = new LinkedHashSet<String>();
for (String participantId : conversation.getParticipants()) {
if (participantId.equals(userId)) continue;
ids.add(participantId);
Participant p = mParticipantProvider.getParticipant(participantId);
if (p == null) continue;
names.add(p.getName());
}
viewHolder.mTitle.setText(TextUtils.join(", ", names));
viewHolder.itemView.setTag(conversation);
viewHolder.itemView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mOnConversationClickListener == null) return;
mOnConversationClickListener.onConversationClick(AtlasAddressBar.this, (Conversation) v.getTag());
}
});
viewHolder.mAvatar.setParticipants(ids);
}
break;
}
}
}
// first are participants; then are conversations
Type getType(int position) {
synchronized (mParticipantIds) {
return (position < mParticipantIds.size()) ? Type.PARTICIPANT : Type.CONVERSATION;
}
}
int adapterPositionToParticipantPosition(int position) {
return position;
}
int adapterPositionToConversationPosition(int position) {
synchronized (mParticipantIds) {
return position - mParticipantIds.size();
}
}
int conversationPositionToAdapterPosition(int position) {
synchronized (mParticipantIds) {
return position + mParticipantIds.size();
}
}
@Override
public int getItemCount() {
synchronized (mParticipantIds) {
return mQueryController.getItemCount() + mParticipantIds.size();
}
}
//==============================================================================================
// Conversation UI update callbacks
//==============================================================================================
@Override
public void onQueryDataSetChanged(RecyclerViewController controller) {
notifyDataSetChanged();
}
@Override
public void onQueryItemChanged(RecyclerViewController controller, int position) {
notifyItemChanged(conversationPositionToAdapterPosition(position));
}
@Override
public void onQueryItemRangeChanged(RecyclerViewController controller, int positionStart, int itemCount) {
notifyItemRangeChanged(conversationPositionToAdapterPosition(positionStart), itemCount);
}
@Override
public void onQueryItemInserted(RecyclerViewController controller, int position) {
notifyItemInserted(conversationPositionToAdapterPosition(position));
}
@Override
public void onQueryItemRangeInserted(RecyclerViewController controller, int positionStart, int itemCount) {
notifyItemRangeInserted(conversationPositionToAdapterPosition(positionStart), itemCount);
}
@Override
public void onQueryItemRemoved(RecyclerViewController controller, int position) {
notifyItemRemoved(conversationPositionToAdapterPosition(position));
}
@Override
public void onQueryItemRangeRemoved(RecyclerViewController controller, int positionStart, int itemCount) {
notifyItemRangeRemoved(conversationPositionToAdapterPosition(positionStart), itemCount);
}
@Override
public void onQueryItemMoved(RecyclerViewController controller, int fromPosition, int toPosition) {
notifyItemMoved(conversationPositionToAdapterPosition(fromPosition), conversationPositionToAdapterPosition(toPosition));
}
//==============================================================================================
// Inner classes
//==============================================================================================
protected class ViewHolder extends RecyclerView.ViewHolder {
private AtlasAvatar mAvatar;
private TextView mTitle;
public ViewHolder(ViewGroup parent) {
super(LayoutInflater.from(parent.getContext()).inflate(R.layout.atlas_address_bar_item, parent, false));
mAvatar = (AtlasAvatar) itemView.findViewById(R.id.avatar);
mTitle = (TextView) itemView.findViewById(R.id.title);
}
}
}
public interface OnConversationClickListener {
void onConversationClick(AtlasAddressBar conversationLauncher, Conversation conversation);
}
public interface OnParticipantSelectionChangeListener {
void onParticipantSelectionChanged(AtlasAddressBar conversationLauncher, List<String> participantIds);
}
}