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