@@ -7,117 +7,103 @@ import UIKit
77@available ( iOS 13 . 0 , * )
88class ASCollectionViewCell : UICollectionViewCell , ASDataSourceConfigurableCell
99{
10- var indexPath : IndexPath ?
1110 var itemID : ASCollectionViewItemUniqueID ?
1211 var hostingController : ASHostingControllerProtocol ?
1312 {
14- didSet
15- {
16- hostingController? . invalidateCellLayoutCallback = invalidateLayoutCallback
17- hostingController? . collectionViewScrollToCellCallback = scrollToCellCallback
18- }
13+ get { _hostingController }
14+ set { _hostingController = newValue; attachView ( ) }
1915 }
2016
21- weak var collectionView : UICollectionView ?
17+ private var _hostingController : ASHostingControllerProtocol ?
2218
19+ weak var collectionViewController : AS_CollectionViewController ?
2320 var selfSizingConfig : ASSelfSizingConfig = . init( selfSizeHorizontally: true , selfSizeVertically: true )
2421
25- var invalidateLayoutCallback : ( ( _ animated: Bool ) -> Void ) ?
26- var scrollToCellCallback : ( ( UICollectionView . ScrollPosition ) -> Void ) ?
27-
28- func willAppear( in vc: UIViewController )
22+ private var hasAppeared : Bool = false // Needed due to the `self-sizing` cell used by UICV
23+ func willAppear( )
2924 {
30- if hostingController? . viewController. parent != vc
31- {
32- hostingController? . viewController. removeFromParent ( )
33- hostingController. map { vc. addChild ( $0. viewController) }
34- attachView ( )
35- hostingController? . viewController. didMove ( toParent: vc)
36- }
25+ hasAppeared = true
26+ attachView ( )
3727 }
3828
3929 func didDisappear( )
4030 {
41- hostingController? . viewController. removeFromParent ( )
31+ hasAppeared = false
32+ detachViews ( )
4233 }
4334
4435 private func attachView( )
4536 {
37+ guard hasAppeared else { return }
4638 guard let hcView = hostingController? . viewController. view else
4739 {
4840 detachViews ( )
4941 return
5042 }
5143 if hcView. superview != contentView
5244 {
45+ hostingController. map { collectionViewController? . addChild ( $0. viewController) }
5346 contentView. subviews. forEach { $0. removeFromSuperview ( ) }
5447 contentView. addSubview ( hcView)
55- setNeedsLayout ( )
48+ hcView. frame = contentView. bounds
49+ hostingController? . viewController. didMove ( toParent: collectionViewController)
5650 }
5751 }
5852
5953 private func detachViews( )
6054 {
55+ hostingController? . viewController. willMove ( toParent: nil )
6156 contentView. subviews. forEach { $0. removeFromSuperview ( ) }
57+ hostingController? . viewController. removeFromParent ( )
6258 }
6359
64- var shouldSkipNextRefresh : Bool = true
65-
6660 override func prepareForReuse( )
6761 {
68- indexPath = nil
6962 itemID = nil
7063 isSelected = false
71- hostingController = nil
72- shouldSkipNextRefresh = true
64+ alpha = 1.0
65+ _hostingController = nil
7366 }
7467
7568 override func layoutSubviews( )
7669 {
7770 super. layoutSubviews ( )
7871
79- attachView ( )
80-
8172 if hostingController? . viewController. view. frame != contentView. bounds
8273 {
83- UIView . performWithoutAnimation {
84- hostingController? . viewController. view. frame = contentView. bounds
85- hostingController? . viewController. view. setNeedsLayout ( )
86- hostingController? . viewController. view. layoutIfNeeded ( )
87- }
74+ hostingController? . viewController. view. frame = contentView. bounds
75+ hostingController? . viewController. view. setNeedsLayout ( )
8876 }
77+ hostingController? . viewController. view. layoutIfNeeded ( )
8978 }
9079
91- override func systemLayoutSizeFitting( _ targetSize: CGSize ) -> CGSize
80+ override func systemLayoutSizeFitting( _ targetSize: CGSize , withHorizontalFittingPriority horizontalFittingPriority : UILayoutPriority , verticalFittingPriority : UILayoutPriority ) -> CGSize
9281 {
93- guard let hc = hostingController else
82+ guard let hostingController = hostingController else { return CGSize ( width: 1 , height: 1 ) }
83+
84+ let selfSizeHorizontal = selfSizingConfig. selfSizeHorizontally ?? ( horizontalFittingPriority != . required)
85+ let selfSizeVertical = selfSizingConfig. selfSizeVertically ?? ( verticalFittingPriority != . required)
86+
87+ guard selfSizeVertical || selfSizeHorizontal else
9488 {
95- return CGSize ( width : 1 , height : 1 )
96- } // Can't return .zero as UICollectionViewLayout will crash
89+ return targetSize
90+ }
9791
98- let size = hc. sizeThatFits (
92+ // We need to calculate a size for self-sizing. Layout the view to get swiftUI to update its state
93+ hostingController. viewController. view. setNeedsLayout ( )
94+ hostingController. viewController. view. layoutIfNeeded ( )
95+ let size = hostingController. sizeThatFits (
9996 in: targetSize,
10097 maxSize: maxSizeForSelfSizing,
101- selfSizeHorizontal: selfSizingConfig . selfSizeHorizontally ,
102- selfSizeVertical: selfSizingConfig . selfSizeVertically )
98+ selfSizeHorizontal: selfSizeHorizontal ,
99+ selfSizeVertical: selfSizeVertical )
103100 return size
104101 }
105102
106- override func systemLayoutSizeFitting( _ targetSize: CGSize , withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority , verticalFittingPriority: UILayoutPriority ) -> CGSize
107- {
108- systemLayoutSizeFitting ( targetSize)
109- }
110-
111- override func preferredLayoutAttributesFitting( _ layoutAttributes: UICollectionViewLayoutAttributes ) -> UICollectionViewLayoutAttributes
112- {
113- layoutAttributes. size = systemLayoutSizeFitting ( layoutAttributes. size)
114- return layoutAttributes
115- }
116-
117103 var maxSizeForSelfSizing : ASOptionalSize
118104 {
119105 ASOptionalSize (
120- width: selfSizingConfig. canExceedCollectionWidth ? nil : collectionView . map { $0. contentSize. width - 0.001 } ,
121- height: selfSizingConfig. canExceedCollectionHeight ? nil : collectionView . map { $0. contentSize. height - 0.001 } )
106+ width: selfSizingConfig. canExceedCollectionWidth ? nil : collectionViewController . map { $0. collectionView . contentSize. width - 0.001 } ,
107+ height: selfSizingConfig. canExceedCollectionHeight ? nil : collectionViewController . map { $0. collectionView . contentSize. height - 0.001 } )
122108 }
123109}
0 commit comments