Search in sources :

Example 1 with Conv

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);
}
Also used : Query(com.google.firebase.database.Query) Intent(android.content.Intent) DataSnapshot(com.google.firebase.database.DataSnapshot) ImageView(android.widget.ImageView) CircleImageView(de.hdodenhof.circleimageview.CircleImageView) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) TextView(android.widget.TextView) DatabaseError(com.google.firebase.database.DatabaseError) Conv(com.example.singhkshitiz.letschat.Conv) FirebaseRecyclerAdapter(com.firebase.ui.database.FirebaseRecyclerAdapter) ValueEventListener(com.google.firebase.database.ValueEventListener) ChildEventListener(com.google.firebase.database.ChildEventListener)

Aggregations

Intent (android.content.Intent)1 RecyclerView (android.support.v7.widget.RecyclerView)1 View (android.view.View)1 ImageView (android.widget.ImageView)1 TextView (android.widget.TextView)1 Conv (com.example.singhkshitiz.letschat.Conv)1 FirebaseRecyclerAdapter (com.firebase.ui.database.FirebaseRecyclerAdapter)1 ChildEventListener (com.google.firebase.database.ChildEventListener)1 DataSnapshot (com.google.firebase.database.DataSnapshot)1 DatabaseError (com.google.firebase.database.DatabaseError)1 Query (com.google.firebase.database.Query)1 ValueEventListener (com.google.firebase.database.ValueEventListener)1 CircleImageView (de.hdodenhof.circleimageview.CircleImageView)1