• 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 { logger } from '../utils/Logger'
16
17const TAG: string = 'BasicDataSource'
18
19class BasicDataSource implements IDataSource {
20  private listeners: DataChangeListener[] = []
21
22  public totalCount(): number {
23    return 0
24  }
25
26  public getData(index: number): any {
27    return undefined
28  }
29
30  registerDataChangeListener(listener: DataChangeListener): void {
31    if (this.listeners.indexOf(listener) < 0) {
32      logger.info(TAG, 'add listener')
33      this.listeners.push(listener)
34    }
35  }
36
37  unregisterDataChangeListener(listener: DataChangeListener): void {
38    const pos = this.listeners.indexOf(listener)
39    if (pos >= 0) {
40      logger.info(TAG, 'remove listener')
41      this.listeners.splice(pos, 1)
42    }
43  }
44
45  notifyDataReload(): void {
46    this.listeners.forEach(listener => {
47      listener.onDataReloaded()
48    })
49  }
50
51  notifyDataAdd(index: number): void {
52    this.listeners.forEach(listener => {
53      listener.onDataAdd(index)
54    })
55  }
56
57  notifyDataChange(index: number): void {
58    this.listeners.forEach(listener => {
59      listener.onDataChange(index)
60    })
61  }
62
63  notifyDataDelete(index: number): void {
64    this.listeners.forEach(listener => {
65      listener.onDataDelete(index)
66    })
67  }
68
69  notifyDataMove(from: number, to: number): void {
70    this.listeners.forEach(listener => {
71      listener.onDataMove(from, to)
72    })
73  }
74}
75
76export class ContactDataSource extends BasicDataSource {
77  private dataArray: any[] = []
78
79  public totalCount(): number {
80    return this.dataArray.length
81  }
82
83  public getData(index: number): any {
84    return this.dataArray[index]
85  }
86
87  public addData(index: number, data: any): void {
88    this.dataArray.splice(index, 0, data)
89    this.notifyDataAdd(index)
90  }
91
92  public pushData(data: any): void {
93    this.dataArray.push(data)
94    this.notifyDataAdd(this.dataArray.length - 1)
95  }
96}