PageComponent.ets 2.57 KB
import { CompDTO, CompStyle, GroupDTO, ItemBean, ItemDTO, ViewType } from 'wdBean';
import { CompUtils, EmptyComponent, ErrorComponent,
  GridLayout01Component,
  LabelComponent, LoadingComponent,
  SingleRow03Component, } from 'wdComponent';
import { CommonConstants } from 'wdConstant';
import { LazyDataSource, Logger, StringUtils } from 'wdKit';


const TAG = 'PageComponent';

@Component
export struct PageComponent {
  @Prop viewType: number = ViewType.LOADED;
  // Group数据及子组件数据
  @State groupList: LazyDataSource<GroupDTO> = new LazyDataSource();
  @State isRefreshing: boolean = false

  build() {
    if (this.viewType == ViewType.LOADING) {
      LoadingComponent()
    } else if (this.viewType == ViewType.ERROR) {
      ErrorComponent()
    } else if (this.viewType == ViewType.EMPTY) {
      EmptyComponent()
    } else {
      Refresh({ refreshing: this.isRefreshing }) {
        List() {
          LazyForEach(this.groupList, (groupDTO: GroupDTO, groupIndex: number) => {
            ListItem() {
              Column() {
                ForEach(groupDTO.compList, (compDTO: CompDTO, compIndex: number) => {
                  this.componentBuilder(compDTO, groupIndex, compIndex)
                })
              }
            }
          })
        }
        .cachedCount(5)
        .height(CommonConstants.FULL_PARENT)

        // .padding({
        //   bottom: (this.currentNavIndex === 0 || this.currentNavIndex === 1) ? $r('app.float.search_bar_height') : 0
        // }) // 当页面顶部有搜索bar时,页面的list增加bottom的padding为搜索bar的高度
      }
      .onRefreshing(() => {
        this.isRefreshing = true
        // this.sendRequest(true)
        // setTimeout(() => {
        //   this.isRefreshing = false
        // }, 2000)
      })
    }
  }

  @Builder
  componentBuilder(compDTO: CompDTO, groupIndex: number, compIndex: number) {
    if (compDTO.compStyle === CompStyle.Label_03) {
      LabelComponent({ label: CompUtils.getLabelTitle(compDTO.extraData) })
    } else if(compDTO.compStyle === CompStyle.Single_Row_03){
      SingleRow03Component({dataList: compDTO.operDataList})
    } else if(compDTO.compStyle === CompStyle.Grid_Layout_01){
      GridLayout01Component({dataList: compDTO.operDataList})
    } else {
      // todo:组件未实现 / Component Not Implemented
      Text(compDTO.compStyle)
        .width(CommonConstants.FULL_PARENT)
        .padding(10)
        // .backgroundColor(Color.Brown) // 展示本页未实现的compStyle
    }
  }

  aboutToAppear() {
    Logger.info(TAG, `aboutToAppear, this.pageId: ${this.viewType}`);
  }
}