1/**
2 * Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development 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 wifi from '@ohos.wifi'
17
18const TAG = "[basicDataSource]"
19
20/**
21 * BasicDataSource page of WiFi test
22 */
23
24class BasicDataSource implements IDataSource {
25  private listeners: DataChangeListener[] = []
26
27  public totalCount(): number {
28    return 0
29  }
30
31  public getData(index: number): any {
32    return undefined
33  }
34
35  registerDataChangeListener(listener: DataChangeListener): void {
36    if ( this.listeners.indexOf(listener) < 0 ) {
37      console.log(TAG , 'add listener')
38      this.listeners.push(listener)
39    } else {
40      console.log(TAG , 'indexOf(listener) >= 0')
41    }
42  }
43
44  unregisterDataChangeListener(listener: DataChangeListener): void {
45    const pos = this.listeners.indexOf(listener);
46    if ( pos >= 0 ) {
47      console.log(TAG , 'remove listener')
48      this.listeners.splice(pos , 1)
49    } else {
50      console.log(TAG , 'pos < 0')
51    }
52  }
53
54  notifyDataReload(): void {
55    this.listeners.forEach(listener => {
56      listener.onDataReloaded()
57    })
58  }
59
60  notifyDataAdd(index: number): void {
61    this.listeners.forEach(listener => {
62      listener.onDataAdd(index)
63    })
64  }
65
66  notifyDataChange(index: number): void {
67    this.listeners.forEach(listener => {
68      listener.onDataChange(index)
69    })
70  }
71
72  notifyDataDelete(index: number): void {
73    this.listeners.forEach(listener => {
74      listener.onDataDelete(index)
75    })
76  }
77
78  notifyDataMove(from: number , to: number): void {
79    this.listeners.forEach(listener => {
80      listener.onDataMove(from , to)
81    })
82  }
83}
84
85export default class WifiDataSource extends BasicDataSource {
86  private dataArray: wifi.WifiScanInfo[] = []
87
88  constructor(data: wifi.WifiScanInfo[]) {
89    super()
90    this.dataArray = data
91  }
92
93  public totalCount(): number {
94    return this.dataArray.length
95  }
96
97  public getData(index: number): any {
98    return this.dataArray[ index ]
99  }
100
101  public addData(index: number , data: wifi.WifiScanInfo): void {
102    this.dataArray.splice(index , 0 , data)
103    this.notifyDataAdd(index)
104  }
105
106  public pushData(data: wifi.WifiScanInfo): void {
107    this.dataArray.push(data)
108    this.notifyDataAdd(this.dataArray.length - 1)
109  }
110}