• 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 { DistributedDataImpl } from '../model/DistributedDataImpl'
16import { ItemDataSource } from './ItemDataSource'
17import { PeerDataItem } from '../data/PeerDataItem'
18import { Log } from '../utils/Log';
19
20const TAG = "DistributedDataSource"
21
22export class DistributedDataSource extends ItemDataSource {
23    private peerDataItems: PeerDataItem[] = [];
24    private distributedDataImpl: DistributedDataImpl = new DistributedDataImpl();
25
26    totalCount(): number {
27        return this.peerDataItems.length;
28    }
29
30    getData(index: number): any{
31        this.peerDataItems[index].index = index;
32        return this.peerDataItems[index];
33    }
34
35    isSelect(): boolean {
36        let isSelect = true;
37        for (let i = 0;i < this.peerDataItems.length; i++) {
38            if (!this.peerDataItems[i].isSelect) {
39                isSelect = false;
40                break;
41            }
42        }
43        return isSelect;
44    }
45
46    setSelect(isSelect: boolean) {
47        this.peerDataItems.forEach((item: PeerDataItem) => {
48            item.setSelect(isSelect);
49        })
50    }
51
52    getSelectedUris(): string[]{
53        let uris: string[] = [];
54        this.peerDataItems.forEach((item: PeerDataItem) => {
55            if (item.isSelect) {
56                uris.push(item.uri);
57            }
58        })
59        return uris;
60    }
61
62    getSelectedCount(): number {
63        let count = 0;
64        for (let i = 0;i < this.peerDataItems.length; i++) {
65            if (this.peerDataItems[i].isSelect) {
66                count++;
67            }
68        }
69        return count;
70    }
71
72    getSelectedItems(): any[]{
73        return [];
74    }
75
76    onDataUpdate(index: number): void {
77        Log.info(TAG, `onDataUpdate ${index}`);
78        this.notifyDataChange(index);
79    }
80
81    dataReload(): void {
82        this.reloadAlbumItemData().then((isEmpty: number) => {
83            this.notifyDataReload();
84        })
85    }
86
87    dataRemove(): void {
88    }
89
90    async reloadAlbumItemData(): Promise<number> {
91        let before = this.peerDataItems.length;
92        this.peerDataItems = await this.distributedDataImpl.reloadAlbumItemData();
93        return this.peerDataItems.length - before;
94    }
95}