TabLiveComponent.ets
7.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import { ContentDetailDTO, LiveRoomItemBean } from 'wdBean/Index'
import { CustomPullToRefresh, EmptyComponent, ErrorComponent, WDViewDefaultType } from 'wdComponent/Index'
import { TabLiveItemComponent } from './TabLiveItemComponent'
import CustomRefreshLoadLayout from 'wdComponent/src/main/ets/components/page/CustomRefreshLoadLayout'
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 { LazyDataSource, StringUtils } from 'wdKit/Index'
import { LiveViewModel } from '../../viewModel/LiveViewModel'
import { Logger } from 'wdKit'
import LoadMoreLayout from 'wdComponent/src/main/ets/components/page/LoadMoreLayout'
import { PeopleShipNoMoreData } from 'wdComponent/src/main/ets/components/reusable/PeopleShipNoMoreData'
const TAG: string = 'TabLiveComponent';
/**
* 非沉浸式直播间组件
*/
@Component
export struct TabLiveComponent {
liveViewModel: LiveViewModel = new LiveViewModel()
@State liveList: LazyDataSource<LiveRoomItemBean> = new LazyDataSource();
@Consume @Watch('updateData') contentDetailData: ContentDetailDTO
@State private pageModel: PageModel = new PageModel()
private scroller: Scroller = new Scroller()
@Prop @Watch("lastInputedCommentChanged") lastInputedComment: LiveRoomItemBean
aboutToAppear(): void {
this.getLiveList()
}
lastInputedCommentChanged(info: string) {
Logger.debug(TAG, "1显示评论》》》: " + JSON.stringify(this.lastInputedComment))
this.liveList.push(this.liveViewModel.deepCopyLiveRoomItem(this.lastInputedComment))
this.pageModel.viewType = ViewType.LOADED;
}
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_NoContent1 })
} else {
CustomPullToRefresh({
alldata: this.liveList,
scroller: this.scroller,
hasMore: false,
customList: () => {
this.ListLayout()
},
onRefresh: (resolve) => {
this.pageModel.currentPage = 1
this.getLiveList(resolve)
},
})
}
}
.alignContent(Alignment.Top)
.backgroundColor('#F5F5F5')
.height('100%')
.width('100%')
}
@Builder
ListLayout() {
List({ scroller: this.scroller }) {
// 下拉刷新
LazyForEach(this.liveList, (item: LiveRoomItemBean, index: number) => {
ListItem() {
TabLiveItemComponent({ item: item })
}
},
(item: LiveRoomItemBean, contentIndex: number) => JSON.stringify(item)
)
// 加载更多
ListItem() {
if (this.pageModel.hasMore && this.liveList && this.liveList.totalCount() > 0) {
LoadMoreLayout({ isVisible: this.pageModel.hasMore })
} else if (!this.pageModel.hasMore && !this.pageModel.isLoading) {
PeopleShipNoMoreData()
}
}
}
.edgeEffect(EdgeEffect.None)
.scrollBar(BarState.Off)
.onReachEnd(() => {
Logger.debug(TAG, "触底了");
if (!this.pageModel.isLoading && this.pageModel.hasMore) {
//加载分页数据
this.getLiveList()
}
})
}
@Builder
LoadingLayout() {
CustomRefreshLoadLayout({
refreshBean: new RefreshLayoutBean(true,
$r('app.media.ic_pull_up_load'), $r('app.string.pull_up_load_text'), this.pageModel.pullDownRefreshHeight)
})
}
async getLiveList(resolve?: (value: string | PromiseLike<string>) => void) {
if (!this.contentDetailData || !this.contentDetailData.newsId) {
// 参数不够,直接拦截接口
return
}
if (this.pageModel.isLoading) {
if (resolve) {
resolve('已更新至最新')
}
return
}
this.pageModel.isLoading = true
this.pageModel.currentPage = 1
this.liveViewModel.getLiveList(
this.pageModel.currentPage,
this.contentDetailData?.liveInfo?.mlive?.mliveId,
this.contentDetailData?.newsId + '',
20)
.then(
(data) => {
this.pageModel.isLoading = false
if (resolve) {
if (this.pageModel.currentPage == 1) {
resolve('已更新至最新')
} else {
resolve('')
}
}
if(this.pageModel.currentPage ===1){
this.liveList.clear()
}
Logger.debug(TAG, '直播间数据:' + `${JSON.stringify(data)}`)
if (data.barrageResponses && data.barrageResponses.length > 0) {
/**
* 在直播聊天添加一条新内容逻辑:
判断 oldNewsId:迁移id为空 且 直播状态不是预约:"wait"
消息内容:
1.头像固定:APP默认头像
2.名称固定:人民日报主持人
3.内容:详情接口的简介,newIntroduction
*/
this.pageModel.viewType = ViewType.LOADED;
this.liveList.push(...data.barrageResponses)
if (data.barrageResponses.length === this.pageModel.pageSize) {
this.pageModel.currentPage++;
this.pageModel.hasMore = true;
} else {
this.pageModel.hasMore = false;
if (StringUtils.isEmpty(this.contentDetailData.oldNewsId)
&& this.contentDetailData
&& this.contentDetailData.liveInfo.liveState != 'wait') {
this.updateLiveListData()
}
}
} else {
if (StringUtils.isEmpty(this.contentDetailData.oldNewsId)
&& this.contentDetailData
&& this.contentDetailData.liveInfo.liveState != 'wait') {
this.pageModel.viewType = ViewType.LOADED;
this.updateLiveListData()
this.pageModel.hasMore = false;
} else {
this.pageModel.viewType = ViewType.EMPTY;
}
}
},
() => {
Logger.debug(TAG, `Error`)
this.pageModel.viewType = ViewType.ERROR;
})
}
updateLiveListData() {
let liveRoomItemBeanTemp: LiveRoomItemBean = {} as LiveRoomItemBean
liveRoomItemBeanTemp.text = this.contentDetailData.newIntroduction
liveRoomItemBeanTemp.senderUserName = '人民日报主持人'
liveRoomItemBeanTemp.pictureUrls = []
liveRoomItemBeanTemp.pictureUrls.push(this.contentDetailData?.fullColumnImgUrls[0]?.url)
liveRoomItemBeanTemp.dataType = 'ZH_TEXT_AND_IMAGE_MSG'
let temp = this.contentDetailData?.fullColumnImgUrls[0]
if (temp) {
liveRoomItemBeanTemp.pictureResolutions = []
liveRoomItemBeanTemp.pictureResolutions.push(`${temp.height}*${temp.weight}`)
}
this.liveList.push(liveRoomItemBeanTemp)
}
aboutToDisappear(): void {
}
updateData() {
if (this.liveList.totalCount() > 0) {
// TODO 拦截刷新?根据业务斟酌下
return
}
this.getLiveList()
}
}