PullUpLoadMore.ets 2.35 KB
import promptAction from '@ohos.promptAction';
import PageModel from '../viewmodel/PageModel';
import { RefreshConstants as Const } from './RefreshConstants';
import PageViewModel from '../viewmodel/PageViewModel';
import { PageDTO,CompDTO } from 'wdBean';
import { DateTimeUtils } from 'wdKit';

export function touchMoveLoadMore(model: PageModel, event: TouchEvent) {
  // list size +1
  if (model.endIndex === model.compList.totalCount() || model.endIndex === model.compList.totalCount() + 1) {
    model.offsetY = event.touches[0].y - model.downY;
    if (Math.abs(model.offsetY) > vp2px(model.pullUpLoadHeight) / 2) {
      model.isCanLoadMore = true;
      model.isVisiblePullUpLoad = true;
      model.offsetY = -vp2px(model.pullUpLoadHeight) + model.offsetY * Const.Y_OFF_SET_COEFFICIENT;
    }
  }
}

export function touchUpLoadMore(model: PageModel) {
  let self: PageModel = model;
  animateTo({
    duration: Const.ANIMATION_DURATION,
  }, () => {
    self.offsetY = 0;
  })
  if ((self.isCanLoadMore === true) && (self.hasMore === true)) {
    self.isLoading = true;
    setTimeout(() => {
      closeLoadMore(model);
      PageViewModel.getPageData(self.pageId, self.groupId, self.channelId, self.currentPage, self.pageSize, getContext())
        .then((data: PageDTO) => {
          self.timestamp = DateTimeUtils.getTimeStamp().toString()
          if (data == null || data.compList == null || data.compList.length == 0) {
            self.hasMore = false;
          } else {
            if (data.compList.length == self.pageSize) {
              self.currentPage++;
              self.hasMore = true;
            } else {
              self.hasMore = false;
            }
            let sizeBefore:number = self.compList.size();
            self.compList.push(...data.compList)
            PageViewModel.getInteractData(data.compList).then((data: CompDTO[]) => {
              // 刷新,替换所有数据
              self.compList.updateItems(sizeBefore, data)
              self.timestamp = DateTimeUtils.getTimeStamp().toString()
            })
          }
        }).catch((err: string | Resource) => {
        promptAction.showToast({ message: err });
      })
    }, Const.DELAY_TIME);
  } else {
    closeLoadMore(self);
  }
}

export function closeLoadMore(model: PageModel) {
  model.isCanLoadMore = false;
  model.isLoading = false;
  model.isVisiblePullUpLoad = false;
}