1/* 2 * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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 { userFileModel } from '../base/UserFileModel'; 17import { Log } from '../utils/Log'; 18import { MenuContext } from './MenuContext'; 19import { ProcessMenuOperation, FindSameOperation } from './ProcessMenuOperation'; 20import { MediaOperationType } from '../models/MediaOperationType'; 21import { BroadcastConstants } from '../constants/BroadcastConstants'; 22import { ItemDataSource } from '../common/ItemDataSource'; 23import { UserFileDataItem } from '../base/UserFileDataItem'; 24import { SimpleAlbumDataItem } from '../common/SimpleAlbumDataItem'; 25 26const TAG = 'MoveMenuOperation'; 27 28export class MoveMenuOperation extends ProcessMenuOperation { 29 albumInfo: SimpleAlbumDataItem; 30 31 constructor(menuContext: MenuContext) { 32 super(menuContext); 33 this.albumInfo = menuContext.albumInfo; 34 } 35 36 doAction(): void { 37 Log.info(TAG, 'move doAction'); 38 if (this.menuContext == null) { 39 Log.warn(TAG, 'menuContext is null, return'); 40 return; 41 } 42 43 let dataSource: ItemDataSource = this.menuContext.dataSource; 44 if (dataSource == null) { 45 this.count = this.menuContext.items.length; 46 } else { 47 this.count = dataSource.getSelectedCount(); 48 } 49 if (this.count <= 0) { 50 Log.warn(TAG, 'count <= 0, return'); 51 return; 52 } 53 54 this.onOperationEnd = this.menuContext.onOperationEnd; 55 let onOperationStart = this.menuContext.onOperationStart; 56 57 if (onOperationStart != null) onOperationStart(); 58 59 this.menuContext.broadCast.emit( 60 BroadcastConstants.SHOW_PROGRESS_DIALOG, [$r('app.string.move_progress_message', this.albumInfo.displayName), 61 MediaOperationType.Move, (): void => this.cancelFuncBindImpl()]); 62 63 if (dataSource == null) { 64 this.items = this.menuContext.items; 65 } else { 66 this.items = dataSource.getSelectedItems(); 67 } 68 this.processOperation(); 69 } 70 71 requestOneBatchOperation(): void { 72 let item = this.items[this.currentBatch] as UserFileDataItem; 73 if (item != null) { 74 item.addToAlbum(this.albumInfo.uri).then<void, void>((): void => { 75 this.currentBatch++; 76 this.menuContext.broadCast.emit(BroadcastConstants.UPDATE_PROGRESS, [this.getExpectProgress(), this.currentBatch]); 77 this.cyclicOperation(); 78 }) 79 } 80 } 81 82 cancelFunc(): void { 83 this.cancelFuncBindImpl(); 84 } 85 86 private cancelFuncBindImpl(): void { 87 Log.info(TAG, 'progress cancel'); 88 this.onOperatePause(); 89 let cancelMessage = $r('app.string.move_cancel_message', this.getExpectProgress().toString()); 90 if (this.menuContext.broadCast != null) { 91 this.menuContext.broadCast.emit(BroadcastConstants.CANCEL_OPERATE, 92 [cancelMessage, (): void => this.onOperateContinueBindImpl(), (): void => this.onOperateCancelledBindImpl()]); 93 } 94 } 95 96 // Move cancel callback 97 onOperateContinue(): void { 98 this.onOperateContinueBindImpl(); 99 } 100 101 private onOperateContinueBindImpl(): void { 102 Log.info(TAG, 'Operate Continue'); 103 this.isPause = false; 104 this.cyclicOperation(); 105 } 106} 107