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 {
17  BroadCastConstants,
18  BrowserOperationFactory,
19  FileAsset,
20  Log,
21  MenuContext,
22  ProcessMenuOperation,
23  SelectManager,
24  UserFileManagerAccess
25} from '@ohos/common';
26import { BusinessError } from '@ohos.base';
27
28const TAG: string = 'browser_BatchRecoverMenuOperation';
29
30export class BatchRecoverMenuOperation extends ProcessMenuOperation {
31  private callbackFunc?: Function;
32
33  constructor(menuContext: MenuContext) {
34    super(menuContext);
35    //初始化绑定this指向
36    this.callbackFunc = (uris: string[]): void => this.callback(uris);
37  }
38
39  doAction(): void {
40    if (this.menuContext == null) {
41      Log.error(TAG, 'menuContext is null, return');
42      return;
43    }
44    let selectManager: SelectManager = this.menuContext.selectManager;
45    if (selectManager == null) {
46      Log.error(TAG, 'selectManager is null, return');
47      return;
48    }
49    this.count = selectManager.getSelectedCount();
50    if (this.count <= 0) {
51      Log.error(TAG, 'count <= 0, return');
52      return;
53    }
54
55    this.onOperationEnd = this.menuContext.onOperationEnd;
56    if (this.menuContext.albumUri == UserFileManagerAccess.getInstance()
57      .getSystemAlbumUri(UserFileManagerAccess.TRASH_ALBUM_SUB_TYPE)) {
58      this.menuContext.selectManager.getDeleteSelection(this);
59    } else {
60      this.menuContext.selectManager.getSelection(this);
61    }
62    let onOperationStart: Function = this.menuContext.onOperationStart;
63    onOperationStart && onOperationStart();
64
65    this.menuContext.broadCast.emit(BroadCastConstants.DELETE_PROGRESS_DIALOG,
66      [$r('app.string.action_recover'), this.count]);
67  }
68
69  // Asynchronous callback for getSelection
70  callback(uris: string[]): void {
71    if (this.isCancelled) {
72      return;
73    }
74    this.uris = uris;
75    this.processOperation();
76  }
77
78  // Delete a batch of data
79  async requestOneBatchOperation(): Promise<void> {
80    if (this.isCancelled) {
81      return;
82    }
83    this.currentBatch++;
84    let startIndex = (this.currentBatch - 1) * this.BATCH_SIZE;
85    let endIndex = this.currentBatch * this.BATCH_SIZE;
86    endIndex = Math.min(endIndex, this.uris.length);
87    let operationImpl = BrowserOperationFactory.getFeature(BrowserOperationFactory.TYPE_PHOTO);
88    let fileAssets: FileAsset[] = [];
89    let uris: string[] = [];
90    for (let index = startIndex; index < endIndex; index++) {
91      uris.push(this.uris[index]);
92
93      let fileItem = await UserFileManagerAccess.getInstance().getTrashAssetByUri(uris[index]);
94      fileAssets.push(fileItem);
95    }
96
97    operationImpl.recoverFromTrash(fileAssets).then(() => {
98      this.onCompleted();
99    }).catch((error: BusinessError) => {
100      Log.error(TAG, `recover error: ${error}, code: ${error?.code}`);
101      this.onError();
102    })
103  }
104}