Skip to content

Commit

Permalink
Renew supplementary node on relayout (#842)
Browse files Browse the repository at this point in the history
* fix SIMULATE_WEB_RESPONSE not imported #449

* Fix to make rangeMode update in right time

* Renew supplementary node on relayout.

* Add support for layoutDelegate (ASCollectionLayout).

* revert space

* Update change log

* fix build error

* refactor

* set default size

* return early when can delegate
  • Loading branch information
wsdwsd0829 authored and Adlai-Holler committed Jun 4, 2018
1 parent 21973c8 commit 9214e3c
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## master
* Add your own contributions to the next release on the line below this with your name.
- [ASDataController] Add capability to renew supplementary views (update map) when size change from zero to non-zero.[Max Wang](https://github.com/wsdwsd0829) [#842](https://github.com/TextureGroup/Texture/pull/842)
- Make `ASPerformMainThreadDeallocation` visible in C. [Adlai Holler](https://github.com/Adlai-Holler)
- Add snapshot test for astextnode2. [Max Wang](https://github.com/wsdwsd0829) [#935](https://github.com/TextureGroup/Texture/pull/935)
- Internal housekeeping on the async transaction (rendering) system. [Adlai Holler](https://github.com/Adlai-Holler)
Expand Down
62 changes: 57 additions & 5 deletions Source/Details/ASDataController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#import <AsyncDisplayKit/ASCellNode.h>
#import <AsyncDisplayKit/ASCollectionElement.h>
#import <AsyncDisplayKit/ASCollectionLayoutContext.h>
#import <AsyncDisplayKit/ASCollectionLayoutState.h>
#import <AsyncDisplayKit/ASDispatch.h>
#import <AsyncDisplayKit/ASDisplayNodeExtras.h>
#import <AsyncDisplayKit/ASElementMap.h>
Expand Down Expand Up @@ -281,6 +282,50 @@ - (void)_repopulateSupplementaryNodesIntoMap:(ASMutableElementMap *)map
}
}

/**
* Update supplementary nodes of all kinds for sections.
*
* @param map The element map into which to apply the change.
* @param traitCollection The trait collection needed to initialize elements
* @param shouldFetchSizeRanges Whether constrained sizes should be fetched from data source
*/
- (void)_updateSupplementaryNodesIntoMap:(ASMutableElementMap *)map
traitCollection:(ASPrimitiveTraitCollection)traitCollection
shouldFetchSizeRanges:(BOOL)shouldFetchSizeRanges
previousMap:(ASElementMap *)previousMap
{
ASDisplayNodeAssertMainThread();
if (self.layoutDelegate != nil) {
// TODO: https://github.com/TextureGroup/Texture/issues/948
return;
}
NSUInteger sectionCount = [self itemCountsFromDataSource].size();
if (sectionCount > 0) {
NSIndexSet *sectionIndexes = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, sectionCount)];
ASSizeRange newSizeRange = ASSizeRangeZero;
for (NSString *kind in [self supplementaryKindsInSections:sectionIndexes]) {
NSArray<NSIndexPath *> *indexPaths = [self _allIndexPathsForItemsOfKind:kind inSections:sectionIndexes];
NSMutableArray<NSIndexPath *> *indexPathsToDeleteForKind = [[NSMutableArray alloc] init];
NSMutableArray<NSIndexPath *> *indexPathsToInsertForKind = [[NSMutableArray alloc] init];
// If supplementary node does exist and size is now zero, remove it.
// If supplementary node doesn't exist and size is now non-zero, insert one.
for (NSIndexPath *indexPath in indexPaths) {
ASCollectionElement *previousElement = [previousMap supplementaryElementOfKind:kind atIndexPath:indexPath];
newSizeRange = [self constrainedSizeForNodeOfKind:kind atIndexPath:indexPath];
BOOL sizeRangeIsZero = ASSizeRangeEqualToSizeRange(ASSizeRangeZero, newSizeRange);
if (previousElement != nil && sizeRangeIsZero) {
[indexPathsToDeleteForKind addObject:indexPath];
} else if (previousElement == nil && !sizeRangeIsZero) {
[indexPathsToInsertForKind addObject:indexPath];
}
}

[map removeSupplementaryElementsAtIndexPaths:indexPathsToDeleteForKind kind:kind];
[self _insertElementsIntoMap:map kind:kind atIndexPaths:indexPathsToInsertForKind traitCollection:traitCollection shouldFetchSizeRanges:shouldFetchSizeRanges changeSet:nil previousMap:previousMap];
}
}
}

/**
* Inserts new elements of a certain kind for some sections
*
Expand Down Expand Up @@ -830,13 +875,20 @@ - (void)relayoutAllNodesWithInvalidationBlock:(nullable void (^)())invalidationB
- (void)_relayoutAllNodes
{
ASDisplayNodeAssertMainThread();
// Aggressively repopulate all supplemtary elements
// Assuming this method is run on the main serial queue, _pending and _visible maps are synced and can be manipulated directly.
ASDisplayNodeAssert(_visibleMap == _pendingMap, @"Expected visible and pending maps to be synchronized: %@", self);

ASMutableElementMap *newMap = [_pendingMap mutableCopy];
[self _updateSupplementaryNodesIntoMap:newMap
traitCollection:[self.node primitiveTraitCollection]
shouldFetchSizeRanges:YES
previousMap:_pendingMap];
_pendingMap = [newMap copy];
_visibleMap = _pendingMap;

for (ASCollectionElement *element in _visibleMap) {
// Ignore this element if it is no longer in the latest data. It is still recognized in the UIKit world but will be deleted soon.
NSIndexPath *indexPathInPendingMap = [_pendingMap indexPathForElement:element];
if (indexPathInPendingMap == nil) {
continue;
}

NSString *kind = element.supplementaryElementKind ?: ASDataControllerRowNodeKind;
ASSizeRange newConstrainedSize = [self constrainedSizeForNodeOfKind:kind atIndexPath:indexPathInPendingMap];

Expand Down
2 changes: 2 additions & 0 deletions Source/Private/ASMutableElementMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ AS_SUBCLASSING_RESTRICTED

- (void)removeSectionsOfItems:(NSIndexSet *)itemSections;

- (void)removeSupplementaryElementsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths kind:(NSString *)kind;

- (void)insertEmptySectionsOfItemsAtIndexes:(NSIndexSet *)sections;

- (void)insertElement:(ASCollectionElement *)element atIndexPath:(NSIndexPath *)indexPath;
Expand Down
5 changes: 5 additions & 0 deletions Source/Private/ASMutableElementMap.m
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ - (void)removeSectionsAtIndexes:(NSIndexSet *)indexes
[_sections removeObjectsAtIndexes:indexes];
}

- (void)removeSupplementaryElementsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths kind:(NSString *)kind
{
[_supplementaryElements[kind] removeObjectsForKeys:indexPaths];
}

- (void)removeAllElements
{
[_sectionsOfItems removeAllObjects];
Expand Down

0 comments on commit 9214e3c

Please sign in to comment.