forked from barros-felipe/Atlas-Android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAtlasConversationsRecyclerView.java
More file actions
184 lines (159 loc) · 9.3 KB
/
AtlasConversationsRecyclerView.java
File metadata and controls
184 lines (159 loc) · 9.3 KB
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.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Typeface;
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.AtlasConversationsAdapter;
import com.layer.atlas.util.AvatarStyle;
import com.layer.atlas.util.ConversationStyle;
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.squareup.picasso.Picasso;
public class AtlasConversationsRecyclerView extends RecyclerView {
AtlasConversationsAdapter mAdapter;
private ItemTouchHelper mSwipeItemTouchHelper;
private ConversationStyle conversationStyle;
public AtlasConversationsRecyclerView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
parseStyle(context, attrs, defStyle);
}
public AtlasConversationsRecyclerView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public AtlasConversationsRecyclerView(Context context) {
super(context);
}
public AtlasConversationsRecyclerView init(LayerClient layerClient, Picasso picasso) {
// Linear layout manager
LinearLayoutManager manager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
manager.setStackFromEnd(false);
setLayoutManager(manager);
// Don't flash items when changing content
setItemAnimator(new NoChangeAnimator());
mAdapter = new AtlasConversationsAdapter(getContext(), layerClient, picasso);
mAdapter.setStyle(conversationStyle);
super.setAdapter(mAdapter);
refresh();
return this;
}
@Override
public void setAdapter(Adapter adapter) {
throw new RuntimeException("AtlasConversationsRecyclerView sets its own Adapter");
}
/**
* Performs cleanup when the Activity/Fragment using the adapter is destroyed.
*/
public void onDestroy() {
if (mAdapter != null) {
mAdapter.onDestroy();
}
}
/**
* Automatically refresh on resume
*/
@Override
protected void onVisibilityChanged(View changedView, int visibility) {
super.onVisibilityChanged(changedView, visibility);
if (visibility != View.VISIBLE) return;
refresh();
}
public AtlasConversationsRecyclerView refresh() {
if (mAdapter != null) mAdapter.refresh();
return this;
}
/**
* Convenience pass-through to this list's AtlasConversationsAdapter.
*
* @see AtlasConversationsAdapter#setOnConversationClickListener(AtlasConversationsAdapter.OnConversationClickListener)
*/
public AtlasConversationsRecyclerView setOnConversationClickListener(AtlasConversationsAdapter.OnConversationClickListener listener) {
mAdapter.setOnConversationClickListener(listener);
return this;
}
public AtlasConversationsRecyclerView setOnConversationSwipeListener(SwipeableItem.OnSwipeListener<Conversation> listener) {
if (mSwipeItemTouchHelper != null) {
mSwipeItemTouchHelper.attachToRecyclerView(null);
}
if (listener == null) {
mSwipeItemTouchHelper = null;
} else {
listener.setBaseAdapter((AtlasConversationsAdapter) getAdapter());
mSwipeItemTouchHelper = new ItemTouchHelper(listener);
mSwipeItemTouchHelper.attachToRecyclerView(this);
}
return this;
}
/**
* Convenience pass-through to this list's AtlasConversationsAdapter.
*
* @see AtlasConversationsAdapter#setInitialHistoricMessagesToFetch(long)
*/
public AtlasConversationsRecyclerView setInitialHistoricMessagesToFetch(long count) {
mAdapter.setInitialHistoricMessagesToFetch(count);
return this;
}
public AtlasConversationsRecyclerView setTypeface(Typeface titleTypeface, Typeface titleUnreadTypeface, Typeface subtitleTypeface, Typeface subtitleUnreadTypeface, Typeface dateTypeface) {
conversationStyle.setTitleTextTypeface(titleTypeface);
conversationStyle.setTitleUnreadTextTypeface(titleUnreadTypeface);
conversationStyle.setSubtitleTextTypeface(subtitleTypeface);
conversationStyle.setSubtitleUnreadTextTypeface(subtitleUnreadTypeface);
conversationStyle.setDateTextTypeface(dateTypeface);
return this;
}
private void parseStyle(Context context, AttributeSet attrs, int defStyle) {
ConversationStyle.Builder styleBuilder = new ConversationStyle.Builder();
TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.AtlasConversationsRecyclerView, R.attr.AtlasConversationsRecyclerView, defStyle);
styleBuilder.titleTextColor(ta.getColor(R.styleable.AtlasConversationsRecyclerView_cellTitleTextColor, context.getResources().getColor(R.color.atlas_text_gray)));
int titleTextStyle = ta.getInt(R.styleable.AtlasConversationsRecyclerView_cellTitleTextStyle, Typeface.NORMAL);
styleBuilder.titleTextStyle(titleTextStyle);
String titleTextTypefaceName = ta.getString(R.styleable.AtlasConversationsRecyclerView_cellTitleTextTypeface);
styleBuilder.titleTextTypeface(titleTextTypefaceName != null ? Typeface.create(titleTextTypefaceName, titleTextStyle) : null);
styleBuilder.titleUnreadTextColor(ta.getColor(R.styleable.AtlasConversationsRecyclerView_cellTitleUnreadTextColor, context.getResources().getColor(R.color.atlas_text_black)));
int titleUnreadTextStyle = ta.getInt(R.styleable.AtlasConversationsRecyclerView_cellTitleUnreadTextStyle, Typeface.BOLD);
styleBuilder.titleUnreadTextStyle(titleUnreadTextStyle);
String titleUnreadTextTypefaceName = ta.getString(R.styleable.AtlasConversationsRecyclerView_cellTitleUnreadTextTypeface);
styleBuilder.titleUnreadTextTypeface(titleUnreadTextTypefaceName != null ? Typeface.create(titleUnreadTextTypefaceName, titleUnreadTextStyle) : null);
styleBuilder.subtitleTextColor(ta.getColor(R.styleable.AtlasConversationsRecyclerView_cellSubtitleTextColor, context.getResources().getColor(R.color.atlas_text_gray)));
int subtitleTextStyle = ta.getInt(R.styleable.AtlasConversationsRecyclerView_cellSubtitleTextStyle, Typeface.NORMAL);
styleBuilder.subtitleTextStyle(subtitleTextStyle);
String subtitleTextTypefaceName = ta.getString(R.styleable.AtlasConversationsRecyclerView_cellSubtitleTextTypeface);
styleBuilder.subtitleTextTypeface(subtitleTextTypefaceName != null ? Typeface.create(subtitleTextTypefaceName, subtitleTextStyle) : null);
styleBuilder.subtitleUnreadTextColor(ta.getColor(R.styleable.AtlasConversationsRecyclerView_cellSubtitleUnreadTextColor, context.getResources().getColor(R.color.atlas_text_black)));
int subtitleUnreadTextStyle = ta.getInt(R.styleable.AtlasConversationsRecyclerView_cellSubtitleUnreadTextStyle, Typeface.NORMAL);
styleBuilder.subtitleUnreadTextStyle(subtitleUnreadTextStyle);
String subtitleUnreadTextTypefaceName = ta.getString(R.styleable.AtlasConversationsRecyclerView_cellSubtitleUnreadTextTypeface);
styleBuilder.subtitleUnreadTextTypeface(subtitleUnreadTextTypefaceName != null ? Typeface.create(subtitleUnreadTextTypefaceName, subtitleUnreadTextStyle) : null);
styleBuilder.cellBackgroundColor(ta.getColor(R.styleable.AtlasConversationsRecyclerView_cellBackgroundColor, Color.TRANSPARENT));
styleBuilder.cellUnreadBackgroundColor(ta.getColor(R.styleable.AtlasConversationsRecyclerView_cellUnreadBackgroundColor, Color.TRANSPARENT));
styleBuilder.dateTextColor(ta.getColor(R.styleable.AtlasConversationsRecyclerView_dateTextColor, context.getResources().getColor(R.color.atlas_color_primary_blue)));
AvatarStyle.Builder avatarStyleBuilder = new AvatarStyle.Builder();
avatarStyleBuilder.avatarTextColor(ta.getColor(R.styleable.AtlasConversationsRecyclerView_avatarTextColor, context.getResources().getColor(R.color.atlas_avatar_text)));
avatarStyleBuilder.avatarBackgroundColor(ta.getColor(R.styleable.AtlasConversationsRecyclerView_avatarBackgroundColor, context.getResources().getColor(R.color.atlas_avatar_background)));
avatarStyleBuilder.avatarBorderColor(ta.getColor(R.styleable.AtlasConversationsRecyclerView_avatarBorderColor, context.getResources().getColor(R.color.atlas_avatar_border)));
styleBuilder.avatarStyle(avatarStyleBuilder.build());
ta.recycle();
conversationStyle = styleBuilder.build();
}
}