• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 photoAccessHelper from '@ohos.file.photoAccessHelper';
17import { userFileModel } from '../base/UserFileModel';
18import { selectManager } from './SelectManager';
19import { Log } from '../utils/Log';
20import { MediaConstants } from '../constants/MediaConstants';
21import dataSharePredicates from '@ohos.data.dataSharePredicates';
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  innerId: number;
37  selectType: number = MediaConstants.SELECT_TYPE_ALL;
38  deviceId: string = '';
39  isSelect: boolean = false;
40  status: number = MediaConstants.UNDEFINED;
41  objectIndex: number;
42  albumType: number;
43  albumSubType: number;
44  fileUir: string = undefined;
45  fileAsset: photoAccessHelper.PhotoAsset;
46  thumbnail: PixelMap = undefined;
47
48  constructor(id: string, count: number, displayName: string, selectType: number, deviceId: string, albumType: number, albumSubType: number) {
49    this.id = id;
50    this.displayName = displayName;
51    this.count = count;
52    this.isDisableRename = MediaConstants.ALBUM_DISABLE_RENAME_LIST.has(id);
53    this.isDisableDelete = MediaConstants.ALBUM_DISABLE_DELETE_LIST.has(id);
54    this.selectType = selectType;
55    this.deviceId = deviceId;
56    this.objectIndex = objectIndex++;
57    this.albumType = albumType;
58    this.albumSubType = albumSubType;
59  }
60
61  getHashCode(): string {
62    return this.objectIndex + '' + this.id + ' ' + this.orientation + ' ' + this.isSelect;
63  }
64
65  async load(): Promise<void> {
66    if (this.status >= MediaConstants.LOADED) {
67      return;
68    }
69    if (this.fileUir !== undefined && this.fileUir !== null) {
70      let fileAsset = (await userFileModel.getMediaItemByUri(this.fileUir));
71      await this.update(fileAsset);
72    }
73  }
74
75  async update(fileAsset: photoAccessHelper.PhotoAsset): Promise<void> {
76    Log.info(TAG, 'this.uri ' + this.displayName);
77    Log.info(TAG, 'this.uri ' + this.uri);
78    if (fileAsset != null) {
79      this.fileUir = fileAsset.uri;
80      this.fileAsset = fileAsset;
81      if (this.fileAsset != null) {
82        await this.getThumbnail();
83      }
84      Log.info(TAG, 'this.fileUri ' + this.fileUir);
85    }
86    this.status = MediaConstants.LOADED;
87    this.isSelect = selectManager.isSelect(this.id, this.isSelect);
88  }
89
90  async getThumbnail(): Promise<PixelMap> {
91    if (this.thumbnail == undefined) {
92      let size = { width: MediaConstants.DEFAULT_SIZE, height: MediaConstants.DEFAULT_SIZE };
93      try {
94        this.thumbnail = await this.fileAsset.getThumbnail(size)
95      } catch (err) {
96        Log.error(TAG, 'getThumbnail error: ' + JSON.stringify(err));
97      }
98    }
99    return this.thumbnail;
100  }
101
102  async getVideoCount(): Promise<number> {
103    if (this.selectType === MediaConstants.SELECT_TYPE_IMAGE) {
104      return 0;
105    }
106    let fileAssets: photoAccessHelper.PhotoAsset[] = [];
107    let albumPredicates = new dataSharePredicates.DataSharePredicates();
108    albumPredicates.equalTo(photoAccessHelper.AlbumKeys.ALBUM_NAME, this.displayName)
109    let albumFetchOption = {
110      fetchColumns: MediaConstants.EMPTY_FETCH_COLUMNS,
111      predicates: albumPredicates
112    };
113    let predicates = new dataSharePredicates.DataSharePredicates();
114    predicates.equalTo(photoAccessHelper.PhotoKeys.PHOTO_TYPE, photoAccessHelper.PhotoType.VIDEO)
115    predicates.orderByDesc(photoAccessHelper.PhotoKeys.DATE_ADDED);
116    let fileFetchOption = {
117      fetchColumns: MediaConstants.FILE_ASSET_FETCH_COLUMNS,
118      predicates: predicates
119    };
120    fileAssets = await userFileModel.getAllMediaItemsByType(this.albumType, this.albumSubType, albumFetchOption, fileFetchOption);
121    return fileAssets.length;
122  }
123
124  setSelect(isSelect: boolean): void {
125    this.isSelect = isSelect;
126    selectManager.setSelect(this.id, this.isSelect);
127  }
128
129  async onDelete(): Promise<boolean> {
130    try {
131      Log.error(TAG, this.displayName);
132      await userFileModel.deleteAlbum(this.displayName);
133      selectManager.deleteSelect(this.uri);
134      this.status = MediaConstants.TRASHED;
135      return true;
136    } catch (err) {
137      Log.error(TAG, 'onDelete error: ' + JSON.stringify(err));
138      return false;
139    }
140  }
141
142  isDeleted(): boolean {
143    return this.status === MediaConstants.TRASHED;
144  }
145}
146