I created an app that has a chat as part of it.
I'm using Firebase firestore to store the messages and to display them for the users.
The chat looks as follows:
However, if one of the users clicks on some button in the app, the other user gets a unique message that shows some information and a button as part of the message as follows:
This message should be seen only to one of the users who recieves the message.
Problem description:
In order to show the messages in the chat, I read a collection in firestore that contains all of the messages. When a unique message is sent, it is also added to the collection and therefore I needed to do tons of if conditions and other operations in order to make sure that only one of the usere will see it and not the other - this seems to me to be very complex and im looking for some easier way to control how some of the message will be shown only to one of the users.
Currently, im getting the messages by using:
public void getChats(String chatID, long messageIndex, EventListener<QuerySnapshot> listener) {
Query lastMessages;
if (messageIndex != 0) {
lastMessages = db.collection( chatID )
.orderBy( "SentTime", Query.Direction.DESCENDING ).startAfter( messageIndex ).limit( 50 );
} else {
lastMessages = db.collection( chatID )
.orderBy( "SentTime", Query.Direction.DESCENDING ).limit( 50 );
}
lastMessages.addSnapshotListener( listener );
}
and then I call an adapter to take care of the views:
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view;
if (viewType == AppConstants.CHAT_SENT_KEY) {
view = LayoutInflater.from( parent.getContext() ).inflate(
R.layout.item_chat_sent,
parent,
false );
return new ChatViewHolder( view );
} else if (viewType == AppConstants.CHAT_RECEIVED_KEY) {
view = LayoutInflater.from( parent.getContext() ).inflate(
R.layout.item_chat_received,
parent,
false );
return new ChatViewHolder( view );
} else {
view = LayoutInflater.from( parent.getContext() ).inflate( R.layout.item_loading, parent, false );
return new LoadingViewHolder( view );
}
}
@Override
public int getItemViewType(int position) {
if (chats.get( position ).getSenderId().contentEquals( userId )) {
if (position == 0 && chats.size() > 49 && snapSize > 49) {
return AppConstants.CHAT_LOADING_KEY;
} else {
return AppConstants.CHAT_SENT_KEY;
}
} else {
if (position == 0 && chats.size() > 49 && snapSize > 49) {
return AppConstants.CHAT_LOADING_KEY;
} else {
return AppConstants.CHAT_RECEIVED_KEY;
}
}
}
@Override
public int getItemCount() {
return chats == null ? 0 : chats.size();
}
class ChatViewHolder extends RecyclerView.ViewHolder {
TextView message, messageTime;
Button confirmShare;
public ChatViewHolder(View itemView) {
super( itemView );
message = itemView.findViewById( R.id.chat_message );
messageTime = itemView.findViewById( R.id.time_message );
confirmShare = itemView.findViewById( R.id.Confirm_Share );
}
public void bind(Chat chat) {
ArrayList Index = new ArrayList();
ArrayList IndexConfirmed = new ArrayList();
message.setText( chat.getMessage() );
Date date = new Date( chat.getTime() );
String TimeFormat = "dd.MM.yyyy 'at' HH:mm";
SimpleDateFormat df2 = new SimpleDateFormat( TimeFormat );
String dateText = df2.format( date );
messageTime.setText( dateText );
for (int i = 0; i < chats.size(); i++) {
if (chats.get( i ).getIsButton()) {
Index.add( i );
if (chats.get( i ).getMessage().equals( "YOU CONFIRMED" )) {
IndexConfirmed.add( i );
}
}
}
if (Index.isEmpty()) {
Index.add( -1 );
}
if (IndexConfirmed.isEmpty()) {
IndexConfirmed.add( -2 );
}
int LastInd = (int) Collections.max( Index );
int LastIndConfirmed = (int) Collections.max( IndexConfirmed );
if (!chats.get( getAdapterPosition() ).getSenderId().contentEquals( userId )) {
if (LastInd > LastIndConfirmed && getAdapterPosition() == LastInd && chat.getIsButton()) {
confirmShare.setVisibility( View.VISIBLE );
confirmShare.setOnClickListener( v -> confirmListener.onClick( btn_ConfirmButton ) );
} else {
confirmShare.setVisibility( View.GONE );
}
} else {
if (chat.getIsButton()) {
message.setVisibility( View.GONE );
messageTime.setVisibility( View.GONE );
}
}
}
}
Is there any other way that I can control which message each user can see even though that I read all of the messages from firestore in order to display them?
Thank you

