diff --git a/client/src/main/java/cn/wildfirechat/remote/ChatManager.java b/client/src/main/java/cn/wildfirechat/remote/ChatManager.java index 5aa67913c..d53842c0c 100644 --- a/client/src/main/java/cn/wildfirechat/remote/ChatManager.java +++ b/client/src/main/java/cn/wildfirechat/remote/ChatManager.java @@ -3004,7 +3004,7 @@ public long getFirstUnreadMessageId(Conversation conversation) { * @param before true, 获取fromIndex之前的消息,即更旧的消息;false,获取fromIndex之后的消息,即更新的消息。都不包含fromIndex对应的消息 * @param count 获取消息条数 * @param withUser 只有会话类型为{@link cn.wildfirechat.model.Conversation.ConversationType#Channel}时生效, channel主用来查询和某个用户的所有消息 - * @return 由于ipc大小限制,本接口获取到的消息列表可能不完整,请使用异步获取 + * @return 由于ipc大小限制,本接口获取到的消息列表可能不完整,并且超级群里面,可能返回为完全加载的消息,请使用异步获取 */ @Deprecated public List getMessages(Conversation conversation, long fromIndex, boolean before, int count, String withUser) { diff --git a/uikit/src/main/java/cn/wildfire/chat/kit/conversation/ConversationViewModel.java b/uikit/src/main/java/cn/wildfire/chat/kit/conversation/ConversationViewModel.java index f4d8b025b..3992ab82a 100644 --- a/uikit/src/main/java/cn/wildfire/chat/kit/conversation/ConversationViewModel.java +++ b/uikit/src/main/java/cn/wildfire/chat/kit/conversation/ConversationViewModel.java @@ -13,6 +13,7 @@ import java.util.ArrayList; import java.util.List; +import java.util.concurrent.CountDownLatch; import cn.wildfire.chat.kit.common.AppScopeViewModel; import cn.wildfire.chat.kit.common.OperateResult; @@ -27,6 +28,7 @@ import cn.wildfirechat.remote.CreateSecretChatCallback; import cn.wildfirechat.remote.GeneralCallback; import cn.wildfirechat.remote.GetMessageCallback; +import cn.wildfirechat.remote.GetOneRemoteMessageCallback; import cn.wildfirechat.remote.GetRemoteMessageCallback; import cn.wildfirechat.remote.SecretChatStateChangeListener; @@ -132,29 +134,77 @@ public void onFail(int errorCode) { public MutableLiveData> loadAroundMessages(Conversation conversation, String withUser, long focusIndex, int count) { MutableLiveData> result = new MutableLiveData<>(); ChatManager.Instance().getWorkHandler().post(() -> { - List oldMessageList = ChatManager.Instance().getMessages(conversation, focusIndex, true, count, withUser); - List oldMessages = new ArrayList<>(); - if (oldMessageList != null) { - for (Message msg : oldMessageList) { - if (conversation.type == Conversation.ConversationType.SecretChat) { - BurnMessageInfo burnMessageInfo = ChatManager.Instance().getBurnMessageInfo(msg.messageId); - oldMessages.add(new UiMessage(msg, burnMessageInfo)); - } else { - oldMessages.add(new UiMessage(msg)); + List oldMessageList = new ArrayList<>(); + List newMessageList = new ArrayList<>(); + CountDownLatch countDownLatch = new CountDownLatch(3); + ChatManager.Instance().getMessages(conversation, focusIndex, true, count, withUser, new GetMessageCallback() { + @Override + public void onSuccess(List messages, boolean hasMore) { + oldMessageList.addAll(messages); + countDownLatch.countDown(); + } + + @Override + public void onFail(int errorCode) { + countDownLatch.countDown(); + } + }); + + ChatManager.Instance().getMessages(conversation, focusIndex, false, count, withUser, new GetMessageCallback() { + + @Override + public void onSuccess(List messages, boolean hasMore) { + newMessageList.addAll(messages); + countDownLatch.countDown(); + } + + @Override + public void onFail(int errorCode) { + countDownLatch.countDown(); + } + }); + + final Message[] focusMessage = {ChatManager.Instance().getMessage(focusIndex)}; + if (focusMessage[0] == null || focusMessage[0].content.notLoaded == 0) { + countDownLatch.countDown(); + } else { + ChatManager.Instance().getRemoteMessage(focusMessage[0].messageUid, new GetOneRemoteMessageCallback() { + @Override + public void onSuccess(Message message) { + focusMessage[0] = message; + countDownLatch.countDown(); } + + @Override + public void onFail(int errorCode) { + countDownLatch.countDown(); + } + }); + } + + try { + countDownLatch.await(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + List oldMessages = new ArrayList<>(); + for (Message msg : oldMessageList) { + if (conversation.type == Conversation.ConversationType.SecretChat) { + BurnMessageInfo burnMessageInfo = ChatManager.Instance().getBurnMessageInfo(msg.messageId); + oldMessages.add(new UiMessage(msg, burnMessageInfo)); + } else { + oldMessages.add(new UiMessage(msg)); } } - Message message = ChatManager.Instance().getMessage(focusIndex); - List newMessageList = ChatManager.Instance().getMessages(conversation, focusIndex, false, count, withUser); + List newMessages = new ArrayList<>(); - if (newMessageList != null) { - for (Message msg : newMessageList) { - if (conversation.type == Conversation.ConversationType.SecretChat) { - BurnMessageInfo burnMessageInfo = ChatManager.Instance().getBurnMessageInfo(msg.messageId); - newMessages.add(new UiMessage(msg, burnMessageInfo)); - } else { - newMessages.add(new UiMessage(msg)); - } + for (Message msg : newMessageList) { + if (conversation.type == Conversation.ConversationType.SecretChat) { + BurnMessageInfo burnMessageInfo = ChatManager.Instance().getBurnMessageInfo(msg.messageId); + newMessages.add(new UiMessage(msg, burnMessageInfo)); + } else { + newMessages.add(new UiMessage(msg)); } } @@ -170,8 +220,8 @@ public MutableLiveData> loadAroundMessages(Conversation conversa tipMessage.messageId = Long.MAX_VALUE; messages.add(new UiMessage(tipMessage)); - if (message != null) { - messages.add(new UiMessage(message)); + if (focusMessage[0] != null) { + messages.add(new UiMessage(focusMessage[0])); } messages.addAll(newMessages); result.postValue(messages); diff --git a/uikit/src/main/java/cn/wildfire/chat/kit/conversationlist/viewholder/ConversationViewHolder.java b/uikit/src/main/java/cn/wildfire/chat/kit/conversationlist/viewholder/ConversationViewHolder.java index d7b1e4a01..a3c44be47 100644 --- a/uikit/src/main/java/cn/wildfire/chat/kit/conversationlist/viewholder/ConversationViewHolder.java +++ b/uikit/src/main/java/cn/wildfire/chat/kit/conversationlist/viewholder/ConversationViewHolder.java @@ -68,7 +68,7 @@ public ConversationViewHolder(Fragment fragment, RecyclerView.Adapter adapter, V this.itemView = itemView; this.adapter = adapter; bindViews(itemView); - conversationListViewModel = new ViewModelProvider(fragment.getActivity(), new ConversationListViewModelFactory(Arrays.asList(Conversation.ConversationType.Single, Conversation.ConversationType.Group, Conversation.ConversationType.SecretChat), Arrays.asList(0))) + conversationListViewModel = new ViewModelProvider(fragment.getActivity(), new ConversationListViewModelFactory(Arrays.asList(Conversation.ConversationType.Single, Conversation.ConversationType.Group, Conversation.ConversationType.SecretChat, Conversation.ConversationType.ChatRoom), Arrays.asList(0))) .get(ConversationListViewModel.class); conversationViewModel = ViewModelProviders.of(fragment).get(ConversationViewModel.class); }