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: string[] = this.uris.slice(startIndex, Math.min(endIndex, this.uris.length)); 110 111 let operationImpl = BrowserOperationFactory.getFeature(BrowserOperationFactory.TYPE_PHOTO); 112 if (this.menuContext.albumInfo && this.menuContext.albumInfo.isTrashAlbum) { 113 TraceControllerUtils.startTraceWithTaskId('delete', this.currentBatch); 114 let fileAssets = new Array<FileAsset>(); 115 let fileItem = await UserFileManagerAccess.getInstance().getTrashAssetByUri(batchUris[0]); 116 fileAssets.push(fileItem); 117 operationImpl.deleteTrash(fileAssets).then(() => { 118 TraceControllerUtils.finishTraceWithTaskId('delete', this.currentBatch) 119 this.onCompleted() 120 }).catch((error) => { 121 Log.error(TAG, `delete error: ${error}`); 122 this.onError(); 123 }) 124 } else { 125 TraceControllerUtils.startTraceWithTaskId('trash', this.currentBatch) 126 operationImpl.trash(batchUris[0], true).then(() => { 127 TraceControllerUtils.finishTraceWithTaskId('trash', this.currentBatch) 128 this.onCompleted() 129 }).catch((error) => { 130 Log.error(TAG, `delete error: ${error}`); 131 this.onError(); 132 }) 133 } 134 } 135 136 confirmCallback(): void { 137 Log.info(TAG, 'Batch delete confirm'); 138 // 1. Variable initialization 139 this.onOperationEnd = this.menuContext.onOperationEnd; 140 // 2. selectManager gets the URI of the data and starts processing deletion in the callback 141 if (this.menuContext.albumInfo && this.menuContext.albumInfo.isTrashAlbum) { 142 this.menuContext.selectManager.getDeleteSelection(this); 143 } else { 144 this.menuContext.selectManager.getSelection(this); 145 } 146 147 // 3. onDeleteStart exit selection mode 148 let onOperationStart: Function = this.menuContext.onOperationStart; 149 onOperationStart && onOperationStart(); 150 let msg = { 151 'type': BigDataConstants.DELETE_LOCAL_ONLY, 152 'FovMode': 0 153 } 154 ReportToBigDataUtil.report(BigDataConstants.DELETE_TYPE_ID, msg); 155 156 this.menuContext.broadCast.emit(BroadCastConstants.DELETE_PROGRESS_DIALOG, 157 [$r('app.string.action_delete'), this.count]); 158 } 159 160 cancelCallback(): void { 161 Log.info(TAG, 'Batch delete cancel'); 162 this.setConfirmText(); 163 if (!this.menuContext.fromSelectMode) { 164 this.menuContext.selectManager.deSelectAll(); 165 } 166 } 167}