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