IM二期UI层的主要修改主要分为两大部分,一是逻辑层返回数据的处理,二是列表刷新的方式。在数据处理上,UI层封装了只在UI层用的MainMsgExt对象,实现了UI数据和业务数据的解耦;提供了一个MainMsg和MainMsgExt互相转换的工具类MainMsgTransferHelper;在列表刷新上,UI层采用了EventBus的异步接收数据并通过RxJAVA进行数据的操作和处理。在需要刷新列表时,摒弃了之前的notifydatachange的方式,做到了单条刷新。
MainMsg的扩展类MainMsgExt类
- MainMsgExt扩展类保存了MainMsg类的所有属性;
- MainMsgExt扩展类根据不同的消息类型,增加了所需要的其他属性;
- MainMsgExt扩展类实现了equals和hashCode方法,便于查找相同的对象。
@Override public boolean equals(Object o) {
if (o == null) {
return false;
}
if (!(o instanceof MainMsgExt)) {
return false;
}
MainMsgExt mainMsgExt = (MainMsgExt) o;
if (!TextUtils.isEmpty(msgChatID) && !TextUtils.isEmpty(mainMsgExt.getMsgChatID())) {
return msgChatID.equals(mainMsgExt.getMsgChatID());
} else if (!TextUtils.isEmpty(msgType) && !TextUtils.isEmpty(mainMsgExt.getMsgType())) {
return msgType.equals(mainMsgExt.getMsgType());
} else {
return false;
}
}
@Override public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((msgChatID == null) ? 0 : msgChatID.hashCode());
result = prime * result + ((msgType == null) ? 0 : msgType.hashCode());
return result;
}
对象转换的工具类MainMsgTransferHelper
- 此类提供逻辑层MainMsg对象和UI层对象MainMsgExt的互相转换的方法;
- 使用MainMsg的MainMsgType属性对对象的转换进行方法的区分;
- 扩展类通过CoreDataControl.getInstance().getResForMainMsg(mainMsg)方法得到UI所需要的数据。
MessageList的改造
- 重新实现了MessageListNewAdapter和单聊多聊群等对应的ViewHolder
1.MessageListNewAdapter提供了指定item刷新的各种方法;
2.在viewHolder的父类MessageBaseRecyclerHolder中,抽取了UI显示的通用代码。 - 消息的接收和处理采用EventBus+RxJAVA
1.异步线程接收消息,RxJAVA处理完通过操作符发送到UI进行更新;
2.使用debounce操作符过滤短时间内接收到多次的列表返回,只处理最后一次。
网友评论