forked from barros-felipe/Atlas-Android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAtlasMessagesRecyclerView.java
184 lines (163 loc) · 6.42 KB
/
AtlasMessagesRecyclerView.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
/*
* Copyright (c) 2015 Layer. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.layer.atlas;
import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.AttributeSet;
import android.view.View;
import com.layer.atlas.adapters.AtlasMessagesAdapter;
import com.layer.atlas.messagetypes.AtlasCellFactory;
import com.layer.atlas.provider.ParticipantProvider;
import com.layer.atlas.util.itemanimators.NoChangeAnimator;
import com.layer.atlas.util.views.SwipeableItem;
import com.layer.sdk.LayerClient;
import com.layer.sdk.messaging.Conversation;
import com.layer.sdk.messaging.Message;
import com.layer.sdk.query.Predicate;
import com.layer.sdk.query.Query;
import com.layer.sdk.query.SortDescriptor;
import com.squareup.picasso.Picasso;
public class AtlasMessagesRecyclerView extends RecyclerView {
private AtlasMessagesAdapter mAdapter;
private LinearLayoutManager mLayoutManager;
private ItemTouchHelper mSwipeItemTouchHelper;
public AtlasMessagesRecyclerView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public AtlasMessagesRecyclerView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public AtlasMessagesRecyclerView(Context context) {
super(context);
}
public AtlasMessagesRecyclerView init(LayerClient layerClient, ParticipantProvider participantProvider, Picasso picasso) {
mLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
mLayoutManager.setStackFromEnd(true);
setLayoutManager(mLayoutManager);
// Create an adapter that auto-scrolls if we're already at the bottom
mAdapter = new AtlasMessagesAdapter(getContext(), layerClient, participantProvider, picasso)
.setRecyclerView(this)
.setOnMessageAppendListener(new AtlasMessagesAdapter.OnMessageAppendListener() {
@Override
public void onMessageAppend(AtlasMessagesAdapter adapter, Message message) {
autoScroll();
}
});
super.setAdapter(mAdapter);
// Don't flash items when changing content
setItemAnimator(new NoChangeAnimator());
addOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
for (AtlasCellFactory factory : mAdapter.getCellFactories()) {
factory.onScrollStateChanged(newState);
}
}
});
return this;
}
@Override
public void setAdapter(Adapter adapter) {
throw new RuntimeException("AtlasMessageList sets its own Adapter");
}
/**
* Automatically refresh on resume
*/
@Override
protected void onVisibilityChanged(View changedView, int visibility) {
super.onVisibilityChanged(changedView, visibility);
if (visibility != View.VISIBLE) return;
refresh();
}
public AtlasMessagesRecyclerView refresh() {
if (mAdapter != null) mAdapter.refresh();
return this;
}
/**
* Updates the underlying AtlasMessagesAdapter with a Query for Messages in the given
* Conversation.
*
* @param conversation Conversation to display Messages for.
* @return This AtlasMessageList.
*/
public AtlasMessagesRecyclerView setConversation(Conversation conversation) {
mAdapter.setQuery(Query.builder(Message.class)
.predicate(new Predicate(Message.Property.CONVERSATION, Predicate.Operator.EQUAL_TO, conversation))
.sortDescriptor(new SortDescriptor(Message.Property.POSITION, SortDescriptor.Order.ASCENDING))
.build()).refresh();
return this;
}
public AtlasMessagesRecyclerView setOnMessageSwipeListener(SwipeableItem.OnSwipeListener<Message> listener) {
if (mSwipeItemTouchHelper != null) {
mSwipeItemTouchHelper.attachToRecyclerView(null);
}
if (listener == null) {
mSwipeItemTouchHelper = null;
} else {
listener.setBaseAdapter((AtlasMessagesAdapter) getAdapter());
mSwipeItemTouchHelper = new ItemTouchHelper(listener);
mSwipeItemTouchHelper.attachToRecyclerView(this);
}
return this;
}
/**
* Convenience pass-through to this list's AtlasMessagesAdapter.
*
* @see AtlasMessagesAdapter#addCellFactories(AtlasCellFactory...)
*/
public AtlasMessagesRecyclerView addCellFactories(AtlasCellFactory... cellFactories) {
mAdapter.addCellFactories(cellFactories);
return this;
}
/**
* Convenience pass-through to this list's LinearLayoutManager.
*
* @see LinearLayoutManager#findLastVisibleItemPosition()
*/
private int findLastVisibleItemPosition() {
return mLayoutManager.findLastVisibleItemPosition();
}
/**
* Convenience pass-through to this list's AtlasMessagesAdapter.
*
* @see AtlasMessagesAdapter#setFooterView(View)
*/
public AtlasMessagesRecyclerView setFooterView(View footerView) {
mAdapter.setFooterView(footerView);
autoScroll();
return this;
}
/**
* Convenience pass-through to this list's AtlasMessagesAdapter.
*
* @see AtlasMessagesAdapter#getFooterView()
*/
public View getFooterView() {
return mAdapter.getFooterView();
}
/**
* Scrolls if the user is at the end
*/
private void autoScroll() {
int end = mAdapter.getItemCount() - 1;
if (end <= 0) return;
int visible = findLastVisibleItemPosition();
// -3 because -1 seems too finicky
if (visible >= (end - 3)) scrollToPosition(end);
}
}