• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 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 */
15
16import ArrayList from '@ohos.util.ArrayList';
17import { Information } from '../../model/Information';
18
19export class MyDataSource {
20  public dataArray: ArrayList<Information> = new ArrayList();
21  private listeners: DataChangeListener[] = [];
22
23  constructor(ele: ArrayList<Information>) {
24    for (let index = 0;index < ele.length; index++) {
25      this.dataArray.add(ele[index]);
26    }
27  }
28
29  public totalCount(): number {
30    return this.dataArray.length;
31  }
32
33  public getData(index: number): Information {
34    return this.dataArray[index];
35  }
36
37  public addData(index: number, data: Information): void {
38    this.dataArray.removeByIndex(index);
39    this.notifyDataAdd(index);
40  }
41
42  registerDataChangeListener(listener: DataChangeListener): void {
43    if (this.listeners.indexOf(listener) < 0) {
44      this.listeners.push(listener);
45    }
46  }
47
48  unregisterDataChangeListener(listener: DataChangeListener): void {
49    const pos = this.listeners.indexOf(listener);
50    if (pos >= 0) {
51      this.listeners.splice(pos, 1);
52    }
53  }
54
55  notifyDataReload(): void {
56    this.listeners.forEach(listener => {
57      listener.onDataReloaded();
58    })
59  }
60
61  notifyDataAdd(index: number): void {
62    this.listeners.forEach(listener => {
63      listener.onDataAdd(index);
64    })
65  }
66
67  notifyDataChange(index: number): void {
68    this.listeners.forEach(listener => {
69      listener.onDataChange(index);
70    })
71  }
72}