TabChatComponent.ets 4.68 KB
import { ContentDetailDTO, LiveDetailsBean, LiveRoomItemBean } from 'wdBean/Index'
import { EmptyComponent, ErrorComponent, WDViewDefaultType } from 'wdComponent/Index'
import CustomRefreshLoadLayout from 'wdComponent/src/main/ets/components/page/CustomRefreshLoadLayout'
import RefreshLayout from 'wdComponent/src/main/ets/components/page/RefreshLayout'
import { RefreshLayoutBean } from 'wdComponent/src/main/ets/components/page/RefreshLayoutBean'
import PageModel from 'wdComponent/src/main/ets/viewmodel/PageModel'
import { ViewType } from 'wdConstant/Index'
import { LiveViewModel } from '../../viewModel/LiveViewModel'
import { TabChatItemComponent } from './TabChatItemComponent'
import { EmitterEventId, EmitterUtils, Logger } from 'wdKit';
import { publishCommentModel } from 'wdComponent/src/main/ets/components/comment/model/PublishCommentModel'

const TAG: string = 'TabChatComponent';

/**
 *
 * 非沉浸式---大家聊
 */
@Component
export struct TabChatComponent {
  @State private pageModel: PageModel = new PageModel()
  liveViewModel: LiveViewModel = new LiveViewModel()
  @State liveChatList: Array<LiveRoomItemBean> = []
  @Consume contentDetailData: ContentDetailDTO
  @Consume publishCommentModel: publishCommentModel

  aboutToAppear(): void {
    this.getLiveChatList()

    //注册通知
    EmitterUtils.receiveEvent(EmitterEventId.COMMENT_PUBLISH, async (targetId?: string) => {
      if (targetId) {
        if (targetId == this.publishCommentModel.targetId) {
          const info = {
            senderUserAvatarUrl: this.publishCommentModel.lastCommentModel.fromUserHeader,
            senderUserName: this.publishCommentModel.lastCommentModel.fromUserName,
            text: this.publishCommentModel.lastCommentModel.commentContent,
          } as LiveRoomItemBean

          this.liveChatList.push(info)
          this.pageModel.viewType = ViewType.LOADED;
          // this.scroller.scrollEdge(Edge.Bottom)
          console.log(TAG, '发布评论:', JSON.stringify(this.publishCommentModel.lastCommentModel))
        }
      }
    })
  }

  build() {
    Stack() {
      if (this.pageModel.viewType == ViewType.LOADING) {
        this.LoadingLayout()
      } else if (this.pageModel.viewType == ViewType.ERROR) {
        ErrorComponent()
      } else if (this.pageModel.viewType == ViewType.EMPTY) {
        EmptyComponent({ emptyType: WDViewDefaultType.WDViewDefaultType_NoComment1 })
      } else {
        this.ListLayout()
      }
    }
    .align(Alignment.Top)
    .backgroundColor('#F5F5F5')
    .height('100%')
    .width('100%')
  }

  @Builder
  ListLayout() {
    List() {
      ListItem() {
        // 下拉刷新 TODO 待对接新的下拉刷新组件
        // RefreshLayout({
        //   refreshBean: new RefreshLayoutBean(this.pageModel.isVisiblePullDown, this.pageModel.pullDownRefreshImage,
        //     this.pageModel.pullDownRefreshText, this.pageModel.pullDownRefreshHeight)
        // })
      }

      ForEach(this.liveChatList, (item: LiveRoomItemBean) => {
        ListItem() {
          TabChatItemComponent({ item: item })
        }
      })
      // 加载更多
      // ListItem() {
      //   if (this.pageModel.hasMore) {
      //     LoadMoreLayout({
      //       refreshBean: new RefreshLayoutBean(this.pageModel.isVisiblePullUpLoad, this.pageModel.pullUpLoadImage,
      //         this.pageModel.pullUpLoadText, this.pageModel.pullUpLoadHeight)
      //     })
      //   } else {
      //     ListHasNoMoreDataUI()
      //   }
      // }
    }
  }

  @Builder
  LoadingLayout() {
    CustomRefreshLoadLayout({
      refreshBean: new RefreshLayoutBean(true,
        $r('app.media.ic_pull_up_load'), $r('app.string.pull_up_load_text'), this.pageModel.pullDownRefreshHeight)
    })
  }

  getLiveChatList() {
    this.pageModel.currentPage = 1
    this.liveViewModel.getLiveChatList(
      this.pageModel.currentPage,
      this.contentDetailData?.liveInfo?.mlive?.mliveId,
     String( this.contentDetailData.newsId),
      20,)
      .then(
        (data) => {
          Logger.debug(TAG, `${JSON.stringify(data)}`)
          if (data.barrageResponses && data.barrageResponses.length > 0) {
            this.pageModel.viewType = ViewType.LOADED;
            this.liveChatList.push(...data.barrageResponses)
            if (data.barrageResponses.length === this.pageModel.pageSize) {
              this.pageModel.currentPage++;
              this.pageModel.hasMore = true;
            } else {
              this.pageModel.hasMore = false;
            }
          } else {
            this.pageModel.viewType = ViewType.EMPTY;
          }
        },
        () => {
          Logger.debug(TAG, `error`)
          this.pageModel.viewType = ViewType.ERROR;
        })
  }

  aboutToDisappear(): void {
  }
}