From 57d32010d052d1ad9778a014ed8da65fe848f227 Mon Sep 17 00:00:00 2001 From: Adlai Holler Date: Fri, 19 Aug 2016 11:19:36 -0700 Subject: [PATCH] Keep working on the unification --- AsyncDisplayKit/ASCollectionView.mm | 2 +- AsyncDisplayKit/ASTableView.mm | 2 +- AsyncDisplayKit/Details/ASDataController.h | 7 +++- AsyncDisplayKit/Details/ASDataController.mm | 42 +++++++++++---------- 4 files changed, 31 insertions(+), 22 deletions(-) diff --git a/AsyncDisplayKit/ASCollectionView.mm b/AsyncDisplayKit/ASCollectionView.mm index b029262b..b170531f 100644 --- a/AsyncDisplayKit/ASCollectionView.mm +++ b/AsyncDisplayKit/ASCollectionView.mm @@ -229,7 +229,7 @@ static NSString * const kCellReuseIdentifier = @"_ASCollectionViewCell"; _rangeController.delegate = self; _rangeController.layoutController = _layoutController; - _dataController = [[ASChangeSetDataController alloc] initWithDataSource:self]; + _dataController = [[ASChangeSetDataController alloc] initWithDataSource:self supplementaryItemSupport:YES]; _dataController.delegate = _rangeController; _dataController.environmentDelegate = self; diff --git a/AsyncDisplayKit/ASTableView.mm b/AsyncDisplayKit/ASTableView.mm index 8dc50524..7c607a0f 100644 --- a/AsyncDisplayKit/ASTableView.mm +++ b/AsyncDisplayKit/ASTableView.mm @@ -188,7 +188,7 @@ static NSString * const kCellReuseIdentifier = @"_ASTableViewCell"; _rangeController.dataSource = self; _rangeController.delegate = self; - _dataController = [[dataControllerClass alloc] initWithDataSource:self]; + _dataController = [[dataControllerClass alloc] initWithDataSource:self supplementaryItemSupport:NO]; _dataController.delegate = _rangeController; _dataController.environmentDelegate = self; diff --git a/AsyncDisplayKit/Details/ASDataController.h b/AsyncDisplayKit/Details/ASDataController.h index 5280ce3e..24ed226e 100644 --- a/AsyncDisplayKit/Details/ASDataController.h +++ b/AsyncDisplayKit/Details/ASDataController.h @@ -124,7 +124,7 @@ FOUNDATION_EXPORT NSString * const ASDataControllerRowNodeKind; @protocol ASFlowLayoutControllerDataSource; @interface ASDataController : ASDealloc2MainObject -- (instancetype)initWithDataSource:(id)dataSource NS_DESIGNATED_INITIALIZER; +- (instancetype)initWithDataSource:(id)dataSource supplementaryItemSupport:(BOOL)supplementaryItemSupport NS_DESIGNATED_INITIALIZER; /** Data source for fetching data info. @@ -136,6 +136,11 @@ FOUNDATION_EXPORT NSString * const ASDataControllerRowNodeKind; */ @property (nonatomic, weak) id delegate; +/** + Delegate to notify when data is updated. + */ +@property (nonatomic, readonly) BOOL supplementaryItemSupport; + /** * */ diff --git a/AsyncDisplayKit/Details/ASDataController.mm b/AsyncDisplayKit/Details/ASDataController.mm index dfa32756..5e1eb849 100644 --- a/AsyncDisplayKit/Details/ASDataController.mm +++ b/AsyncDisplayKit/Details/ASDataController.mm @@ -62,7 +62,7 @@ NSString * const ASDataControllerRowNodeKind = @"_ASDataControllerRowNodeKind"; BOOL _dataSourceImplementsSupplementaryNodeBlockOfKindAtIndexPath; - NSMutableDictionary *> *_pendingContexts; + NSMutableDictionary *> *_pendingSupplementaryContexts; } @end @@ -71,7 +71,7 @@ NSString * const ASDataControllerRowNodeKind = @"_ASDataControllerRowNodeKind"; #pragma mark - Lifecycle -- (instancetype)initWithDataSource:(id)dataSource +- (instancetype)initWithDataSource:(id)dataSource supplementaryItemSupport:(BOOL)supplementaryItemSupport { if (!(self = [super init])) { return nil; @@ -86,11 +86,15 @@ NSString * const ASDataControllerRowNodeKind = @"_ASDataControllerRowNodeKind"; _completedNodes[ASDataControllerRowNodeKind] = [NSMutableArray array]; _editingNodes[ASDataControllerRowNodeKind] = [NSMutableArray array]; - _pendingContexts = [NSMutableDictionary dictionary]; + _pendingSupplementaryContexts = [NSMutableDictionary dictionary]; + _supplementaryItemSupport = supplementaryItemSupport; _dataSourceImplementsSupplementaryNodeBlockOfKindAtIndexPath = [dataSource respondsToSelector:@selector(dataController:supplementaryNodeBlockOfKind:atIndexPath:)]; - - ASDisplayNodeAssertTrue(_dataSourceImplementsSupplementaryNodeBlockOfKindAtIndexPath || [dataSource respondsToSelector:@selector(dataController:supplementaryNodeOfKind:atIndexPath:)]); + + if (supplementaryItemSupport) { + ASDisplayNodeAssertTrue(_dataSourceImplementsSupplementaryNodeBlockOfKindAtIndexPath || [dataSource respondsToSelector:@selector(dataController:supplementaryNodeOfKind:atIndexPath:)]); + + } _mainSerialQueue = [[ASMainSerialQueue alloc] init]; @@ -106,7 +110,7 @@ NSString * const ASDataControllerRowNodeKind = @"_ASDataControllerRowNodeKind"; { ASDisplayNodeFailAssert(@"Failed to call designated initializer."); id fakeDataSource = nil; - return [self initWithDataSource:fakeDataSource]; + return [self initWithDataSource:fakeDataSource supplementaryItemSupport:NO]; } - (void)setDelegate:(id)delegate @@ -413,12 +417,12 @@ NSString * const ASDataControllerRowNodeKind = @"_ASDataControllerRowNodeKind"; NSIndexSet *sectionIndexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, sectionCount)]; NSArray *contexts = [self _populateFromDataSourceWithSectionIndexSet:sectionIndexSet]; - // Allow subclasses to perform setup before going into the edit transaction + // Populate supplementaries for (NSString *kind in [self supplementaryKinds]) { LOG(@"Populating elements of kind: %@", kind); NSMutableArray *contexts = [NSMutableArray array]; [self _populateSupplementaryNodesOfKind:kind withMutableContexts:contexts]; - _pendingContexts[kind] = contexts; + _pendingSupplementaryContexts[kind] = contexts; } dispatch_group_async(_editingTransactionGroup, _editingTransactionQueue, ^{ @@ -435,7 +439,7 @@ NSString * const ASDataControllerRowNodeKind = @"_ASDataControllerRowNodeKind"; } // Supplementary node handling - [_pendingContexts enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull kind, NSMutableArray * _Nonnull contexts, __unused BOOL * _Nonnull stop) { + [_pendingSupplementaryContexts enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull kind, NSMutableArray * _Nonnull contexts, __unused BOOL * _Nonnull stop) { // Remove everything that existed before the reload, now that we're ready to insert replacements NSArray *indexPaths = [self indexPathsForEditingNodesOfKind:kind]; [self deleteNodesOfKind:kind atIndexPaths:indexPaths completion:nil]; @@ -455,7 +459,7 @@ NSString * const ASDataControllerRowNodeKind = @"_ASDataControllerRowNodeKind"; [self insertNodes:nodes ofKind:kind atIndexPaths:indexPaths completion:nil]; }]; }]; - [_pendingContexts removeAllObjects]; + [_pendingSupplementaryContexts removeAllObjects]; // Insert empty sections NSMutableArray *sections = [NSMutableArray arrayWithCapacity:sectionCount]; @@ -681,12 +685,12 @@ NSString * const ASDataControllerRowNodeKind = @"_ASDataControllerRowNodeKind"; LOG(@"Populating elements of kind: %@, for sections: %@", kind, sections); NSMutableArray *contexts = [NSMutableArray array]; [self _populateSupplementaryNodesOfKind:kind withSections:sections mutableContexts:contexts]; - _pendingContexts[kind] = contexts; + _pendingSupplementaryContexts[kind] = contexts; } dispatch_group_async(_editingTransactionGroup, _editingTransactionQueue, ^{ // Supplementary node handling - [_pendingContexts enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull kind, NSMutableArray * _Nonnull contexts, BOOL * _Nonnull stop) { + [_pendingSupplementaryContexts enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull kind, NSMutableArray * _Nonnull contexts, BOOL * _Nonnull stop) { NSMutableArray *sectionArray = [NSMutableArray arrayWithCapacity:sections.count]; for (NSUInteger i = 0; i < sections.count; i++) { [sectionArray addObject:[NSMutableArray array]]; @@ -697,7 +701,7 @@ NSString * const ASDataControllerRowNodeKind = @"_ASDataControllerRowNodeKind"; [self insertNodes:nodes ofKind:kind atIndexPaths:indexPaths completion:nil]; }]; }]; - [_pendingContexts removeAllObjects]; + [_pendingSupplementaryContexts removeAllObjects]; LOG(@"Edit Transaction - insertSections: %@", sections); NSMutableArray *sectionArray = [NSMutableArray arrayWithCapacity:sections.count]; @@ -825,18 +829,18 @@ NSString * const ASDataControllerRowNodeKind = @"_ASDataControllerRowNodeKind"; LOG(@"Populating elements of kind: %@, for index paths: %@", kind, indexPaths); NSMutableArray *contexts = [NSMutableArray array]; [self _populateSupplementaryNodesOfKind:kind atIndexPaths:indexPaths mutableContexts:contexts]; - _pendingContexts[kind] = contexts; + _pendingSupplementaryContexts[kind] = contexts; } dispatch_group_async(_editingTransactionGroup, _editingTransactionQueue, ^{ // Supplementary node handling - [_pendingContexts enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull kind, NSMutableArray * _Nonnull contexts, BOOL * _Nonnull stop) { + [_pendingSupplementaryContexts enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull kind, NSMutableArray * _Nonnull contexts, BOOL * _Nonnull stop) { [self batchLayoutNodesFromContexts:contexts batchCompletion:^(NSArray *nodes, NSArray *indexPaths) { [self insertNodes:nodes ofKind:kind atIndexPaths:indexPaths completion:nil]; }]; }]; - [_pendingContexts removeAllObjects]; + [_pendingSupplementaryContexts removeAllObjects]; LOG(@"Edit Transaction - insertRows: %@", indexPaths); [self _batchLayoutAndInsertNodesFromContexts:contexts withAnimationOptions:animationOptions]; @@ -863,7 +867,7 @@ NSString * const ASDataControllerRowNodeKind = @"_ASDataControllerRowNodeKind"; for (NSString *kind in [self supplementaryKinds]) { NSMutableArray *contexts = [NSMutableArray array]; [self _populateSupplementaryNodesOfKind:kind atIndexPaths:indexPaths mutableContexts:contexts]; - _pendingContexts[kind] = contexts; + _pendingSupplementaryContexts[kind] = contexts; } dispatch_group_async(_editingTransactionGroup, _editingTransactionQueue, ^{ @@ -876,7 +880,7 @@ NSString * const ASDataControllerRowNodeKind = @"_ASDataControllerRowNodeKind"; // If any of the contexts remain after the deletion, re-insert them, e.g. // UICollectionElementKindSectionHeader remains even if item 0 is deleted. NSMutableArray *reinsertedContexts = [NSMutableArray array]; - for (ASIndexedNodeContext *context in _pendingContexts[kind]) { + for (ASIndexedNodeContext *context in _pendingSupplementaryContexts[kind]) { if ([deletedIndexPaths containsObject:context.indexPath]) { [reinsertedContexts addObject:context]; } @@ -886,7 +890,7 @@ NSString * const ASDataControllerRowNodeKind = @"_ASDataControllerRowNodeKind"; [self insertNodes:nodes ofKind:kind atIndexPaths:indexPaths completion:nil]; }]; } - [_pendingContexts removeAllObjects]; + [_pendingSupplementaryContexts removeAllObjects]; LOG(@"Edit Transaction - deleteRows: %@", indexPaths); [self _deleteNodesAtIndexPaths:sortedIndexPaths withAnimationOptions:animationOptions];