• 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';
16
17const TAG = "ItemDataSource"
18
19export class LazyItem<T> {
20    item: T
21    onItemUpdate: Function
22    index: number = -1
23
24    constructor(item:T, index: number, onItemUpdate?: Function) {
25        this.item = item
26        this.onItemUpdate = onItemUpdate
27        this.index = index
28    }
29
30    update(item: T){
31        if (this.onItemUpdate && this.index != -1) {
32            this.onItemUpdate(this.index, item)
33        }
34    }
35
36    getHashCode(): string{
37        // @ts-ignore
38        return `${this.index}` + this.item.getHashCode()
39    }
40
41    get() :T{
42        return this.item
43    }
44
45    set(item:T){
46        this.item = item
47    }
48
49}
50
51export abstract class ItemDataSource implements IDataSource {
52    private listeners: DataChangeListener[] = [];
53
54    abstract totalCount(): number;
55
56    abstract getData(index: number): any;
57
58    abstract isSelect(): boolean;
59
60    abstract getSelectedCount(): number;
61
62    abstract getSelectedItems(): any[];
63
64    abstract getSelectedUris(): any[];
65
66    abstract dataRemove(): void;
67
68    registerDataChangeListener(listener: DataChangeListener): void {
69        Log.info(TAG, 'registerDataChangeListener');
70        if (this.listeners.indexOf(listener) < 0) {
71            this.listeners.push(listener);
72            Log.info(TAG, `registerDataChangeListener, add listener, length: ${this.listeners.length}`);
73        }
74    }
75
76    unregisterDataChangeListener(listener: DataChangeListener): void {
77        Log.info(TAG, 'unregisterDataChangeListener');
78        const pos = this.listeners.indexOf(listener);
79        if (pos >= 0) {
80            this.listeners.splice(pos, 1);
81            Log.info(TAG, `registerDataChangeListener, remove listener, length: ${this.listeners.length}`);
82        }
83    }
84
85    notifyDataChange(index: number): void {
86        this.listeners.forEach(listener => {
87            listener.onDataChange(index);
88        })
89    }
90
91    notifyDataReload(): void {
92        this.listeners.forEach(listener => {
93            listener.onDataReloaded();
94        })
95    }
96
97    notifyDataDelete(index: number): void {
98        this.listeners.forEach(listener => {
99            listener.onDataDelete(index);
100        })
101    }
102}