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 */ 15import { ItemDataSource } from '../vm/ItemDataSource'; 16import { MenuContext } from './MenuContext'; 17import { BatchDeleteMenuOperation } from './BatchDeleteMenuOperation'; 18import { Log } from '../utils/Log'; 19import { BroadcastConstants } from '../constants/BroadcastConstants'; 20 21const TAG = "ClearRecycleMenuOperation" 22 23export class ClearRecycleMenuOperation extends BatchDeleteMenuOperation { 24 constructor(menuContext: MenuContext) { 25 super(menuContext); 26 } 27 28 doAction(): void { 29 Log.info(TAG, 'delete doAction'); 30 if (this.menuContext == null) { 31 Log.warn(TAG, 'menuContext is null, return'); 32 return; 33 } 34 35 let dataSource: ItemDataSource = this.menuContext.dataSource; 36 if (dataSource == null) { 37 this.count = this.menuContext.items.length; 38 } else { 39 //@ts-ignore 40 this.count = dataSource.getItems().length; 41 } 42 if (this.count <= 0) { 43 Log.warn(TAG, 'count <= 0, return'); 44 return; 45 } 46 47 this.confirmCallback = this.confirmCallback.bind(this); 48 this.cancelCallback = this.cancelCallback.bind(this); 49 50 this.menuContext.broadCast.emit(BroadcastConstants.SHOW_DELETE_DIALOG, [$r('app.string.recycleAlbum_clear_message'), $r('app.string.dialog_clear'), this.confirmCallback, this.cancelCallback]); 51 } 52 53 confirmCallback(): void { 54 Log.info(TAG, 'Clear Recycle confirm'); 55 // 1. Variable initialization 56 this.onOperationEnd = this.menuContext.onOperationEnd; 57 58 // 2. onDeleteStart exit selection mode 59 let onOperationStart: Function = this.menuContext.onOperationStart; 60 onOperationStart && onOperationStart(); 61 62 this.menuContext.broadCast.emit(BroadcastConstants.DELETE_PROGRESS_DIALOG, 63 [$r('app.string.action_delete'), this.count]); 64 65 // 3. selectManager gets the URI of the data and starts processing deletion in the callback 66 let dataSource: ItemDataSource = this.menuContext.dataSource; 67 if (dataSource == null) { 68 this.items = this.menuContext.items; 69 } else { 70 //@ts-ignore 71 this.items = dataSource.getItems(); 72 } 73 this.processOperation(); 74 } 75 76}