zhangbo1_wd

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

1 import { CommonConstants, ViewType } from 'wdConstant'; 1 import { CommonConstants, ViewType } from 'wdConstant';
2 -import { CollectionUtils, Logger } from 'wdKit'; 2 +import { CollectionUtils, DateTimeUtils, Logger } from 'wdKit';
3 import { CompDTO } from '../../repository/bean/CompDTO'; 3 import { CompDTO } from '../../repository/bean/CompDTO';
4 import PageViewModel from '../../viewmodel/PageViewModel'; 4 import PageViewModel from '../../viewmodel/PageViewModel';
5 import { EmptyComponent } from '../view/EmptyComponent'; 5 import { EmptyComponent } from '../view/EmptyComponent';
@@ -65,7 +65,9 @@ export struct PageComponent { @@ -65,7 +65,9 @@ export struct PageComponent {
65 CompParser({ compDTO: compDTO, compIndex: compIndex }); 65 CompParser({ compDTO: compDTO, compIndex: compIndex });
66 } 66 }
67 } 67 }
68 - }) 68 + },
  69 + (compDTO: CompDTO, compIndex: number) => compDTO.id + compIndex.toString() + this.pageModel.timestamp
  70 + )
69 71
70 // 加载更多 72 // 加载更多
71 ListItem() { 73 ListItem() {
@@ -136,6 +138,11 @@ export struct PageComponent { @@ -136,6 +138,11 @@ export struct PageComponent {
136 } else { 138 } else {
137 this.pageModel.hasMore = false; 139 this.pageModel.hasMore = false;
138 } 140 }
  141 + PageViewModel.getInteractData(pageDto.compList).then((data: CompDTO[]) => {
  142 + // 刷新,替换所有数据
  143 + this.pageModel.compList.replaceAll(...data)
  144 + this.pageModel.timestamp = DateTimeUtils.getCurrentTimeMillis().toString()
  145 + })
139 } else { 146 } else {
140 Logger.debug(TAG, 'aboutToAppear, data response page ' + this.pageId + ', comp list is empty.'); 147 Logger.debug(TAG, 'aboutToAppear, data response page ' + this.pageId + ', comp list is empty.');
141 this.pageModel.viewType = ViewType.EMPTY; 148 this.pageModel.viewType = ViewType.EMPTY;
@@ -35,6 +35,8 @@ export struct BigPicCardComponent { @@ -35,6 +35,8 @@ export struct BigPicCardComponent {
35 Column() { 35 Column() {
36 36
37 Column() { 37 Column() {
  38 + // TODO 测试代码
  39 + Text("likeNum " + this.contentDTO?.interactData?.likeNum)
38 //新闻标题 40 //新闻标题
39 Text(this.compDTO.operDataList[0].newsTitle) 41 Text(this.compDTO.operDataList[0].newsTitle)
40 .fontSize(17) 42 .fontSize(17)
1 /** 1 /**
2 * 批查接口查询互动相关数据,返回数据bean 2 * 批查接口查询互动相关数据,返回数据bean
3 */ 3 */
4 -export interface InteractDataDTO { 4 +export class InteractDataDTO {
5 collectNum: number; 5 collectNum: number;
6 commentNum: number; 6 commentNum: number;
7 contentId: string; 7 contentId: string;
@@ -4,6 +4,8 @@ import { RefreshConstants as Const, RefreshState } from './RefreshConstants'; @@ -4,6 +4,8 @@ import { RefreshConstants as Const, RefreshState } from './RefreshConstants';
4 import PageViewModel from '../viewmodel/PageViewModel'; 4 import PageViewModel from '../viewmodel/PageViewModel';
5 import { PageDTO } from '../repository/bean/PageDTO'; 5 import { PageDTO } from '../repository/bean/PageDTO';
6 import { touchMoveLoadMore, touchUpLoadMore } from './PullUpLoadMore'; 6 import { touchMoveLoadMore, touchUpLoadMore } from './PullUpLoadMore';
  7 +import { CompDTO } from '../repository/bean/CompDTO';
  8 +import { DateTimeUtils } from 'wdKit/src/main/ets/utils/DateTimeUtils';
7 9
8 export function listTouchEvent(pageModel: PageModel, event: TouchEvent) { 10 export function listTouchEvent(pageModel: PageModel, event: TouchEvent) {
9 switch (event.type) { 11 switch (event.type) {
@@ -73,6 +75,7 @@ export function touchUpPullRefresh(pageModel: PageModel) { @@ -73,6 +75,7 @@ export function touchUpPullRefresh(pageModel: PageModel) {
73 75
74 PageViewModel.getPageData(self) 76 PageViewModel.getPageData(self)
75 .then((data: PageDTO) => { 77 .then((data: PageDTO) => {
  78 + self.timestamp = DateTimeUtils.getCurrentTimeMillis().toString()
76 if (data == null || data.compList == null || data.compList.length == 0) { 79 if (data == null || data.compList == null || data.compList.length == 0) {
77 self.hasMore = false; 80 self.hasMore = false;
78 } else { 81 } else {
@@ -84,6 +87,11 @@ export function touchUpPullRefresh(pageModel: PageModel) { @@ -84,6 +87,11 @@ export function touchUpPullRefresh(pageModel: PageModel) {
84 } 87 }
85 // 刷新,替换所有数据 88 // 刷新,替换所有数据
86 self.compList.replaceAll(...data.compList) 89 self.compList.replaceAll(...data.compList)
  90 + PageViewModel.getInteractData(data.compList).then((data: CompDTO[]) => {
  91 + // 刷新,替换所有数据
  92 + self.compList.replaceAll(...data)
  93 + self.timestamp = DateTimeUtils.getCurrentTimeMillis().toString()
  94 + })
87 } 95 }
88 closeRefresh(self, true); 96 closeRefresh(self, true);
89 }).catch((err: string | Resource) => { 97 }).catch((err: string | Resource) => {
@@ -3,6 +3,8 @@ import PageModel from '../viewmodel/PageModel'; @@ -3,6 +3,8 @@ import PageModel from '../viewmodel/PageModel';
3 import { RefreshConstants as Const } from './RefreshConstants'; 3 import { RefreshConstants as Const } from './RefreshConstants';
4 import PageViewModel from '../viewmodel/PageViewModel'; 4 import PageViewModel from '../viewmodel/PageViewModel';
5 import { PageDTO } from '../repository/bean/PageDTO'; 5 import { PageDTO } from '../repository/bean/PageDTO';
  6 +import { CompDTO } from '../repository/bean/CompDTO';
  7 +import { DateTimeUtils } from 'wdKit';
6 8
7 export function touchMoveLoadMore(model: PageModel, event: TouchEvent) { 9 export function touchMoveLoadMore(model: PageModel, event: TouchEvent) {
8 // list size +1 10 // list size +1
@@ -38,7 +40,13 @@ export function touchUpLoadMore(model: PageModel) { @@ -38,7 +40,13 @@ export function touchUpLoadMore(model: PageModel) {
38 } else { 40 } else {
39 self.hasMore = false; 41 self.hasMore = false;
40 } 42 }
  43 + let sizeBefore = self.compList.size();
41 self.compList.push(...data.compList) 44 self.compList.push(...data.compList)
  45 + PageViewModel.getInteractData(data.compList).then((data: CompDTO[]) => {
  46 + // 刷新,替换所有数据
  47 + self.compList.updateItems(sizeBefore, data)
  48 + self.timestamp = DateTimeUtils.getCurrentTimeMillis().toString()
  49 + })
42 } 50 }
43 }).catch((err: string | Resource) => { 51 }).catch((err: string | Resource) => {
44 promptAction.showToast({ message: err }); 52 promptAction.showToast({ message: err });
@@ -34,4 +34,6 @@ export default class PageModel { @@ -34,4 +34,6 @@ export default class PageModel {
34 isPullRefreshOperation = false; 34 isPullRefreshOperation = false;
35 isLoading: boolean = false; 35 isLoading: boolean = false;
36 isCanLoadMore: boolean = false; 36 isCanLoadMore: boolean = false;
  37 + // keyGenerator相关字符串,用于刷新list布局
  38 + timestamp: String = '1';
37 } 39 }
@@ -147,14 +147,6 @@ export class PageViewModel extends BaseViewModel { @@ -147,14 +147,6 @@ export class PageViewModel extends BaseViewModel {
147 return; 147 return;
148 } 148 }
149 success(resDTO.data); 149 success(resDTO.data);
150 - // TODO 打开同步请求互动数据,待优化为异步加载  
151 - if (CollectionUtils.isEmpty(resDTO.data.compList)) {  
152 - success(resDTO.data);  
153 - } else {  
154 - this.getInteractData(resDTO.data.compList).then(() => {  
155 - success(resDTO.data);  
156 - })  
157 - }  
158 }) 150 })
159 .catch((err: Error) => { 151 .catch((err: Error) => {
160 Logger.error(TAG, `getPageData catch, error.name : ${err.name}, error.message:${err.message}`); 152 Logger.error(TAG, `getPageData catch, error.name : ${err.name}, error.message:${err.message}`);
@@ -249,7 +241,8 @@ export class PageViewModel extends BaseViewModel { @@ -249,7 +241,8 @@ export class PageViewModel extends BaseViewModel {
249 } 241 }
250 if (id == content.objectId) { 242 if (id == content.objectId) {
251 content.interactData = interactData; 243 content.interactData = interactData;
252 - content.interactData.likeNum = 109; 244 + // TODO 测试代码,待删除
  245 + // content.interactData.likeNum = Math.floor(Math.random() * Math.floor(999));;
253 break outer; 246 break outer;
254 } 247 }
255 } 248 }
@@ -254,4 +254,10 @@ export class LazyDataSource<T> extends BasicDataSource<T> { @@ -254,4 +254,10 @@ export class LazyDataSource<T> extends BasicDataSource<T> {
254 sort(comparator: (firstValue: T, secondValue: T) => number): void { 254 sort(comparator: (firstValue: T, secondValue: T) => number): void {
255 this.dataArray.sort(comparator) 255 this.dataArray.sort(comparator)
256 } 256 }
  257 +
  258 + public updateItems(start: number, data: T[]): void {
  259 + // 从数组中的start位置开始删除dataArray.length个元素,并在同一位置插入((1个或多个))新元素。
  260 + this.dataArray.splice(start, this.dataArray.length, ...data);
  261 + this.notifyDataReload()
  262 + }
257 } 263 }