15
15
package com .firebase .uidemo .database ;
16
16
17
17
import android .content .Context ;
18
- import android .graphics .PorterDuff ;
19
- import android .graphics .drawable .GradientDrawable ;
20
- import android .graphics .drawable .RotateDrawable ;
21
18
import android .os .Bundle ;
22
19
import android .support .annotation .NonNull ;
23
- import android .support .v4 .content .ContextCompat ;
24
20
import android .support .v7 .app .AppCompatActivity ;
25
21
import android .support .v7 .widget .LinearLayoutManager ;
26
22
import android .support .v7 .widget .RecyclerView ;
27
23
import android .util .Log ;
28
- import android .view .Gravity ;
29
24
import android .view .View ;
30
25
import android .widget .Button ;
31
26
import android .widget .EditText ;
32
- import android .widget .FrameLayout ;
33
- import android .widget .LinearLayout ;
34
- import android .widget .RelativeLayout ;
35
27
import android .widget .TextView ;
36
28
import android .widget .Toast ;
37
29
48
40
import com .google .firebase .database .FirebaseDatabase ;
49
41
import com .google .firebase .database .Query ;
50
42
51
- @ SuppressWarnings ("LogConditional" )
52
43
public class ChatActivity extends AppCompatActivity implements FirebaseAuth .AuthStateListener {
53
44
private static final String TAG = "RecyclerViewDemo" ;
54
45
@@ -60,8 +51,8 @@ public class ChatActivity extends AppCompatActivity implements FirebaseAuth.Auth
60
51
61
52
private RecyclerView mMessages ;
62
53
private LinearLayoutManager mManager ;
63
- private FirebaseRecyclerAdapter <Chat , ChatHolder > mRecyclerViewAdapter ;
64
- private View mEmptyListView ;
54
+ private FirebaseRecyclerAdapter <Chat , ChatHolder > mAdapter ;
55
+ private TextView mEmptyListMessage ;
65
56
66
57
@ Override
67
58
protected void onCreate (Bundle savedInstanceState ) {
@@ -73,8 +64,7 @@ protected void onCreate(Bundle savedInstanceState) {
73
64
74
65
mSendButton = (Button ) findViewById (R .id .sendButton );
75
66
mMessageEdit = (EditText ) findViewById (R .id .messageEdit );
76
-
77
- mEmptyListView = findViewById (R .id .emptyTextView );
67
+ mEmptyListMessage = (TextView ) findViewById (R .id .emptyTextView );
78
68
79
69
mRef = FirebaseDatabase .getInstance ().getReference ();
80
70
mChatRef = mRef .child ("chats" );
@@ -114,18 +104,18 @@ public void onStart() {
114
104
// Default Database rules do not allow unauthenticated reads, so we need to
115
105
// sign in before attaching the RecyclerView adapter otherwise the Adapter will
116
106
// not be able to read any data from the Database.
117
- if (!isSignedIn ()) {
118
- signInAnonymously ();
119
- } else {
107
+ if (isSignedIn ()) {
120
108
attachRecyclerViewAdapter ();
109
+ } else {
110
+ signInAnonymously ();
121
111
}
122
112
}
123
113
124
114
@ Override
125
115
public void onStop () {
126
116
super .onStop ();
127
- if (mRecyclerViewAdapter != null ) {
128
- mRecyclerViewAdapter .cleanup ();
117
+ if (mAdapter != null ) {
118
+ mAdapter .cleanup ();
129
119
}
130
120
}
131
121
@@ -144,38 +134,37 @@ public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
144
134
145
135
private void attachRecyclerViewAdapter () {
146
136
Query lastFifty = mChatRef .limitToLast (50 );
147
- mRecyclerViewAdapter = new FirebaseRecyclerAdapter <Chat , ChatHolder >(
137
+ mAdapter = new FirebaseRecyclerAdapter <Chat , ChatHolder >(
148
138
Chat .class , R .layout .message , ChatHolder .class , lastFifty ) {
149
139
@ Override
150
- public void populateViewHolder (ChatHolder chatView , Chat chat , int position ) {
151
- chatView .setName (chat .getName ());
152
- chatView .setText (chat .getMessage ());
140
+ public void populateViewHolder (ChatHolder holder , Chat chat , int position ) {
141
+ holder .setName (chat .getName ());
142
+ holder .setText (chat .getMessage ());
153
143
154
144
FirebaseUser currentUser = mAuth .getCurrentUser ();
155
145
if (currentUser != null && chat .getUid ().equals (currentUser .getUid ())) {
156
- chatView .setIsSender (true );
146
+ holder .setIsSender (true );
157
147
} else {
158
- chatView .setIsSender (false );
148
+ holder .setIsSender (false );
159
149
}
160
150
}
161
151
162
152
@ Override
163
153
protected void onDataChanged () {
164
- // if there are no chat messages, show a view that invites the user to add a message
165
- mEmptyListView .setVisibility (mRecyclerViewAdapter .getItemCount () == 0 ?
166
- View .VISIBLE : View .INVISIBLE );
154
+ // If there are no chat messages, show a view that invites the user to add a message.
155
+ mEmptyListMessage .setVisibility (mAdapter .getItemCount () == 0 ? View .VISIBLE : View .GONE );
167
156
}
168
157
};
169
158
170
159
// Scroll to bottom on new messages
171
- mRecyclerViewAdapter .registerAdapterDataObserver (new RecyclerView .AdapterDataObserver () {
160
+ mAdapter .registerAdapterDataObserver (new RecyclerView .AdapterDataObserver () {
172
161
@ Override
173
162
public void onItemRangeInserted (int positionStart , int itemCount ) {
174
- mManager .smoothScrollToPosition (mMessages , null , mRecyclerViewAdapter .getItemCount ());
163
+ mManager .smoothScrollToPosition (mMessages , null , mAdapter .getItemCount ());
175
164
}
176
165
});
177
166
178
- mMessages .setAdapter (mRecyclerViewAdapter );
167
+ mMessages .setAdapter (mAdapter );
179
168
}
180
169
181
170
private void signInAnonymously () {
@@ -190,108 +179,16 @@ public void onSuccess(AuthResult result) {
190
179
.addOnCompleteListener (new SignInResultNotifier (this ));
191
180
}
192
181
193
- public boolean isSignedIn () {
182
+ private boolean isSignedIn () {
194
183
return mAuth .getCurrentUser () != null ;
195
184
}
196
185
197
- public void updateUI () {
186
+ private void updateUI () {
198
187
// Sending only allowed when signed in
199
188
mSendButton .setEnabled (isSignedIn ());
200
189
mMessageEdit .setEnabled (isSignedIn ());
201
190
}
202
191
203
- public static class Chat {
204
- private String mName ;
205
- private String mMessage ;
206
- private String mUid ;
207
-
208
- public Chat () {
209
- // Needed for Firebase
210
- }
211
-
212
- public Chat (String name , String message , String uid ) {
213
- mName = name ;
214
- mMessage = message ;
215
- mUid = uid ;
216
- }
217
-
218
- public String getName () {
219
- return mName ;
220
- }
221
-
222
- public void setName (String name ) {
223
- mName = name ;
224
- }
225
-
226
- public String getMessage () {
227
- return mMessage ;
228
- }
229
-
230
- public void setMessage (String message ) {
231
- mMessage = message ;
232
- }
233
-
234
- public String getUid () {
235
- return mUid ;
236
- }
237
-
238
- public void setUid (String uid ) {
239
- mUid = uid ;
240
- }
241
- }
242
-
243
- public static class ChatHolder extends RecyclerView .ViewHolder {
244
- private final TextView mNameField ;
245
- private final TextView mTextField ;
246
- private final FrameLayout mLeftArrow ;
247
- private final FrameLayout mRightArrow ;
248
- private final RelativeLayout mMessageContainer ;
249
- private final LinearLayout mMessage ;
250
- private final int mGreen300 ;
251
- private final int mGray300 ;
252
-
253
- public ChatHolder (View itemView ) {
254
- super (itemView );
255
- mNameField = (TextView ) itemView .findViewById (R .id .name_text );
256
- mTextField = (TextView ) itemView .findViewById (R .id .message_text );
257
- mLeftArrow = (FrameLayout ) itemView .findViewById (R .id .left_arrow );
258
- mRightArrow = (FrameLayout ) itemView .findViewById (R .id .right_arrow );
259
- mMessageContainer = (RelativeLayout ) itemView .findViewById (R .id .message_container );
260
- mMessage = (LinearLayout ) itemView .findViewById (R .id .message );
261
- mGreen300 = ContextCompat .getColor (itemView .getContext (), R .color .material_green_300 );
262
- mGray300 = ContextCompat .getColor (itemView .getContext (), R .color .material_gray_300 );
263
- }
264
-
265
- public void setIsSender (boolean isSender ) {
266
- final int color ;
267
- if (isSender ) {
268
- color = mGreen300 ;
269
- mLeftArrow .setVisibility (View .GONE );
270
- mRightArrow .setVisibility (View .VISIBLE );
271
- mMessageContainer .setGravity (Gravity .END );
272
- } else {
273
- color = mGray300 ;
274
- mLeftArrow .setVisibility (View .VISIBLE );
275
- mRightArrow .setVisibility (View .GONE );
276
- mMessageContainer .setGravity (Gravity .START );
277
- }
278
-
279
- ((GradientDrawable ) mMessage .getBackground ()).setColor (color );
280
- ((RotateDrawable ) mLeftArrow .getBackground ()).getDrawable ()
281
- .setColorFilter (color , PorterDuff .Mode .SRC );
282
- ((RotateDrawable ) mRightArrow .getBackground ()).getDrawable ()
283
- .setColorFilter (color , PorterDuff .Mode .SRC );
284
- }
285
-
286
- public void setName (String name ) {
287
- mNameField .setText (name );
288
- }
289
-
290
- public void setText (String text ) {
291
- mTextField .setText (text );
292
- }
293
- }
294
-
295
192
/**
296
193
* Notifies the user of sign in successes or failures beyond the lifecycle of an activity.
297
194
*/
0 commit comments