EmptyComponent.ets 5.57 KB
import { CommonConstants } from 'wdConstant';
import { Logger } from 'wdKit';

const TAG = 'EmptyComponent';

/**
 * WDViewDefaultType 缺省页
 */
export const enum WDViewDefaultType {
  ///默认
  WDViewDefaultType_Default,
  ///无网
  WDViewDefaultType_NoNetwork,
  ///暂无内容(列表页)
  WDViewDefaultType_NoListContent,
  ///内容找不到了(内容详情页)
  WDViewDefaultType_NoContent,
  ///无搜索内容
  WDViewDefaultType_NoSearchResult,
  ///无消息内容
  WDViewDefaultType_NoMessage,
  ///无收藏内容
  WDViewDefaultType_NoCollection,
  ///无历史记录
  WDViewDefaultType_NoHistory,
  ///网络失败 请稍后重试-倒计时
  WDViewDefaultType_NetworkFailed,
  ///内容获取失败
  WDViewDefaultType_ContentFailed,
  ///无预约内容
  WDViewDefaultType_NoBooking,
  ///无评论内容
  WDViewDefaultType_NoComment,
  ///暂无作品
  WDViewDefaultType_NoCreation,
  ///该号主无法访问
  WDViewDefaultType_NoVisitAccount,
  ///暂无关注
  WDViewDefaultType_NoFollow
}

/**
 * 空数据/无数据
 */
@Preview
@Component
export struct EmptyComponent {
  // private emptySize: SizeOptions = {};
  @State emptyWidth: string | number = CommonConstants.FULL_PARENT;
  @State emptyHeight: string | number = CommonConstants.FULL_PARENT;
  @State emptyType: number = WDViewDefaultType.WDViewDefaultType_Default
  /**
   * The empty image width percentage setting.
   */
  readonly EMPTY_IMAGE_WIDTH: string = '15%';
  /**
   * The empty image height percentage setting.
   */
  readonly EMPTY_IMAGE_HEIGHT: string = '15%';
  /**
   * The empty data text component margin top.
   */
  readonly EMPTY_TIP_TEXT_MARGIN_TOP: string = '10';
  /**
   * The empty data text opacity.
   */
  readonly TEXT_OPACITY: number = 0.4;

  build() {
    this.noProgrammeData();
  }

  /**
   * 无数据,空白view组件
   */
  @Builder
  noProgrammeData() {
    Column() {
      Image(this.buildNoDataTipImage())
        .width('this.EMPTY_IMAGE_WIDTH')
        .height(this.EMPTY_IMAGE_HEIGHT)
        .objectFit(ImageFit.Contain)
      // .border({ width: 1, color: Color.Red, radius: 6 })

      Text(this.buildNoDataTip())
        .fontSize($r('app.float.normal_text_size'))
        .fontColor('#000000')
        .fontWeight(FontWeight.Normal)
        .opacity(this.TEXT_OPACITY)
        .margin({ top: this.EMPTY_TIP_TEXT_MARGIN_TOP })
        .onClick((event: ClickEvent) => {
          Logger.info(TAG, `noProgrammeData onClick event?.source: ${event.source}`);
        })
    }
    .justifyContent(FlexAlign.Center)
    .width(this.emptyWidth)
    .height(this.emptyHeight)
  }

  buildNoDataTip(): string {
    Logger.info(TAG, "buildNoDataTip");
    let contentString: string = '暂无内容'
    if (this.emptyType === WDViewDefaultType.WDViewDefaultType_NoCollection) {
      contentString = '暂无收藏'
    } else if (this.emptyType === WDViewDefaultType.WDViewDefaultType_NoMessage) {
      contentString = '暂无消息'
    } else if (this.emptyType === WDViewDefaultType.WDViewDefaultType_NoComment) {
      contentString = '暂无评论'
    } else if (this.emptyType === WDViewDefaultType.WDViewDefaultType_NoSearchResult) {
      contentString = '没有找到相关内容'
    } else if (this.emptyType === WDViewDefaultType.WDViewDefaultType_NoNetwork) {
      contentString = '网络出小差了,请检查网络后重试'
    } else if (this.emptyType === WDViewDefaultType.WDViewDefaultType_ContentFailed) {
      contentString = '获取内容失败请重试'
    } else if (this.emptyType === WDViewDefaultType.WDViewDefaultType_NoCreation) {
      contentString = '暂无作品'
    } else if (this.emptyType === WDViewDefaultType.WDViewDefaultType_NoBooking) {
      contentString = '暂无预约'
    } else if (this.emptyType === WDViewDefaultType.WDViewDefaultType_NetworkFailed) {
      contentString = '' // 前方拥堵,请耐心等待
    } else if (this.emptyType === WDViewDefaultType.WDViewDefaultType_NoVisitAccount) {
      contentString = '该号主暂时无法访问' // 前方拥堵,请耐心等待
    }

    return contentString
  }

  buildNoDataTipImage(): Resource | string {
    Logger.info(TAG, "buildNoDataTip");
    let imageString: Resource | string = $r('app.media.icon_no_content')
    if (this.emptyType === WDViewDefaultType.WDViewDefaultType_NoCollection) {
      imageString = $r('app.media.icon_no_collection')
    } else if (this.emptyType === WDViewDefaultType.WDViewDefaultType_NoMessage) {
      imageString = $r('app.media.icon_no_message')
    } else if (this.emptyType === WDViewDefaultType.WDViewDefaultType_NoComment) {
      imageString = $r('app.media.icon_no_comment')
    } else if (this.emptyType === WDViewDefaultType.WDViewDefaultType_NoSearchResult) {
      imageString = $r('app.media.icon_no_result')
    } else if (this.emptyType === WDViewDefaultType.WDViewDefaultType_NoNetwork) {
      imageString = $r('app.media.icon_no_net')
    } else if (this.emptyType === WDViewDefaultType.WDViewDefaultType_ContentFailed) {
      imageString = $r('app.media.icon_no_content')
    } else if (this.emptyType === WDViewDefaultType.WDViewDefaultType_NoCreation) {
      imageString = $r('app.media.icon_no_appointmentMade')
    } else if (this.emptyType === WDViewDefaultType.WDViewDefaultType_NoBooking) {
      imageString = $r('app.media.icon_no_appointmentMade')
    } else if (this.emptyType === WDViewDefaultType.WDViewDefaultType_NetworkFailed) {
      imageString = $r('app.media.icon_no_net')
    } else if (this.emptyType === WDViewDefaultType.WDViewDefaultType_NoVisitAccount) {
      imageString = $r('app.media.icon_no_master1')
    }
    return imageString
  }
}