## 聊天室组件

 本组件依赖于云直播sdk，请先对接云直播sdk

### 版本历史

| 版本号 | 更新时间  | 更新描述                   |
| ------ | --------- | -------------------------- |
| 1.2.5  | 2023.11.3 | 组件上线，依赖云直播4.14.0 |
| 1.2.6  | 2023.12.1 | 升级依赖Glide4.x版本       |
| 1.2.7  | 2024.2.2  | UI优化                     |



### 组件引入

注意：所有使用互动组件必须要引入basemodule库，最新版本见 [互动组件对接文档](互动组件/互动组件对接文档)

项目根目录build.gradle引入：

```groovy
 repositories {
     maven { url 'http://nexus-app.bokecc.com/repository/sdk-group/' }
 }
```


module的build.gradle引入：

```java
 dependencies {
     api ('com.bokecc:basemodule:x.x.x')
     api ('com.bokecc.module:chatroommodule:x.x.x')
     api ('com.bokecc:dwlivesdk:x.x.x')
 }
```

Application的onCreate方法中，调用`DWLiveEngine.init`初始化dwlivesdk后，添加代码：

```java
ChatRoomInteractSDK.getInstance().init(app);
```



### 对接说明

1. 确认使用CCChatView组件之前，云直播sdk已经初始化并已经登录直播间。

