1/* 2 * Copyright (c) 2022 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 { Log } from '../../utils/Log'; 17import { SelectManager } from '../../model/browser/SelectManager'; 18import { MenuContext } from './MenuContext'; 19import { BrowserOperationFactory } from '../../interface/BrowserOperationFactory'; 20import { BroadCastConstants } from '../../model/common/BroadCastConstants'; 21import { ProcessMenuOperation } from './ProcessMenuOperation'; 22import { TraceControllerUtils } from '../../utils/TraceControllerUtils'; 23import { BigDataConstants, ReportToBigDataUtil } from '../../utils/ReportToBigDataUtil'; 24import { Constants } from '../../model/common/Constants'; 25 26const TAG = "ThirdDeleteMenuOperation" 27 28export class ThirdDeleteOperation extends ProcessMenuOperation { 29 private isTrash = true; 30 31 constructor(menuContext: MenuContext) { 32 super(menuContext); 33 this.callback = this.callback.bind(this); 34 } 35 36 setConfirmText(): void { 37 if (!this.isTrash) { 38 AppStorage.setOrCreate<Resource>(Constants.CONFIRM_TEXT_KEY, $r('app.string.dialog_delete_permanently')); 39 } else { 40 AppStorage.setOrCreate<Resource>(Constants.CONFIRM_TEXT_KEY, $r('app.string.dialog_delete')); 41 } 42 } 43 44 doAction(): void { 45 if (this.menuContext == null) { 46 Log.warn(TAG, 'menuContext is null, return'); 47 return; 48 } 49 50 let selectManager: SelectManager = this.menuContext.selectManager; 51 if (selectManager == null) { 52 Log.warn(TAG, 'selectManager is null, return'); 53 return; 54 } 55 this.count = selectManager.getSelectedCount(); 56 if (this.count <= 0) { 57 Log.warn(TAG, 'count <= 0, return'); 58 return; 59 } 60 61 this.confirmCallback = this.confirmCallback.bind(this); 62 this.cancelCallback = this.cancelCallback.bind(this); 63 this.setConfirmText(); 64 65 this.menuContext.broadCast.emit(BroadCastConstants.SHOW_THIRD_DELETE_DIALOG, 66 [(this.count == 1 ? $r('app.string.recycle_single_file_tips') : 67 $r('app.string.recycle_files_tips', this.count)), this.confirmCallback, this.cancelCallback]); 68 } 69 70 // Asynchronous callback for getSelection 71 callback(uris: string[]): void { 72 if (this.isCancelled) { 73 return; 74 } 75 this.uris = uris; 76 this.processOperation(); 77 } 78 79 // Delete a batch of data 80 async requestOneBatchOperation(): Promise<void> { 81 Log.info(TAG, `requestOneBatchOperation`); 82 if (this.isCancelled) { 83 return; 84 } 85 this.currentBatch++; 86 let startIndex: number = (this.currentBatch - 1) * this.BATCH_SIZE; 87 const endIndex: number = Math.min(this.currentBatch * this.BATCH_SIZE, this.uris.length); 88 let batchUris: Array<string> = new Array(); 89 for (let index = startIndex; index < endIndex; index++) { 90 batchUris.push(this.uris[index]); 91 } 92 93 let operationImpl = BrowserOperationFactory.getFeature(BrowserOperationFactory.TYPE_PHOTO); 94 95 TraceControllerUtils.startTraceWithTaskId('trash', this.currentBatch) 96 operationImpl.trash(batchUris).then(() => { 97 TraceControllerUtils.finishTraceWithTaskId('trash', this.currentBatch) 98 this.onCompleted() 99 }).catch((error) => { 100 Log.error(TAG, `delete error: ${error}`); 101 this.onError(); 102 }) 103 } 104 105 confirmCallback(): void { 106 Log.info(TAG, 'Batch delete confirm'); 107 // 1. Variable initialization 108 this.onOperationEnd = this.menuContext.onOperationEnd; 109 // 2. selectManager gets the URI of the data and starts processing deletion in the callback 110 if (this.menuContext.albumInfo && this.menuContext.albumInfo.isTrashAlbum) { 111 this.menuContext.selectManager.getDeleteSelection(this); 112 } else { 113 this.menuContext.selectManager.getSelection(this); 114 } 115 116 // 3. onDeleteStart exit selection mode 117 let onOperationStart: Function = this.menuContext.onOperationStart; 118 onOperationStart && onOperationStart(); 119 let msg = { 120 'type': BigDataConstants.DELETE_LOCAL_ONLY, 121 'FovMode': 0 122 } 123 ReportToBigDataUtil.report(BigDataConstants.DELETE_TYPE_ID, msg); 124 125 this.menuContext.broadCast.emit(BroadCastConstants.DELETE_PROGRESS_DIALOG, 126 [$r('app.string.action_delete'), this.count]); 127 } 128 129 cancelCallback(): void { 130 Log.info(TAG, 'Batch delete cancel'); 131 let onOperationCancel: Function = this.menuContext.onOperationCancel; 132 onOperationCancel && onOperationCancel(); 133 } 134}