Skip to content

Commit dd6dd6a

Browse files
pufsamtstern
authored andcommitted
This change allows easy detection of the list being empty *and* respo… (#487)
1 parent 97ac613 commit dd6dd6a

File tree

6 files changed

+72
-7
lines changed

6 files changed

+72
-7
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,15 @@ public class ChatActivity extends AppCompatActivity implements FirebaseAuth.Auth
5151
private static final String TAG = "RecyclerViewDemo";
5252

5353
private FirebaseAuth mAuth;
54+
private DatabaseReference mRef;
5455
private DatabaseReference mChatRef;
5556
private Button mSendButton;
5657
private EditText mMessageEdit;
5758

5859
private RecyclerView mMessages;
5960
private LinearLayoutManager mManager;
6061
private FirebaseRecyclerAdapter<Chat, ChatHolder> mRecyclerViewAdapter;
62+
private View mEmptyListView;
6163

6264
@Override
6365
protected void onCreate(Bundle savedInstanceState) {
@@ -70,7 +72,10 @@ protected void onCreate(Bundle savedInstanceState) {
7072
mSendButton = (Button) findViewById(R.id.sendButton);
7173
mMessageEdit = (EditText) findViewById(R.id.messageEdit);
7274

73-
mChatRef = FirebaseDatabase.getInstance().getReference().child("chats");
75+
mEmptyListView = findViewById(R.id.emptyTextView);
76+
77+
mRef = FirebaseDatabase.getInstance().getReference();
78+
mChatRef = mRef.child("chats");
7479

7580
mSendButton.setOnClickListener(new View.OnClickListener() {
7681
@Override
@@ -153,6 +158,12 @@ public void populateViewHolder(ChatHolder chatView, Chat chat, int position) {
153158
chatView.setIsSender(false);
154159
}
155160
}
161+
162+
@Override
163+
protected void onDataChanged() {
164+
// if there are no chat messages, show a view that invites the user to add a message
165+
mEmptyListView.setVisibility(mRecyclerViewAdapter.getItemCount() == 0 ? View.VISIBLE : View.INVISIBLE);
166+
}
156167
};
157168

158169
// Scroll to bottom on new messages

app/src/main/res/layout/activity_chat.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
android:layout_height="match_parent"
77
tools:context=".database.ChatActivity">
88

9+
<TextView
10+
android:id="@+id/emptyTextView"
11+
android:layout_width="match_parent"
12+
android:layout_height="wrap_content"
13+
android:text="@string/start_chatting"
14+
android:padding="16dp" />
15+
916
<android.support.v7.widget.RecyclerView
1017
android:id="@+id/messagesList"
1118
android:layout_width="match_parent"

app/src/main/res/values/strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,7 @@
6464
<string name="choose_image">Choose Image</string>
6565
<string name="accessibility_downloaded_image">Downloaded image</string>
6666
<string name="drive_file">Drive File</string>
67+
68+
<!-- strings for Auth UI demo activities -->
69+
<string name="start_chatting">No messages. Start chatting at the bottom!</string>
6770
</resources>

database/src/main/java/com/firebase/ui/database/FirebaseArray.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,24 @@
1818
import com.google.firebase.database.DataSnapshot;
1919
import com.google.firebase.database.DatabaseError;
2020
import com.google.firebase.database.Query;
21+
import com.google.firebase.database.ValueEventListener;
2122

2223
import java.util.ArrayList;
2324
import java.util.List;
2425

2526
/**
2627
* This class implements an array-like collection on top of a Firebase location.
2728
*/
28-
class FirebaseArray implements ChildEventListener {
29+
class FirebaseArray implements ChildEventListener, ValueEventListener {
2930
public interface OnChangedListener {
3031
enum EventType {ADDED, CHANGED, REMOVED, MOVED}
3132

32-
void onChanged(EventType type, int index, int oldIndex);
33+
void onChildChanged(EventType type, int index, int oldIndex);
34+
35+
void onDataChanged();
3336

3437
void onCancelled(DatabaseError databaseError);
38+
3539
}
3640

3741
private Query mQuery;
@@ -41,10 +45,12 @@ enum EventType {ADDED, CHANGED, REMOVED, MOVED}
4145
public FirebaseArray(Query ref) {
4246
mQuery = ref;
4347
mQuery.addChildEventListener(this);
48+
mQuery.addValueEventListener(this);
4449
}
4550

4651
public void cleanup() {
47-
mQuery.removeEventListener(this);
52+
mQuery.removeEventListener((ValueEventListener) this);
53+
mQuery.removeEventListener((ChildEventListener) this);
4854
}
4955

5056
public int getCount() {
@@ -100,6 +106,11 @@ public void onChildMoved(DataSnapshot snapshot, String previousChildKey) {
100106
notifyChangedListeners(OnChangedListener.EventType.MOVED, newIndex, oldIndex);
101107
}
102108

109+
@Override
110+
public void onDataChange(DataSnapshot dataSnapshot) {
111+
mListener.onDataChanged();
112+
}
113+
103114
@Override
104115
public void onCancelled(DatabaseError error) {
105116
notifyCancelledListeners(error);
@@ -115,7 +126,7 @@ protected void notifyChangedListeners(OnChangedListener.EventType type, int inde
115126

116127
protected void notifyChangedListeners(OnChangedListener.EventType type, int index, int oldIndex) {
117128
if (mListener != null) {
118-
mListener.onChanged(type, index, oldIndex);
129+
mListener.onChildChanged(type, index, oldIndex);
119130
}
120131
}
121132

database/src/main/java/com/firebase/ui/database/FirebaseListAdapter.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,34 @@ public abstract class FirebaseListAdapter<T> extends BaseAdapter {
6969

7070
mSnapshots.setOnChangedListener(new FirebaseArray.OnChangedListener() {
7171
@Override
72-
public void onChanged(EventType type, int index, int oldIndex) {
72+
public void onChildChanged(EventType type, int index, int oldIndex) {
7373
notifyDataSetChanged();
7474
}
7575

7676
@Override
7777
public void onCancelled(DatabaseError databaseError) {
7878
FirebaseListAdapter.this.onCancelled(databaseError);
7979
}
80+
81+
@Override
82+
public void onDataChanged() {
83+
FirebaseListAdapter.this.onDataChanged();
84+
}
8085
});
8186
}
8287

88+
89+
/* This method will be triggered each time updates from the database have been completely processed.
90+
* So the first time this method is called, the initial data has been loaded - including the case
91+
* when no data at all is available. Each next time the method is called, a complete update (potentially
92+
* consisting of updates to multiple child items) has been completed.
93+
* <p>
94+
* You would typically override this method to hide a loading indicator (after the initial load) or
95+
* to complete a batch update to a UI element.
96+
*/
97+
protected void onDataChanged() {
98+
}
99+
83100
/**
84101
* @param activity The activity containing the ListView
85102
* @param modelClass Firebase will marshall the data at a location into

database/src/main/java/com/firebase/ui/database/FirebaseRecyclerAdapter.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public abstract class FirebaseRecyclerAdapter<T, VH extends RecyclerView.ViewHol
9191

9292
mSnapshots.setOnChangedListener(new FirebaseArray.OnChangedListener() {
9393
@Override
94-
public void onChanged(EventType type, int index, int oldIndex) {
94+
public void onChildChanged(EventType type, int index, int oldIndex) {
9595
switch (type) {
9696
case ADDED:
9797
notifyItemInserted(index);
@@ -114,9 +114,25 @@ public void onChanged(EventType type, int index, int oldIndex) {
114114
public void onCancelled(DatabaseError databaseError) {
115115
FirebaseRecyclerAdapter.this.onCancelled(databaseError);
116116
}
117+
118+
@Override
119+
public void onDataChanged() {
120+
FirebaseRecyclerAdapter.this.onDataChanged();
121+
}
117122
});
118123
}
119124

125+
/* This method will be triggered each time updates from the database have been completely processed.
126+
* So the first time this method is called, the initial data has been loaded - including the case
127+
* when no data at all is available. Each next time the method is called, a complete update (potentially
128+
* consisting of updates to multiple child items) has been completed.
129+
* <p>
130+
* You would typically override this method to hide a loading indicator (after the initial load) or
131+
* to complete a batch update to a UI element.
132+
*/
133+
protected void onDataChanged() {
134+
}
135+
120136
/**
121137
* @param modelClass Firebase will marshall the data at a location into
122138
* an instance of a class that you provide

0 commit comments

Comments
 (0)