Skip to content

Commit 5f9b9bd

Browse files
committed
Add getAuth() to base activity, re-order methods
Fixes #39 Fixes #40 Also fixed a naming issue and cleanup in the sample app.
1 parent ef78e2f commit 5f9b9bd

File tree

2 files changed

+41
-29
lines changed

2 files changed

+41
-29
lines changed

app/src/main/java/com/firebase/uidemo/RecyclerViewDemoActivity.java

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ public class RecyclerViewDemoActivity extends FirebaseLoginBaseActivity {
3030
public static String TAG = "FirebaseUI.chat";
3131
private Firebase mRef;
3232
private Query mChatRef;
33-
private AuthData mAuthData;
34-
private String name;
35-
private String uid;
33+
private String mName;
3634
private Button mSendButton;
3735
private EditText mMessageEdit;
3836

@@ -60,7 +58,7 @@ public void onClick(View view) {
6058
mSendButton.setOnClickListener(new View.OnClickListener() {
6159
@Override
6260
public void onClick(View v) {
63-
Chat chat = new Chat(name, mAuthData.getUid(), mMessageEdit.getText().toString());
61+
Chat chat = new Chat(mName, getAuth().getUid(), mMessageEdit.getText().toString());
6462
mRef.push().setValue(chat, new Firebase.CompletionListener() {
6563
@Override
6664
public void onComplete(FirebaseError firebaseError, Firebase firebase) {
@@ -83,11 +81,11 @@ public void onComplete(FirebaseError firebaseError, Firebase firebase) {
8381

8482
mRecycleViewAdapter = new FirebaseRecyclerAdapter<Chat, ChatHolder>(Chat.class, R.layout.message, ChatHolder.class, mChatRef) {
8583
@Override
86-
public void populateViewHolder(ChatHolder chatView, Chat chat) {
84+
public void populateViewHolder(ChatHolder chatView, Chat chat, int position) {
8785
chatView.setName(chat.getName());
8886
chatView.setText(chat.getText());
8987

90-
if (mAuthData != null && chat.getUid().equals(mAuthData.getUid())) {
88+
if (getAuth() != null && chat.getUid().equals(getAuth().getUid())) {
9189
chatView.setIsSender(true);
9290
} else {
9391
chatView.setIsSender(false);
@@ -101,9 +99,9 @@ public void populateViewHolder(ChatHolder chatView, Chat chat) {
10199
@Override
102100
protected void onStart() {
103101
super.onStart();
104-
// setEnabledAuthProvider(SocialProvider.facebook);
105-
// setEnabledAuthProvider(SocialProvider.twitter);
106-
// setEnabledAuthProvider(SocialProvider.google);
102+
setEnabledAuthProvider(SocialProvider.facebook);
103+
setEnabledAuthProvider(SocialProvider.twitter);
104+
setEnabledAuthProvider(SocialProvider.google);
107105
setEnabledAuthProvider(SocialProvider.password);
108106
}
109107

@@ -116,10 +114,10 @@ public boolean onCreateOptionsMenu(Menu menu) {
116114

117115
@Override
118116
public boolean onPrepareOptionsMenu(Menu menu) {
119-
menu.findItem(R.id.login_menu_item).setVisible(mAuthData == null);
120-
menu.findItem(R.id.logout_menu_item).setVisible(mAuthData != null);
121-
mSendButton.setEnabled(mAuthData != null);
122-
mMessageEdit.setEnabled(mAuthData != null);
117+
menu.findItem(R.id.login_menu_item).setVisible(getAuth() == null);
118+
menu.findItem(R.id.logout_menu_item).setVisible(getAuth() != null);
119+
mSendButton.setEnabled(getAuth() != null);
120+
mMessageEdit.setEnabled(getAuth() != null);
123121

124122
return true;
125123
}
@@ -140,14 +138,13 @@ public boolean onOptionsItemSelected(MenuItem item) {
140138
@Override
141139
public void onFirebaseLoggedIn(AuthData authData) {
142140
Log.i(TAG, "Logged in to " + authData.getProvider().toString());
143-
mAuthData = authData;
144141

145-
switch (mAuthData.getProvider()) {
142+
switch (authData.getProvider()) {
146143
case "password":
147-
name = (String) mAuthData.getProviderData().get("email");
144+
mName = (String) authData.getProviderData().get("email");
148145
break;
149146
default:
150-
name = (String) mAuthData.getProviderData().get("displayName");
147+
mName = (String) authData.getProviderData().get("displayName");
151148
break;
152149
}
153150

@@ -158,8 +155,7 @@ public void onFirebaseLoggedIn(AuthData authData) {
158155
@Override
159156
public void onFirebaseLoggedOut() {
160157
Log.i(TAG, "Logged out");
161-
mAuthData = null;
162-
name = "";
158+
mName = "";
163159
invalidateOptionsMenu();
164160
mRecycleViewAdapter.notifyDataSetChanged();
165161
}

library/src/main/java/com/firebase/ui/auth/core/FirebaseLoginBaseActivity.java

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,44 @@ public abstract class FirebaseLoginBaseActivity extends AppCompatActivity {
1212
private final String TAG = "FirebaseLoginBaseAct";
1313

1414
private Firebase.AuthStateListener mAuthStateListener;
15+
private AuthData mAuthData;
1516
private FirebaseLoginDialog mDialog;
1617
private TokenAuthHandler mHandler;
1718

19+
20+
/**
21+
* Subclasses of this activity must implement this method and return a valid Firebase reference that
22+
* can be used to call authentication related methods on. The method is guaranteed *not* to be called
23+
* before onCreate() has finished.
24+
*
25+
* @return a Firebase reference that can be used to call authentication related methods on
26+
*/
27+
protected abstract Firebase getFirebaseRef();
28+
29+
/**
30+
* Returns the data for the currently authenticated users, or null if no user is authenticated.
31+
*
32+
* @return the data for the currently authenticated users, or null if no user is authenticated.
33+
*/
34+
public AuthData getAuth() {
35+
return mAuthData;
36+
}
37+
1838
/**
1939
* Subclasses of this activity may implement this method to handle when a user logs in.
2040
*
2141
* @return void
2242
*/
23-
protected abstract void onFirebaseLoggedIn(AuthData authData);
43+
protected void onFirebaseLoggedIn(AuthData authData) {
44+
}
2445

2546
/**
2647
* Subclasses of this activity may implement this method to handle when a user logs out.
2748
*
2849
* @return void
2950
*/
30-
protected abstract void onFirebaseLoggedOut();
51+
protected void onFirebaseLoggedOut() {
52+
}
3153

3254
/**
3355
* Subclasses of this activity may implement this method to handle any potential provider errors
@@ -45,14 +67,6 @@ public abstract class FirebaseLoginBaseActivity extends AppCompatActivity {
4567
*/
4668
protected abstract void onFirebaseLoginUserError(FirebaseLoginError firebaseError);
4769

48-
/**
49-
* Subclasses of this activity must implement this method and return a valid Firebase reference that
50-
* can be used to call authentication related methods on.
51-
*
52-
* @return a Firebase reference that can be used to call authentication related methods on
53-
*/
54-
protected abstract Firebase getFirebaseRef();
55-
5670
public void logout() {
5771
mDialog.logout();
5872
}
@@ -102,8 +116,10 @@ public void onProviderError(FirebaseLoginError err) {
102116
@Override
103117
public void onAuthStateChanged(AuthData authData) {
104118
if (authData != null) {
119+
mAuthData = authData;
105120
onFirebaseLoggedIn(authData);
106121
} else {
122+
mAuthData = null;
107123
onFirebaseLoggedOut();
108124
}
109125
}

0 commit comments

Comments
 (0)