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