PageComponent.ets 3.18 KB
import { CommonConstants, CompStyle, ViewType } from 'wdConstant';
import { LazyDataSource, Logger } from 'wdKit';
import { CompDTO } from '../repository/bean/CompDTO';
import PageViewModel from '../viewmodel/PageViewModel';
import { EmptyComponent } from './EmptyComponent';
import { ErrorComponent } from './ErrorComponent';
import { LabelComponent } from './LabelComponent';
import { LoadingComponent } from './LoadingComponent';
import { TitleAbbrComponent } from './TitleAbbrComponent';
import { TitleAllComponent } from './TitleAllComponent';

const TAG = 'PageComponent';

@Component
export struct PageComponent {
  @State viewType: number = ViewType.LOADED;
  // Group数据及子组件数据
  @State compList: LazyDataSource<CompDTO> = new LazyDataSource();
  @State currentTopNavSelectedIndex: number = 0;
  @State pageId: string = "";
  @State channelId: string = "";

  // @Link @Watch('onChange') paIndex:number

  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.compList, (compDTO: CompDTO, compIndex: number) => {
          ListItem() {
            Column() {
              this.componentBuilder(compDTO, compIndex)
            }
          }
        })
      }
      .cachedCount(5)
      .height(CommonConstants.FULL_PARENT)
    }
  }

  @Builder
  componentBuilder(compDTO: CompDTO, compIndex: number) {
    if (compDTO.compStyle === CompStyle.Label_03) {
      LabelComponent({ compDTO: compDTO })
    } else if (compDTO.compStyle === CompStyle.Title_Abbr_01) {
      TitleAbbrComponent({ compDTO: compDTO })
    } else if (compDTO.compStyle === CompStyle.Title_All_01) {
      TitleAllComponent({ compDTO: compDTO })
    } else {
      // todo:组件未实现 / Component Not Implemented
      Text(compDTO.compStyle)
        .width(CommonConstants.FULL_PARENT)
        .padding(10)
      // .backgroundColor(Color.Brown) // 展示本页未实现的compStyle
    }
  }

  async aboutToAppear() {
    Logger.info(TAG, `aboutToAppear, this.pageId: ${this.viewType} this.currentTopNavSelectedIndex: ${this.currentTopNavSelectedIndex}`);
    // if (this.currentTopNavSelectedIndex === 1) { // 顶导tab的第0个item是【热点】,第1个item是【推荐】
    //   this.compList.replaceAll()
    //   let pageDto = await PageViewModel.getPageData2(getContext(this))
    //   if (pageDto && pageDto.compList) {
    //     this.compList.push(...pageDto.compList)
    //   }
    // } else {
    //   let pageDto = await PageViewModel.getPageData1(getContext(this))
    //   if (pageDto && pageDto.compList) {
    //     this.compList.push(...pageDto.compList)
    //   }
    // }

    // if (this.currentTopNavSelectedIndex != 1) { // 顶导tab的第0个item是【热点】,第1个item是【推荐】
    //   return
    // }
    Logger.debug(TAG,'lllllalalal: ' + this.pageId+' , ' + this.channelId);
    let pageDto = await PageViewModel.getPageData(this.pageId, this.pageId, this.channelId, getContext(this))
    if (pageDto && pageDto.compList) {
      this.compList.push(...pageDto.compList)
    }
  }
}