This repository was archived by the owner on Feb 2, 2023. It is now read-only.
-
Couldn't load subscription status.
- Fork 2.2k
[ASCollectionView] Repopulate supplementary views on item-level changes #1629
Merged
appleguy
merged 7 commits into
facebookarchive:master
from
erichoracek:recalculate-supplementaries-on-item-changes
Jun 6, 2016
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fc5eef3
Repopulate supplementary views on item-level changes
erichoracek 8f3e511
Fix warning
erichoracek d89504e
Fix index path arithmetic error
erichoracek dc7f67c
Populate more aggresively
erichoracek 30dba28
Remove _pendingContexts dictionary mutation during enumeration
erichoracek ed9fcdc
Rebase
erichoracek 70fbbe0
Whitespace
erichoracek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -156,6 +156,61 @@ - (void)willMoveSection:(NSInteger)section toSection:(NSInteger)newSection | |
| } | ||
| } | ||
|
|
||
| - (void)prepareForInsertRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths | ||
| { | ||
| for (NSString *kind in [self supplementaryKinds]) { | ||
| LOG(@"Populating elements of kind: %@, for index paths: %@", kind, indexPaths); | ||
| NSMutableArray<ASIndexedNodeContext *> *contexts = [NSMutableArray array]; | ||
| [self _populateSupplementaryNodesOfKind:kind atIndexPaths:indexPaths mutableContexts:contexts]; | ||
| _pendingContexts[kind] = contexts; | ||
| } | ||
| } | ||
|
|
||
| - (void)willInsertRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths | ||
| { | ||
| NSArray *keys = _pendingContexts.allKeys; | ||
| for (NSString *kind in keys) { | ||
| NSMutableArray<ASIndexedNodeContext *> *contexts = _pendingContexts[kind]; | ||
|
|
||
| [self batchLayoutNodesFromContexts:contexts ofKind:kind completion:^(NSArray<ASCellNode *> *nodes, NSArray<NSIndexPath *> *indexPaths) { | ||
| [self insertNodes:nodes ofKind:kind atIndexPaths:indexPaths completion:nil]; | ||
| }]; | ||
| [_pendingContexts removeObjectForKey:kind]; | ||
| } | ||
| } | ||
|
|
||
| - (void)willDeleteRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths | ||
| { | ||
| for (NSString *kind in [self supplementaryKinds]) { | ||
| NSArray<NSIndexPath *> *deletedIndexPaths = ASIndexPathsInMultidimensionalArrayIntersectingIndexPaths([self editingNodesOfKind:kind], indexPaths); | ||
| [self deleteNodesOfKind:kind atIndexPaths:deletedIndexPaths completion:nil]; | ||
| } | ||
| } | ||
|
|
||
| - (void)prepareForReloadRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths | ||
| { | ||
| for (NSString *kind in [self supplementaryKinds]) { | ||
| NSMutableArray<ASIndexedNodeContext *> *contexts = [NSMutableArray array]; | ||
| [self _populateSupplementaryNodesOfKind:kind atIndexPaths:indexPaths mutableContexts:contexts]; | ||
| _pendingContexts[kind] = contexts; | ||
| } | ||
| } | ||
|
|
||
| - (void)willReloadRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths | ||
| { | ||
| NSArray *keys = _pendingContexts.allKeys; | ||
| for (NSString *kind in keys) { | ||
| NSMutableArray<ASIndexedNodeContext *> *contexts = _pendingContexts[kind]; | ||
|
|
||
| [self deleteNodesOfKind:kind atIndexPaths:indexPaths completion:nil]; | ||
| // reinsert the elements | ||
| [self batchLayoutNodesFromContexts:contexts ofKind:kind completion:^(NSArray<ASCellNode *> *nodes, NSArray<NSIndexPath *> *indexPaths) { | ||
| [self insertNodes:nodes ofKind:kind atIndexPaths:indexPaths completion:nil]; | ||
| }]; | ||
| [_pendingContexts removeObjectForKey:kind]; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment about mutating |
||
| } | ||
| } | ||
|
|
||
| - (void)_populateSupplementaryNodesOfKind:(NSString *)kind withMutableContexts:(NSMutableArray<ASIndexedNodeContext *> *)contexts | ||
| { | ||
| id<ASEnvironment> environment = [self.environmentDelegate dataControllerEnvironment]; | ||
|
|
@@ -167,21 +222,7 @@ - (void)_populateSupplementaryNodesOfKind:(NSString *)kind withMutableContexts:( | |
| NSUInteger rowCount = [self.collectionDataSource dataController:self supplementaryNodesOfKind:kind inSection:i]; | ||
| for (NSUInteger j = 0; j < rowCount; j++) { | ||
| NSIndexPath *indexPath = [sectionIndexPath indexPathByAddingIndex:j]; | ||
|
|
||
| ASCellNodeBlock supplementaryCellBlock; | ||
| if (_dataSourceImplementsSupplementaryNodeBlockOfKindAtIndexPath) { | ||
| supplementaryCellBlock = [self.collectionDataSource dataController:self supplementaryNodeBlockOfKind:kind atIndexPath:indexPath]; | ||
| } else { | ||
| ASCellNode *supplementaryNode = [self.collectionDataSource dataController:self supplementaryNodeOfKind:kind atIndexPath:indexPath]; | ||
| supplementaryCellBlock = ^{ return supplementaryNode; }; | ||
| } | ||
|
|
||
| ASSizeRange constrainedSize = [self constrainedSizeForNodeOfKind:kind atIndexPath:indexPath]; | ||
| ASIndexedNodeContext *context = [[ASIndexedNodeContext alloc] initWithNodeBlock:supplementaryCellBlock | ||
| indexPath:indexPath | ||
| constrainedSize:constrainedSize | ||
| environmentTraitCollection:environmentTraitCollection]; | ||
| [contexts addObject:context]; | ||
| [self _populateSupplementaryNodeOfKind:kind atIndexPath:indexPath mutableContexts:contexts environmentTraitCollection:environmentTraitCollection]; | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -196,7 +237,33 @@ - (void)_populateSupplementaryNodesOfKind:(NSString *)kind withSections:(NSIndex | |
| NSIndexPath *sectionIndex = [[NSIndexPath alloc] initWithIndex:idx]; | ||
| for (NSUInteger i = 0; i < rowNum; i++) { | ||
| NSIndexPath *indexPath = [sectionIndex indexPathByAddingIndex:i]; | ||
| [self _populateSupplementaryNodeOfKind:kind atIndexPath:indexPath mutableContexts:contexts environmentTraitCollection:environmentTraitCollection]; | ||
| } | ||
| }]; | ||
| } | ||
|
|
||
| - (void)_populateSupplementaryNodesOfKind:(NSString *)kind atIndexPaths:(NSArray<NSIndexPath *> *)indexPaths mutableContexts:(NSMutableArray<ASIndexedNodeContext *> *)contexts | ||
| { | ||
| id<ASEnvironment> environment = [self.environmentDelegate dataControllerEnvironment]; | ||
| ASEnvironmentTraitCollection environmentTraitCollection = environment.environmentTraitCollection; | ||
|
|
||
| NSMutableIndexSet *sections = [NSMutableIndexSet indexSet]; | ||
| for (NSIndexPath *indexPath in indexPaths) { | ||
| [sections addIndex:indexPath.section]; | ||
| } | ||
|
|
||
| [sections enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) { | ||
| NSUInteger rowNum = [self.collectionDataSource dataController:self supplementaryNodesOfKind:kind inSection:idx]; | ||
| NSIndexPath *sectionIndex = [[NSIndexPath alloc] initWithIndex:idx]; | ||
| for (NSUInteger i = 0; i < rowNum; i++) { | ||
| NSIndexPath *indexPath = [sectionIndex indexPathByAddingIndex:i]; | ||
| [self _populateSupplementaryNodeOfKind:kind atIndexPath:indexPath mutableContexts:contexts environmentTraitCollection:environmentTraitCollection]; | ||
| } | ||
| }]; | ||
| } | ||
|
|
||
| - (void)_populateSupplementaryNodeOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath mutableContexts:(NSMutableArray<ASIndexedNodeContext *> *)contexts environmentTraitCollection:(ASEnvironmentTraitCollection)environmentTraitCollection | ||
| { | ||
| ASCellNodeBlock supplementaryCellBlock; | ||
| if (_dataSourceImplementsSupplementaryNodeBlockOfKindAtIndexPath) { | ||
| supplementaryCellBlock = [self.collectionDataSource dataController:self supplementaryNodeBlockOfKind:kind atIndexPath:indexPath]; | ||
|
|
@@ -211,8 +278,6 @@ - (void)_populateSupplementaryNodesOfKind:(NSString *)kind withSections:(NSIndex | |
| constrainedSize:constrainedSize | ||
| environmentTraitCollection:environmentTraitCollection]; | ||
| [contexts addObject:context]; | ||
| } | ||
| }]; | ||
| } | ||
|
|
||
| #pragma mark - Sizing query | ||
|
|
@@ -264,4 +329,4 @@ - (void)setDataSource:(id<ASDataControllerSource>)dataSource | |
| ASDisplayNodeAssertTrue(_dataSourceImplementsSupplementaryNodeBlockOfKindAtIndexPath || [self.collectionDataSource respondsToSelector:@selector(dataController:supplementaryNodeOfKind:atIndexPath:)]); | ||
| } | ||
|
|
||
| @end | ||
| @end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,6 +53,28 @@ static void ASRecursivelyFindIndexPathsForMultidimensionalArray(NSObject *obj, N | |
| } | ||
| } | ||
|
|
||
| static BOOL ASElementExistsAtIndexPathForMultidimensionalArray(NSArray *array, NSIndexPath *indexPath) { | ||
| NSUInteger indexLength = indexPath.length; | ||
| ASDisplayNodeCAssert(indexLength != 0, @"Must have a non-zero indexPath length"); | ||
| NSUInteger firstIndex = [indexPath indexAtPosition:0]; | ||
| BOOL elementExists = firstIndex < array.count; | ||
|
|
||
| if (indexLength == 1) { | ||
| return elementExists; | ||
| } | ||
|
|
||
| if (!elementExists) { | ||
| return NO; | ||
| } | ||
|
|
||
| NSUInteger indexesLength = indexLength - 1; | ||
| NSUInteger indexes[indexesLength]; | ||
| [indexPath getIndexes:indexes range:NSMakeRange(1, indexesLength)]; | ||
| NSIndexPath *indexPathByRemovingFirstIndex = [NSIndexPath indexPathWithIndexes:indexes length:indexesLength]; | ||
|
|
||
| return ASElementExistsAtIndexPathForMultidimensionalArray(array[firstIndex], indexPathByRemovingFirstIndex); | ||
| } | ||
|
|
||
| #pragma mark - Public Methods | ||
|
|
||
| NSObject<NSCopying> *ASMultidimensionalArrayDeepMutableCopy(NSObject<NSCopying> *obj) | ||
|
|
@@ -142,6 +164,18 @@ void ASDeleteElementsInMultidimensionalArrayAtIndexPaths(NSMutableArray *mutable | |
| return res; | ||
| } | ||
|
|
||
| NSArray<NSIndexPath *> *ASIndexPathsInMultidimensionalArrayIntersectingIndexPaths(NSArray *multidimensionalArray, NSArray<NSIndexPath *> *indexPaths) | ||
| { | ||
| NSMutableArray *res = [NSMutableArray array]; | ||
| for (NSIndexPath *indexPath in indexPaths) { | ||
| if (ASElementExistsAtIndexPathForMultidimensionalArray(multidimensionalArray, indexPath)) { | ||
| [res addObject:indexPath]; | ||
| } | ||
| } | ||
|
|
||
| return res; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Returning mutable arrays is the precedent set by the code in this file—I wasn't sure if I should change it as part of this |
||
| } | ||
|
|
||
| NSArray *ASIndexPathsForTwoDimensionalArray(NSArray <NSArray *>* twoDimensionalArray) | ||
| { | ||
| NSMutableArray *result = [NSMutableArray array]; | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are mutating
_pendingContextswhile enumerating over it. Shouldn't this be throwing an exception?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like it doesn't throw an exception but does not do what you expect either. Try this in a playground:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I just realized that
_pendingContextsis a dictionary not an array. Sorry about that.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While I definitely agree that this is an issue, this is the precedent set by the code above in this file at https://github.com/facebook/AsyncDisplayKit/blob/master/AsyncDisplayKit/Details/ASCollectionDataController.mm#L90-L101
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@erichoracek please do fix that, as it seems to be an oversight.