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 let endIndex: number = this.currentBatch * this.BATCH_SIZE; 88 let batchUris: string[] = this.uris.slice(startIndex, Math.min(endIndex, this.uris.length)); 89 90 let operationImpl = BrowserOperationFactory.getFeature(BrowserOperationFactory.TYPE_PHOTO); 91 92 TraceControllerUtils.startTraceWithTaskId('trash', this.currentBatch) 93 operationImpl.trash(batchUris[0], true).then(() => { 94 TraceControllerUtils.finishTraceWithTaskId('trash', this.currentBatch) 95 this.onCompleted() 96 }).catch((error) => { 97 Log.error(TAG, `delete error: ${error}`); 98 this.onError(); 99 }) 100 } 101 102 confirmCallback(): void { 103 Log.info(TAG, 'Batch delete confirm'); 104 // 1. Variable initialization 105 this.onOperationEnd = this.menuContext.onOperationEnd; 106 // 2. selectManager gets the URI of the data and starts processing deletion in the callback 107 if (this.menuContext.albumInfo && this.menuContext.albumInfo.isTrashAlbum) { 108 this.menuContext.selectManager.getDeleteSelection(this); 109 } else { 110 this.menuContext.selectManager.getSelection(this); 111 } 112 113 // 3. onDeleteStart exit selection mode 114 let onOperationStart: Function = this.menuContext.onOperationStart; 115 onOperationStart && onOperationStart(); 116 let msg = { 117 'type': BigDataConstants.DELETE_LOCAL_ONLY, 118 'FovMode': 0 119 } 120 ReportToBigDataUtil.report(BigDataConstants.DELETE_TYPE_ID, msg); 121 122 this.menuContext.broadCast.emit(BroadCastConstants.DELETE_PROGRESS_DIALOG, 123 [$r('app.string.action_delete'), this.count]); 124 } 125 126 cancelCallback(): void { 127 Log.info(TAG, 'Batch delete cancel'); 128 let onOperationCancel: Function = this.menuContext.onOperationCancel; 129 onOperationCancel && onOperationCancel(); 130 } 131}