• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 { Log } from '@ohos/base/src/main/ets/utils/Log';
17import { WindowConstants } from '@ohos/base/src/main/ets/constants/WindowConstants';
18import mediaModel from '@ohos/base/src/main/ets/model/MediaModel';
19import screenManager from '@ohos/base/src/main/ets/manager/ScreenManager';
20import { TimelineDataItem } from '../data/TimelineDataItem';
21import { MediaDataItem } from '@ohos/base/src/main/ets/data/MediaDataItem';
22
23const TAG = "TimelineDataImpl"
24
25export class TimelineDataImpl {
26    async getTimelineItemData(): Promise<TimelineDataItem[]> {
27        Log.info(TAG, "getTimelineItemData");
28        let fileAssets: Array<MediaLib.FileAsset> = await this.getTimelineItemFileAssets();
29
30        let count = this.getCount();
31        let mediaFileAsset: Array<MediaLib.FileAsset> = await this.getMediaItemFileAssets(0, count);
32        let itemGroup: TimelineDataItem[] = [];
33        let index = 0;
34        fileAssets.forEach((fileAsset: MediaLib.FileAsset) => {
35            let item: TimelineDataItem = new TimelineDataItem(fileAsset, mediaFileAsset, index);
36            index += item.groupChild.length;
37            itemGroup.push(item);
38        })
39
40        // do not use await to avoid load cost too much time
41        this.loadReset(itemGroup, count);
42        return itemGroup;
43    }
44
45    private async getTimelineItemFileAssets(): Promise<MediaLib.FileAsset[]> {
46        let selections: string = MediaLib.FileKey.MEDIA_TYPE + ' = ? or ' + MediaLib.FileKey.MEDIA_TYPE + ' = ?';
47        let selectionArgs: Array<string> = [MediaLib.MediaType.IMAGE.toString(), MediaLib.MediaType.VIDEO.toString()];
48        let fetchOption: MediaLib.MediaFetchOptions = {
49            selections: selections,
50            selectionArgs: selectionArgs,
51            extendArgs: 'DATE(date_added,"unixepoch")',
52            order: `date_added DESC`
53        };
54        return await mediaModel.getAllMediaItems(fetchOption);
55    }
56
57    private async getMediaItemFileAssets(start: number, count: number): Promise<MediaLib.FileAsset[]> {
58        let selections: string = MediaLib.FileKey.MEDIA_TYPE + ' = ? or ' + MediaLib.FileKey.MEDIA_TYPE + ' = ?';
59        let selectionArgs: Array<string> = [MediaLib.MediaType.IMAGE.toString(), MediaLib.MediaType.VIDEO.toString()];
60        let fetchOption: MediaLib.MediaFetchOptions = {
61            selections: selections,
62            selectionArgs: selectionArgs,
63            order: `date_added DESC LIMIT ${start},${count}`
64        };
65        return await mediaModel.getAllMediaItems(fetchOption);
66    }
67
68    private getCount(): number {
69        let sideBarWidth = screenManager.isSidebar() ? WindowConstants.TAB_BAR_WIDTH : 0;
70        let contentWidth = screenManager.getWinWidth() - sideBarWidth;
71        let maxThumbWidth = px2vp(WindowConstants.GRID_IMAGE_SIZE) * WindowConstants.GRID_MAX_SIZE_RATIO;
72        let columns = Math.max(WindowConstants.GRID_MIN_COUNT, Math.ceil((contentWidth + WindowConstants.GRID_GUTTER) / (maxThumbWidth + WindowConstants.GRID_GUTTER)));
73        let contentHeight = screenManager.getWinHeight() - WindowConstants.ACTION_BAR_HEIGHT - screenManager.getNaviBarHeight();
74        let rows = Math.ceil((contentHeight + WindowConstants.GRID_GUTTER) / (maxThumbWidth + WindowConstants.GRID_GUTTER)) + 4;
75        return columns * rows
76    }
77
78    private async loadReset(itemGroup: TimelineDataItem[], count) {
79        let items: MediaDataItem[] = [];
80        itemGroup.forEach((group: TimelineDataItem) => {
81            group.groupChild.forEach((child: MediaDataItem) => {
82                items.push(child);
83            })
84        })
85        let itemLen = items.length;
86        let countLen = Math.ceil(itemLen / count);
87        for (let i = 1;i < countLen; i++) {
88            let mediaFileAsset: Array<MediaLib.FileAsset> = await this.getMediaItemFileAssets(i * count, count)
89            for (let j = 0;j < count; j++) {
90                if (i * count + j >= itemLen) {
91                    return;
92                }
93                items[i * count+j].update(mediaFileAsset[j]);
94            }
95        }
96    }
97}