use of com.example.singhkshitiz.letschat.Conv in project Lets-Chat by kshitiz1007.
the class ChatFragment method onStart.
@Override
public void onStart() {
super.onStart();
// ---ADDING THE RECYCLERVIEW TO FIREBASE DATABASE DIRECTLY----
// --ORDERING THE MESSAGE BY TIME----
Query conversationQuery = mConvDatabase.orderByChild("time_stamp");
FirebaseRecyclerAdapter<Conv, ConvViewHolder> friendsConvAdapter = new FirebaseRecyclerAdapter<Conv, ConvViewHolder>(// --CLASS FETCHED FROM DATABASE-- LAYOUT OF THE SINGLE ITEM--- HOLDER CLASS(DEFINED BELOW)---QUERY
Conv.class, R.layout.recycle_list_single_user, ConvViewHolder.class, conversationQuery) {
// ---- GETTING DATA FROM DATABSE AND ADDING TO VIEWHOLDER-----
@Override
protected void populateViewHolder(final ConvViewHolder convViewHolder, final Conv conv, int position) {
final String list_user_id = getRef(position).getKey();
Query lastMessageQuery = mMessageDatabase.child(list_user_id).limitToLast(1);
// ---IT WORKS WHENEVER CHILD OF mMessageDatabase IS CHANGED---
lastMessageQuery.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String data = dataSnapshot.child("message").getValue().toString();
convViewHolder.setMessage(data, conv.isSeen());
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
// ---ADDING NAME , IMAGE, ONLINE FEATURE , AND OPENING CHAT ACTIVITY ON CLICK----
mUsersDatabase.child(list_user_id).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
final String userName = dataSnapshot.child("name").getValue().toString();
String userThumb = dataSnapshot.child("thumb_image").getValue().toString();
if (dataSnapshot.hasChild("online")) {
String userOnline = dataSnapshot.child("online").getValue().toString();
convViewHolder.setUserOnline(userOnline);
}
convViewHolder.setName(userName);
convViewHolder.setUserImage(userThumb, getContext());
// --OPENING CHAT ACTIVITY FOR CLICKED USER----
convViewHolder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent chatIntent = new Intent(getContext(), ChatActivity.class);
chatIntent.putExtra("user_id", list_user_id);
chatIntent.putExtra("user_name", userName);
startActivity(chatIntent);
}
});
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
};
mConvList.setAdapter(friendsConvAdapter);
}
Aggregations