• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// @ts-nocheck
2/*
3 * Copyright (c) 2022 Huawei Device Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16import MediaLib from '@ohos.multimedia.mediaLibrary';
17import { Log } from '@ohos/base/src/main/ets/utils/Log';
18import { ViewType } from '@ohos/base/src/main/ets/data/ViewType';
19import { MediaDataItem } from '@ohos/base/src/main/ets/data/MediaDataItem';
20import mediaDataItemCache from '@ohos/base/src/main/ets/data/MediaDataItemCache';
21import { ItemDataSource } from '@ohos/base/src/main/ets/vm/ItemDataSource';
22
23const TAG = "TimelineDataItem"
24
25export class TimelineDataItem {
26    readonly viewType = ViewType.GROUP_TITLE;
27    dateAdded: number;
28    groupChild: MediaDataItem[] = [];
29
30    constructor(fileAssert: MediaLib.FileAsset, mediaFileAsset: Array<MediaLib.FileAsset>, index: number) {
31        Log.info(TAG, `construct ${fileAssert.dateAdded}:${fileAssert.count}`);
32        this.dateAdded = fileAssert.dateAdded * 1000;
33        this.groupChild = new Array(fileAssert.count);
34        let selections: string = MediaLib.FileKey.MEDIA_TYPE + ' = ? or ' + MediaLib.FileKey.MEDIA_TYPE + ' = ?';
35        let selectionArgs: Array<string> = [MediaLib.MediaType.IMAGE.toString(), MediaLib.MediaType.VIDEO.toString()];
36        for (let i = 0;i < this.groupChild.length; i++) {
37            this.groupChild[i] = new MediaDataItem(selections, selectionArgs, "", index + i)
38            if (index + i < mediaFileAsset.length) {
39                if (mediaDataItemCache.hasKey(mediaFileAsset[index+i].uri)) {
40                    this.groupChild[i] = mediaDataItemCache.get(mediaFileAsset[index+i].uri);
41                } else {
42                    mediaDataItemCache.set(mediaFileAsset[index+i].uri, this.groupChild[i]);
43                }
44                this.groupChild[i].update(mediaFileAsset[index+i]);
45            }
46        }
47    }
48
49    getHashCode(): string {
50        return `${this.dateAdded}` + `${this.isSelect()}` + `${this.groupChild.length}`;
51    }
52
53    isSelect(): boolean {
54        let status = true;
55        for (let i = 0;i < this.groupChild.length; i++) {
56            if (!this.groupChild[i].isSelect) {
57                status = false;
58                break;
59            }
60        }
61        return status;
62    }
63
64    setSelect(isSelect: boolean) {
65        this.groupChild.forEach((child: MediaDataItem) => {
66            child.setSelect(isSelect);
67        })
68    }
69
70    getSelectedCount(): number{
71        let count = 0;
72        this.groupChild.forEach((child: MediaDataItem) => {
73            if (child.isSelect) {
74                count++;
75            }
76        })
77        return count;
78    }
79
80    dataRemove(): number[] {
81        let removeList: number[] = [];
82        for (let i = this.groupChild.length - 1;i >= 0; i--) {
83            if (this.groupChild[i].isDeleted()) {
84                this.groupChild.splice(i, 1);
85                removeList.push(i);
86            }
87        }
88        return removeList;
89    }
90}