26
26
import android .widget .TextView ;
27
27
import android .widget .Toast ;
28
28
29
- import com .firebase .ui .database .FirebaseIndexRecyclerAdapter ;
30
29
import com .firebase .ui .database .FirebaseRecyclerAdapter ;
31
30
import com .firebase .uidemo .R ;
32
31
import com .firebase .uidemo .util .SignInResultNotifier ;
33
32
import com .google .android .gms .tasks .OnSuccessListener ;
34
33
import com .google .firebase .auth .AuthResult ;
35
34
import com .google .firebase .auth .FirebaseAuth ;
36
- import com .google .firebase .database .DataSnapshot ;
37
35
import com .google .firebase .database .DatabaseError ;
38
36
import com .google .firebase .database .DatabaseReference ;
39
37
import com .google .firebase .database .FirebaseDatabase ;
38
+ import com .google .firebase .database .Query ;
40
39
41
- public class ChatActivity extends AppCompatActivity implements FirebaseAuth .AuthStateListener {
40
+ public class ChatActivity extends AppCompatActivity implements FirebaseAuth .AuthStateListener , View . OnClickListener {
42
41
private static final String TAG = "RecyclerViewDemo" ;
43
42
44
43
private FirebaseAuth mAuth ;
45
- private DatabaseReference mChatIndicesRef ;
46
- private DatabaseReference mChatRef ;
44
+ protected DatabaseReference mChatRef ;
47
45
private Button mSendButton ;
48
- private EditText mMessageEdit ;
46
+ protected EditText mMessageEdit ;
49
47
50
48
private RecyclerView mMessages ;
51
49
private LinearLayoutManager mManager ;
52
- private FirebaseRecyclerAdapter <Chat , ChatHolder > mAdapter ;
53
- private TextView mEmptyListMessage ;
50
+ protected FirebaseRecyclerAdapter <Chat , ChatHolder > mAdapter ;
51
+ protected TextView mEmptyListMessage ;
54
52
55
53
@ Override
56
54
protected void onCreate (Bundle savedInstanceState ) {
@@ -64,31 +62,9 @@ protected void onCreate(Bundle savedInstanceState) {
64
62
mMessageEdit = (EditText ) findViewById (R .id .messageEdit );
65
63
mEmptyListMessage = (TextView ) findViewById (R .id .emptyTextView );
66
64
67
- DatabaseReference ref = FirebaseDatabase .getInstance ().getReference ();
68
- mChatIndicesRef = ref .child ("chatIndices" );
69
- mChatRef = ref .child ("chats" );
65
+ mChatRef = FirebaseDatabase .getInstance ().getReference ().child ("chats" );
70
66
71
- mSendButton .setOnClickListener (new View .OnClickListener () {
72
- @ Override
73
- public void onClick (View v ) {
74
- String uid = mAuth .getCurrentUser ().getUid ();
75
- String name = "User " + uid .substring (0 , 6 );
76
-
77
- Chat chat = new Chat (name , mMessageEdit .getText ().toString (), uid );
78
- DatabaseReference chatRef = mChatRef .push ();
79
- mChatIndicesRef .child (chatRef .getKey ()).setValue (true );
80
- chatRef .setValue (chat , new DatabaseReference .CompletionListener () {
81
- @ Override
82
- public void onComplete (DatabaseError error , DatabaseReference reference ) {
83
- if (error != null ) {
84
- Log .e (TAG , "Failed to write message" , error .toException ());
85
- }
86
- }
87
- });
88
-
89
- mMessageEdit .setText ("" );
90
- }
91
- });
67
+ mSendButton .setOnClickListener (this );
92
68
93
69
mManager = new LinearLayoutManager (this );
94
70
mManager .setReverseLayout (false );
@@ -128,50 +104,61 @@ public void onDestroy() {
128
104
}
129
105
}
130
106
107
+ @ Override
108
+ public void onClick (View v ) {
109
+ String uid = mAuth .getCurrentUser ().getUid ();
110
+ String name = "User " + uid .substring (0 , 6 );
111
+
112
+ Chat chat = new Chat (name , mMessageEdit .getText ().toString (), uid );
113
+ mChatRef .push ().setValue (chat , new DatabaseReference .CompletionListener () {
114
+ @ Override
115
+ public void onComplete (DatabaseError error , DatabaseReference reference ) {
116
+ if (error != null ) {
117
+ Log .e (TAG , "Failed to write message" , error .toException ());
118
+ }
119
+ }
120
+ });
121
+
122
+ mMessageEdit .setText ("" );
123
+ }
124
+
131
125
@ Override
132
126
public void onAuthStateChanged (@ NonNull FirebaseAuth firebaseAuth ) {
133
127
updateUI ();
134
128
}
135
129
136
130
private void attachRecyclerViewAdapter () {
137
- mAdapter = new FirebaseIndexRecyclerAdapter <Chat , ChatHolder >(
131
+ mAdapter = getAdapter ();
132
+
133
+ // Scroll to bottom on new messages
134
+ mAdapter .registerAdapterDataObserver (new RecyclerView .AdapterDataObserver () {
135
+ @ Override
136
+ public void onItemRangeInserted (int positionStart , int itemCount ) {
137
+ mManager .smoothScrollToPosition (mMessages , null , mAdapter .getItemCount ());
138
+ }
139
+ });
140
+
141
+ mMessages .setAdapter (mAdapter );
142
+ }
143
+
144
+ protected FirebaseRecyclerAdapter <Chat , ChatHolder > getAdapter () {
145
+ Query lastFifty = mChatRef .limitToLast (50 );
146
+ return new FirebaseRecyclerAdapter <Chat , ChatHolder >(
138
147
Chat .class ,
139
148
R .layout .message ,
140
149
ChatHolder .class ,
141
- mChatIndicesRef .limitToLast (50 ),
142
- mChatRef ) {
150
+ lastFifty ) {
143
151
@ Override
144
152
public void populateViewHolder (ChatHolder holder , Chat chat , int position ) {
145
153
holder .bind (chat );
146
154
}
147
155
148
- @ Override
149
- public void onChildChanged (EventType type ,
150
- DataSnapshot snapshot ,
151
- int index ,
152
- int oldIndex ) {
153
- super .onChildChanged (type , snapshot , index , oldIndex );
154
-
155
- // TODO temporary fix for https://github.com/firebase/FirebaseUI-Android/issues/546
156
- onDataChanged ();
157
- }
158
-
159
156
@ Override
160
157
public void onDataChanged () {
161
158
// If there are no chat messages, show a view that invites the user to add a message.
162
159
mEmptyListMessage .setVisibility (getItemCount () == 0 ? View .VISIBLE : View .GONE );
163
160
}
164
161
};
165
-
166
- // Scroll to bottom on new messages
167
- mAdapter .registerAdapterDataObserver (new RecyclerView .AdapterDataObserver () {
168
- @ Override
169
- public void onItemRangeInserted (int positionStart , int itemCount ) {
170
- mManager .smoothScrollToPosition (mMessages , null , mAdapter .getItemCount ());
171
- }
172
- });
173
-
174
- mMessages .setAdapter (mAdapter );
175
162
}
176
163
177
164
private void signInAnonymously () {
0 commit comments