• 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 { FileAsset, 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_DeleteMenuOperation';
29
30export class DeleteMenuOperation implements MenuOperation, MenuOperationCallback {
31  private menuContext: MenuContext;
32  private isTrash = true;
33
34  constructor(menuContext: MenuContext) {
35    this.menuContext = menuContext;
36  }
37
38  doAction(): void {
39    if (this.menuContext == null) {
40      Log.error(TAG, 'menuContext is null, return');
41      return;
42    }
43    let mediaItem = this.menuContext.mediaItem;
44    if (mediaItem == null) {
45      Log.error(TAG, 'mediaItem is null, return');
46      return;
47    }
48    if (this.menuContext.albumInfo && this.menuContext.albumInfo.isTrashAlbum) {
49      this.isTrash = false;
50    }
51    this.confirmCallback = this.confirmCallback.bind(this);
52    this.cancelCallback = this.cancelCallback.bind(this);
53    let message = mediaItem.mediaType == UserFileManagerAccess.MEDIA_TYPE_VIDEO
54      ? $r('app.string.recycle_video_tips') : $r('app.string.recycle_picture_tips');
55    this.setConfirmText();
56    if (!this.isTrash) {
57      message = $r('app.plural.recycleAlbum_delete_message', 1);
58    }
59    this.menuContext.broadCast.emit(BroadCastConstants.SHOW_DELETE_DIALOG,
60      [message, this.confirmCallback, this.cancelCallback]);
61  }
62
63  setConfirmText(): void {
64    if (!this.isTrash) {
65      AppStorage.SetOrCreate<Resource>(Constants.CONFIRM_TEXT_KEY, $r('app.string.dialog_delete_permanently'));
66    } else {
67      AppStorage.SetOrCreate<Resource>(Constants.CONFIRM_TEXT_KEY, $r('app.string.dialog_delete'));
68    }
69  }
70
71  onCompleted(): void {
72    Log.info(TAG, 'Delete data succeed!');
73    this.menuContext.broadCast.emit(BrowserConstants.DELETE, []);
74  }
75
76  onError(): void {
77    Log.error(TAG, 'Delete data failed!');
78  }
79
80  private async confirmCallback() {
81    this.menuContext.broadCast.emit(BrowserConstants.PHOTO_BROWSER_DELETE_CONFIRM, []);
82
83    Log.info(TAG, 'Delete confirm');
84    let mediaItem = this.menuContext.mediaItem;
85    if (mediaItem == null) {
86      Log.error(TAG, 'mediaItem is null, return');
87      return;
88    }
89    let operationImpl = BrowserOperationFactory.getFeature(BrowserOperationFactory.TYPE_PHOTO);
90    try {
91      if (this.isTrash) {
92        await operationImpl.trash(mediaItem.uri, true);
93      } else {
94        let fileAssets = new Array<FileAsset>();
95        fileAssets.push(await UserFileManagerAccess.getInstance().getTrashAssetByUri(mediaItem.uri));
96        await operationImpl.deleteTrash(fileAssets);
97      }
98      this.onCompleted()
99    } catch (error) {
100      Log.error(TAG, `delete error: ${error}`);
101      this.onError();
102    }
103    let msg = {
104      'type': BigDataConstants.DELETE_LOCAL_ONLY,
105      'FovMode': 0,
106      'mode': this.isTrash ? 'trash' : 'delete'
107    }
108    ReportToBigDataUtil.report(BigDataConstants.DELETE_TYPE_ID, msg);
109    Log.info(TAG, 'Single delete confirm');
110  }
111
112  private cancelCallback(): void {
113    this.setConfirmText();
114    Log.info(TAG, 'Delete cancel');
115
116  }
117}