@@ -73,21 +73,29 @@ export const estimatedItemSizeProperty = new Property<CollectionView, boolean>({
7373 valueConverter : booleanConverter
7474} ) ;
7575
76+ export const autoSizeProperty = new Property < CollectionView , boolean > ( {
77+ name : 'autoSize' ,
78+ defaultValue : false ,
79+ valueConverter : booleanConverter
80+ } ) ;
81+
7682export enum SnapPosition {
7783 START = - 1 , // = androidx.recyclerview.widget.LinearSmoothScroller.SNAP_TO_START,
7884 END = 1 // = androidx.recyclerview.widget.LinearSmoothScroller.SNAP_TO_END
7985}
8086
8187export class CollectionView extends CollectionViewBase {
82- private _layout : UICollectionViewLayout ;
83- private _dataSource : CollectionViewDataSource ;
84- private _delegate : UICollectionViewDelegateImpl | UICollectionViewDelegateFixedSizeImpl ;
88+ _layout : UICollectionViewLayout ;
89+ _dataSource : CollectionViewDataSource ;
90+ _delegate : UICollectionViewDelegateImpl | UICollectionViewDelegateFixedSizeImpl ;
8591 private _preparingCell : boolean = false ;
8692 // private _sizes: number[][];
8793 private _map : Map < CollectionViewCell , ItemView > ;
8894 _measureCellMap : Map < string , { cell : CollectionViewCell ; view : View } > ;
8995 _lastLayoutKey : string ;
9096
97+ autoSize = false ;
98+
9199 reorderLongPressGesture : UILongPressGestureRecognizer ;
92100 reorderLongPressHandler : ReorderLongPressImpl ;
93101 reorderStartingRow = - 1 ;
@@ -120,7 +128,9 @@ export class CollectionView extends CollectionViewBase {
120128 layout . minimumLineSpacing = 0 ;
121129 layout . minimumInteritemSpacing = 0 ;
122130 }
123- const view = UICollectionView . alloc ( ) . initWithFrameCollectionViewLayout ( CGRectMake ( 0 , 0 , 0 , 0 ) , layout ) ;
131+ // const view = UICollectionViewImpl.initWithFrameCollectionViewLayout(CGRectMake(0, 0, 0, 0), layout) as UICollectionViewImpl;
132+
133+ const view = UICollectionViewImpl . initWithOwner ( this , layout ) ;
124134 view . backgroundColor = UIColor . clearColor ;
125135 this . _itemTemplatesInternal . forEach ( ( t ) => {
126136 view . registerClassForCellWithReuseIdentifier ( CollectionViewCell . class ( ) , t . key . toLowerCase ( ) ) ;
@@ -160,6 +170,9 @@ export class CollectionView extends CollectionViewBase {
160170 if ( layoutStyle && layoutStyle . createDelegate ) {
161171 this . _delegate = layoutStyle . createDelegate ( this ) ;
162172 this . nativeViewProtected . delegate = this . _delegate ;
173+ } else if ( this . autoSize ) {
174+ this . _delegate = UICollectionViewDelegateImpl . initWithOwner ( this ) ;
175+ this . nativeViewProtected . delegate = this . _delegate ;
163176 }
164177
165178 this . _setNativeClipToBounds ( ) ;
@@ -466,7 +479,7 @@ export class CollectionView extends CollectionViewBase {
466479 if ( layoutView instanceof UICollectionViewFlowLayout ) {
467480 if ( this . _effectiveRowHeight && this . _effectiveColWidth ) {
468481 layoutView . itemSize = CGSizeMake ( Utils . layout . toDeviceIndependentPixels ( this . _effectiveColWidth ) , Utils . layout . toDeviceIndependentPixels ( this . _effectiveRowHeight ) ) ;
469- } else if ( this . estimatedItemSize ) {
482+ } else if ( this . estimatedItemSize && ! this . autoSize ) {
470483 layoutView . estimatedItemSize = CGSizeMake ( Utils . layout . toDeviceIndependentPixels ( this . _effectiveColWidth ) , Utils . layout . toDeviceIndependentPixels ( this . _effectiveRowHeight ) ) ;
471484 }
472485 layoutView . invalidateLayout ( ) ;
@@ -1202,6 +1215,7 @@ export class CollectionView extends CollectionViewBase {
12021215}
12031216contentInsetAdjustmentBehaviorProperty . register ( CollectionView ) ;
12041217estimatedItemSizeProperty . register ( CollectionView ) ;
1218+ autoSizeProperty . register ( CollectionView ) ;
12051219
12061220interface ViewItemIndex { }
12071221
@@ -1233,9 +1247,48 @@ class CollectionViewCell extends UICollectionViewCell {
12331247}
12341248
12351249@NativeClass
1236- class UICollectionViewFlowLayoutImpl extends UICollectionViewFlowLayout {
1250+ class UICollectionViewImpl extends UICollectionView {
12371251 _owner : WeakRef < CollectionView > ;
1252+ sizeThatFits ( size : CGSize ) : CGSize {
1253+ const owner = this . _owner ?. get ( ) ;
1254+ if ( owner ?. autoSize ) {
1255+ if ( this . superview ) {
1256+ this . superview ?. layoutIfNeeded ( ) ;
1257+ }
1258+ const horizontal = owner . orientation === 'horizontal' ;
1259+ // Calculate the total size based on the cells' sizes
1260+ let width = 0 ;
1261+ let height = 0 ;
1262+ const dataSource = owner . _dataSource ;
1263+ const delegate = owner . _delegate as UICollectionViewDelegateFlowLayout ;
1264+ if ( dataSource && delegate ) {
1265+ const numberOfItems = dataSource . collectionViewNumberOfItemsInSection ( this , 0 ) ;
1266+ for ( let index = 0 ; index < numberOfItems ; index ++ ) {
1267+ const indexPath = NSIndexPath . indexPathForItemInSection ( index , 0 ) ;
1268+ const estimatedSize = delegate . collectionViewLayoutSizeForItemAtIndexPath ( this , owner . _layout , indexPath ) ?? CGSizeZero ;
1269+ if ( horizontal ) {
1270+ width = Math . max ( width , estimatedSize . width ) ;
1271+ height += estimatedSize . height ;
1272+ } else {
1273+ height = Math . max ( height , estimatedSize . height ) ;
1274+ width += estimatedSize . width ;
1275+ }
1276+ }
1277+ return CGSizeMake ( width , height ) ;
1278+ }
1279+ }
1280+ return super . sizeThatFits ( size ) ;
1281+ }
1282+ static initWithOwner ( owner : CollectionView , layout ) {
1283+ const view = UICollectionViewImpl . alloc ( ) . initWithFrameCollectionViewLayout ( CGRectMake ( 0 , 0 , 0 , 0 ) , layout ) as UICollectionViewImpl ;
1284+ view . _owner = new WeakRef ( owner ) ;
1285+ return view ;
1286+ }
1287+ }
12381288
1289+ @NativeClass
1290+ class UICollectionViewFlowLayoutImpl extends UICollectionViewFlowLayout {
1291+ _owner : WeakRef < CollectionView > ;
12391292 static initWithOwner ( owner : CollectionView ) {
12401293 const layout = UICollectionViewFlowLayoutImpl . new ( ) as UICollectionViewFlowLayoutImpl ;
12411294 layout . _owner = new WeakRef ( owner ) ;
0 commit comments