@@ -156,6 +156,61 @@ - (void)willMoveSection:(NSInteger)section toSection:(NSInteger)newSection
156156 }
157157}
158158
159+ - (void )prepareForInsertRowsAtIndexPaths : (NSArray <NSIndexPath *> *)indexPaths
160+ {
161+ for (NSString *kind in [self supplementaryKinds ]) {
162+ LOG (@" Populating elements of kind: %@ , for index paths: %@ " , kind, indexPaths);
163+ NSMutableArray <ASIndexedNodeContext *> *contexts = [NSMutableArray array ];
164+ [self _populateSupplementaryNodesOfKind: kind atIndexPaths: indexPaths mutableContexts: contexts];
165+ _pendingContexts[kind] = contexts;
166+ }
167+ }
168+
169+ - (void )willInsertRowsAtIndexPaths : (NSArray <NSIndexPath *> *)indexPaths
170+ {
171+ NSArray *keys = _pendingContexts.allKeys ;
172+ for (NSString *kind in keys) {
173+ NSMutableArray <ASIndexedNodeContext *> *contexts = _pendingContexts[kind];
174+
175+ [self batchLayoutNodesFromContexts: contexts ofKind: kind completion: ^(NSArray <ASCellNode *> *nodes, NSArray <NSIndexPath *> *indexPaths) {
176+ [self insertNodes: nodes ofKind: kind atIndexPaths: indexPaths completion: nil ];
177+ }];
178+ [_pendingContexts removeObjectForKey: kind];
179+ }
180+ }
181+
182+ - (void )willDeleteRowsAtIndexPaths : (NSArray <NSIndexPath *> *)indexPaths
183+ {
184+ for (NSString *kind in [self supplementaryKinds ]) {
185+ NSArray <NSIndexPath *> *deletedIndexPaths = ASIndexPathsInMultidimensionalArrayIntersectingIndexPaths ([self editingNodesOfKind: kind], indexPaths);
186+ [self deleteNodesOfKind: kind atIndexPaths: deletedIndexPaths completion: nil ];
187+ }
188+ }
189+
190+ - (void )prepareForReloadRowsAtIndexPaths : (NSArray <NSIndexPath *> *)indexPaths
191+ {
192+ for (NSString *kind in [self supplementaryKinds ]) {
193+ NSMutableArray <ASIndexedNodeContext *> *contexts = [NSMutableArray array ];
194+ [self _populateSupplementaryNodesOfKind: kind atIndexPaths: indexPaths mutableContexts: contexts];
195+ _pendingContexts[kind] = contexts;
196+ }
197+ }
198+
199+ - (void )willReloadRowsAtIndexPaths : (NSArray <NSIndexPath *> *)indexPaths
200+ {
201+ NSArray *keys = _pendingContexts.allKeys ;
202+ for (NSString *kind in keys) {
203+ NSMutableArray <ASIndexedNodeContext *> *contexts = _pendingContexts[kind];
204+
205+ [self deleteNodesOfKind: kind atIndexPaths: indexPaths completion: nil ];
206+ // reinsert the elements
207+ [self batchLayoutNodesFromContexts: contexts ofKind: kind completion: ^(NSArray <ASCellNode *> *nodes, NSArray <NSIndexPath *> *indexPaths) {
208+ [self insertNodes: nodes ofKind: kind atIndexPaths: indexPaths completion: nil ];
209+ }];
210+ [_pendingContexts removeObjectForKey: kind];
211+ }
212+ }
213+
159214- (void )_populateSupplementaryNodesOfKind : (NSString *)kind withMutableContexts : (NSMutableArray <ASIndexedNodeContext *> *)contexts
160215{
161216 id <ASEnvironment> environment = [self .environmentDelegate dataControllerEnvironment ];
@@ -167,21 +222,7 @@ - (void)_populateSupplementaryNodesOfKind:(NSString *)kind withMutableContexts:(
167222 NSUInteger rowCount = [self .collectionDataSource dataController: self supplementaryNodesOfKind: kind inSection: i];
168223 for (NSUInteger j = 0 ; j < rowCount; j++) {
169224 NSIndexPath *indexPath = [sectionIndexPath indexPathByAddingIndex: j];
170-
171- ASCellNodeBlock supplementaryCellBlock;
172- if (_dataSourceImplementsSupplementaryNodeBlockOfKindAtIndexPath) {
173- supplementaryCellBlock = [self .collectionDataSource dataController: self supplementaryNodeBlockOfKind: kind atIndexPath: indexPath];
174- } else {
175- ASCellNode *supplementaryNode = [self .collectionDataSource dataController: self supplementaryNodeOfKind: kind atIndexPath: indexPath];
176- supplementaryCellBlock = ^{ return supplementaryNode; };
177- }
178-
179- ASSizeRange constrainedSize = [self constrainedSizeForNodeOfKind: kind atIndexPath: indexPath];
180- ASIndexedNodeContext *context = [[ASIndexedNodeContext alloc ] initWithNodeBlock: supplementaryCellBlock
181- indexPath: indexPath
182- constrainedSize: constrainedSize
183- environmentTraitCollection: environmentTraitCollection];
184- [contexts addObject: context];
225+ [self _populateSupplementaryNodeOfKind: kind atIndexPath: indexPath mutableContexts: contexts environmentTraitCollection: environmentTraitCollection];
185226 }
186227 }
187228}
@@ -196,7 +237,33 @@ - (void)_populateSupplementaryNodesOfKind:(NSString *)kind withSections:(NSIndex
196237 NSIndexPath *sectionIndex = [[NSIndexPath alloc ] initWithIndex: idx];
197238 for (NSUInteger i = 0 ; i < rowNum; i++) {
198239 NSIndexPath *indexPath = [sectionIndex indexPathByAddingIndex: i];
240+ [self _populateSupplementaryNodeOfKind: kind atIndexPath: indexPath mutableContexts: contexts environmentTraitCollection: environmentTraitCollection];
241+ }
242+ }];
243+ }
244+
245+ - (void )_populateSupplementaryNodesOfKind : (NSString *)kind atIndexPaths : (NSArray <NSIndexPath *> *)indexPaths mutableContexts : (NSMutableArray <ASIndexedNodeContext *> *)contexts
246+ {
247+ id <ASEnvironment> environment = [self .environmentDelegate dataControllerEnvironment ];
248+ ASEnvironmentTraitCollection environmentTraitCollection = environment.environmentTraitCollection ;
249+
250+ NSMutableIndexSet *sections = [NSMutableIndexSet indexSet ];
251+ for (NSIndexPath *indexPath in indexPaths) {
252+ [sections addIndex: indexPath.section];
253+ }
254+
255+ [sections enumerateIndexesUsingBlock: ^(NSUInteger idx, BOOL *stop) {
256+ NSUInteger rowNum = [self .collectionDataSource dataController: self supplementaryNodesOfKind: kind inSection: idx];
257+ NSIndexPath *sectionIndex = [[NSIndexPath alloc ] initWithIndex: idx];
258+ for (NSUInteger i = 0 ; i < rowNum; i++) {
259+ NSIndexPath *indexPath = [sectionIndex indexPathByAddingIndex: i];
260+ [self _populateSupplementaryNodeOfKind: kind atIndexPath: indexPath mutableContexts: contexts environmentTraitCollection: environmentTraitCollection];
261+ }
262+ }];
263+ }
199264
265+ - (void )_populateSupplementaryNodeOfKind : (NSString *)kind atIndexPath : (NSIndexPath *)indexPath mutableContexts : (NSMutableArray <ASIndexedNodeContext *> *)contexts environmentTraitCollection : (ASEnvironmentTraitCollection)environmentTraitCollection
266+ {
200267 ASCellNodeBlock supplementaryCellBlock;
201268 if (_dataSourceImplementsSupplementaryNodeBlockOfKindAtIndexPath) {
202269 supplementaryCellBlock = [self .collectionDataSource dataController: self supplementaryNodeBlockOfKind: kind atIndexPath: indexPath];
@@ -211,8 +278,6 @@ - (void)_populateSupplementaryNodesOfKind:(NSString *)kind withSections:(NSIndex
211278 constrainedSize: constrainedSize
212279 environmentTraitCollection: environmentTraitCollection];
213280 [contexts addObject: context];
214- }
215- }];
216281}
217282
218283#pragma mark - Sizing query
@@ -264,4 +329,4 @@ - (void)setDataSource:(id<ASDataControllerSource>)dataSource
264329 ASDisplayNodeAssertTrue (_dataSourceImplementsSupplementaryNodeBlockOfKindAtIndexPath || [self .collectionDataSource respondsToSelector: @selector (dataController:supplementaryNodeOfKind:atIndexPath: )]);
265330}
266331
267- @end
332+ @end
0 commit comments