Android 环信聊天历史记录搜索分组展示

最近开发IM,用到了环信,其中一个功能未聊天记录搜索功能,先上图

搜索聊天记录界面

1、好友和群聊搜索适合自己的服务器交互的不涉及环信

2、聊天记录搜索列表

  1. 聊天搜索结果列表是以会话列表分组的所以首先获取聊天的所有会话列表,环信提供了获取所有会话列表的方法
    protected List<EMConversation> loadConversationList() {
            // get all conversations
            Map<String, EMConversation> conversations = EMClient.getInstance().chatManager().getAllConversations();
    //        onHandleEMConversation(conversations);
    
            List<Pair<Long, EMConversation>> sortList = new ArrayList<Pair<Long, EMConversation>>();
            /**
             * lastMsgTime will change if there is new message during sorting
             * so use synchronized to make sure timestamp of last message won't change.
             */
            synchronized (conversations) {
                for (EMConversation conversation : conversations.values()) {
                    if (conversation.getAllMessages().size() != 0 && !conversation.getExtField().equals("toTop")) {
                        sortList.add(new Pair<Long, EMConversation>(conversation.getLastMessage().getMsgTime(), conversation));
                    }
                }
            }
            try {
                // Internal is TimSort algorithm, has bug
                sortConversationByLastChatTime(sortList);
            } catch (Exception e) {
                e.printStackTrace();
            }
            List<EMConversation> list = new ArrayList<EMConversation>();
         
            for (Pair<Long, EMConversation> sortItem : sortList) {
                list.add(sortItem.second);
            }
            return list;
        }

    按时间排序
     

    private void sortConversationByLastChatTime(List<Pair<Long, EMConversation>> conversationList) {
            Collections.sort(conversationList, new Comparator<Pair<Long, EMConversation>>() {
                @Override
                public int compare(final Pair<Long, EMConversation> con1, final Pair<Long, EMConversation> con2) {
                    if (con1.first.equals(con2.first)) {
                        return 0;
                    } else if (con2.first.longValue() > con1.first.longValue()) {
                        return 1;
                    } else {
                        return -1;
                    }
                }
            });
        }

    2.通过获取到的会话列表进行关键词搜索,把结果读取到内存中

     

    for (EMConversation conversation : conversationList) {
                List<EMMessage> list = conversation.searchMsgFromDB(mKeyWords, System.currentTimeMillis(), 100, null, EMSearchDirection.UP);
                Iterator<EMMessage> iterator = list.iterator();
                while (iterator.hasNext()) {
                    //对搜索结果二次筛选,因为搜索结果群聊中用户的名字包含关键词的也会搜出来,表情符在数据库里存的
    //是英文字符也会被搜出来,所以啊要二次筛选
                    EMMessage emMessage = iterator.next();
                    if (!((EMTextMessageBody) emMessage.getBody()).getMessage().contains(mKeyWords)) {
                        iterator.remove();
                    }
                }
                if (list.size() > 0) {
                    ChatHistoryEntity.ResBean.ListBean.HistoryListBean listBean = new ChatHistoryEntity.ResBean.ListBean.HistoryListBean();
                    listBean.setNum(list.size() + "");
                    if (list.size() == 1) {
                        listBean.setContent(((EMTextMessageBody) list.get(0).getBody()).getMessage());
                    }
                    listBean.setConversationId(conversation.conversationId());
                    if (conversation.getType() == EMConversationType.GroupChat) {//保存单聊的聊天结果
                        String groupName = EMClient.getInstance().groupManager().getGroup(conversation.conversationId()).getGroupName();
                        listBean.setAvatar(conversation.getLastMessage().getStringAttribute(Constant.Em_SENT_GROUP_AVATAR, ""));
                        listBean.setNickname(groupName);
                        listBean.setChattype("2");
                        mChatGroups.add(listBean);
                    } else {//保存群聊的聊天结果
                        listBean.setAvatar(conversation.getLastMessage().getStringAttribute(Constant.Em_RECEIVE_USER_AVATAR, ""));
                        listBean.setNickname(conversation.getLastMessage().getStringAttribute(Constant.Em_RECEIVE_USER_NICKNAME, ""));
                        listBean.setChattype("1");
                        mChatFriends.add(listBean);
                    }
                    mStringListMap.put(conversation.conversationId(), list);
                }
            }

    现在读取到内存中的是每个会话列表和关键词相关的Message。在组装成自己需要的数据列表。
    3.聊天搜索结果二级界面数据,通过EventBus传递到下级界面
     

    List<EMMessage> emMessages = mStringListMap.get(mChatBeanAlls.get(position).getConversationId());
                                        EventBus.getDefault().postSticky(emMessages);
发布了13 篇原创文章 · 获赞 6 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/old_land/article/details/102921058
今日推荐