PageComponent.ets 3.29 KB
import { CompDTO, CompStyle, GroupDTO, ViewType } from 'wdBean';
import { CommonConstants } from 'wdConstant';
import { LazyDataSource, Logger } from 'wdKit';
import { PageViewModel } from '../viewmodel/PageViewModel';
import { BannerComponent } from './BannerComponent';
import { EmptyComponent } from './EmptyComponent';
import { ErrorComponent } from './ErrorComponent';
import { GridLayout01Component } from './GridLayout01Component';
import { LabelComponent } from './LabelComponent';
import { LoadingComponent } from './LoadingComponent';
import { SingleColumnComponent } from './SingleColumnComponent';
import { SingleRow03Component } from './SingleRow03Component';
import { WaterFlowComponent } from './WaterFlowComponent';

const TAG = 'PageComponent';

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

  build() {
    if (this.viewType == ViewType.LOADING) {
      LoadingComponent()
    } else if (this.viewType == ViewType.ERROR) {
      ErrorComponent()
    } else if (this.viewType == ViewType.EMPTY) {
      EmptyComponent()
    } else {
      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)
    }
  }

  @Builder
  componentBuilder(compDTO: CompDTO, groupIndex: number, compIndex: number) {
    if (compDTO.compStyle === CompStyle.Label_03) {
      LabelComponent({ compDTO: compDTO })
    } else if (compDTO.compStyle === CompStyle.Carousel_Layout_01) {
      BannerComponent({ compDTO: compDTO })
    } else if (compDTO.compStyle === CompStyle.Single_Row_03) {
      SingleRow03Component({ dataList: compDTO.operDataList })
    } else if (compDTO.compStyle === CompStyle.Single_Column_01 || compDTO.compStyle === CompStyle.Single_Column_02) {
      SingleColumnComponent({ compDTO: compDTO })
    } else if (compDTO.compStyle === CompStyle.Grid_Layout_01) {
      GridLayout01Component({ dataList: compDTO.operDataList })
    } else if (compDTO.compStyle === CompStyle.Masonry_Layout_01) {
      WaterFlowComponent({ compDTO: compDTO })
    } 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} this.currentTopNavSelectedIndex: ${this.currentTopNavSelectedIndex}`);
    if (this.currentTopNavSelectedIndex === 1) { // 顶导tab的第0个item是【热点】,第1个item是【推荐】
      this.groupList.replaceAll()
      let groupDto = PageViewModel.getGroup2DTO(getContext(this))
      if (groupDto) {
        this.groupList.push(groupDto)
      }
    } else {
      let groupDto = PageViewModel.getGroupDTO(getContext(this))
      if (groupDto) {
        this.groupList.push(groupDto)
      }
    }
  }
}