mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-09-13 15:48:18 +00:00
This diff resolves all known consistency issues with ASTableView and ASCollectionView. It includes significantly more aggressive thrash-testing in ASTableViewStressTest, which now passes on a variety of device and simulator configurations. It also updates the unit tests run on every commit to ensure any regression is caught quickly. A few of the salient changes in this diff: - ASTableView now uses Rene's ASCollectionViewLayoutController, and actually uses a UICollectionViewFlowLayout without any UICollectionView. This resolves an issue where ASFlowLayoutController was generating slightly out-of-bounds indicies when programmatically scrolling past the end of the table content. Because the custom implementation is likely faster, I will revisit this later with profiling and possibly returning to the custom impl. - There is now a second copy of the _nodes array maintained by ASDataController. It shares the same node instances, but this does add some overhead to manipulating the arrays. I've filed a task to follow up with optimization, as there are several great opportunities to make it faster. However, I don't believe the overhead is a significant issue, and it does guarantee correctness in even the toughest app usage scenarios. - ASDataController no longer supports calling its delegate /before/ edit operations. No other class was relying on this behavior, and it would be unusual for an app developer to use ASDataController directly. However, it is possible that someone with a custom view that integrates with ASDataController and ASRangeController could be affected by this. - Further cleanup of organization, naming, additional comments, reduced code length wherever possible. Overall, significantly more accessible to a new reader.
178 lines
7.2 KiB
Plaintext
178 lines
7.2 KiB
Plaintext
/* Copyright (c) 2015-present, Facebook, Inc.
|
|
* All rights reserved.
|
|
*
|
|
* This source code is licensed under the BSD-style license found in the
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
*/
|
|
|
|
#import "ASCollectionViewLayoutController.h"
|
|
|
|
#include <vector>
|
|
|
|
#import "ASAssert.h"
|
|
#import "ASCollectionView.h"
|
|
#import "CGRect+ASConvenience.h"
|
|
|
|
struct ASDirectionalScreenfulBuffer {
|
|
CGFloat positiveDirection; // Positive relative to iOS Core Animation layer coordinate space.
|
|
CGFloat negativeDirection;
|
|
};
|
|
typedef struct ASDirectionalScreenfulBuffer ASDirectionalScreenfulBuffer;
|
|
|
|
ASDirectionalScreenfulBuffer ASDirectionalScreenfulBufferHorizontal(ASScrollDirection scrollDirection,
|
|
ASRangeTuningParameters rangeTuningParameters)
|
|
{
|
|
ASDirectionalScreenfulBuffer horizontalBuffer = {0, 0};
|
|
BOOL movingRight = ASScrollDirectionContainsRight(scrollDirection);
|
|
horizontalBuffer.positiveDirection = movingRight ? rangeTuningParameters.leadingBufferScreenfuls :
|
|
rangeTuningParameters.trailingBufferScreenfuls;
|
|
horizontalBuffer.negativeDirection = movingRight ? rangeTuningParameters.trailingBufferScreenfuls :
|
|
rangeTuningParameters.leadingBufferScreenfuls;
|
|
return horizontalBuffer;
|
|
}
|
|
|
|
ASDirectionalScreenfulBuffer ASDirectionalScreenfulBufferVertical(ASScrollDirection scrollDirection,
|
|
ASRangeTuningParameters rangeTuningParameters)
|
|
{
|
|
ASDirectionalScreenfulBuffer verticalBuffer = {0, 0};
|
|
BOOL movingDown = ASScrollDirectionContainsDown(scrollDirection);
|
|
verticalBuffer.positiveDirection = movingDown ? rangeTuningParameters.leadingBufferScreenfuls :
|
|
rangeTuningParameters.trailingBufferScreenfuls;
|
|
verticalBuffer.negativeDirection = movingDown ? rangeTuningParameters.trailingBufferScreenfuls :
|
|
rangeTuningParameters.leadingBufferScreenfuls;
|
|
return verticalBuffer;
|
|
}
|
|
|
|
struct ASRangeGeometry {
|
|
CGRect rangeBounds;
|
|
CGRect updateBounds;
|
|
};
|
|
typedef struct ASRangeGeometry ASRangeGeometry;
|
|
|
|
|
|
#pragma mark -
|
|
#pragma mark ASCollectionViewLayoutController
|
|
|
|
@interface ASCollectionViewLayoutController ()
|
|
{
|
|
UIScrollView * __weak _scrollView;
|
|
UICollectionViewLayout * __strong _collectionViewLayout;
|
|
std::vector<CGRect> _updateRangeBoundsIndexedByRangeType;
|
|
ASScrollDirection _scrollableDirections;
|
|
}
|
|
@end
|
|
|
|
@implementation ASCollectionViewLayoutController
|
|
|
|
- (instancetype)initWithCollectionView:(ASCollectionView *)collectionView
|
|
{
|
|
if (!(self = [super init])) {
|
|
return nil;
|
|
}
|
|
|
|
_scrollableDirections = [collectionView scrollableDirections];
|
|
_scrollView = collectionView;
|
|
_collectionViewLayout = [collectionView collectionViewLayout];
|
|
_updateRangeBoundsIndexedByRangeType = std::vector<CGRect>(ASLayoutRangeTypeCount);
|
|
return self;
|
|
}
|
|
|
|
- (instancetype)initWithScrollView:(UIScrollView *)scrollView collectionViewLayout:(UICollectionViewLayout *)layout
|
|
{
|
|
if (!(self = [super init])) {
|
|
return nil;
|
|
}
|
|
|
|
_scrollableDirections = ASScrollDirectionVerticalDirections;
|
|
_scrollView = scrollView;
|
|
_collectionViewLayout = layout;
|
|
_updateRangeBoundsIndexedByRangeType = std::vector<CGRect>(ASLayoutRangeTypeCount);
|
|
return self;
|
|
}
|
|
|
|
|
|
#pragma mark -
|
|
#pragma mark Index Paths in Range
|
|
|
|
- (NSSet *)indexPathsForScrolling:(ASScrollDirection)scrollDirection
|
|
viewportSize:(CGSize)viewportSize
|
|
rangeType:(ASLayoutRangeType)rangeType
|
|
{
|
|
ASRangeGeometry rangeGeometry = [self rangeGeometryWithScrollDirection:scrollDirection
|
|
rangeTuningParameters:[self tuningParametersForRangeType:rangeType]];
|
|
_updateRangeBoundsIndexedByRangeType[rangeType] = rangeGeometry.updateBounds;
|
|
return [self indexPathsForItemsWithinRangeBounds:rangeGeometry.rangeBounds];
|
|
}
|
|
|
|
- (ASRangeGeometry)rangeGeometryWithScrollDirection:(ASScrollDirection)scrollDirection
|
|
rangeTuningParameters:(ASRangeTuningParameters)rangeTuningParameters
|
|
{
|
|
CGRect rangeBounds = _scrollView.bounds;
|
|
CGRect updateBounds = _scrollView.bounds;
|
|
|
|
BOOL canScrollHorizontally = ASScrollDirectionContainsHorizontalDirection(_scrollableDirections);
|
|
if (canScrollHorizontally) {
|
|
ASDirectionalScreenfulBuffer horizontalBuffer = ASDirectionalScreenfulBufferHorizontal(scrollDirection,
|
|
rangeTuningParameters);
|
|
rangeBounds = asdk_CGRectExpandHorizontally(rangeBounds,
|
|
horizontalBuffer.negativeDirection,
|
|
horizontalBuffer.positiveDirection);
|
|
// Update bounds is at most 95% of the next/previous screenful and at least half of tuning parameter value.
|
|
updateBounds = asdk_CGRectExpandHorizontally(updateBounds,
|
|
MIN(horizontalBuffer.negativeDirection * 0.5, 0.95),
|
|
MIN(horizontalBuffer.positiveDirection * 0.5, 0.95));
|
|
}
|
|
|
|
BOOL canScrollVertically = ASScrollDirectionContainsVerticalDirection(_scrollableDirections);
|
|
if (canScrollVertically) {
|
|
ASDirectionalScreenfulBuffer verticalBuffer = ASDirectionalScreenfulBufferVertical(scrollDirection,
|
|
rangeTuningParameters);
|
|
rangeBounds = asdk_CGRectExpandVertically(rangeBounds,
|
|
verticalBuffer.negativeDirection,
|
|
verticalBuffer.positiveDirection);
|
|
// Update bounds is at most 95% of the next/previous screenful and at least half of tuning parameter value.
|
|
updateBounds = asdk_CGRectExpandVertically(updateBounds,
|
|
MIN(verticalBuffer.negativeDirection * 0.5, 0.95),
|
|
MIN(verticalBuffer.positiveDirection * 0.5, 0.95));
|
|
}
|
|
|
|
return {rangeBounds, updateBounds};
|
|
}
|
|
|
|
- (NSSet *)indexPathsForItemsWithinRangeBounds:(CGRect)rangeBounds
|
|
{
|
|
NSMutableSet *indexPathSet = [[NSMutableSet alloc] init];
|
|
NSArray *layoutAttributes = [_collectionViewLayout layoutAttributesForElementsInRect:rangeBounds];
|
|
for (UICollectionViewLayoutAttributes *la in layoutAttributes) {
|
|
[indexPathSet addObject:la.indexPath];
|
|
}
|
|
return indexPathSet;
|
|
}
|
|
|
|
#pragma mark -
|
|
#pragma mark Should Update Range
|
|
|
|
- (BOOL)shouldUpdateForVisibleIndexPaths:(NSArray *)indexPaths
|
|
viewportSize:(CGSize)viewportSize
|
|
rangeType:(ASLayoutRangeType)rangeType
|
|
{
|
|
CGRect updateRangeBounds = _updateRangeBoundsIndexedByRangeType[rangeType];
|
|
if (CGRectIsEmpty(updateRangeBounds)) {
|
|
return YES;
|
|
}
|
|
|
|
CGRect currentBounds = _scrollView.bounds;
|
|
if (CGRectIsEmpty(currentBounds)) {
|
|
currentBounds = CGRectMake(0, 0, viewportSize.width, viewportSize.height);
|
|
}
|
|
|
|
if (CGRectContainsRect(updateRangeBounds, currentBounds)) {
|
|
return NO;
|
|
} else {
|
|
return YES;
|
|
}
|
|
}
|
|
|
|
@end
|