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 { 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 { AlbumDefine } from '../../model/browser/AlbumDefine'; 22import { ProcessMenuOperation } from './ProcessMenuOperation'; 23import { TraceControllerUtils } from '../../utils/TraceControllerUtils'; 24import { BigDataConstants, ReportToBigDataUtil } from '../../utils/ReportToBigDataUtil'; 25import { Constants } from '../../model/common/Constants'; 26import { FileAsset, UserFileManagerAccess } from '../../access/UserFileManagerAccess'; 27 28const TAG: string = 'common_BatchDeleteMenuOperation'; 29 30export class BatchDeleteMenuOperation extends ProcessMenuOperation { 31 private isTrash = true; 32 33 constructor(menuContext: MenuContext) { 34 super(menuContext); 35 //初始化绑定this指向 36 this.callback = this.callback.bind(this) 37 } 38 39 setConfirmText(): void { 40 if (!this.isTrash) { 41 AppStorage.setOrCreate<Resource>(Constants.CONFIRM_TEXT_KEY, $r('app.string.dialog_delete_permanently')); 42 } else { 43 AppStorage.setOrCreate<Resource>(Constants.CONFIRM_TEXT_KEY, $r('app.string.dialog_delete')); 44 } 45 } 46 47 doAction(): void { 48 if (this.menuContext == null) { 49 Log.error(TAG, 'menuContext is null, return'); 50 return; 51 } 52 if (this.menuContext.albumInfo && this.menuContext.albumInfo.isTrashAlbum) { 53 this.isTrash = false; 54 } 55 let selectManager: SelectManager = this.menuContext.selectManager; 56 if (selectManager == null) { 57 Log.error(TAG, 'selectManager is null, return'); 58 return; 59 } 60 this.count = selectManager.getSelectedCount(); 61 if (this.count <= 0) { 62 Log.error(TAG, 'count <= 0, return'); 63 return; 64 } 65 66 this.confirmCallback = this.confirmCallback.bind(this); 67 this.cancelCallback = this.cancelCallback.bind(this); 68 this.setConfirmText(); 69 if (this.menuContext.albumInfo && this.menuContext.albumInfo.isTrashAlbum) { 70 if (selectManager.isAllSelected) { 71 this.menuContext.broadCast.emit(BroadCastConstants.SHOW_DELETE_DIALOG, 72 [$r('app.string.recycleAlbum_delete_all_images_message', this.count), 73 this.confirmCallback, this.cancelCallback]); 74 } else if (this.count === 1) { 75 this.menuContext.broadCast.emit(BroadCastConstants.SHOW_DELETE_DIALOG, 76 [$r('app.plural.recycleAlbum_delete_message', this.count, this.count), 77 this.confirmCallback, this.cancelCallback]); 78 } else { 79 this.menuContext.broadCast.emit(BroadCastConstants.SHOW_DELETE_DIALOG, 80 [$r('app.plural.recycleAlbum_delete_multiple_images_message', this.count, this.count), 81 this.confirmCallback, this.cancelCallback]); 82 } 83 } else { 84 this.menuContext.broadCast.emit(BroadCastConstants.SHOW_DELETE_DIALOG, 85 [selectManager.isAllSelected ? $r('app.string.recycle_all_files_tips') : 86 (this.count == 1 ? $r('app.string.recycle_single_file_tips') : 87 $r('app.string.recycle_files_tips', this.count)), this.confirmCallback, this.cancelCallback]); 88 } 89 } 90 91 // Asynchronous callback for getSelection 92 callback(uris: string[]): void { 93 if (this.isCancelled) { 94 return; 95 } 96 this.uris = uris; 97 this.processOperation(); 98 } 99 100 // Delete a batch of data 101 async requestOneBatchOperation(): Promise<void> { 102 Log.info(TAG, `requestOneBatchOperation`); 103 if (this.isCancelled) { 104 return; 105 } 106 this.currentBatch++; 107 let startIndex: number = (this.currentBatch - 1) * this.BATCH_SIZE; 108 let endIndex: number = this.currentBatch * this.BATCH_SIZE; 109 let batchUris: Array<string> = new Array(); 110 for (let index = startIndex; index < endIndex; index++) { 111 batchUris.push(this.uris[index]); 112 } 113 114 let operationImpl = BrowserOperationFactory.getFeature(BrowserOperationFactory.TYPE_PHOTO); 115 if (this.menuContext.albumInfo && this.menuContext.albumInfo.isTrashAlbum) { 116 TraceControllerUtils.startTraceWithTaskId('delete', this.currentBatch); 117 let fileAssets = new Array<FileAsset>(); 118 let fileItem = await UserFileManagerAccess.getInstance().getTrashAssetByUri(batchUris[0]); 119 fileAssets.push(fileItem); 120 operationImpl.deleteTrash(fileAssets).then(() => { 121 TraceControllerUtils.finishTraceWithTaskId('delete', this.currentBatch) 122 this.onCompleted() 123 }).catch((error) => { 124 Log.error(TAG, `delete error: ${error}`); 125 this.onError(); 126 }) 127 } else { 128 TraceControllerUtils.startTraceWithTaskId('trash', this.currentBatch) 129 operationImpl.trash(batchUris).then(() => { 130 TraceControllerUtils.finishTraceWithTaskId('trash', this.currentBatch) 131 this.onCompleted() 132 }).catch((error) => { 133 Log.error(TAG, `delete error: ${error}`); 134 this.onError(); 135 }) 136 } 137 } 138 139 confirmCallback(): void { 140 Log.info(TAG, 'Batch delete confirm'); 141 // 1. Variable initialization 142 this.onOperationEnd = this.menuContext.onOperationEnd; 143 // 2. selectManager gets the URI of the data and starts processing deletion in the callback 144 if (this.menuContext.albumInfo && this.menuContext.albumInfo.isTrashAlbum) { 145 this.menuContext.selectManager.getDeleteSelection(this); 146 } else { 147 this.menuContext.selectManager.getSelection(this); 148 } 149 150 // 3. onDeleteStart exit selection mode 151 let onOperationStart: Function = this.menuContext.onOperationStart; 152 onOperationStart && onOperationStart(); 153 let msg = { 154 'type': BigDataConstants.DELETE_LOCAL_ONLY, 155 'FovMode': 0 156 } 157 ReportToBigDataUtil.report(BigDataConstants.DELETE_TYPE_ID, msg); 158 159 this.menuContext.broadCast.emit(BroadCastConstants.DELETE_PROGRESS_DIALOG, 160 [$r('app.string.action_delete'), this.count]); 161 } 162 163 cancelCallback(): void { 164 Log.info(TAG, 'Batch delete cancel'); 165 this.setConfirmText(); 166 if (!this.menuContext.fromSelectMode) { 167 this.menuContext.selectManager.deSelectAll(); 168 } 169 } 170}