00c2634c创建于 2025年9月23日历史提交
/*
 * Copyright (c) 2023 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { promptAction } from '@kit.ArkUI';
import { media } from '@kit.MediaKit';
import Logger from '../common/util/Logger';
import { HomeConstants } from '../common/constants/HomeConstants';
import { AvplayerStatus, Events } from '../common/constants/CommonConstants';
import { HomeTabModel } from '../common/model/HomeTabModel';
import { BusinessError } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';

const uiContext: UIContext | undefined = AppStorage.get('uiContext');

export class HomeDialogModel {
  public homeTabModel: HomeTabModel;
  private avPlayer: media.AVPlayer | null = null;
  private url: string = '';
  private duration: number = 0;
  private checkFlag: number = 0;
  private isLoading: boolean = false;

  constructor() {
    this.homeTabModel = new HomeTabModel();
    this.isLoading = false;
  }

  /**
   * Creates a videoPlayer object.
   */
  createAvPlayer() {
    media.createAVPlayer().then((video: media.AVPlayer) => {
      if (video != null) {
        this.avPlayer = video;
        this.bindState();
        this.url = this.homeTabModel.src;
        this.avPlayer.url = this.url;
      } else {
        Logger.info(`[HomeDialogModel] createAVPlayer fail`);
      }
    }).catch((err: Error) => {
      this.failureCallback(err);
    });
  }

  bindState() {
    if (this.avPlayer === null) {
      return;
    }
    this.avPlayer.on(Events.STATE_CHANGE, async (state: media.AVPlayerState) => {
      if (this.avPlayer === null) {
        return;
      }
      if (state === AvplayerStatus.INITIALIZED) {
        this.avPlayer.prepare();
      } else if (state === AvplayerStatus.PREPARED) {
        this.duration = this.avPlayer.duration;
        this.checkUrlValidity();
      } else if (state === AvplayerStatus.ERROR) {
        this.avPlayer.reset();
      }
    });
    this.avPlayer.on(Events.ERROR, (error: Error) => {
      this.isLoading = false;
      this.homeTabModel.linkCheck = $r('app.string.link_check');
      this.homeTabModel.loadColor = $r('app.color.index_tab_selected_font_color');
      if (this.avPlayer !== null) {
        this.avPlayer.release();
      }
      this.failureCallback(error);
    })
  }

  /**
   * Verifying Network Connections.
   *
   * @param checkFlag Determine whether to verify or add data.
   */
  async checkSrcValidity(checkFlag: number) {
    if (this.isLoading) {
      return;
    }
    this.isLoading = true;
    this.homeTabModel.linkCheck = $r('app.string.link_checking');
    this.homeTabModel.loadColor = $r('app.color.index_tab_unselected_font_color');
    this.checkFlag = checkFlag;
    this.createAvPlayer();
  }

  /**
   * Verifying Network Connections.
   */
  checkUrlValidity() {
    this.isLoading = false;
    this.homeTabModel.linkCheck = $r('app.string.link_check');
    this.homeTabModel.loadColor = $r('app.color.index_tab_selected_font_color');
    if (this.avPlayer !== null) {
      this.avPlayer.release().catch((err: BusinessError) => {
        hilog.error(0x0000, 'HomeDialogModel', `release failed. code=${err.code}, message=${err.message}`);
      });
    }
    if (this.duration === HomeConstants.DURATION_TWO) {
      // Failed to verify the link
      this.showPrompt($r('app.string.link_check_fail'));
    } else if (this.duration === HomeConstants.DURATION_ONE) {
      // The address is incorrect or no network is available
      this.showPrompt($r('app.string.link_check_address_internet'));
    } else {
      this.duration = 0;
      if (this.checkFlag === 0) {
        this.showPrompt($r('app.string.link_check_success'));
      } else {
        this.homeTabModel!.confirm();
        this.homeTabModel!.controller!.close();
      }
    }
  }

  /**
   * This parameter is used to report error information when an error occurs in function invoking.
   *
   * @param error Error information.
   */
  failureCallback(error: Error) {
    Logger.error(`[HomeDialogModel] error happened: ` + JSON.stringify(error));
    this.showPrompt($r('app.string.link_check_fail'));
  }

  /**
   * Prompt dialog box.
   *
   * @param msg Verification Information.
   */
  showPrompt(msg: Resource) {
    try {
      uiContext!.getPromptAction().showToast({
        duration: HomeConstants.DURATION,
        message: msg
      });
    } catch (error) {
      let err = error as BusinessError;
      hilog.error(0x0000, 'HomeDialogModel', `showToast failed. code=${err.code}, message=${err.message}`);
    }
  }

  /**
   * Check whether the playback path is empty.
   */
  checkSrcNull(context: UIContext): boolean {
    if (this.isLoading) {
      return false;
    }
    if (this.homeTabModel.src.trim() === '') {
      try {
        context.getPromptAction().showToast({
          duration: HomeConstants.DURATION,
          message: $r('app.string.place_holder_src')
        });
      } catch (error) {
        let err = error as BusinessError;
        hilog.error(0x0000, 'HomeDialogModel', `showToast failed. code=${err.code}, message=${err.message}`);
      }
      return false;
    }
    return true;
  }

  /**
   * Check whether the name is empty.
   */
  checkNameNull(context: UIContext): boolean {
    if (this.isLoading) {
      return false;
    }
    if (this.homeTabModel.name.trim() === '') {
      try {
        context.getPromptAction().showToast({
          duration: HomeConstants.DURATION,
          message: $r('app.string.place_holder_name')
        });
      } catch (error) {
        let err = error as BusinessError;
        hilog.error(0x0000, 'HomeDialogModel', `showToast failed. code=${err.code}, message=${err.message}`);
      }
      return false;
    }
    return true;
  }
}