import { Layout , WrapGridLayoutManager , LayoutManager } from "../layoutmanager/LayoutManager" ; /**
 * Created by talha.naqvi on 05/04/17.
 * You can create a new instance or inherit and override default methods
 * You may need access to data provider here, it might make sense to pass a function which lets you fetch the latest data provider
 * Why only indexes? The answer is to allow data virtualization in the future. Since layouts are accessed much before the actual render assuming having all
 * data upfront will only limit possibilites in the future.
 *
 * By design LayoutProvider forces you to think in terms of view types. What that means is that you'll always be dealing with a finite set of view templates
 * with deterministic dimensions. We want to eliminate unnecessary re-layouts that happen when height, by mistake, is not taken into consideration.
 * This patters ensures that your scrolling is as smooth as it gets. You can always increase the number of types to handle non deterministic scenarios.
 *
 * NOTE: You can also implement features such as ListView/GridView switch by simple changing your layout provider.
 */ export abstract class BaseLayoutProvider { public shouldRefreshWithAnchoring : boolean = true ; public abstract newLayoutManager ( renderWindowSize , isHorizontal ? , cachedLayouts ? ) ; public abstract getLayoutTypeForIndex ( index ) ; public abstract checkDimensionDiscrepancy ( dimension , type , index ) ; } export class LayoutProvider extends BaseLayoutProvider { private _getLayoutTypeForIndex : ( index : number ) => string | number ; private _setLayoutForType : ( type : string | number , dim : Dimension , index : number ) => void ; private _tempDim : Dimension ; private _lastLayoutManager : WrapGridLayoutManager | undefined ; constructor ( getLayoutTypeForIndex , setLayoutForType ) { super ( ) ; this . _getLayoutTypeForIndex = getLayoutTypeForIndex ; this . _setLayoutForType = setLayoutForType ; this . _tempDim = { height : 0 , width : 0 } ; } public newLayoutManager ( renderWindowSize , isHorizontal ? , cachedLayouts ? ) { this . _lastLayoutManager = new WrapGridLayoutManager ( this , renderWindowSize , isHorizontal , cachedLayouts ) ; return this . _lastLayoutManager ; } public getLayoutTypeForIndex ( index ) { return this . _getLayoutTypeForIndex ( index ) ; } public setComputedLayout ( type , dimension , index ) { return this . _setLayoutForType ( type , dimension , index ) ; } public checkDimensionDiscrepancy ( dimension , type , index ) { const dimension1 = dimension ; this . setComputedLayout ( type , this . _tempDim , index ) ; const dimension2 = this . _tempDim ; if ( this . _lastLayoutManager ) { this . _lastLayoutManager . setMaxBounds ( dimension2 ) ; } return dimension1 . height !== dimension2 . height || dimension1 . width !== dimension2 . width ; } } export interface Dimension { height : number ; width : number ; }