LaunchPage.ets 3.41 KB
import media from '@ohos.multimedia.media'
import App from '@system.app'
import Router from '@system.router'
import router from '@ohos.router'
import common from '@ohos.app.ability.common'
import CustomDialogComponent from '../view/CustomDialogComponent'
import preferences from '@ohos.data.preferences'
import { GlobalContext } from '../common/utils/GlobalContext'

@Entry
@Component
struct LaunchPage {
  private context?: common.UIAbilityContext;
  private timerId: number = 0;
  private isJumpToAdvertising: boolean = false;

  dialogController: CustomDialogController = new CustomDialogController({
    builder: CustomDialogComponent(
      {
        cancel: () => {
          this.onCancel();
        },
        confirm: () => {
          this.onConfirm();
        }
      }),
    alignment: DialogAlignment.Center,
    offset: { dx: 0, dy: '-24' },
    customStyle: true,
    autoCancel: false
  });

  onCancel() {
    // Exit the application.
    this.context?.terminateSelf();
  }

  onConfirm() {
    // Save privacy agreement status.
    this.saveIsPrivacy();
    this.jumpToAdvertisingPage();
  }

  jumpToAdvertisingPage() {
    this.timerId = setTimeout(() => {
      this.isJumpToAdvertising = true;
      router.pushUrl({
        url: 'pages/LaunchAdvertisingPage'
      }).catch((error: Error) => {
        //Logger.error(CommonConstants.LAUNCHER_PAGE_TAG, 'LauncherPage pushUrl error ' + JSON.stringify(error));
      });
    }, 1000);
  }

  onPageShow() {
    this.context = getContext(this) as common.UIAbilityContext;
    // Get the operation class for saving data.
    this.getDataPreferences(this).then((preferences: preferences.Preferences) => {
      preferences.get('isPrivacy', true).then((value: preferences.ValueType) => {
        //Logger.info(CommonConstants.LAUNCHER_PAGE_TAG, 'onPageShow value: ' + value);
        if (value) {
          // let isJumpPrivacy: boolean = globalThis.isJumpPrivacy ?? false;
          // let isJumpPrivacy: boolean = (GlobalContext.getContext().getObject('isJumpPrivacy') as boolean) ?? false;
          // if (!isJumpPrivacy) {
            this.dialogController.open();
          // }
        } else {
          this.jumpToAdvertisingPage();
        }
      });
    });
  }

  onPageHide() {
    if (this.isJumpToAdvertising) {
      router.clear();
    }
    // globalThis.isJumpPrivacy = true;
    //GlobalContext.getContext().setObject('isJumpPrivacy', true);
    clearTimeout(this.timerId);
  }

  getDataPreferences(common: Object) {
    return preferences.getPreferences(getContext(common), 'myStore');
  }

  saveIsPrivacy() {
    let preferences: Promise<preferences.Preferences> = this.getDataPreferences(this);
    preferences.then((result: preferences.Preferences) => {
      let privacyPut = result.put('isPrivacy', false);
      result.flush();
      privacyPut.then(() => {
        //Logger.info('LauncherPage', 'Put the value of startup Successfully.');
      }).catch((err: Error) => {
        //Logger.error('LauncherPage', 'Put the value of startup Failed, err: ' + err);
      });
    }).catch((err: Error) => {
      //Logger.error('LauncherPage', 'Get the preferences Failed, err: ' + err);
    });
  }



  build(){

    Stack({alignContent:Alignment.Bottom}){
      Image($r('app.media.LaunchPage_logo'))
        .width('278lpx')
        .height('154lpx')
        .margin({
          bottom:'48lpx'
        })

    }
    .width('100%')
    .height('100%')
    .backgroundColor(Color.White)


  }



}