2. 创建CCChatView

   ```
   //1. 横屏样式
   ccChatView = new CCChatView(this);
   //1. 竖屏样式创建
   ccChatView = new CCChatView(this, ChatBaseLayout.STYLE_DARK);
   //2. 添加view到视图树容器
   ll.addView(ccChatView);
   //3. 初始化ccChatView
   String accountId = "";
           String userId = "";
           String roomId = "";
           String nickName = "";
           String avatar = "";
           String sessionId = "";
           try {
               //从登录信息中获取（上个界面传入）
               accountId = getIntent().getStringExtra("userId");
               userId = DWLiveCoreHandler.getInstance().getViewer().getId();
               roomId = getIntent().getStringExtra("roomId");
               nickName = DWLiveCoreHandler.getInstance().getViewer().getName();
               avatar = DWLiveCoreHandler.getInstance().getViewer().getAvatar();
               sessionId = DWLiveCoreHandler.getInstance().getViewer().getKey();
           } catch (Exception e) {
               e.printStackTrace();
           }
   
           //云直播大班课模板默认 2001，云直播研讨课模板 3001
           String business = "2001";
           if (DWLiveCoreHandler.getInstance().getLiveSceneMode() == LiveDigestBean.SCENE_SEMINAR) {
               business = "3001";
           }
   
           int isSeminar = DWLiveCoreHandler.getInstance().getLiveSceneMode();
   
           ccChatView.initialize(new ChatConfig()
                   .setAccountId(accountId)
                   .setRoomId(roomId)
                   .setUserId(userId)
                   .setBusinessLine(business)
                   .setRole(RoleConstrant.student)
                   .setNickName(nickName)
                   .setAvatar(avatar)
                   .setSessionId(sessionId)
                   .setIsSeminar(isSeminar)
                   .setChatClient(DWLive.getInstance())
           );
           //添加礼物图标到聊天室组件上
           int giftSwitch = DWLiveCoreHandler.getInstance().getInteractSwitch(DWLiveCoreHandler.INTERACT_GIFT);
           if (giftSwitch > 0) {
               List<CCInteractionViewBean> ccInteractionViewBeanList = new ArrayList<>();
               CCInteractionViewBean ccInteractionViewBean = new CCInteractionViewBean("", R.drawable.cc_chat_icon_gift, R.drawable.cc_chat_icon_gift, false);
               ccInteractionViewBean.setOnClickListener(new CCInteractionViewBean.OnClickListener() {
                   @Override
                   public void onClick(CCInteractionViewBean ccInteractionViewBean) {
                       if (giftListView != null) {
                           giftListView.setVisibility(VISIBLE);
                       }
                   }
               });
               ccInteractionViewBeanList.add(ccInteractionViewBean);
               ccChatView.addInteractionView(ccInteractionViewBeanList);
           }
           List<CCInteractionViewBean> moreViewList = new ArrayList<>();
   
           //添加图标到聊天室更多按钮: 礼物、红包记录、投票记录、问卷
           if (giftSwitch > 0) {
               CCInteractionViewBean ccInteractionViewBean = new CCInteractionViewBean(" 礼物", R.drawable.icon_interact_gift_enter, R.drawable.icon_interact_gift_enter, false);
               ccInteractionViewBean.setOnClickListener(new CCInteractionViewBean.OnClickListener() {
                   @Override
                   public void onClick(CCInteractionViewBean ccInteractionViewBean) {
                       if (giftListView != null) {
                           giftListView.setVisibility(VISIBLE);
                       }
                   }
               });
               moreViewList.add(ccInteractionViewBean);
           }
           if (DWLiveCoreHandler.getInstance().getInteractSwitch(DWLiveCoreHandler.INTERACT_RED_PACKET) > 0) {
               CCInteractionViewBean ccInteractionViewBean = new CCInteractionViewBean("红包记录", R.drawable.icon_interact_red_enter, R.drawable.icon_interact_red_enter, false);
               ccInteractionViewBean.setOnClickListener(new CCInteractionViewBean.OnClickListener() {
                   @Override
                   public void onClick(CCInteractionViewBean ccInteractionViewBean) {
                       showRed();
                   }
               });
               moreViewList.add(ccInteractionViewBean);
           }
           if (DWLiveCoreHandler.getInstance().getInteractSwitch(DWLiveCoreHandler.INTERACT_VOTE) > 0) {
               CCInteractionViewBean ccInteractionViewBean = new CCInteractionViewBean("投票记录", R.drawable.icon_interact_vote_enter, R.drawable.icon_interact_vote_enter, false);
               ccInteractionViewBean.setOnClickListener(new CCInteractionViewBean.OnClickListener() {
                   @Override
                   public void onClick(CCInteractionViewBean ccInteractionViewBean) {
                       showVote();
                   }
               });
               moreViewList.add(ccInteractionViewBean);
           }
           if (DWLiveCoreHandler.getInstance().getInteractSwitch(DWLiveCoreHandler.INTERACT_QUESTIONNAIRE) > 0) {
               CCInteractionViewBean ccInteractionViewBean = new CCInteractionViewBean("问卷", R.drawable.icon_interact_questionnaire_enter, R.drawable.icon_interact_questionnaire_enter, false);
               ccInteractionViewBean.setOnClickListener(new CCInteractionViewBean.OnClickListener() {
                   @Override
                   public void onClick(CCInteractionViewBean ccInteractionViewBean) {
                       showQuestionnaire(false);
                   }
               });
               moreViewList.add(ccInteractionViewBean);
           }
           ccChatView.setInteractionDialogData(moreViewList);
   
           ccChatView.setEmojiClickListener(new IEmojiLayoutListener() {
               @Override
               public void change(boolean b) {
                   isChatEmojiShow = b;
                   if (b) {
                       hideRightFloatButtons();
                   } else {
                       showRightFloatButtons();
                   }
               }
   
           });
           ChatRoomManagerImpl.getInstance().setListener(new IChatRoomListener() {
               @Override
               public void onMessage(String event, String message) {
                   //横屏显示弹幕
                   //公聊事件为chat_message
                   if (!event.equals("chat_message")||getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE){
                       return;
                   }
                   try{
                       boolean flag = false;
                       JSONObject jsonObject = new JSONObject(message);
                       ChatMessage msg = new ChatMessage(jsonObject,true);
                       if (ChatImageUtils.isImgChatMessage(msg.getMessage())) {
                           return;
                       }
                       if (DWLive.getInstance()!=null
                               &&DWLive.getInstance().getViewer()!=null
                               &&!TextUtils.isEmpty(DWLive.getInstance().getViewer().getId())
                               &&!TextUtils.isEmpty(msg.getUserId())
                               &&DWLive.getInstance().getViewer().getId().equals(msg.getUserId())){
                           flag = true;
                       }
   
                       if (flag||"0".equals(msg.getStatus())) {
                           mLiveRoomLayout.addDanmaku(msg.getMessage(),msg.getUserRole());
                       }
                   }catch (Exception e){
                        e.printStackTrace();
                   }
               }
   
               @Override
               public void onInitSuccess() {
   
               }
   
               @Override
               public void onConnectFailure() {
   
               }
   
               @Override
               public void onConnect() {
   
               }
           });
   ```

3. 退出房间销毁CCChatView

   ```
   if (ccChatView != null) {
        ccChatView.onDestroy();
   }
   ```



详细代码请参考云直播demo。
