Skip to content

Commit 81a3800

Browse files
committed
Push Firebase initialization out of LoginActivity.onCreate
This means that the derived class can initialize its ref in onCreate as is common practice. It also means the LoginActivity.onCreate doesn't require Firebase.setAndroidContext to have been called anymore
1 parent 0c9c4c9 commit 81a3800

File tree

2 files changed

+18
-20
lines changed

2 files changed

+18
-20
lines changed

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import android.view.View;
1212
import android.widget.Button;
1313
import android.widget.EditText;
14-
import android.widget.LinearLayout;
1514
import android.widget.TextView;
1615

1716
import com.firebase.client.AuthData;
@@ -43,6 +42,8 @@ protected void onCreate(Bundle savedInstanceState) {
4342
messages.setHasFixedSize(true);
4443
messages.setLayoutManager(new LinearLayoutManager(this));
4544

45+
mRef = new Firebase("https://firebaseui.firebaseio.com/chat");
46+
4647
mSendButton.setOnClickListener(new View.OnClickListener() {
4748
@Override
4849
public void onClick(View v) {
@@ -143,17 +144,12 @@ public void onFirebaseLoginCancel() {
143144
}
144145

145146
@Override
146-
public Firebase setupFirebase() {
147-
if (mRef == null) {
148-
mRef = new Firebase("https://firebaseui.firebaseio.com/chat");
149-
}
150-
147+
public Firebase getFirebaseRef() {
151148
return mRef;
152149
}
153150

154151
// End of FirebaseLoginBaseActivity
155152

156-
157153
public static class Chat {
158154
String name;
159155
String text;

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

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,31 @@ public abstract class FirebaseLoginBaseActivity extends AppCompatActivity {
2020

2121
private final String LOG_TAG = "FirebaseLoginBaseAct";
2222

23-
private Firebase mRef;
24-
2523
private GoogleAuthHelper mGoogleAuthHelper;
2624

2725
public SocialProvider mChosenProvider;
2826

2927
/* Abstract methods for Login Events */
30-
public abstract void onFirebaseLogin(AuthData authData);
28+
protected abstract void onFirebaseLogin(AuthData authData);
3129

32-
public abstract void onFirebaseLogout();
30+
protected abstract void onFirebaseLogout();
3331

34-
public abstract void onFirebaseLoginError(FirebaseError firebaseError);
32+
protected abstract void onFirebaseLoginError(FirebaseError firebaseError);
3533

36-
public abstract void onFirebaseLoginCancel();
34+
protected abstract void onFirebaseLoginCancel();
3735

38-
public abstract Firebase setupFirebase();
36+
/**
37+
* Subclasses of this activity must implement this method and return a valid Firebase reference that
38+
* can be used to call authentication related methods on.
39+
*
40+
* @return a Firebase reference that can be used to call authentication related methods on
41+
*/
42+
protected abstract Firebase getFirebaseRef();
3943

4044
/* Login/Logout */
4145

4246
public void loginWithProvider(SocialProvider provider) {
47+
// TODO: what should happen if you're already authenticated?
4348
switch (provider) {
4449
case google:
4550
mGoogleAuthHelper.login();
@@ -61,16 +66,13 @@ public void logout() {
6166
case twitter:
6267
throw new UnsupportedOperationException();
6368
}
64-
mRef.unauth();
69+
getFirebaseRef().unauth();
6570
}
6671

6772

6873
@Override
6974
protected void onCreate(Bundle savedInstanceState) {
7075
super.onCreate(savedInstanceState);
71-
Firebase.setAndroidContext(this);
72-
73-
mRef = setupFirebase();
7476

7577
mGoogleAuthHelper = new GoogleAuthHelper(this, new TokenAuthHandler() {
7678
@Override
@@ -93,7 +95,7 @@ public void onError(Exception ex) {
9395
@Override
9496
protected void onStart() {
9597
super.onStart();
96-
mRef.addAuthStateListener(new Firebase.AuthStateListener() {
98+
getFirebaseRef().addAuthStateListener(new Firebase.AuthStateListener() {
9799
@Override
98100
public void onAuthStateChanged(AuthData authData) {
99101
if (authData != null) {
@@ -107,7 +109,7 @@ public void onAuthStateChanged(AuthData authData) {
107109
}
108110

109111
private void authenticateRefWithProvider(String provider, String token) {
110-
mRef.authWithOAuthToken(provider, token, new Firebase.AuthResultHandler() {
112+
getFirebaseRef().authWithOAuthToken(provider, token, new Firebase.AuthResultHandler() {
111113
@Override
112114
public void onAuthenticated(AuthData authData) {
113115
// Do nothing. Auth updates are handled in the AuthStateListener

0 commit comments

Comments
 (0)