14
14
15
15
package com .firebase .uidemo .database ;
16
16
17
+ import android .content .Context ;
17
18
import android .graphics .PorterDuff ;
18
19
import android .graphics .drawable .GradientDrawable ;
19
20
import android .graphics .drawable .RotateDrawable ;
37
38
import com .firebase .ui .database .FirebaseRecyclerAdapter ;
38
39
import com .firebase .uidemo .R ;
39
40
import com .google .android .gms .tasks .OnCompleteListener ;
41
+ import com .google .android .gms .tasks .OnSuccessListener ;
40
42
import com .google .android .gms .tasks .Task ;
41
43
import com .google .firebase .auth .AuthResult ;
42
44
import com .google .firebase .auth .FirebaseAuth ;
@@ -86,9 +88,9 @@ public void onClick(View v) {
86
88
Chat chat = new Chat (name , mMessageEdit .getText ().toString (), uid );
87
89
mChatRef .push ().setValue (chat , new DatabaseReference .CompletionListener () {
88
90
@ Override
89
- public void onComplete (DatabaseError databaseError , DatabaseReference reference ) {
90
- if (databaseError != null ) {
91
- Log .e (TAG , "Failed to write message" , databaseError .toException ());
91
+ public void onComplete (DatabaseError error , DatabaseReference reference ) {
92
+ if (error != null ) {
93
+ Log .e (TAG , "Failed to write message" , error .toException ());
92
94
}
93
95
}
94
96
});
@@ -97,11 +99,10 @@ public void onComplete(DatabaseError databaseError, DatabaseReference reference)
97
99
}
98
100
});
99
101
100
- mMessages = (RecyclerView ) findViewById (R .id .messagesList );
101
-
102
102
mManager = new LinearLayoutManager (this );
103
103
mManager .setReverseLayout (false );
104
104
105
+ mMessages = (RecyclerView ) findViewById (R .id .messagesList );
105
106
mMessages .setHasFixedSize (false );
106
107
mMessages .setLayoutManager (mManager );
107
108
}
@@ -145,7 +146,6 @@ private void attachRecyclerViewAdapter() {
145
146
Query lastFifty = mChatRef .limitToLast (50 );
146
147
mRecyclerViewAdapter = new FirebaseRecyclerAdapter <Chat , ChatHolder >(
147
148
Chat .class , R .layout .message , ChatHolder .class , lastFifty ) {
148
-
149
149
@ Override
150
150
public void populateViewHolder (ChatHolder chatView , Chat chat , int position ) {
151
151
chatView .setName (chat .getName ());
@@ -180,20 +180,13 @@ public void onItemRangeInserted(int positionStart, int itemCount) {
180
180
private void signInAnonymously () {
181
181
Toast .makeText (this , "Signing in..." , Toast .LENGTH_SHORT ).show ();
182
182
mAuth .signInAnonymously ()
183
- .addOnCompleteListener (this , new OnCompleteListener <AuthResult >() {
183
+ .addOnSuccessListener (this , new OnSuccessListener <AuthResult >() {
184
184
@ Override
185
- public void onComplete (@ NonNull Task <AuthResult > task ) {
186
- Log .d (TAG , "signInAnonymously:onComplete:" + task .isSuccessful ());
187
- if (task .isSuccessful ()) {
188
- Toast .makeText (ChatActivity .this , "Signed In" ,
189
- Toast .LENGTH_SHORT ).show ();
190
- attachRecyclerViewAdapter ();
191
- } else {
192
- Toast .makeText (ChatActivity .this , "Sign In Failed" ,
193
- Toast .LENGTH_SHORT ).show ();
194
- }
185
+ public void onSuccess (AuthResult result ) {
186
+ attachRecyclerViewAdapter ();
195
187
}
196
- });
188
+ })
189
+ .addOnCompleteListener (new SignInResultNotifier (this ));
197
190
}
198
191
199
192
public boolean isSignedIn () {
@@ -297,4 +290,24 @@ public void setText(String text) {
297
290
mTextField .setText (text );
298
291
}
299
292
}
293
+
294
+ /**
295
+ * Notifies the user of sign in successes or failures beyond the lifecycle of an activity.
296
+ */
297
+ private static class SignInResultNotifier implements OnCompleteListener <AuthResult > {
298
+ private Context mContext ;
299
+
300
+ public SignInResultNotifier (Context context ) {
301
+ mContext = context .getApplicationContext ();
302
+ }
303
+
304
+ @ Override
305
+ public void onComplete (@ NonNull Task <AuthResult > task ) {
306
+ if (task .isSuccessful ()) {
307
+ Toast .makeText (mContext , R .string .signed_in , Toast .LENGTH_SHORT ).show ();
308
+ } else {
309
+ Toast .makeText (mContext , R .string .sign_in_failed , Toast .LENGTH_SHORT ).show ();
310
+ }
311
+ }
312
+ }
300
313
}
0 commit comments