Android group chat display only empty listview without old messages just new messages when user join group

374 Views Asked by At

ISo i have group chat in my app. I have "rooms" and button join "room", when user click join button all old messages appears on screen but I want to display only new ones, I'm using firebase list adapter for displaying...everything works fine but just don't know how to display empty list and only new messages, ty.

     btnJoin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DatabaseReference db = FirebaseDatabase.getInstance().getReference("EventsUsers").child(uniqEventID);
            db.child(user.getUid()).setValue("");


        }
    });

   btnSend.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DatabaseReference db = FirebaseDatabase.getInstance().getReference("EventsUsers").child(uniqEventID).child("Messages").push();
            db.setValue(new ChatMessage(ePlain.getText().toString(),user.getDisplayName()));
            ePlain.setText("");
            ePlain.requestFocus();

        }
    });
    private void displayChat() {
    btnSend.setVisibility(View.VISIBLE);
    eventChat.setVisibility(View.VISIBLE);
    FirebaseListAdapter<ChatMessage> adapter = new FirebaseListAdapter<ChatMessage>(EventDetails.this,ChatMessage.class,R.layout.one_chat_list,FirebaseDatabase.getInstance().getReference("EventsUsers").child(uniqEventID).child("Messages")) {
        @Override
        protected void populateView(View v, ChatMessage model, int position) {
            TextView messageTextm,messageUser,messageTime;
            messageTextm = (TextView) v.findViewById(R.id.chatMessage);
            messageUser = (TextView)v.findViewById(R.id.chatUserName);
            messageTime = (TextView)v.findViewById(R.id.message_time);

            messageTextm.setText(model.getMessageText());
            messageUser.setText(model.getMessageUser());
            messageTime.setText(DateFormat.format("dd-MM-yyyy (HH:mm)", model.getMessageTime()));

        }
    };
    eventChat.setAdapter(adapter);

}
    DatabaseReference check = FirebaseDatabase.getInstance().getReference("EventsUsers").child(uniqEventID);
            check.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    if(dataSnapshot.hasChild(user.getUid())){
                        displayChat();
                    }
                }

So when user clicks join button listview with messages displays with all old messages before he joined...and sorry for not uploading any code I am new here, tell me if you need something else of my code This is my database structure https://ibb.co/d47D9v

1

There are 1 best solutions below

0
Matt Goodwin On

You'll have to keep track of time in firebase. First you need to keep track of when the messages are posted, second, you need to keep track of when a user joins the room. Then, when you query the messages you can

sortBy('time').startAt(timeUserJoined)

EDIT on your button join onClick you have

FirebaseDatabase.getInstance().getReference("EventsUsers")
    .child(uniqEventID).child(user.getUid()).setValue("");

set the value to the current time instead of just adding an empty node as a flag.

Then, when you add a message you need to add the time it was added as well. So just add this to the class you already made

db.setValue(new ChatMessage(ePlain.getText().toString(),
    user.getDisplayName()), System.currentTimeMillis());

Finally, in your adapter, your reference should be more like

FirebaseDatabase.getInstance().getReference("EventsUsers")
    .child(uniqEventID).child("Messages")
    .orderBy("time").beginAt(timeUserJoined)

timeUserJoined is the value the user's id as a key from above

FirebaseDatabase.getInstance().getReference("EventsUsers").child(uniqEventID)
    .setSingleValueListener( result-> timeUserJoing = result.value)

this is not perfect code but i think you get the point