• 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 { Log } from '../utils/Log';
16import { GroupDataImpl } from '../model/GroupDataImpl';
17import { LazyItem, ItemDataSource } from '../vm/ItemDataSource';
18import { MediaDataItem } from '../data/MediaDataItem';
19
20const TAG = "GroupItemDataSource"
21
22export class GroupItemDataSource extends ItemDataSource {
23    groupDataItem: MediaDataItem[] = [];
24    private groupDataImpl: GroupDataImpl = new GroupDataImpl();
25
26    constructor() {
27        super()
28    }
29
30    setSelectType(selectType: number) {
31        this.groupDataImpl.setSelectType(selectType);
32    }
33
34    setAlbumId(id: string) {
35        Log.info(TAG, `setAlbumId: ${id}`);
36        this.groupDataImpl.setAlbumId(id);
37    }
38
39    setDeviceId(id: string) {
40        Log.info(TAG, `setDeviceId: ${id}`);
41        this.groupDataImpl.setDeviceId(id);
42    }
43
44    totalCount(): number {
45        return this.groupDataItem.length;
46    }
47
48    getIndexByItem(item: MediaDataItem): number {
49        let index = -1;
50        let length = this.groupDataItem.length;
51        for (let i = 0;i < length; i++) {
52            if (this.groupDataItem[i].uri == item.uri) {
53                index = i;
54                break;
55            }
56        }
57        return index;
58    }
59
60    getData(index: number): LazyItem<MediaDataItem> {
61        if (index < 0 || index >= this.groupDataItem.length) {
62            Log.warn(TAG, `${index}/${this.groupDataItem.length}`);
63            return undefined;
64        }
65        if (this.groupDataItem[index] != null && this.groupDataItem[index] != undefined) {
66            this.groupDataItem[index].index = index;
67        }
68        return new LazyItem<MediaDataItem>(this.groupDataItem[index], index, this.onDataUpdate.bind(this));
69    }
70
71    getDataByIndex(index: number): MediaDataItem {
72        if (index < 0 || index >= this.groupDataItem.length) {
73            Log.warn(TAG, `${index}/${this.groupDataItem.length}`);
74            return undefined;
75        }
76        if (this.groupDataItem[index] != null && this.groupDataItem[index] != undefined) {
77            this.groupDataItem[index].index = index;
78        }
79        return this.groupDataItem[index];
80    }
81
82    isSelect(): boolean {
83        let isSelect = true;
84        for (let i = 0;i < this.groupDataItem.length; i++) {
85            if (!this.groupDataItem[i].isSelect) {
86                isSelect = false;
87                break;
88            }
89        }
90        return isSelect;
91    }
92
93    getSelectedCount(): number {
94        let count = 0;
95        this.groupDataItem.forEach((item: MediaDataItem) => {
96            if (item.isSelect) {
97                count++;
98            }
99        })
100        return count;
101    }
102
103    getItems(): MediaDataItem[] {
104        let items: MediaDataItem[] = [];
105        this.groupDataItem.forEach((item: MediaDataItem) => {
106            items.push(item);
107        })
108        return items;
109    }
110
111    getSelectedItems(): MediaDataItem[] {
112        let items: MediaDataItem[] = [];
113        this.groupDataItem.forEach((item: MediaDataItem) => {
114            if (item.isSelect) {
115                items.push(item);
116            }
117        })
118        return items;
119    }
120
121    getSelectedUris(): string[] {
122        let uris: string[] = [];
123        this.groupDataItem.forEach((item: MediaDataItem) => {
124            if (item.isSelect) {
125                uris.push(item.uri);
126            }
127        })
128        return uris;
129    }
130
131    setSelect(isSelect: boolean) {
132        this.groupDataItem.forEach((item: MediaDataItem) => {
133            item.setSelect(isSelect);
134        })
135        this.notifyDataReload();
136    }
137
138    async reloadGroupItemData(isGrid: boolean): Promise<boolean> {
139        this.groupDataItem = await this.groupDataImpl.reloadGroupItemData(isGrid);
140        return this.groupDataItem.length == 0;
141    }
142
143    onDataUpdate(index: number): void {
144        Log.debug(TAG, `onDataUpdate ${index}`);
145        if (index != -1) {
146            this.notifyDataChange(index);
147        }
148    }
149
150    dataReload(isGrid: boolean) {
151        this.reloadGroupItemData(isGrid).then((isEmpty: boolean) => {
152            this.notifyDataReload();
153        })
154    }
155
156    dataRemove() {
157        for (let i = this.groupDataItem.length - 1;i >= 0; i--) {
158            if (this.groupDataItem[i] != undefined && this.groupDataItem[i].isDeleted()) {
159                this.groupDataItem.splice(i, 1);
160                super.notifyDataDelete(i);
161            }
162        }
163    }
164
165    dataDelete(uri: string) {
166        const mediaDataItemIndex = this.groupDataItem.findIndex((item: MediaDataItem) => {
167            return item.uri === uri;
168        })
169        if (mediaDataItemIndex != -1 && this.groupDataItem[mediaDataItemIndex].isDeleted()) {
170            this.groupDataItem.splice(mediaDataItemIndex, 1);
171            super.notifyDataDelete(mediaDataItemIndex);
172        }
173    }
174}