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