• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import { UserFileManagerAccess } from '../../access/UserFileManagerAccess';
17import { Log } from '../../utils/Log';
18import type { MenuOperationCallback } from './MenuOperationCallback';
19import type { MenuOperation } from './MenuOperation';
20import { MenuContext } from './MenuContext';
21import { BrowserOperationFactory } from '../../interface/BrowserOperationFactory';
22import { BroadCastConstants } from '../../model/common/BroadCastConstants';
23import { AlbumDefine } from '../../model/browser/AlbumDefine';
24import { Constants as BrowserConstants } from '../../model/browser/photo/Constants';
25import { BigDataConstants, ReportToBigDataUtil } from '../../utils/ReportToBigDataUtil';
26import { Constants } from '../../model/common/Constants';
27
28const TAG: string = 'common_RemoveMenuOperation';
29
30export class RemoveMenuOperation implements MenuOperation, MenuOperationCallback {
31  private menuContext: MenuContext;
32
33  constructor(menuContext: MenuContext) {
34    this.menuContext = menuContext;
35  }
36
37  doAction(): void {
38    if (this.menuContext == null) {
39      Log.error(TAG, 'menuContext is null, return');
40      return;
41    }
42    let mediaItem = this.menuContext.mediaItem;
43    if (mediaItem == null) {
44      Log.error(TAG, 'mediaItem is null, return');
45      return;
46    }
47    this.confirmCallback = this.confirmCallback.bind(this);
48    this.cancelCallback = this.cancelCallback.bind(this);
49    let message = mediaItem.mediaType == UserFileManagerAccess.MEDIA_TYPE_VIDEO
50      ? $r('app.string.remove_video_tips') : $r('app.string.remove_picture_tips');
51    this.setConfirmText();
52    this.menuContext.broadCast.emit(BroadCastConstants.SHOW_REMOVE_DIALOG,
53      [message, this.confirmCallback, this.cancelCallback]);
54  }
55
56  setConfirmText(): void {
57    AppStorage.setOrCreate<Resource>(Constants.CONFIRM_TEXT_KEY, $r('app.string.dialog_remove'));
58  }
59
60  onCompleted(): void {
61    Log.info(TAG, 'Remove data succeed!');
62    this.menuContext.broadCast.emit(BrowserConstants.Remove, []);
63  }
64
65  onError(): void {
66    Log.error(TAG, 'Remove data failed!');
67  }
68
69  private async confirmCallback() {
70    this.menuContext.broadCast.emit(BrowserConstants.PHOTO_BROWSER_REMOVE_CONFIRM, []);
71
72    Log.info(TAG, 'Remove confirm');
73    let mediaItem = this.menuContext.mediaItem;
74    if (mediaItem == null) {
75      Log.error(TAG, 'mediaItem is null, return');
76      return;
77    }
78    let operationImpl = BrowserOperationFactory.getFeature(BrowserOperationFactory.TYPE_PHOTO);
79    try {
80      await operationImpl.remove([mediaItem.uri], this.menuContext.albumUri);
81      this.onCompleted()
82    } catch (error) {
83      Log.error(TAG, `remove error: ${error}`);
84      this.onError();
85    }
86    let msg = {
87      'type': BigDataConstants.REMOVE,
88      'FovMode': 0,
89      'mode': 'remove'
90    }
91    ReportToBigDataUtil.report(BigDataConstants.REMOVE_TYPE_ID, msg);
92    Log.info(TAG, 'Single remove confirm');
93  }
94
95  private cancelCallback(): void {
96    this.setConfirmText();
97    Log.info(TAG, 'remove cancel');
98
99  }
100}