Skip to content

Commit 758b692

Browse files
committed
Add separate activity for index stuff
Signed-off-by: Alex Saveau <[email protected]>
1 parent b2ffadf commit 758b692

File tree

3 files changed

+113
-56
lines changed

3 files changed

+113
-56
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
<activity
3131
android:name=".database.ChatActivity"
3232
android:label="@string/name_chat"/>
33+
<activity
34+
android:name=".database.ChatIndexActivity"
35+
android:label="@string/name_chat"/>
3336

3437
<!-- Auth UI demo -->
3538
<activity

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

Lines changed: 43 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -26,31 +26,29 @@
2626
import android.widget.TextView;
2727
import android.widget.Toast;
2828

29-
import com.firebase.ui.database.FirebaseIndexRecyclerAdapter;
3029
import com.firebase.ui.database.FirebaseRecyclerAdapter;
3130
import com.firebase.uidemo.R;
3231
import com.firebase.uidemo.util.SignInResultNotifier;
3332
import com.google.android.gms.tasks.OnSuccessListener;
3433
import com.google.firebase.auth.AuthResult;
3534
import com.google.firebase.auth.FirebaseAuth;
36-
import com.google.firebase.database.DataSnapshot;
3735
import com.google.firebase.database.DatabaseError;
3836
import com.google.firebase.database.DatabaseReference;
3937
import com.google.firebase.database.FirebaseDatabase;
38+
import com.google.firebase.database.Query;
4039

41-
public class ChatActivity extends AppCompatActivity implements FirebaseAuth.AuthStateListener {
40+
public class ChatActivity extends AppCompatActivity implements FirebaseAuth.AuthStateListener, View.OnClickListener {
4241
private static final String TAG = "RecyclerViewDemo";
4342

4443
private FirebaseAuth mAuth;
45-
private DatabaseReference mChatIndicesRef;
46-
private DatabaseReference mChatRef;
44+
protected DatabaseReference mChatRef;
4745
private Button mSendButton;
48-
private EditText mMessageEdit;
46+
protected EditText mMessageEdit;
4947

5048
private RecyclerView mMessages;
5149
private LinearLayoutManager mManager;
52-
private FirebaseRecyclerAdapter<Chat, ChatHolder> mAdapter;
53-
private TextView mEmptyListMessage;
50+
protected FirebaseRecyclerAdapter<Chat, ChatHolder> mAdapter;
51+
protected TextView mEmptyListMessage;
5452

5553
@Override
5654
protected void onCreate(Bundle savedInstanceState) {
@@ -64,31 +62,9 @@ protected void onCreate(Bundle savedInstanceState) {
6462
mMessageEdit = (EditText) findViewById(R.id.messageEdit);
6563
mEmptyListMessage = (TextView) findViewById(R.id.emptyTextView);
6664

67-
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
68-
mChatIndicesRef = ref.child("chatIndices");
69-
mChatRef = ref.child("chats");
65+
mChatRef = FirebaseDatabase.getInstance().getReference().child("chats");
7066

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);
9268

9369
mManager = new LinearLayoutManager(this);
9470
mManager.setReverseLayout(false);
@@ -128,50 +104,61 @@ public void onDestroy() {
128104
}
129105
}
130106

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+
131125
@Override
132126
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
133127
updateUI();
134128
}
135129

136130
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>(
138147
Chat.class,
139148
R.layout.message,
140149
ChatHolder.class,
141-
mChatIndicesRef.limitToLast(50),
142-
mChatRef) {
150+
lastFifty) {
143151
@Override
144152
public void populateViewHolder(ChatHolder holder, Chat chat, int position) {
145153
holder.bind(chat);
146154
}
147155

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-
159156
@Override
160157
public void onDataChanged() {
161158
// If there are no chat messages, show a view that invites the user to add a message.
162159
mEmptyListMessage.setVisibility(getItemCount() == 0 ? View.VISIBLE : View.GONE);
163160
}
164161
};
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);
175162
}
176163

177164
private void signInAnonymously() {
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.firebase.uidemo.database;
2+
3+
import android.os.Bundle;
4+
import android.view.View;
5+
6+
import com.firebase.ui.database.FirebaseIndexRecyclerAdapter;
7+
import com.firebase.ui.database.FirebaseRecyclerAdapter;
8+
import com.firebase.uidemo.R;
9+
import com.google.firebase.auth.FirebaseAuth;
10+
import com.google.firebase.database.DataSnapshot;
11+
import com.google.firebase.database.DatabaseReference;
12+
import com.google.firebase.database.FirebaseDatabase;
13+
14+
public class ChatIndexActivity extends ChatActivity {
15+
private DatabaseReference mChatIndicesRef;
16+
17+
@Override
18+
protected void onCreate(Bundle savedInstanceState) {
19+
super.onCreate(savedInstanceState);
20+
mChatIndicesRef = FirebaseDatabase.getInstance().getReference().child("chatIndices");
21+
}
22+
23+
@Override
24+
public void onClick(View v) {
25+
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
26+
String name = "User " + uid.substring(0, 6);
27+
Chat chat = new Chat(name, mMessageEdit.getText().toString(), uid);
28+
29+
DatabaseReference chatRef = mChatRef.push();
30+
mChatIndicesRef.child(chatRef.getKey()).setValue(true);
31+
chatRef.setValue(chat);
32+
33+
mMessageEdit.setText("");
34+
}
35+
36+
@Override
37+
protected FirebaseRecyclerAdapter<Chat, ChatHolder> getAdapter() {
38+
return new FirebaseIndexRecyclerAdapter<Chat, ChatHolder>(
39+
Chat.class,
40+
R.layout.message,
41+
ChatHolder.class,
42+
mChatIndicesRef.limitToLast(50),
43+
mChatRef) {
44+
@Override
45+
public void populateViewHolder(ChatHolder holder, Chat chat, int position) {
46+
holder.bind(chat);
47+
}
48+
49+
@Override
50+
public void onChildChanged(EventType type,
51+
DataSnapshot snapshot,
52+
int index,
53+
int oldIndex) {
54+
super.onChildChanged(type, snapshot, index, oldIndex);
55+
56+
// TODO temporary fix for https://github.com/firebase/FirebaseUI-Android/issues/546
57+
onDataChanged();
58+
}
59+
60+
@Override
61+
public void onDataChanged() {
62+
// If there are no chat messages, show a view that invites the user to add a message.
63+
mEmptyListMessage.setVisibility(getItemCount() == 0 ? View.VISIBLE : View.GONE);
64+
}
65+
};
66+
}
67+
}

0 commit comments

Comments
 (0)