这是群聊应用程序,当用户将消息分组时,所有其他消息都会被重复和无序排列。有时消息发送者和消息内容以及所有其他参数不匹配。 这是聊天活动,其中显示消息:
Mono<Void> validateCost = validateCost(buyRequest);
Mono<Void> validateBalance = validateBalance(buyRequest);
Mono<IssuePerkResponse> issuePerkResponseMono = Mono.when(validateCost, validateBalance)
.then(issuePerk(buyRequest));
Mono<IssueAndPayResult> payResultMono = issuePerkResponseMono
.flatMap(issuePerkResponse -> paymentService.doPayment(buyRequest)
.map(paymentDetails -> new IssueAndPayResult(issuePerkResponse.getPerkIssueId(),
paymentDetails)));
return payResultMono
.doOnError(th -> {
issuePerkResponseMono.flatMap(issuePerkResponse -> revertPerkIssue(issuePerkResponse))
.subscribe();
})
.flatMap(issueAndPayResult -> saveOrder(issueAndPayResult, buyRequest))
.doOnError(th -> {
issuePerkResponseMono.flatMap(issuePerkResponse -> revertPerkIssue(issuePerkResponse))
.subscribe();
payResultMono.flatMap(issueAndPayResult -> revertPayment(issueAndPayResult))
.subscribe();
});
这是消息适配器
public class GroupChatActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_group_chat2);
SendMessageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String message=userMessageInput.getText().toString();
SaveMessageInfoToDatabase("text", message);
}
});
}
@Override
protected void onStart() {
super.onStart();
messagesList.clear();
messageAdapter.notifyDataSetChanged();
groupChatRefForCurrentGroup.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
if (dataSnapshot.exists()) {
Message messages=dataSnapshot.getValue(Message.class);
messagesList.add(messages);
messageAdapter.notifyDataSetChanged();
userMessagesList.smoothScrollToPosition(userMessagesList.getAdapter().getItemCount());
}
}
@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) {
}
});
}
private void SaveMessageInfoToDatabase(String messageType, String message) {
HashMap<String, Object> groupMessageKey=new HashMap<>();
groupChatRefForCurrentGroup.updateChildren(groupMessageKey);
String messagekEY=groupChatRefForCurrentGroup.push().getKey();
Message message1=new Message(currentUserID, message,
messageType, currentGroupId, messagekEY, currentTime, currentDate);
groupChatRefForCurrentGroup.child(messagekEY).setValue(message1);
}}
这是自定义消息布局
public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.MessageViewHolder> {
@Override
public void onBindViewHolder(@NonNull final MessageViewHolder messageViewHolder, final int position) {
message=userMessagesList.get(position);
if (!senderUserId.equals(currentUserId)) {
messageViewHolder.receiverProfileImage.setVisibility(View.VISIBLE);
}
messageViewHolder.senderMessageText.setTextIsSelectable(true);
messageViewHolder.receiverMessageText.setTextIsSelectable(true);
if (fromMessageType.equals("text")) {
if (senderUserId.equals(currentUserId)) {
messageViewHolder.senderlayout.setVisibility(View.VISIBLE);
messageViewHolder.senderDate.setText(message.getTime() + " - " + message.getDate());
messageViewHolder.senderMessageText.setText(message.getMessage());
} else {
messageViewHolder.receiverlayout.setVisibility(View.VISIBLE);
messageViewHolder.receiverDate.setText(message.getTime() + " - " + message.getDate());
messageViewHolder.receiverMessageText.setText(message.getMessage());
}
} else if (fromMessageType.equals("image")) {
if (senderUserId.equals(currentUserId)) {
messageViewHolder.messageSenderPicture.setVisibility(View.VISIBLE);
Picasso.get()
.load(String.valueOf(message.getMessage()))
.into(messageViewHolder.messageSenderPicture);
} else {
messageViewHolder.messageReceiverPicture.setVisibility(View.VISIBLE);
messageViewHolder.download_image_receiver.setVisibility(View.VISIBLE);
}
} else if (fromMessageType.equals("pdf")) {
if (senderUserId.equals(currentUserId)) {
messageViewHolder.senderLayoutPdf.setVisibility(View.VISIBLE);
} else {
messageViewHolder.receiverLayoutPdf.setVisibility(View.VISIBLE);
}
} else if (fromMessageType.equals("topic")) {
messageViewHolder.group_topic_layout.setVisibility(View.VISIBLE);
messageViewHolder.topic_text.setText(message.getMessage());
messageViewHolder.topic_date_time.setText(message.getTime() + " - " + message.getDate());
}}}
这是数据库节点设计
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:focusableInTouchMode="true"
android:orientation="vertical"
>
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/message_profile_image"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_marginTop="10dp"
android:src="@drawable/profile_image"
android:visibility="gone" />
<LinearLayout
android:id="@+id/receiver_message_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_toEndOf="@+id/message_profile_image"
android:background="@drawable/receiver_messages_layout"
android:gravity="bottom|left"
android:layout_gravity="left"
android:orientation="vertical"
android:paddingStart="@dimen/margine_sub"
android:paddingTop="10dp"
android:paddingEnd="@dimen/margine_sub"
android:paddingBottom="10dp"
android:visibility="gone">
<TextView
android:id="@+id/receiver_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/lato"
android:text=""
android:textColor="#0889EC"
android:textSize="@dimen/text_inputs" />
<TextView
android:id="@+id/receiver_message_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/lato"
android:longClickable="true"
android:paddingBottom="2dp"
android:text="receiver messages here"
android:textColor="@color/recevierMessage"
android:textIsSelectable="true"
android:textSize="@dimen/title1" />
<TextView
android:id="@+id/receiver_message_date_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/lato"
android:paddingTop="2dp"
android:paddingBottom="4dp"
android:text="1:30 PM-May 1, 2020"
android:textColor="@color/recevierDateTime"
android:textSize="8dp" />
</LinearLayout>
<ImageView
android:id="@+id/message_sender_image_view"
android:layout_width="170dp"
android:layout_height="170dp"
android:layout_alignParentEnd="true"
android:layout_margin="5dp"
android:scaleType="centerCrop"
android:visibility="gone" />
<ImageView
android:id="@+id/message_receiver_image_view"
android:layout_width="170dp"
android:layout_height="170dp"
android:layout_margin="5dp"
android:layout_toEndOf="@+id/message_profile_image"
android:scaleType="centerCrop"
android:visibility="gone" />
<ImageView
android:id="@+id/download_image_receiver"
android:layout_width="@dimen/widget_size36"
android:layout_height="@dimen/widget_size36"
android:layout_margin="5dp"
android:layout_toRightOf="@+id/message_receiver_image_view"
android:src="@drawable/download"
android:visibility="gone" />
<LinearLayout
android:id="@+id/sender_message_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:background="@drawable/sender_messages_layout"
android:gravity="bottom|right"
android:layout_gravity="right"
android:orientation="vertical"
android:paddingStart="@dimen/margine_sub"
android:paddingTop="10dp"
android:paddingEnd="@dimen/margine_sub"
android:paddingBottom="10dp"
android:visibility="gone">
<TextView
android:id="@+id/sender_messsage_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/lato"
android:longClickable="true"
android:text="sender messages"
android:textColor="@color/recevierMessage"
android:textIsSelectable="true"
android:textSize="@dimen/title1" />
<TextView
android:id="@+id/sender_messsage_date_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:fontFamily="@font/lato"
android:text="1:30 PM-May 1, 2020"
android:textColor="@color/grey"
android:textSize="8sp" />
<TextView
android:id="@+id/isSeen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:text="seen"
android:textSize="8sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/layout_recevier_pdf"
android:layout_width="170dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:background="@drawable/bg_rounded_grey_border"
android:orientation="horizontal"
android:padding="5dp"
android:visibility="gone">
<ImageView
android:id="@+id/message_receiver_pdf_view"
android:layout_width="@dimen/widget_size36"
android:layout_height="@dimen/widget_size36"
android:layout_gravity="start"
android:scaleType="centerCrop"
android:src="@drawable/documents" />
<ImageView
android:id="@+id/download_pdf"
android:layout_width="@dimen/widget_size36"
android:layout_height="@dimen/widget_size36"
android:src="@drawable/download" />
</LinearLayout>
<LinearLayout
android:id="@+id/layout_sender_pdf"
android:layout_width="170dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:background="@drawable/bg_rounded_grey_border"
android:orientation="horizontal"
android:padding="5dp"
android:visibility="gone">
<ImageView
android:id="@+id/message_sender_pdf_view"
android:layout_width="@dimen/widget_size36"
android:layout_height="@dimen/widget_size36"
android:layout_gravity="start"
android:scaleType="centerCrop"
android:src="@drawable/documents" />
<!-- <ImageView
android:id="@+id/download_pdf"
android:layout_width="@dimen/widget_size36"
android:layout_height="@dimen/widget_size36"
android:src="@drawable/download" />
-->
</LinearLayout>
<LinearLayout
android:id="@+id/group_topic_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_marginEnd="@dimen/widget_size36"
android:layout_toEndOf="@+id/message_profile_image"
android:background="@drawable/receiver_messages_layout"
android:elevation="16dp"
android:gravity="bottom"
android:orientation="vertical"
android:paddingStart="@dimen/margine_sub"
android:paddingTop="10dp"
android:paddingEnd="@dimen/margine_sub"
android:paddingBottom="10dp"
android:visibility="gone">
<TextView
android:id="@+id/publisher_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/lato"
android:text="publisher"
android:textColor="#0889EC"
android:textSize="@dimen/text_inputs" />
<TextView
android:id="@+id/topic_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:fontFamily="@font/lato"
android:inputType="textMultiLine"
android:longClickable="true"
android:maxLines="4"
android:editable="false"
android:paddingBottom="2dp"
android:text="Detailed Topic here"
android:textIsSelectable="true"
android:textSize="@dimen/title1" />
<TextView
android:id="@+id/topic_date_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/lato"
android:paddingTop="2dp"
android:paddingBottom="4dp"
android:text="1:30 PM-May 1, 2020"
android:textColor="@color/recevierDateTime"
android:textSize="8dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="3dp">
<ImageView
android:id="@+id/like"
android:layout_width="@dimen/widget_size"
android:layout_height="match_parent"
android:layout_gravity="center"
android:paddingEnd="5dp"
android:src="@drawable/like_border" />
<TextView
android:id="@+id/no_of_likes"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignTop="@+id/reply"
android:layout_gravity="center"
android:layout_marginEnd="10dp"
android:layout_toEndOf="@+id/reply"
android:gravity="center"
android:text="0"
android:textSize="@dimen/title4" />
<TextView
android:id="@+id/reply"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:text="Reply"
android:textColor="@color/colorPrimary"
android:textSize="@dimen/title4" />
<TextView
android:id="@+id/no_of_replies"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/reply"
android:layout_marginStart="4dp"
android:layout_toEndOf="@+id/reply"
android:text="0"
android:textSize="@dimen/title4" />
</LinearLayout>
</LinearLayout>
</LinearLayout>