1/* 2 * Copyright (c) 2023 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 { Log } from '../utils/Log'; 17 18const TAG = 'ItemDataSource' 19 20export class LazyItem<T> { 21 item: T; 22 onItemUpdate: Function; 23 index: number = -1; 24 25 constructor(item: T, index: number, onItemUpdate?: Function) { 26 this.item = item; 27 this.onItemUpdate = onItemUpdate; 28 this.index = index; 29 } 30 31 update(item: T): void { 32 if (this.onItemUpdate != null && this.index !== -1) { 33 this.onItemUpdate(this.index, item); 34 } 35 } 36 37 getHashCode(): string { 38 // @ts-ignore 39 return this.index + '' + this.item.getHashCode(); 40 } 41 42 get(): T { 43 return this.item; 44 } 45 46 set(item: T): void { 47 this.item = item; 48 } 49} 50 51export abstract class ItemDataSource implements IDataSource { 52 private listeners: DataChangeListener[] = []; 53 54 abstract totalCount(): number; 55 56 abstract getData(index: number): Object; 57 58 abstract isSelect(): boolean; 59 60 abstract getSelectedCount(): number; 61 62 abstract getSelectedItems(): Object[]; 63 64 abstract getSelectedUris(): string[]; 65 66 abstract dataRemove(): void; 67 68 registerDataChangeListener(listener: DataChangeListener): void { 69 Log.info(TAG, 'registerDataChangeListener'); 70 if (this.listeners.indexOf(listener) < 0) { 71 this.listeners.push(listener); 72 Log.info(TAG, 'registerDataChangeListener, add listener, length: ' + this.listeners.length); 73 } 74 } 75 76 unregisterDataChangeListener(listener: DataChangeListener): void { 77 Log.info(TAG, 'unregisterDataChangeListener'); 78 const pos = this.listeners.indexOf(listener); 79 if (pos >= 0) { 80 this.listeners.splice(pos, 1); 81 Log.info(TAG, 'registerDataChangeListener, remove listener, length: ' + this.listeners.length); 82 } 83 } 84 85 notifyDataChange(index: number): void { 86 this.listeners.forEach((listener: DataChangeListener): void => { 87 listener.onDataChange(index); 88 }) 89 } 90 91 notifyDataReload(): void { 92 this.listeners.forEach((listener: DataChangeListener): void => { 93 listener.onDataReloaded(); 94 }) 95 } 96 97 notifyDataDelete(index: number): void { 98 this.listeners.forEach((listener: DataChangeListener): void => { 99 listener.onDataDelete(index); 100 }) 101 } 102} 103