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