// The list surface Angular binds statically: inputs, outputs and projected templates. Kept apart // from the components so the windowing lifecycle reads on its own import { ContentChild, Directive, EventEmitter, Input, Output, TemplateRef, type Type, } from '@angular/core'; import { DEFAULT_INITIAL_NUM_TO_RENDER, DEFAULT_MAX_TO_RENDER_PER_BATCH, DEFAULT_UPDATE_CELLS_BATCHING_PERIOD, DEFAULT_WINDOW_SIZE, ITEM_RENDERER, pickItemRenderer, type IInnerViewRef, type IViewabilityConfig, type IViewabilityConfigCallbackPair, type IViewableItemsChangedInfo, } from '@symbiote-native/components'; import type { IStyleProp, ISymbioteEvent, IViewStyle, } from '@symbiote-native/engine'; import { VListCellDirective, VListEmptyDirective, VListFooterDirective, VListHeaderDirective, VListItemDirective, VListSeparatorDirective, type IVListCellContext, type IVListItemContext, type IVListSeparatorContext, } from './directives'; import { ListEventsBase } from './list-events'; // What `VirtualizedList` and `FlatList` share: the windowing inputs and the slot templates @Directive() export abstract class ListInputsBase extends ListEventsBase { @Output() readonly viewableItemsChanged = new EventEmitter< IViewableItemsChangedInfo >(); @Output() readonly scrollToIndexFailed = new EventEmitter<{ index: number; highestMeasuredFrameIndex: number; averageItemLength: number; }>(); @Input() keyExtractor?: (item: ItemT, index: number) => string; @Input() getItemLayout?: ( data: unknown, index: number, ) => { length: number; offset: number; index: number }; @Input() horizontal?: boolean; @Input() inverted?: boolean; @Input() extraData?: unknown; @Input() onEndReachedThreshold?: number; @Input() onStartReachedThreshold?: number; @Input() refreshing?: boolean | null; @Input() progressViewOffset?: number; @Input() viewabilityConfig?: IViewabilityConfig; @Input() viewabilityConfigCallbackPairs?: IViewabilityConfigCallbackPair[]; @Input() initialNumToRender?: number; @Input() initialScrollIndex?: number; @Input() maxToRenderPerBatch?: number; @Input() updateCellsBatchingPeriod?: number; @Input() windowSize?: number; // Mounts every cell from the top and paints no spacer, the window only grows toward the end @Input() disableVirtualization?: boolean; @Input() stickyHeaderIndices?: number[]; @Input() maintainVisibleContentPosition?: { minIndexForVisible: number; autoscrollToTopThreshold?: number; }; @Input() onScroll?: (event: ISymbioteEvent) => void; @Input() onScrollBeginDrag?: (event: ISymbioteEvent) => void; @Input() onScrollEndDrag?: (event: ISymbioteEvent) => void; @Input() onMomentumScrollBegin?: (event: ISymbioteEvent) => void; @Input() onMomentumScrollEnd?: (event: ISymbioteEvent) => void; @Input() onContentSizeChange?: (width: number, height: number) => void; @Input() scrollEventThrottle?: number; @Input() keyboardShouldPersistTaps?: boolean | 'always' | 'never' | 'handled'; @Input() keyboardDismissMode?: 'none' | 'on-drag' | 'interactive'; @Input() removeClippedSubviews?: boolean; @Input() nestedScrollEnabled?: boolean; @Input() stickyHeaderHiddenOnScroll?: boolean; @Input() innerViewRef?: IInnerViewRef; @Input() style?: IStyleProp; @Input() contentContainerStyle?: IStyleProp; @Input() listHeaderComponentStyle?: IStyleProp; @Input() listFooterComponentStyle?: IStyleProp; @Input() testID?: string; @Input() nativeID?: string; // RN's `ListItemComponent`: a component taking `item`, `index` and `separators` inputs, in place // of the `vListItem` template, and it wins when both are given @Input() listItemComponent?: Type; // The cell and slot templates the app authors, captured from projected content @ContentChild(VListItemDirective) itemDir?: VListItemDirective; @ContentChild(VListHeaderDirective) headerDir?: VListHeaderDirective; @ContentChild(VListFooterDirective) footerDir?: VListFooterDirective; @ContentChild(VListEmptyDirective) emptyDir?: VListEmptyDirective; @ContentChild(VListSeparatorDirective) separatorDir?: VListSeparatorDirective; @ContentChild(VListCellDirective) cellDir?: VListCellDirective; get isHorizontal(): boolean { return this.horizontal === true; } // The app's `vListCell`, which a list built on this one may replace with its own get cellRendererTpl(): TemplateRef | undefined { return this.cellDir?.templateRef; } // The component to stamp per cell, RN's `_renderElement`: it wins over the item template, and a // cell with neither throws get itemComponent(): Type | undefined { const kind = pickItemRenderer({ hasRenderItem: this.hasItemTemplate, hasComponent: this.listItemComponent !== undefined, }); return kind === ITEM_RENDERER.component ? this.listItemComponent : undefined; } protected get hasItemTemplate(): boolean { return this.itemDir !== undefined; } protected get isInverted(): boolean { return this.inverted === true; } protected get windowSizeValue(): number { return this.windowSize ?? DEFAULT_WINDOW_SIZE; } protected get initialNumToRenderValue(): number { return this.initialNumToRender ?? DEFAULT_INITIAL_NUM_TO_RENDER; } protected get maxToRenderPerBatchValue(): number { return this.maxToRenderPerBatch ?? DEFAULT_MAX_TO_RENDER_PER_BATCH; } protected get updateCellsBatchingPeriodValue(): number { return ( this.updateCellsBatchingPeriod ?? DEFAULT_UPDATE_CELLS_BATCHING_PERIOD ); } } @Directive() export abstract class VirtualizedListInputs< ItemT, > extends ListInputsBase { @Input({ required: true }) data!: unknown; @Input({ required: true }) getItem!: (data: unknown, index: number) => ItemT; @Input({ required: true }) getItemCount!: (data: unknown) => number; // Set by a wrapper (`FlatList`, `VirtualizedSectionList`) that always binds `(refresh)` here to // re-forward the event, which keeps `refresh.observed` permanently true. The wrapper passes its // own public `refresh.observed` instead, direct usage falls back to this component's own @Input() refreshRequested?: boolean; // A wrapping list hands its app's cell and separator templates straight in, so a cell is one // outlet deep instead of a wrapper outlet around the app's own. Each wins over projection @Input() itemTemplate?: TemplateRef>; @Input() itemSeparatorTemplate?: TemplateRef>; @Input() cellRendererTemplate?: TemplateRef; override get cellRendererTpl(): TemplateRef | undefined { return this.cellRendererTemplate ?? this.cellDir?.templateRef; } get cellTemplate(): TemplateRef> | undefined { return this.itemTemplate ?? this.itemDir?.templateRef; } protected override get hasItemTemplate(): boolean { return this.cellTemplate !== undefined; } get separatorTemplate(): TemplateRef> | undefined { return this.itemSeparatorTemplate ?? this.separatorDir?.templateRef; } }