Skip to content

Commit

Permalink
超级群组会话中,如果收到未加载的消息,只保留最新已加载的消息
Browse files Browse the repository at this point in the history
  • Loading branch information
imndx committed Oct 22, 2024
1 parent 18b7fb8 commit faec1ee
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 138 deletions.
11 changes: 7 additions & 4 deletions chat/src/main/java/cn/wildfire/chat/app/main/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import cn.wildfire.chat.kit.conversation.ConversationViewModel;
import cn.wildfire.chat.kit.conversation.CreateConversationActivity;
import cn.wildfire.chat.kit.conversation.forward.ForwardActivity;
import cn.wildfire.chat.kit.conversation.message.model.UiMessage;
import cn.wildfire.chat.kit.conversationlist.ConversationListFragment;
import cn.wildfire.chat.kit.conversationlist.ConversationListViewModel;
import cn.wildfire.chat.kit.conversationlist.ConversationListViewModelFactory;
Expand Down Expand Up @@ -172,10 +173,12 @@ protected void afterViews() {
}
});
MessageViewModel messageViewModel = ViewModelProviders.of(this).get(MessageViewModel.class);
messageViewModel.messageLiveData().observe(this, uiMessage -> {
if (uiMessage.message.messageId > 0 && (uiMessage.message.content.getMessageContentType() == MessageContentType.MESSAGE_CONTENT_TYPE_FEED
|| uiMessage.message.content.getMessageContentType() == MessageContentType.MESSAGE_CONTENT_TYPE_FEED_COMMENT)) {
updateMomentBadgeView();
messageViewModel.messageLiveData().observe(this, uiMessages -> {
for (UiMessage uiMessage : uiMessages) {
if (uiMessage.message.messageId > 0 && (uiMessage.message.content.getMessageContentType() == MessageContentType.MESSAGE_CONTENT_TYPE_FEED
|| uiMessage.message.content.getMessageContentType() == MessageContentType.MESSAGE_CONTENT_TYPE_FEED_COMMENT)) {
updateMomentBadgeView();
}
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import androidx.lifecycle.ViewModelProvider;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -58,7 +58,7 @@ protected int contentLayout() {

@Override
protected void afterViews() {
pickUserViewModel = ViewModelProviders.of(this).get(PickUserViewModel.class);
pickUserViewModel = new ViewModelProvider(this).get(PickUserViewModel.class);
pickUserViewModel.userCheckStatusUpdateLiveData().observeForever(contactCheckStatusUpdateLiveDataObserver);
Intent intent = getIntent();
int maxCount = intent.getIntExtra(PARAM_MAX_COUNT, 0);
Expand Down Expand Up @@ -102,8 +102,8 @@ public boolean onOptionsItemSelected(MenuItem item) {
private void initView() {
PickContactFragment fragment = new PickContactFragment();
getSupportFragmentManager().beginTransaction()
.replace(R.id.containerFrameLayout, fragment)
.commit();
.replace(R.id.containerFrameLayout, fragment)
.commit();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,84 +169,105 @@ public class ConversationFragment extends Fragment implements

private String backgroundImageUri = null;
private int backgroundImageResId = 0;
private boolean isCommercialServer = false;

private Observer<UiMessage> messageLiveDataObserver = new Observer<UiMessage>() {
private Observer<List<UiMessage>> messageLiveDataObserver = new Observer<List<UiMessage>>() {
@Override
public void onChanged(@Nullable UiMessage uiMessage) {
if (!isMessageInCurrentConversation(uiMessage)) {
return;
public void onChanged(List<UiMessage> uiMessages) {

boolean hasUnloadMsg = false;
if (conversation.type == Conversation.ConversationType.Group && isCommercialServer) {
for (UiMessage uimsg : uiMessages) {
if (uimsg.message.content.notLoaded > 0) {
hasUnloadMsg = true;
break;
}
}
}
MessageContent content = uiMessage.message.content;

if (content instanceof MultiCallOngoingMessageContent) {
MultiCallOngoingMessageContent ongoingCall = (MultiCallOngoingMessageContent) content;
AVEngineKit.CallSession callSession = AVEngineKit.Instance().getCurrentSession();
if (ongoingCall.getInitiator().equals(ChatManager.Instance().getUserId())
|| ongoingCall.getTargets().contains(ChatManager.Instance().getUserId())
|| (callSession != null && callSession.getState() != AVEngineKit.CallState.Idle)) {
return;
if (hasUnloadMsg) {
uiMessages = uiMessages.subList(uiMessages.size() - 15, uiMessages.size());
if (isMessageInCurrentConversation(uiMessages.get(0))) {
adapter.setMessages(new ArrayList<>());
adapter.notifyDataSetChanged();
}
}

if (ongoingCalls == null) {
ongoingCalls = new HashMap<>();
for (UiMessage uiMessage : uiMessages) {
if (!isMessageInCurrentConversation(uiMessage)) {
return;
}
ongoingCalls.put(ongoingCall.getCallId(), uiMessage.message);

if (ongoingCalls.size() > 0) {
ongoingCallRecyclerView.setVisibility(View.VISIBLE);
if (ongoingCallAdapter == null) {
ongoingCallAdapter = new OngoingCallAdapter();
ongoingCallRecyclerView.setAdapter(ongoingCallAdapter);
ongoingCallRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
MessageContent content = uiMessage.message.content;

if (content instanceof MultiCallOngoingMessageContent) {
MultiCallOngoingMessageContent ongoingCall = (MultiCallOngoingMessageContent) content;
AVEngineKit.CallSession callSession = AVEngineKit.Instance().getCurrentSession();
if (ongoingCall.getInitiator().equals(ChatManager.Instance().getUserId())
|| ongoingCall.getTargets().contains(ChatManager.Instance().getUserId())
|| (callSession != null && callSession.getState() != AVEngineKit.CallState.Idle)) {
return;
}
ongoingCallAdapter.setOngoingCalls(new ArrayList<>(ongoingCalls.values()));
} else {
ongoingCallRecyclerView.setVisibility(View.GONE);
ongoingCallAdapter.setOngoingCalls(null);
}
cleanExpiredOngoingCalls();

return;
}
if (ongoingCalls == null) {
ongoingCalls = new HashMap<>();
}
ongoingCalls.put(ongoingCall.getCallId(), uiMessage.message);

if (ongoingCalls.size() > 0) {
ongoingCallRecyclerView.setVisibility(View.VISIBLE);
if (ongoingCallAdapter == null) {
ongoingCallAdapter = new OngoingCallAdapter();
ongoingCallRecyclerView.setAdapter(ongoingCallAdapter);
ongoingCallRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
}
ongoingCallAdapter.setOngoingCalls(new ArrayList<>(ongoingCalls.values()));
} else {
ongoingCallRecyclerView.setVisibility(View.GONE);
ongoingCallAdapter.setOngoingCalls(null);
}
cleanExpiredOngoingCalls();

if (isDisplayableMessage(uiMessage) && !(content instanceof RecallMessageContent)) {
// 消息定位时,如果收到新消息、或者发送消息,需要重新加载消息列表
if (shouldContinueLoadNewMessage) {
shouldContinueLoadNewMessage = false;
reloadMessage();
return;
}
adapter.addNewMessage(uiMessage);
if (moveToBottom || uiMessage.message.sender.equals(ChatManager.Instance().getUserId())) {
UIUtils.postTaskDelay(() -> {

int position = adapter.getItemCount() - 1;
if (position < 0) {
return;
}
recyclerView.scrollToPosition(position);
},
100);
}
}
if (content instanceof TypingMessageContent && uiMessage.message.direction == MessageDirection.Receive) {
if (typingMessageMap == null) {
typingMessageMap = new HashMap<>();
}
long now = System.currentTimeMillis();
if (now - uiMessage.message.serverTime + ChatManager.Instance().getServerDeltaTime() < TYPING_INTERNAL) {
typingMessageMap.put(uiMessage.message.sender, uiMessage.message);

if (isDisplayableMessage(uiMessage) && !(content instanceof RecallMessageContent)) {
// 消息定位时,如果收到新消息、或者发送消息,需要重新加载消息列表
if (shouldContinueLoadNewMessage) {
shouldContinueLoadNewMessage = false;
reloadMessage();
return;
}
adapter.addNewMessage(uiMessage);
if (moveToBottom || uiMessage.message.sender.equals(ChatManager.Instance().getUserId())) {
UIUtils.postTaskDelay(() -> {

int position = adapter.getItemCount() - 1;
if (position < 0) {
return;
}
recyclerView.scrollToPosition(position);
},
100);
}
}
updateTypingStatusTitle();
} else {
if (uiMessage.message.direction == MessageDirection.Receive && typingMessageMap != null) {
typingMessageMap.remove(uiMessage.message.sender);
if (content instanceof TypingMessageContent && uiMessage.message.direction == MessageDirection.Receive) {
if (typingMessageMap == null) {
typingMessageMap = new HashMap<>();
}
long now = System.currentTimeMillis();
if (now - uiMessage.message.serverTime + ChatManager.Instance().getServerDeltaTime() < TYPING_INTERNAL) {
typingMessageMap.put(uiMessage.message.sender, uiMessage.message);
}
updateTypingStatusTitle();
} else {
if (uiMessage.message.direction == MessageDirection.Receive && typingMessageMap != null) {
typingMessageMap.remove(uiMessage.message.sender);
}
updateTypingStatusTitle();
}
updateTypingStatusTitle();
}

if (getLifecycle().getCurrentState() == Lifecycle.State.RESUMED && uiMessage.message.direction == MessageDirection.Receive) {
conversationViewModel.clearUnreadStatus(conversation);
if (getLifecycle().getCurrentState() == Lifecycle.State.RESUMED && uiMessage.message.direction == MessageDirection.Receive) {
conversationViewModel.clearUnreadStatus(conversation);
}
}
}
};
Expand Down Expand Up @@ -601,7 +622,6 @@ public void onChanged(Pair<String, ChatManager.SecretChatState> stringSecretChat
showGroupMemberName = show;
adapter.notifyDataSetChanged();
}
reloadMessage();
};

settingViewModel = ViewModelProviders.of(this).get(SettingViewModel.class);
Expand All @@ -616,6 +636,8 @@ public void onChanged(Pair<String, ChatManager.SecretChatState> stringSecretChat
} else if (backgroundImageResId != 0) {
setConversationBackgroundImage(backgroundImageResId);
}

isCommercialServer = ChatManager.Instance().isCommercialServer();
}

private void setupConversation(Conversation conversation) {
Expand Down Expand Up @@ -1069,7 +1091,7 @@ private void loadMoreOldMessages() {

private void loadMoreOldMessages(boolean scrollToBottom) {

conversationViewModel.loadOldMessages(conversation, targetUser, adapter.oldestMessageId, adapter.oldestMessageUid, MESSAGE_LOAD_COUNT_PER_TIME, true)
conversationViewModel.loadOldMessages(conversation, targetUser, adapter.getOldestMessageId(), adapter.getOldestMessageUid(), MESSAGE_LOAD_COUNT_PER_TIME, true)
.observe(this, uiMessages -> {
adapter.addMessagesAtHead(uiMessages);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

package cn.wildfire.chat.kit.conversation;

import android.os.Build;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
Expand All @@ -29,7 +28,6 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.wildfire.chat.kit.R;
import cn.wildfire.chat.kit.annotation.EnableContextMenu;
Expand Down Expand Up @@ -63,9 +61,6 @@ public class ConversationMessageAdapter extends RecyclerView.Adapter<RecyclerVie
private OnPortraitLongClickListener onPortraitLongClickListener;
private OnMessageReceiptClickListener onMessageReceiptClickListener;

long oldestMessageUid = Long.MAX_VALUE;
long oldestMessageId = Long.MAX_VALUE;

public ConversationMessageAdapter(ConversationFragment fragment) {
super();
this.fragment = fragment;
Expand All @@ -79,6 +74,25 @@ public void setMode(int mode) {
this.mode = mode;
}

public long getOldestMessageUid() {
if (this.messages == null || this.messages.isEmpty()) {
return Long.MAX_VALUE;
}
return this.messages.get(0).message.messageUid;
}

public long getOldestMessageId() {
if (this.messages == null || this.messages.isEmpty()) {
return Long.MAX_VALUE;
}
long id = this.messages.get(0).message.messageId;
// 以下是新消息 那条提示消息的 messageId 是 Long.MAX_VALUE
if (id == Long.MAX_VALUE && this.messages.size() > 1) {
id = this.messages.get(1).message.messageId;
}
return id;
}

public void clearMessageCheckStatus() {
if (messages == null) {
return;
Expand Down Expand Up @@ -106,37 +120,7 @@ public List<UiMessage> getMessages() {

public void setMessages(List<UiMessage> messages) {
if (messages != null && !messages.isEmpty()) {
oldestMessageUid = messages.get(0).message.messageUid;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
messages = messages.stream().filter(m -> m.message.messageId != 0).collect(Collectors.toList());
if (!messages.isEmpty()) {
for (int i = 0; i < messages.size(); i++) {
Message msg = messages.get(i).message;
// 以下是新消息 那条提示消息的 messageId 是 Long.MAX_VALUE
if (msg.messageId != Long.MAX_VALUE) {
oldestMessageId = msg.messageId;
break;
}
}
}
this.messages = messages;
} else {
for (UiMessage uiMsg : messages) {
if (uiMsg.message.messageId != 0) {
this.messages.add(uiMsg);
}
}
if (!this.messages.isEmpty()) {
for (int i = 0; i < messages.size(); i++) {
Message msg = messages.get(i).message;
// 以下是新消息 那条提示消息的 messageId 是 Long.MAX_VALUE
if (msg.messageId != Long.MAX_VALUE) {
oldestMessageId = msg.messageId;
break;
}
}
}
}
this.messages = messages;
} else {
this.messages = new ArrayList<>();
}
Expand Down Expand Up @@ -169,7 +153,7 @@ public void setOnMessageReceiptClickListener(OnMessageReceiptClickListener onMes
}

public void addNewMessage(UiMessage message) {
if (message == null) {
if (message == null || message.message.messageId == 0) {
return;
}
int index = contains(message);
Expand All @@ -185,7 +169,6 @@ public void addMessagesAtHead(List<UiMessage> newMessages) {
if (newMessages == null || newMessages.isEmpty()) {
return;
}
oldestMessageUid = newMessages.get(0).message.messageUid;
List<UiMessage> filteredMsgs = new ArrayList<>();
for (UiMessage m : newMessages) {
if (m.message.messageId != 0) {
Expand All @@ -195,8 +178,6 @@ public void addMessagesAtHead(List<UiMessage> newMessages) {
newMessages = filteredMsgs;
if (newMessages.isEmpty()) {
return;
} else {
oldestMessageId = newMessages.get(0).message.messageId;
}
this.messages.addAll(0, newMessages);
notifyItemRangeInserted(0, newMessages.size());
Expand Down
Loading

0 comments on commit faec1ee

Please sign in to comment.