Skip to content

Commit

Permalink
[ASDataController ] Merge willUpdateWithChangeSet and didUpdateWithCh…
Browse files Browse the repository at this point in the history
…angeSet delegate methods #trivial (#445)

* Merge willUpdateWithChangeSet and didUpdateWithChangeSet delegate methods into one
- After #420, there is no change occurs between those 2 methods. Having them separately doesn't achieve anything and can cause confusions.

* Minor change
  • Loading branch information
nguyenhuy committed Jul 17, 2017
1 parent 292dc3c commit eb5bde0
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 65 deletions.
29 changes: 8 additions & 21 deletions Source/ASCollectionView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1954,45 +1954,32 @@ - (NSString *)nameForRangeControllerDataSource

#pragma mark - ASRangeControllerDelegate

- (void)rangeController:(ASRangeController *)rangeController willUpdateWithChangeSet:(_ASHierarchyChangeSet *)changeSet
- (void)rangeController:(ASRangeController *)rangeController updateWithChangeSet:(_ASHierarchyChangeSet *)changeSet updates:(dispatch_block_t)updates
{
ASDisplayNodeAssertMainThread();

if (!self.asyncDataSource || _superIsPendingDataLoad) {
updates();
[changeSet executeCompletionHandlerWithFinished:NO];
return; // if the asyncDataSource has become invalid while we are processing, ignore this request to avoid crashes
}

if (changeSet.includesReloadData) {
//TODO Do we need to notify _layoutFacilitator?
return;
}


//TODO Do we need to notify _layoutFacilitator before reloadData?
for (_ASHierarchyItemChange *change in [changeSet itemChangesOfType:_ASHierarchyChangeTypeDelete]) {
[_layoutFacilitator collectionViewWillEditCellsAtIndexPaths:change.indexPaths batched:YES];
}

for (_ASHierarchySectionChange *change in [changeSet sectionChangesOfType:_ASHierarchyChangeTypeDelete]) {
[_layoutFacilitator collectionViewWillEditSectionsAtIndexSet:change.indexSet batched:YES];
}

for (_ASHierarchySectionChange *change in [changeSet sectionChangesOfType:_ASHierarchyChangeTypeInsert]) {
[_layoutFacilitator collectionViewWillEditSectionsAtIndexSet:change.indexSet batched:YES];
}

for (_ASHierarchyItemChange *change in [changeSet itemChangesOfType:_ASHierarchyChangeTypeInsert]) {
[_layoutFacilitator collectionViewWillEditCellsAtIndexPaths:change.indexPaths batched:YES];
}
}

