Skip to content

Commit e9395fc

Browse files
committed
Make auth less prominent in DB sample
Bug: 28741205 Change-Id: Icf804a6cbb6ee432364c2fb63c7f31fb23422d4e
1 parent 541bab4 commit e9395fc

File tree

3 files changed

+47
-57
lines changed

3 files changed

+47
-57
lines changed

app/build.gradle

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
apply plugin: 'com.android.application'
22
apply plugin: 'com.neenbedankt.android-apt'
3+
apply from: "../common/constants.gradle"
34

45
android {
56
compileSdkVersion 23
@@ -28,7 +29,12 @@ dependencies {
2829
compile 'com.android.support:recyclerview-v7:23.2.1'
2930
compile 'com.android.support:support-v4:23.2.1'
3031

31-
compile project(':library')
32+
compile project(path: ':auth')
33+
compile project(path: ':database')
34+
35+
compile "com.google.firebase:firebase-database:${project.ext.firebase_version}"
36+
compile "com.google.firebase:firebase-auth:${project.ext.firebase_version}"
37+
compile "com.google.android.gms:play-services-auth:${project.ext.firebase_version}"
3238

3339
// The following dependencies are not required to use the Firebase UI library.
3440
// They are used to make some aspects of the demo app implementation simpler for

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

Lines changed: 40 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111
import android.support.v7.widget.RecyclerView;
1212
import android.util.Log;
1313
import android.view.Gravity;
14-
import android.view.Menu;
15-
import android.view.MenuInflater;
16-
import android.view.MenuItem;
1714
import android.view.View;
1815
import android.widget.Button;
1916
import android.widget.EditText;
@@ -45,6 +42,7 @@ public class ChatActivity extends AppCompatActivity {
4542
private EditText mMessageEdit;
4643

4744
private RecyclerView mMessages;
45+
private LinearLayoutManager mManager;
4846
private FirebaseRecyclerAdapter<Chat, ChatHolder> mRecyclerViewAdapter;
4947

5048
@Override
@@ -56,7 +54,7 @@ protected void onCreate(Bundle savedInstanceState) {
5654
mAuth.addAuthStateListener(new FirebaseAuth.AuthStateListener() {
5755
@Override
5856
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
59-
invalidateOptionsMenu();
57+
updateUI();
6058
}
6159
});
6260

@@ -88,17 +86,36 @@ public void onComplete(DatabaseError databaseError, DatabaseReference reference)
8886

8987
mMessages = (RecyclerView) findViewById(R.id.messagesList);
9088

91-
LinearLayoutManager manager = new LinearLayoutManager(this);
92-
manager.setReverseLayout(false);
89+
mManager = new LinearLayoutManager(this);
90+
mManager.setReverseLayout(false);
9391

9492
mMessages.setHasFixedSize(false);
95-
mMessages.setLayoutManager(manager);
93+
mMessages.setLayoutManager(mManager);
9694
}
9795

9896
@Override
9997
public void onStart() {
10098
super.onStart();
10199

100+
// Default Database rules do not allow unauthenticated reads, so we need to
101+
// sign in before attaching the RecyclerView adapter otherwise the Adapter will
102+
// not be able to read any data from the Database.
103+
if (!isSignedIn()) {
104+
signInAnonymously();
105+
} else {
106+
attachRecyclerViewAdapter();
107+
}
108+
}
109+
110+
@Override
111+
public void onStop() {
112+
super.onStop();
113+
if (mRecyclerViewAdapter != null) {
114+
mRecyclerViewAdapter.cleanup();
115+
}
116+
}
117+
118+
private void attachRecyclerViewAdapter() {
102119
mRecyclerViewAdapter = new FirebaseRecyclerAdapter<Chat, ChatHolder>(
103120
Chat.class, R.layout.message, ChatHolder.class, mChatRef) {
104121

@@ -116,49 +133,19 @@ public void populateViewHolder(ChatHolder chatView, Chat chat, int position) {
116133
}
117134
};
118135

119-
mMessages.setAdapter(mRecyclerViewAdapter);
120-
}
121-
122-
@Override
123-
public void onStop() {
124-
super.onStop();
125-
126-
mRecyclerViewAdapter.cleanup();
127-
}
128-
129-
@Override
130-
public boolean onCreateOptionsMenu(Menu menu) {
131-
MenuInflater inflater = getMenuInflater();
132-
inflater.inflate(R.menu.chat_login_menu, menu);
133-
return true;
134-
}
135-
136-
@Override
137-
public boolean onPrepareOptionsMenu(Menu menu) {
138-
menu.findItem(R.id.login_menu_item).setVisible(!isSignedIn());
139-
menu.findItem(R.id.logout_menu_item).setVisible(isSignedIn());
140-
141-
mSendButton.setEnabled(isSignedIn());
142-
mMessageEdit.setEnabled(isSignedIn());
143-
144-
return true;
145-
}
146-
147-
@Override
148-
public boolean onOptionsItemSelected(MenuItem item) {
149-
switch (item.getItemId()) {
150-
case R.id.login_menu_item:
151-
signInAnonymously();
152-
return true;
153-
case R.id.logout_menu_item:
154-
mAuth.signOut();
155-
return true;
156-
}
136+
// Scroll to bottom on new messages
137+
mRecyclerViewAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
138+
@Override
139+
public void onItemRangeInserted(int positionStart, int itemCount) {
140+
mManager.smoothScrollToPosition(mMessages, null, mRecyclerViewAdapter.getItemCount());
141+
}
142+
});
157143

158-
return super.onOptionsItemSelected(item);
144+
mMessages.setAdapter(mRecyclerViewAdapter);
159145
}
160146

161147
private void signInAnonymously() {
148+
Toast.makeText(this, "Signing in...", Toast.LENGTH_SHORT).show();
162149
mAuth.signInAnonymously()
163150
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
164151
@Override
@@ -167,6 +154,7 @@ public void onComplete(@NonNull Task<AuthResult> task) {
167154
if (task.isSuccessful()) {
168155
Toast.makeText(ChatActivity.this, "Signed In",
169156
Toast.LENGTH_SHORT).show();
157+
attachRecyclerViewAdapter();
170158
} else {
171159
Toast.makeText(ChatActivity.this, "Sign In Failed",
172160
Toast.LENGTH_SHORT).show();
@@ -179,6 +167,12 @@ public boolean isSignedIn() {
179167
return (mAuth.getCurrentUser() != null);
180168
}
181169

170+
public void updateUI() {
171+
// Sending only allowed when signed in
172+
mSendButton.setEnabled(isSignedIn());
173+
mMessageEdit.setEnabled(isSignedIn());
174+
}
175+
182176
public static class Chat {
183177

184178
String name;

app/src/main/res/menu/chat_login_menu.xml

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)