• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2025 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
16
17/**
18 * 实现页面加载所需要的对象,用于LazyForEach加载数据
19 */
20export class KeyboardDataSource {
21  private dataArray: string[] = [];
22  private listeners: DataChangeListener[] = [];
23
24  constructor(dataArray: string[]) {
25    for (let i = 0; i < dataArray.length; i++) {
26      this.dataArray.push(dataArray[i]);
27    }
28  }
29
30  /**
31   * 获取索引对应的数据
32   * @param index 数组索引
33   * @returns
34   */
35  public getData(index: number): string {
36    return this.dataArray[index];
37  }
38
39  /**
40   * 通知控制器数据重新加载
41   */
42  notifyDataReload(): void {
43    this.listeners.forEach(listener => {
44      listener.onDataReloaded();
45    })
46  }
47
48  /**
49   * 通知控制器数据增加
50   * @param index 数组索引
51   */
52  notifyDataAdd(index: number): void {
53    this.listeners.forEach(listener => {
54      listener.onDataAdd(index);
55    })
56  }
57
58  /**
59   * 通知控制器数据变化
60   * @param index 数组索引
61   */
62  notifyDataChange(index: number): void {
63    this.listeners.forEach(listener => {
64      listener.onDataChange(index);
65    })
66  }
67
68  /**
69   * 通知控制器数据删除
70   * @param index 数组索引
71   */
72  notifyDataDelete(index: number): void {
73    this.listeners.forEach(listener => {
74      listener.onDataDelete(index);
75    })
76  }
77
78  /**
79   * 通知控制器数据位置变化
80   * @param from 起始位置
81   * @param to 最终位置
82   */
83  notifyDataMove(from: number, to: number): void {
84    this.listeners.forEach(listener => {
85      listener.onDataMove(from, to);
86    })
87  }
88
89  /**
90   * 获取数据总数
91   * @returns
92   */
93  public totalCount(): number {
94    return this.dataArray.length;
95  }
96
97  /**
98   * 注册改变数据的控制器
99   * @param listener 数据控制器
100   */
101  registerDataChangeListener(listener: DataChangeListener): void {
102    if (this.listeners.indexOf(listener) < 0) {
103      this.listeners.push(listener);
104    }
105  }
106
107  /**
108   * 注销改变数据的控制器
109   * @param listener 数据控制器
110   */
111  unregisterDataChangeListener(listener: DataChangeListener): void {
112    const pos = this.listeners.indexOf(listener)
113    if (pos >= 0) {
114      this.listeners.splice(pos, 1);
115    }
116  }
117
118  /**
119   * 增加数据
120   */
121  public add1stItem(): void {
122    this.dataArray.splice(0, 0, this.dataArray[this.dataArray.length]);
123    this.notifyDataAdd(0);
124  }
125
126  /**
127   * 在数据尾部增加一个元素
128   */
129  public addLastItem(): void {
130    this.dataArray.splice(this.dataArray.length, 0, this.dataArray[this.dataArray.length]);
131    this.notifyDataAdd(this.dataArray.length - 1);
132  }
133
134  /**
135   * 在指定索引位置增加一个元素
136   * @param index
137   */
138  public addItem(index: number): void {
139    this.dataArray.splice(index, 0, this.dataArray[this.dataArray.length]);
140    this.notifyDataAdd(index);
141  }
142
143  /**
144   * 删除第一个元素
145   */
146  public delete1stItem(): void {
147    this.dataArray.splice(0, 1);
148    this.notifyDataDelete(0);
149  }
150
151  /**
152   * 删除第二个元素
153   */
154  public delete2ndItem(): void {
155    this.dataArray.splice(1, 1);
156    this.notifyDataDelete(1);
157  }
158
159  /**
160   * 删除最后一个元素
161   */
162  public deleteLastItem(): void {
163    this.dataArray.splice(-1, 1);
164    this.notifyDataDelete(this.dataArray.length);
165  }
166
167  /**
168   * 重新加载数据
169   */
170  public reload(): void {
171    this.notifyDataReload();
172  }
173
174  /**
175   * 改变数组数据
176   * @param data:新数组
177   */
178  public modifyAllData(data: string[]): void {
179    this.dataArray = data;
180    this.notifyDataReload();
181  }
182}