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 */ 15 16import type { AsyncCallback, MenuOperation, MenuOperationCallback } from '@ohos/common'; 17import { 18 AlbumDefine, 19 AlbumInfo, 20 BigDataConstants, 21 BroadCastConstants, 22 BrowserDataFactory, 23 BrowserOperationFactory, 24 DateUtil, 25 Log, 26 MenuContext, 27 ReportToBigDataUtil, 28 SelectManager, 29 UiUtil, 30 UserFileManagerAccess 31} from '@ohos/common'; 32import userFileManager from '@ohos.filemanagement.userFileManager'; 33 34const TAG: string = 'browser_AlbumSetRenameMenuOperation'; 35 36export class AlbumSetRenameMenuOperation implements MenuOperation, MenuOperationCallback { 37 private menuContext: MenuContext; 38 private sourceAlbum; 39 private onOperationEnd: Function; 40 private dataImpl = BrowserDataFactory.getFeature(BrowserDataFactory.TYPE_ALBUM); 41 42 constructor(menuContext: MenuContext) { 43 this.menuContext = menuContext; 44 } 45 46 doAction(): void { 47 if (DateUtil.isEmpty(this.menuContext) || DateUtil.isEmpty(this.menuContext.fromSelectMode)) { 48 Log.error(TAG, 'menuContext or fromSelectMode is empty, return'); 49 return; 50 } 51 let msg = { 52 'Type': BigDataConstants.ALBUM_RENAME 53 }; 54 ReportToBigDataUtil.report(BigDataConstants.ALBUM_OPERATION_ID, msg); 55 let uri; 56 if (this.menuContext.fromSelectMode) { 57 let selectManager: SelectManager = this.menuContext.selectManager; 58 if (DateUtil.isEmpty(selectManager)) { 59 Log.error(TAG, 'selectManager is null, return'); 60 return; 61 } 62 let count = this.menuContext.selectManager.clickedSet.size; 63 if (count != 1) { 64 Log.error(TAG, 'count != 1, return'); 65 return; 66 } 67 uri = Array.from(this.menuContext.selectManager.clickedSet)[0]; 68 } else { 69 let albumInfo: AlbumInfo = this.menuContext.albumInfo; 70 if (DateUtil.isEmpty(albumInfo)) { 71 Log.error(TAG, 'albumInfo is null, return'); 72 return; 73 } 74 uri = albumInfo.uri; 75 } 76 this.confirmCallback = this.confirmCallback.bind(this); 77 this.cancelCallback = this.cancelCallback.bind(this); 78 79 UserFileManagerAccess.getInstance().getAlbumByUri(uri).then(async (album: userFileManager.Album) => { 80 this.sourceAlbum = album; 81 Log.info(TAG, `The name of clicked album is ${this.sourceAlbum.albumName}`); 82 83 this.menuContext.broadCast.emit(BroadCastConstants.SHOW_RENAME_PHOTO_DIALOG, 84 [this.sourceAlbum.albumName, this.confirmCallback, this.cancelCallback]); 85 }); 86 } 87 88 onCompleted(): void { 89 Log.info(TAG, 'Rename data succeed!'); 90 this.onOperationEnd && this.onOperationEnd(); 91 } 92 93 onError(): void { 94 Log.error(TAG, 'Rename data failed!'); 95 this.onOperationEnd && this.onOperationEnd(); 96 } 97 98 private async confirmCallback(newName: string) { 99 Log.info(TAG, `AlbumSet rename confirm and the new name is: ${newName}`); 100 101 this.onOperationEnd = this.menuContext.onOperationEnd; 102 let onOperationStart: Function = this.menuContext.onOperationStart; 103 onOperationStart && onOperationStart(); 104 105 this.rename(newName); 106 } 107 108 private async rename(name) { 109 try { 110 let targetAlbum = await UserFileManagerAccess.getInstance().getAlbumByName(name); 111 if (targetAlbum) { 112 UiUtil.showToast($r('app.string.name_already_use')); 113 this.onError(); 114 return; 115 } 116 await UserFileManagerAccess.getInstance().renameAlbum(this.sourceAlbum, name); 117 this.onCompleted(); 118 } catch (error) { 119 let msg = { 120 'Type': BigDataConstants.ALBUM_RENAME_ERROR, 121 'errMsg': JSON.stringify(error) 122 }; 123 ReportToBigDataUtil.errEventReport(BigDataConstants.ALBUM_OPERATION_ERROR_ID, msg); 124 Log.error(TAG, `AlbumSet rename failed: ${error}`); 125 this.onError(); 126 } 127 } 128 129 private cancelCallback(): void { 130 Log.info(TAG, 'AlbumSet rename cancel'); 131 } 132}