77//
88
99#import " ASPagerNode.h"
10+ #import " ASDelegateProxy.h"
1011
1112#import < AsyncDisplayKit/AsyncDisplayKit.h>
1213
13- @interface ASPagerNode () <ASCollectionViewDataSource , ASCollectionViewDelegateFlowLayout> {
14+ @interface ASPagerNode () <ASCollectionDataSource , ASCollectionViewDelegateFlowLayout, ASDelegateProxyInterceptor > {
1415 UICollectionViewFlowLayout *_flowLayout;
16+ ASPagerNodeProxy *_proxy;
17+ id <ASPagerNodeDataSource> _pagerDataSource;
1518}
1619
1720@end
1821
1922@implementation ASPagerNode
23+ @dynamic delegate;
2024
2125- (instancetype )init
2226{
@@ -25,24 +29,55 @@ - (instancetype)init
2529 flowLayout.minimumInteritemSpacing = 0 ;
2630 flowLayout.minimumLineSpacing = 0 ;
2731
32+ return [self initWithFlowLayout: flowLayout];
33+ }
34+
35+ - (instancetype )initWithFlowLayout : (UICollectionViewFlowLayout *)flowLayout
36+ {
2837 self = [super initWithCollectionViewLayout: flowLayout];
2938 if (self != nil ) {
3039 _flowLayout = flowLayout;
3140 }
3241 return self;
3342}
3443
44+ - (ASCollectionView *)collectionView
45+ {
46+ return self.view ;
47+ }
48+
49+ - (void )setDataSource : (id <ASPagerNodeDataSource>)pagerDataSource
50+ {
51+ if (pagerDataSource != _pagerDataSource) {
52+ _pagerDataSource = pagerDataSource;
53+ _proxy = pagerDataSource ? [[ASPagerNodeProxy alloc ] initWithTarget: pagerDataSource interceptor: self ] : nil ;
54+ super.dataSource = (id <ASCollectionDataSource>)_proxy;
55+ }
56+ }
57+
58+ - (void )proxyTargetHasDeallocated : (ASDelegateProxy *)proxy
59+ {
60+ [self setDataSource: nil ];
61+ }
62+
63+ - (id <ASPagerNodeDataSource>)dataSource
64+ {
65+ return _pagerDataSource;
66+ }
67+
3568- (void )didLoad
3669{
3770 [super didLoad ];
3871
39- self.view .asyncDataSource = self;
40- self.view .asyncDelegate = self;
72+ ASCollectionView *cv = self.view ;
73+ cv.asyncDataSource = self;
74+ cv.asyncDelegate = self;
4175
42- self.view .pagingEnabled = YES ;
43- self.view .allowsSelection = NO ;
44- self.view .showsVerticalScrollIndicator = NO ;
45- self.view .showsHorizontalScrollIndicator = NO ;
76+ cv.pagingEnabled = YES ;
77+ cv.allowsSelection = NO ;
78+ cv.showsVerticalScrollIndicator = NO ;
79+ cv.showsHorizontalScrollIndicator = NO ;
80+ cv.scrollsToTop = NO ;
4681
4782 ASRangeTuningParameters preloadParams = { .leadingBufferScreenfuls = 2.0 , .trailingBufferScreenfuls = 2.0 };
4883 ASRangeTuningParameters renderParams = { .leadingBufferScreenfuls = 1.0 , .trailingBufferScreenfuls = 1.0 };
@@ -62,14 +97,14 @@ - (void)scrollToPageAtIndex:(NSInteger)index animated:(BOOL)animated
6297
6398- (ASCellNode *)collectionView : (ASCollectionView *)collectionView nodeForItemAtIndexPath : (NSIndexPath *)indexPath
6499{
65- ASDisplayNodeAssert (self. dataSource != nil , @" ASPagerNode must have a data source to load paging nodes" );
66- return [self .dataSource pagerNode: self nodeAtIndex: indexPath.item];
100+ ASDisplayNodeAssert (_pagerDataSource != nil , @" ASPagerNode must have a data source to load paging nodes" );
101+ return [_pagerDataSource pagerNode: self nodeAtIndex: indexPath.item];
67102}
68103
69104- (NSInteger )collectionView : (UICollectionView *)collectionView numberOfItemsInSection : (NSInteger )section
70105{
71- ASDisplayNodeAssert (self. dataSource != nil , @" ASPagerNode must have a data source to load paging nodes" );
72- return [self .dataSource numberOfPagesInPagerNode: self ];
106+ ASDisplayNodeAssert (_pagerDataSource != nil , @" ASPagerNode must have a data source to load paging nodes" );
107+ return [_pagerDataSource numberOfPagesInPagerNode: self ];
73108}
74109
75110- (ASSizeRange)collectionView : (ASCollectionView *)collectionView constrainedSizeForNodeAtIndexPath : (NSIndexPath *)indexPath
0 commit comments