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 MediaLib from '@ohos.multimedia.mediaLibrary'; 16import mediaModel from '../model/MediaModel'; 17 18import selectManager from '../manager/SelectManager'; 19import { Log } from '../utils/Log'; 20import { MediaConstants } from '../constants/MediaConstants'; 21import { getFetchOptions } from '../helper/MediaDataHelper'; 22 23const TAG = "AlbumDataItem" 24 25let objectIndex = 0 26 27export class AlbumDataItem { 28 index: number; 29 id: string; 30 uri: string; 31 orientation: number; 32 displayName: string; 33 count: number; 34 isDisableRename: boolean; 35 isDisableDelete: boolean; 36 relativePath: string; 37 innerId: number; 38 selectType: number = MediaConstants.SELECT_TYPE_ALL; 39 deviceId: string = ""; 40 isSelect: boolean = false; 41 status: number = MediaConstants.UNDEFINED; 42 objectIndex: number 43 44 constructor(id: string, count: number, displayName: string, selectType: number, deviceId: string) { 45 this.id = id; 46 this.displayName = displayName; 47 this.count = count; 48 this.isDisableRename = MediaConstants.ALBUM_DISABLE_RENAME_LIST.has(id); 49 this.isDisableDelete = MediaConstants.ALBUM_DISABLE_DELETE_LIST.has(id); 50 this.selectType = selectType; 51 this.deviceId = deviceId; 52 this.objectIndex = objectIndex++ 53 } 54 55 getHashCode(): string { 56 return `${this.objectIndex}${this.id} ${this.orientation} ${this.isSelect}` 57 } 58 59 async load(): Promise<void> { 60 if (this.status >= MediaConstants.LOADED) { 61 return; 62 } 63 let fetchOption: MediaLib.MediaFetchOptions = await getFetchOptions(this.selectType, this.id, this.deviceId); 64 let fileAsset = (await mediaModel.getAllMediaItem(this.id, fetchOption, false)).fileAsset; 65 this.update(fileAsset); 66 } 67 68 update(fileAsset: MediaLib.FileAsset) { 69 this.uri = fileAsset.uri; 70 this.orientation = fileAsset.orientation; 71 this.relativePath = fileAsset.relativePath; 72 73 this.status = MediaConstants.LOADED; 74 this.isSelect = selectManager.isSelect(this.id, this.isSelect); 75 } 76 77 async getRelativePath(): Promise<string> { 78 await this.load(); 79 return this.relativePath; 80 } 81 82 getThumbnail(): string { 83 Log.debug(TAG, `this.uri ${this.uri}`); 84 return this.uri + `/thumbnail/256/256`; 85 } 86 87 async getVideoCount(): Promise<number> { 88 if (this.selectType == MediaConstants.SELECT_TYPE_IMAGE) { 89 return 0; 90 } 91 let videoFetchOption: MediaLib.MediaFetchOptions = await getFetchOptions(MediaConstants.SELECT_TYPE_VIDEO, this.id, this.deviceId); 92 return (await mediaModel.getAllMediaItem(this.id, videoFetchOption, true)).counts; 93 } 94 95 setSelect(isSelect: boolean) { 96 this.isSelect = isSelect; 97 selectManager.setSelect(this.id, this.isSelect); 98 } 99 100 async onDelete(): Promise<boolean> { 101 try { 102 let fetchOption: MediaLib.MediaFetchOptions = await getFetchOptions(this.selectType, this.id, ""); 103 await mediaModel.deleteAll(fetchOption); 104 selectManager.deleteSelect(this.uri); 105 this.status = MediaConstants.TRASHED; 106 return true; 107 } catch (err) { 108 Log.error(TAG, `onDelete error: ${JSON.stringify(err)}`); 109 return false; 110 } 111 } 112 113 isDeleted(): boolean { 114 return this.status == MediaConstants.TRASHED; 115 } 116}