4_3-带货场景-聊天室
1 Feature Overview
The chat room module includes features such as posting messages, liking, history records, and welcome messages. Compared to other modules, the chat room module is more complex and extensive in terms of UI, interaction, and functionality. Therefore, designing, building, and maintaining a chat room is a time-consuming and labor-intensive task. We recommend directly using the chat room module encapsulated by Polyv. This part of the code is fully open-source, supporting direct use and secondary development.
2 Usage Demo
In the livestream shopping scenario, the chat room is divided into the chat room on the live homepage PLVECLiveHomeFragment and the chat room on the playback homepage PLVECPalybackHomeFragment.
The chat room follows the MVP pattern, making method calls straightforward and clear. In View, you can freely select chat room events of interest to listen to and call Presenter related methods to operate the chat room.
Taking the playback chat room listening to announcement events as an example, the code is as follows:
// PLVECPalybackHomeFragment.java
private IPLVChatroomContract.IChatroomView chatroomView = new PLVAbsChatroomView() {
// 实现了接受公告事件的接口方法,监听公告事件
@Override
public void onBulletinEvent(@NonNull PolyvBulletinVO bulletinVO) {
super.onBulletinEvent(bulletinVO);
// 在acceptBulletinMessage方法中处理监听接受到的公告事件,显示到界面当中
acceptBulletinMessage(bulletinVO);
}
// 实现了接受移除公告事件的方法,监听移除公告事件
@Override
public void onRemoveBulletinEvent() {
super.onRemoveBulletinEvent();
// 移除界面中的公告
removeBulletin();
}
};
3 Implementation Introduction
3.1 Playback - PLVECPalybackHomeFragment
The playback chat room in the livestream shopping scenario is used to display announcement updates. Communication between this class and the chat room mvp-presenter is achieved through the chat room mvp-view defined in this class. When the registerView method of the chat room mvp-presenter is called, the chat room mvp-presenter can notify the chat room mvp-view of updates, and the chat room mvp-view can also call methods of the chat room mvp-presenter. Example code for defining the chat room mvp-view:
// 聊天室presenter(定义在父类PLVECCommonHomeFragment当中)
protected IPLVChatroomContract.IChatroomPresenter chatroomPresenter;
// PLVECLiveHomeFragment.java
// 聊天室mvp-view
private IPLVChatroomContract.IChatroomView chatroomView = new PLVAbsChatroomView() {
// 聊天室需要监听事件的抽象方法实现...
}
// 注册mvp-view
@Override
protected void registerChatroomView() {
chatroomPresenter.registerView(chatroomView);
//设置信息索引,需在chatroomPresenter.registerView后设置
chatMessageAdapter.setMsgIndex(chatroomPresenter.getViewIndex(chatroomView));
}
3.2 Live - PLVECLiveHomeFragment
The live chat room in the livestream shopping scenario includes elements such as a chat message list, message input box, like layout, welcome messages, and announcements. The construction logic between mvp-view and mvp-presenter is similar to that of the playback scenario. The live chat room includes more chat room event use cases required for live streaming scenarios. For details, please refer to the chat room related code blocks in PLVECLiveHomeFragment of the demo.
