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 { BroadCastConstants } from '../../model/common/BroadCastConstants'; 17import { Constants } from '../../model/common/Constants'; 18import { AddMenuOperation } from './AddMenuOperation'; 19import { MenuContext } from './MenuContext'; 20import { MenuOperationFactory } from '../../interface/MenuOperationFactory'; 21import { MoveMenuOperation } from './MoveMenuOperation'; 22import { Log } from '../../utils/Log'; 23import router from '@ohos.router'; 24import { AlbumInfo } from '../../model/browser/album/AlbumInfo'; 25import type { BroadCast } from '../../utils/BroadCast'; 26import type { SelectManager } from '../../model/browser/SelectManager'; 27import { UserFileManagerAccess } from '../../access/UserFileManagerAccess'; 28import { AlbumDefine } from '../../model/browser/AlbumDefine'; 29 30const TAG: string = 'common_MoveOrCopyBroadCastProp'; 31 32export class MoveOrCopyBroadCastProp { 33 private broadCast: BroadCast; 34 35 private constructor() { 36 } 37 38 /** 39 * Get MoveOrCopyBroadCastProp instance 40 */ 41 public static getInstance(): MoveOrCopyBroadCastProp { 42 if (AppStorage.Get(Constants.INSTANCE_MOVE_OR_COPY_BROADCAST_PROP) == null) { 43 AppStorage.SetOrCreate(Constants.INSTANCE_MOVE_OR_COPY_BROADCAST_PROP, new MoveOrCopyBroadCastProp()); 44 } 45 return AppStorage.Get(Constants.INSTANCE_MOVE_OR_COPY_BROADCAST_PROP); 46 } 47 48 /** 49 * sendMoveOrCopyBroadCast 50 * 51 * @param broadCast broadCast 52 */ 53 public sendMoveOrAddBroadCast(broadCast: BroadCast) { 54 if (broadCast === null || broadCast === undefined) { 55 Log.error(TAG, 'sendMoveOrAddBroadCast error: null or undefined broadcast'); 56 return; 57 } 58 this.setBroadCast(broadCast); 59 60 // 如果是系统相册,则直接添加到,而不是弹窗选择添加还是移动 61 let isSystemAlbumSource: boolean = false; 62 let sourceAlbumUri: string = AppStorage.Get(Constants.APP_KEY_NEW_ALBUM_SOURCE); 63 let systemAlbumUris: IterableIterator<string> = UserFileManagerAccess.getInstance().getSystemAlbumUriMap().values(); 64 for (let uri of systemAlbumUris) { 65 if (sourceAlbumUri === uri) { 66 isSystemAlbumSource = true; 67 break; 68 } 69 } 70 if (isSystemAlbumSource) { 71 this.addOperation(); 72 } else { 73 this.broadCast.emit(BroadCastConstants.SHOW_COPY_OR_MOVE_DIALOG, 74 [this.moveOperation.bind(this), this.addOperation.bind(this)]); 75 } 76 } 77 78 /** 79 * DoCopyOperation 80 * 81 * @param broadCast broadCast 82 */ 83 public doAddOperation(broadCast: BroadCast) { 84 if (broadCast === null || broadCast === undefined) { 85 Log.error(TAG, 'doCopyOperation error: null or undefined broadcast'); 86 return; 87 } 88 this.setBroadCast(broadCast); 89 this.addOperation(); 90 } 91 92 private setBroadCast(broadCast: BroadCast) { 93 this.broadCast = broadCast; 94 } 95 96 private async addOperation() { 97 let selectManager: SelectManager = AppStorage.Get(Constants.APP_KEY_NEW_ALBUM_SELECTED); 98 let targetAlbumName: string = AppStorage.Get(Constants.APP_KEY_NEW_ALBUM_TARGET); 99 let targetAlbumUri: string = AppStorage.Get(Constants.APP_KEY_NEW_ALBUM_TARGET_URI); 100 if (this.broadCast === null || this.broadCast === undefined) { 101 Log.error(TAG, 'addOperation error: null or undefined broadcast'); 102 return; 103 } 104 105 let menuContext = new MenuContext(); 106 this.onOperationStart = this.onOperationStart.bind(this); 107 this.onOperationEnd = this.onOperationEnd.bind(this); 108 menuContext 109 .withSelectManager(selectManager) 110 .withOperationStartCallback(this.onOperationStart) 111 .withOperationEndCallback(this.onOperationEnd) 112 .withBroadCast(this.broadCast) 113 menuContext.withTargetAlbumName(targetAlbumName).withAlbumUri(targetAlbumUri); 114 let menuOperation = MenuOperationFactory.getInstance().createMenuOperation(AddMenuOperation, menuContext); 115 menuOperation.doAction(); 116 } 117 118 private async moveOperation() { 119 let selectManager: SelectManager = AppStorage.Get(Constants.APP_KEY_NEW_ALBUM_SELECTED); 120 let targetAlbumName: string = AppStorage.Get(Constants.APP_KEY_NEW_ALBUM_TARGET); 121 let targetAlbumUri: string = AppStorage.Get(Constants.APP_KEY_NEW_ALBUM_TARGET_URI); 122 if (this.broadCast === null || this.broadCast === undefined) { 123 Log.error(TAG, 'moveOperation error: null or undefined broadcast'); 124 return; 125 } 126 127 let menuContext = new MenuContext(); 128 this.onOperationStart = this.onOperationStart.bind(this); 129 this.onOperationEnd = this.onOperationEnd.bind(this); 130 menuContext 131 .withSelectManager(selectManager) 132 .withOperationStartCallback(this.onOperationStart) 133 .withOperationEndCallback(this.onOperationEnd) 134 .withBroadCast(this.broadCast) 135 menuContext.withTargetAlbumName(targetAlbumName).withAlbumUri(targetAlbumUri); 136 let menuOperation = MenuOperationFactory.getInstance().createMenuOperation(MoveMenuOperation, menuContext); 137 menuOperation.doAction(); 138 } 139 140 private onOperationStart(): void { 141 AppStorage.SetOrCreate(Constants.IS_DATA_FREEZE, true); 142 } 143 144 private async onOperationEnd(): Promise<void> { 145 AppStorage.SetOrCreate(Constants.IS_DATA_FREEZE, false); 146 let isNewAlbum: boolean = AppStorage.Get(Constants.APP_KEY_NEW_ALBUM); 147 let selectManager: SelectManager = AppStorage.Get(Constants.APP_KEY_NEW_ALBUM_SELECTED); 148 let targetAlbumName: string = AppStorage.Get(Constants.APP_KEY_NEW_ALBUM_TARGET); 149 150 let album = await UserFileManagerAccess.getInstance().getAlbumByName(targetAlbumName); 151 let albumInfo: AlbumInfo = new AlbumInfo(album); 152 albumInfo.setAlbumName(targetAlbumName); 153 let fetchOpt = AlbumDefine.getFileFetchOpt(); 154 let fetchResult = await album.getPhotoAssets(fetchOpt); 155 let count = fetchResult.getCount(); 156 fetchResult.close(); 157 albumInfo.setCount(count); 158 159 if (isNewAlbum) { 160 selectManager.onModeChange(false); 161 AppStorage.Delete(Constants.APP_KEY_NEW_ALBUM); 162 AppStorage.Delete(Constants.APP_KEY_NEW_ALBUM_TARGET); 163 AppStorage.Delete(Constants.APP_KEY_NEW_ALBUM_TARGET_URI); 164 AppStorage.Delete(Constants.APP_KEY_NEW_ALBUM_SELECTED); 165 router.pushUrl({ 166 url: 'pages/PhotoGridPage', 167 params: { 168 item: JSON.stringify(albumInfo), 169 } 170 }); 171 } else { 172 AppStorage.SetOrCreate(Constants.KEY_OF_PHOTO_GRID_VIEW_ALBUM_ITEM, { item: JSON.stringify(albumInfo) }); 173 AppStorage.SetOrCreate(Constants.KEY_OF_ALBUM_URI, albumInfo.uri); 174 router.back({ 175 url: 'pages/index', 176 }); 177 } 178 } 179}