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'; 26 27const TAG: string = 'common_BatchRemoveMenuOperation'; 28 29export class BatchRemoveMenuOperation extends ProcessMenuOperation { 30 constructor(menuContext: MenuContext) { 31 super(menuContext); 32 //初始化绑定this指向 33 this.callback = this.callback.bind(this) 34 } 35 36 setConfirmText(): void { 37 AppStorage.setOrCreate<Resource>(Constants.CONFIRM_TEXT_KEY, $r('app.string.dialog_remove')); 38 } 39 40 doAction(): void { 41 if (this.menuContext == null) { 42 Log.error(TAG, 'menuContext is null, return'); 43 return; 44 } 45 let selectManager: SelectManager = this.menuContext.selectManager; 46 if (selectManager == null) { 47 Log.error(TAG, 'selectManager is null, return'); 48 return; 49 } 50 this.count = selectManager.getSelectedCount(); 51 if (this.count <= 0) { 52 Log.error(TAG, 'count <= 0, return'); 53 return; 54 } 55 56 this.confirmCallback = this.confirmCallback.bind(this); 57 this.cancelCallback = this.cancelCallback.bind(this); 58 this.setConfirmText(); 59 this.menuContext.broadCast.emit(BroadCastConstants.SHOW_REMOVE_DIALOG, 60 [selectManager.isAllSelected ? $r('app.string.remove_all_files_tips') : 61 (this.count == 1 ? $r('app.string.remove_single_file_tips') : 62 $r('app.string.remove_files_tips', this.count)), this.confirmCallback, this.cancelCallback]); 63 } 64 65 // Asynchronous callback for getSelection 66 callback(uris: string[]): void { 67 if (this.isCancelled) { 68 return; 69 } 70 this.uris = uris; 71 this.processOperation(); 72 } 73 74 // Remove a batch of data 75 requestOneBatchOperation(): void { 76 Log.info(TAG, `requestOneBatchOperation`); 77 if (this.isCancelled) { 78 return; 79 } 80 this.currentBatch++; 81 let startIndex: number = (this.currentBatch - 1) * this.BATCH_SIZE; 82 let endIndex: number = this.currentBatch * this.BATCH_SIZE; 83 let batchUris: string[] = this.uris.slice(startIndex, Math.min(endIndex, this.uris.length)); 84 85 let operationImpl = BrowserOperationFactory.getFeature(BrowserOperationFactory.TYPE_PHOTO); 86 TraceControllerUtils.startTraceWithTaskId('remove', this.currentBatch) 87 operationImpl.remove(batchUris, this.menuContext.albumUri).then(() => { 88 TraceControllerUtils.finishTraceWithTaskId('remove', this.currentBatch) 89 this.onCompleted() 90 }).catch((error) => { 91 Log.error(TAG, `remove error: ${error}`); 92 this.onError(); 93 }) 94 } 95 96 confirmCallback(): void { 97 Log.info(TAG, 'Batch remove confirm'); 98 99 // 1. Variable initialization 100 this.onOperationEnd = this.menuContext.onOperationEnd; 101 102 // 2. selectManager gets the URI of the data and starts processing deletion in the callback 103 this.menuContext.selectManager.getSelection(this); 104 105 // 3. onDeleteStart exit selection mode 106 let onOperationStart: Function = this.menuContext.onOperationStart; 107 onOperationStart && onOperationStart(); 108 let msg = { 109 'type': BigDataConstants.REMOVE, 110 'FovMode': 0 111 } 112 ReportToBigDataUtil.report(BigDataConstants.REMOVE_TYPE_ID, msg); 113 114 this.menuContext.broadCast.emit(BroadCastConstants.REMOVE_PROGRESS_DIALOG, 115 [$r('app.string.action_remove'), this.count]); 116 } 117 118 cancelCallback(): void { 119 Log.info(TAG, 'Batch delete cancel'); 120 this.setConfirmText(); 121 if (!this.menuContext.fromSelectMode) { 122 this.menuContext.selectManager.deSelectAll(); 123 } 124 } 125}