- (void)rangeController:(ASRangeController *)rangeController didUpdateWithChangeSet:(_ASHierarchyChangeSet *)changeSet updates:(dispatch_block_t)updates
{
ASDisplayNodeAssertMainThread();
if (!self.asyncDataSource || _superIsPendingDataLoad) {
updates();
[changeSet executeCompletionHandlerWithFinished:NO];
return; // if the asyncDataSource has become invalid while we are processing, ignore this request to avoid crashes
}

ASPerformBlockWithoutAnimation(!changeSet.animated, ^{
as_activity_scope(as_activity_create("Commit collection update", changeSet.rootActivity, OS_ACTIVITY_FLAG_DEFAULT));
if (changeSet.includesReloadData) {
Expand Down
23 changes: 8 additions & 15 deletions Source/ASTableView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1474,27 +1474,15 @@ - (NSString *)nameForRangeControllerDataSource

#pragma mark - ASRangeControllerDelegate

- (void)rangeController:(ASRangeController *)rangeController willUpdateWithChangeSet:(_ASHierarchyChangeSet *)changeSet
{
ASDisplayNodeAssertMainThread();
if (!self.asyncDataSource) {
return; // if the asyncDataSource has become invalid while we are processing, ignore this request to avoid crashes
}

if (_automaticallyAdjustsContentOffset && !changeSet.includesReloadData) {
[self beginAdjustingContentOffset];
}
}

- (void)rangeController:(ASRangeController *)rangeController didUpdateWithChangeSet:(_ASHierarchyChangeSet *)changeSet updates:(dispatch_block_t)updates
- (void)rangeController:(ASRangeController *)rangeController updateWithChangeSet:(_ASHierarchyChangeSet *)changeSet updates:(dispatch_block_t)updates
{
ASDisplayNodeAssertMainThread();
if (!self.asyncDataSource || _updatingInResponseToInteractiveMove) {
updates();
[changeSet executeCompletionHandlerWithFinished:NO];
return; // if the asyncDataSource has become invalid while we are processing, ignore this request to avoid crashes
}

if (changeSet.includesReloadData) {
LOG(@"UITableView reloadData");
ASPerformBlockWithoutAnimation(!changeSet.animated, ^{
Expand All @@ -1510,6 +1498,11 @@ - (void)rangeController:(ASRangeController *)rangeController didUpdateWithChange
});
return;
}

BOOL shouldAdjustContentOffset = (_automaticallyAdjustsContentOffset && !changeSet.includesReloadData);
if (shouldAdjustContentOffset) {
[self beginAdjustingContentOffset];
}

NSUInteger numberOfUpdates = 0;

Expand Down Expand Up @@ -1620,7 +1613,7 @@ - (void)rangeController:(ASRangeController *)rangeController didUpdateWithChange
[_rangeController updateIfNeeded];
[self _scheduleCheckForBatchFetchingForNumberOfChanges:numberOfUpdates];
});
if (_automaticallyAdjustsContentOffset) {
if (shouldAdjustContentOffset) {
[self endAdjustingContentOffsetAnimated:changeSet.animated];
}
[changeSet executeCompletionHandlerWithFinished:YES];
Expand Down
9 changes: 1 addition & 8 deletions Source/Details/ASDataController.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,6 @@ extern NSString * const ASCollectionInvalidUpdateException;
*/
@protocol ASDataControllerDelegate <NSObject>

/**
* Called before updating with given change set.
*
* @param changeSet The change set that includes all updates
*/
- (void)dataController:(ASDataController *)dataController willUpdateWithChangeSet:(_ASHierarchyChangeSet *)changeSet;

/**
* Called for change set updates.
*
Expand All @@ -126,7 +119,7 @@ extern NSString * const ASCollectionInvalidUpdateException;
* It should be called at the time the backing view is ready to process the updates,
* i.e inside the updates block of `-[UICollectionView performBatchUpdates:completion:] or after calling `-[UITableView beginUpdates]`.
*/
- (void)dataController:(ASDataController *)dataController didUpdateWithChangeSet:(_ASHierarchyChangeSet *)changeSet updates:(dispatch_block_t)updates;
- (void)dataController:(ASDataController *)dataController updateWithChangeSet:(_ASHierarchyChangeSet *)changeSet updates:(dispatch_block_t)updates;

@end

Expand Down
5 changes: 1 addition & 4 deletions Source/Details/ASDataController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -536,11 +536,8 @@ - (void)updateWithChangeSet:(_ASHierarchyChangeSet *)changeSet
dispatch_block_t completion = ^() {
[_mainSerialQueue performBlockOnMainThread:^{
as_activity_scope_leave(&preparationScope);
// TODO Merge the two delegate methods below
[_delegate dataController:self willUpdateWithChangeSet:changeSet];

// Step 4: Inform the delegate
[_delegate dataController:self didUpdateWithChangeSet:changeSet updates:^{
[_delegate dataController:self updateWithChangeSet:changeSet updates:^{
// Step 5: Deploy the new data as "completed"
//
// Note that since the backing collection view might be busy responding to user events (e.g scrolling),
Expand Down
11 changes: 2 additions & 9 deletions Source/Details/ASRangeController.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,7 @@ AS_SUBCLASSING_RESTRICTED
@protocol ASRangeControllerDelegate <NSObject>

/**
* Called before updating with given change set.
*
* @param changeSet The change set that includes all updates
*/
- (void)rangeController:(ASRangeController *)rangeController willUpdateWithChangeSet:(_ASHierarchyChangeSet *)changeSet;

/**
* Called after updating with given change set.
* Called to update with given change set.
*
* @param changeSet The change set that includes all updates
*
Expand All @@ -163,7 +156,7 @@ AS_SUBCLASSING_RESTRICTED
* It should be called at the time the backing view is ready to process the updates,
* i.e inside the updates block of `-[UICollectionView performBatchUpdates:completion:] or after calling `-[UITableView beginUpdates]`.
*/
- (void)rangeController:(ASRangeController *)rangeController didUpdateWithChangeSet:(_ASHierarchyChangeSet *)changeSet updates:(dispatch_block_t)updates;
- (void)rangeController:(ASRangeController *)rangeController updateWithChangeSet:(_ASHierarchyChangeSet *)changeSet updates:(dispatch_block_t)updates;

@end

Expand Down
10 changes: 2 additions & 8 deletions Source/Details/ASRangeController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -493,20 +493,14 @@ - (ASRangeTuningParameters)tuningParametersForRangeMode:(ASLayoutRangeMode)range

#pragma mark - ASDataControllerDelegete

- (void)dataController:(ASDataController *)dataController willUpdateWithChangeSet:(_ASHierarchyChangeSet *)changeSet
- (void)dataController:(ASDataController *)dataController updateWithChangeSet:(_ASHierarchyChangeSet *)changeSet updates:(dispatch_block_t)updates
{
ASDisplayNodeAssertMainThread();
if (changeSet.includesReloadData) {
[self _setVisibleNodes:nil];
}
[_delegate rangeController:self willUpdateWithChangeSet:changeSet];
}

- (void)dataController:(ASDataController *)dataController didUpdateWithChangeSet:(_ASHierarchyChangeSet *)changeSet updates:(dispatch_block_t)updates
{
ASDisplayNodeAssertMainThread();
_rangeIsValid = NO;
[_delegate rangeController:self didUpdateWithChangeSet:changeSet updates:updates];
[_delegate rangeController:self updateWithChangeSet:changeSet updates:updates];
}

#pragma mark - Memory Management
Expand Down

0 comments on commit eb5bde0

Please sign in to comment.