zhangbo1_wd

首页请求互动数据,优化为异步加载

import { CommonConstants, ViewType } from 'wdConstant';
import { CollectionUtils, Logger } from 'wdKit';
import { CollectionUtils, DateTimeUtils, Logger } from 'wdKit';
import { CompDTO } from '../../repository/bean/CompDTO';
import PageViewModel from '../../viewmodel/PageViewModel';
import { EmptyComponent } from '../view/EmptyComponent';
... ... @@ -65,7 +65,9 @@ export struct PageComponent {
CompParser({ compDTO: compDTO, compIndex: compIndex });
}
}
})
},
(compDTO: CompDTO, compIndex: number) => compDTO.id + compIndex.toString() + this.pageModel.timestamp
)
// 加载更多
ListItem() {
... ... @@ -136,6 +138,11 @@ export struct PageComponent {
} else {
this.pageModel.hasMore = false;
}
PageViewModel.getInteractData(pageDto.compList).then((data: CompDTO[]) => {
// 刷新,替换所有数据
this.pageModel.compList.replaceAll(...data)
this.pageModel.timestamp = DateTimeUtils.getCurrentTimeMillis().toString()
})
} else {
Logger.debug(TAG, 'aboutToAppear, data response page ' + this.pageId + ', comp list is empty.');
this.pageModel.viewType = ViewType.EMPTY;
... ...
... ... @@ -35,6 +35,8 @@ export struct BigPicCardComponent {
Column() {
Column() {
// TODO 测试代码
Text("likeNum " + this.contentDTO?.interactData?.likeNum)
//新闻标题
Text(this.compDTO.operDataList[0].newsTitle)
.fontSize(17)
... ...
/**
* 批查接口查询互动相关数据,返回数据bean
*/
export interface InteractDataDTO {
export class InteractDataDTO {
collectNum: number;
commentNum: number;
contentId: string;
... ...
... ... @@ -4,6 +4,8 @@ import { RefreshConstants as Const, RefreshState } from './RefreshConstants';
import PageViewModel from '../viewmodel/PageViewModel';
import { PageDTO } from '../repository/bean/PageDTO';
import { touchMoveLoadMore, touchUpLoadMore } from './PullUpLoadMore';
import { CompDTO } from '../repository/bean/CompDTO';
import { DateTimeUtils } from 'wdKit/src/main/ets/utils/DateTimeUtils';
export function listTouchEvent(pageModel: PageModel, event: TouchEvent) {
switch (event.type) {
... ... @@ -73,6 +75,7 @@ export function touchUpPullRefresh(pageModel: PageModel) {
PageViewModel.getPageData(self)
.then((data: PageDTO) => {
self.timestamp = DateTimeUtils.getCurrentTimeMillis().toString()
if (data == null || data.compList == null || data.compList.length == 0) {
self.hasMore = false;
} else {
... ... @@ -84,6 +87,11 @@ export function touchUpPullRefresh(pageModel: PageModel) {
}
// 刷新,替换所有数据
self.compList.replaceAll(...data.compList)
PageViewModel.getInteractData(data.compList).then((data: CompDTO[]) => {
// 刷新,替换所有数据
self.compList.replaceAll(...data)
self.timestamp = DateTimeUtils.getCurrentTimeMillis().toString()
})
}
closeRefresh(self, true);
}).catch((err: string | Resource) => {
... ...
... ... @@ -3,6 +3,8 @@ import PageModel from '../viewmodel/PageModel';
import { RefreshConstants as Const } from './RefreshConstants';
import PageViewModel from '../viewmodel/PageViewModel';
import { PageDTO } from '../repository/bean/PageDTO';
import { CompDTO } from '../repository/bean/CompDTO';
import { DateTimeUtils } from 'wdKit';
export function touchMoveLoadMore(model: PageModel, event: TouchEvent) {
// list size +1
... ... @@ -38,7 +40,13 @@ export function touchUpLoadMore(model: PageModel) {
} else {
self.hasMore = false;
}
let sizeBefore = self.compList.size();
self.compList.push(...data.compList)
PageViewModel.getInteractData(data.compList).then((data: CompDTO[]) => {
// 刷新,替换所有数据
self.compList.updateItems(sizeBefore, data)
self.timestamp = DateTimeUtils.getCurrentTimeMillis().toString()
})
}
}).catch((err: string | Resource) => {
promptAction.showToast({ message: err });
... ...
... ... @@ -34,4 +34,6 @@ export default class PageModel {
isPullRefreshOperation = false;
isLoading: boolean = false;
isCanLoadMore: boolean = false;
// keyGenerator相关字符串,用于刷新list布局
timestamp: String = '1';
}
\ No newline at end of file
... ...
... ... @@ -147,14 +147,6 @@ export class PageViewModel extends BaseViewModel {
return;
}
success(resDTO.data);
// TODO 打开同步请求互动数据,待优化为异步加载
if (CollectionUtils.isEmpty(resDTO.data.compList)) {
success(resDTO.data);
} else {
this.getInteractData(resDTO.data.compList).then(() => {
success(resDTO.data);
})
}
})
.catch((err: Error) => {
Logger.error(TAG, `getPageData catch, error.name : ${err.name}, error.message:${err.message}`);
... ... @@ -249,7 +241,8 @@ export class PageViewModel extends BaseViewModel {
}
if (id == content.objectId) {
content.interactData = interactData;
content.interactData.likeNum = 109;
// TODO 测试代码,待删除
// content.interactData.likeNum = Math.floor(Math.random() * Math.floor(999));;
break outer;
}
}
... ...
... ... @@ -254,4 +254,10 @@ export class LazyDataSource<T> extends BasicDataSource<T> {
sort(comparator: (firstValue: T, secondValue: T) => number): void {
this.dataArray.sort(comparator)
}
public updateItems(start: number, data: T[]): void {
// 从数组中的start位置开始删除dataArray.length个元素,并在同一位置插入((1个或多个))新元素。
this.dataArray.splice(start, this.dataArray.length, ...data);
this.notifyDataReload()
}
}
\ No newline at end of file
... ...