1
0
mirror of https://github.com/danog/Telegram.git synced 2024-12-03 09:57:46 +01:00
Telegram/Telegraph/TGSharedMediaCollectionLayout.m
2015-10-01 19:19:52 +03:00

73 lines
2.7 KiB
Objective-C

#import "TGSharedMediaCollectionLayout.h"
#import "TGSharedMediaSectionHeader.h"
@interface TGSharedMediaCollectionLayout ()
{
bool _updatingCollectionItems;
NSArray *_sectionHeaders;
}
@end
@implementation TGSharedMediaCollectionLayout
- (UICollectionViewLayoutAttributes *)initialLayoutAttributesForAppearingItemAtIndexPath:(NSIndexPath *)itemIndexPath
{
if (_updatingCollectionItems || itemIndexPath.section != 0)
return [super initialLayoutAttributesForAppearingItemAtIndexPath:itemIndexPath];
return nil;
}
- (UICollectionViewLayoutAttributes *)finalLayoutAttributesForDisappearingItemAtIndexPath:(NSIndexPath *)itemIndexPath
{
if (_updatingCollectionItems || itemIndexPath.section != 0)
return [super finalLayoutAttributesForDisappearingItemAtIndexPath:itemIndexPath];
return [self layoutAttributesForItemAtIndexPath:itemIndexPath];
}
- (void)prepareLayout
{
[super prepareLayout];
NSMutableArray *sectionHeaders = [[NSMutableArray alloc] init];
id<UICollectionViewDataSource> dataSource = self.collectionView.dataSource;
NSUInteger numberOfSections = 1;
if ([dataSource respondsToSelector:@selector(numberOfSectionsInCollectionView:)])
numberOfSections = [dataSource numberOfSectionsInCollectionView:self.collectionView];
for (NSUInteger i = 0; i < numberOfSections; i++)
{
NSUInteger itemCount = [dataSource collectionView:self.collectionView numberOfItemsInSection:i];
if (itemCount != 0)
{
UICollectionViewLayoutAttributes *firstItemAttributes = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:i]];
UICollectionViewLayoutAttributes *lastItemAttributes = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:itemCount - 1 inSection:i]];
TGSharedMediaSectionHeader *sectionHeader = [[TGSharedMediaSectionHeader alloc] init];
sectionHeader.index = i;
CGFloat headerHeight = 36.0f;
sectionHeader.bounds = CGRectMake(0.0f, 0.0f, self.collectionView.bounds.size.width, headerHeight);
sectionHeader.floatingFrame = CGRectMake(0.0f, firstItemAttributes.frame.origin.y - sectionHeader.bounds.size.height, sectionHeader.bounds.size.width, CGRectGetMaxY(lastItemAttributes.frame) - (firstItemAttributes.frame.origin.y - sectionHeader.bounds.size.height));
[sectionHeaders addObject:sectionHeader];
}
}
_sectionHeaders = sectionHeaders;
}
- (NSArray *)sectionHeaders
{
return _sectionHeaders;
}
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)__unused newBounds
{
return false;
}
@end