Skip to content

Commit

Permalink
Merge pull request #266 from gavrix/flowlayout_crash_fix
Browse files Browse the repository at this point in the history
#255 fix and better supplementary views support in animations
  • Loading branch information
steipete committed Mar 9, 2013
2 parents 7dce692 + 3ed0e9f commit f5c6dfa
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 49 deletions.
51 changes: 30 additions & 21 deletions PSTCollectionView/PSTCollectionView.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -1654,38 +1654,47 @@ - (void)updateWithItems:(NSArray *)items {
for (PSTCollectionViewItemKey *key in [_allVisibleViewsDict keyEnumerator]) { for (PSTCollectionViewItemKey *key in [_allVisibleViewsDict keyEnumerator]) {
PSTCollectionReusableView *view = _allVisibleViewsDict[key]; PSTCollectionReusableView *view = _allVisibleViewsDict[key];


NSInteger oldGlobalIndex = [_update[@"oldModel"] globalIndexForItemAtIndexPath:key.indexPath]; if (key.type == PSTCollectionViewItemTypeCell) {
NSArray *oldToNewIndexMap = _update[@"oldToNewIndexMap"]; NSInteger oldGlobalIndex = [_update[@"oldModel"] globalIndexForItemAtIndexPath:key.indexPath];
NSInteger newGlobalIndex = NSNotFound; NSArray *oldToNewIndexMap = _update[@"oldToNewIndexMap"];
if (oldGlobalIndex >= 0 && oldGlobalIndex < [oldToNewIndexMap count]) { NSInteger newGlobalIndex = NSNotFound;
newGlobalIndex = [oldToNewIndexMap[oldGlobalIndex] intValue]; if (oldGlobalIndex >= 0 && oldGlobalIndex < [oldToNewIndexMap count]) {
} newGlobalIndex = [oldToNewIndexMap[oldGlobalIndex] intValue];
NSIndexPath *newIndexPath = newGlobalIndex == NSNotFound ? nil : [_update[@"newModel"] indexPathForItemAtGlobalIndex:newGlobalIndex];
if (newIndexPath) {


PSTCollectionViewLayoutAttributes* startAttrs = nil;
PSTCollectionViewLayoutAttributes* finalAttrs = nil;

if (view.layoutAttributes && view.layoutAttributes.isSupplementaryView){
startAttrs = [_layout layoutAttributesForSupplementaryViewOfKind:view.layoutAttributes.representedElementKind atIndexPath:newIndexPath];
finalAttrs = [_layout layoutAttributesForSupplementaryViewOfKind:view.layoutAttributes.representedElementKind atIndexPath:newIndexPath];

} }
else{ NSIndexPath *newIndexPath = newGlobalIndex == NSNotFound ? nil : [_update[@"newModel"] indexPathForItemAtGlobalIndex:newGlobalIndex];
if (newIndexPath) {


PSTCollectionViewLayoutAttributes* startAttrs = nil;
PSTCollectionViewLayoutAttributes* finalAttrs = nil;

startAttrs = [_layout initialLayoutAttributesForAppearingItemAtIndexPath:newIndexPath]; startAttrs = [_layout initialLayoutAttributesForAppearingItemAtIndexPath:newIndexPath];
finalAttrs = [_layout layoutAttributesForItemAtIndexPath:newIndexPath]; finalAttrs = [_layout layoutAttributesForItemAtIndexPath:newIndexPath];


NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:@{@"view":view}];
if (startAttrs) dic[@"previousLayoutInfos"] = startAttrs;
if (finalAttrs) dic[@"newLayoutInfos"] = finalAttrs;

[animations addObject:dic];
PSTCollectionViewItemKey* newKey = [key copy];
[newKey setIndexPath:newIndexPath];
newAllVisibleView[newKey] = view;
} }
} else if (key.type == PSTCollectionViewItemTypeSupplementaryView) {
PSTCollectionViewLayoutAttributes* startAttrs = nil;
PSTCollectionViewLayoutAttributes* finalAttrs = nil;

startAttrs = view.layoutAttributes;
finalAttrs = [_layout layoutAttributesForSupplementaryViewOfKind:view.layoutAttributes.representedElementKind atIndexPath:key.indexPath];


NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:@{@"view":view}]; NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:@{@"view":view}];
if (startAttrs) dic[@"previousLayoutInfos"] = startAttrs; if (startAttrs) dic[@"previousLayoutInfos"] = startAttrs;
if (finalAttrs) dic[@"newLayoutInfos"] = finalAttrs; if (finalAttrs) dic[@"newLayoutInfos"] = finalAttrs;

[animations addObject:dic]; [animations addObject:dic];
PSTCollectionViewItemKey* newKey = [key copy]; PSTCollectionViewItemKey* newKey = [key copy];
[newKey setIndexPath:newIndexPath];
newAllVisibleView[newKey] = view; newAllVisibleView[newKey] = view;

} }
} }
NSArray *allNewlyVisibleItems = [_layout layoutAttributesForElementsInRect:self.visibleBoundRects]; NSArray *allNewlyVisibleItems = [_layout layoutAttributesForElementsInRect:self.visibleBoundRects];
Expand Down
33 changes: 22 additions & 11 deletions PSTCollectionView/PSTCollectionViewFlowLayout.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -215,22 +215,33 @@ - (PSTCollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSInd
- (PSTCollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { - (PSTCollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
NSUInteger sectionIndex = indexPath.section; NSUInteger sectionIndex = indexPath.section;


PSTCollectionViewLayoutAttributes *layoutAttributes = nil;

if (sectionIndex < _data.sections.count) { if (sectionIndex < _data.sections.count) {
PSTGridLayoutSection *section = _data.sections[sectionIndex]; PSTGridLayoutSection *section = _data.sections[sectionIndex];
CGRect normalizedHeaderFrame = section.headerFrame;

CGRect normilazedFrame = CGRectZero;
if (!CGRectIsEmpty(normalizedHeaderFrame)) {
normalizedHeaderFrame.origin.x += section.frame.origin.x; if ([kind isEqualToString:PSTCollectionElementKindSectionHeader]) {
normalizedHeaderFrame.origin.y += section.frame.origin.y; normilazedFrame = section.headerFrame;

}
PSTCollectionViewLayoutAttributes *layoutAttributes = [[[self class] layoutAttributesClass] layoutAttributesForSupplementaryViewOfKind:PSTCollectionElementKindSectionHeader withIndexPath:[NSIndexPath indexPathForItem:0 inSection:sectionIndex]]; else if ([kind isEqualToString:PSTCollectionElementKindSectionFooter]) {
layoutAttributes.frame = normalizedHeaderFrame; normilazedFrame = section.footerFrame;

return layoutAttributes;
} }

if (!CGRectIsEmpty(normilazedFrame)) {
normilazedFrame.origin.x += section.frame.origin.x;
normilazedFrame.origin.y += section.frame.origin.y;

layoutAttributes = [[[self class] layoutAttributesClass] layoutAttributesForSupplementaryViewOfKind:kind withIndexPath:[NSIndexPath indexPathForItem:0 inSection:sectionIndex]];
layoutAttributes.frame = normilazedFrame;

}


} }


return nil; return layoutAttributes;
} }


- (PSTCollectionViewLayoutAttributes *)layoutAttributesForDecorationViewWithReuseIdentifier:(NSString*)identifier atIndexPath:(NSIndexPath *)indexPath { - (PSTCollectionViewLayoutAttributes *)layoutAttributesForDecorationViewWithReuseIdentifier:(NSString*)identifier atIndexPath:(NSIndexPath *)indexPath {
Expand Down
44 changes: 27 additions & 17 deletions PSTCollectionView/PSTCollectionViewLayout.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -298,32 +298,35 @@ - (void)prepareForCollectionViewUpdates:(NSArray *)updateItems {


for (PSTCollectionReusableView *view in [[_collectionView visibleViewsDict] objectEnumerator]) { for (PSTCollectionReusableView *view in [[_collectionView visibleViewsDict] objectEnumerator]) {
PSTCollectionViewLayoutAttributes *attr = [view.layoutAttributes copy]; PSTCollectionViewLayoutAttributes *attr = [view.layoutAttributes copy];

if (attr.isCell) {
PSTCollectionViewData* oldModel = update[@"oldModel"];
NSInteger index = [oldModel globalIndexForItemAtIndexPath:[attr indexPath]]; NSInteger index = [update[@"oldModel"] globalIndexForItemAtIndexPath:[attr indexPath]];


if(index != NSNotFound) {
index = [update[@"oldToNewIndexMap"][index] intValue];
if(index != NSNotFound) { if(index != NSNotFound) {
[attr setIndexPath:[update[@"newModel"] indexPathForItemAtGlobalIndex:index]]; index = [update[@"oldToNewIndexMap"][index] intValue];
_initialAnimationLayoutAttributesDict[[PSTCollectionViewItemKey collectionItemKeyForLayoutAttributes:attr]] = attr; if(index != NSNotFound) {
[attr setIndexPath:[update[@"newModel"] indexPathForItemAtGlobalIndex:index]];
}
} }
} }
_initialAnimationLayoutAttributesDict[[PSTCollectionViewItemKey collectionItemKeyForLayoutAttributes:attr]] = attr;
} }


PSTCollectionViewData* collectionViewData = [_collectionView collectionViewData]; PSTCollectionViewData* collectionViewData = [_collectionView collectionViewData];


CGRect bounds = [_collectionView visibleBoundRects]; CGRect bounds = [_collectionView visibleBoundRects];


for (PSTCollectionViewLayoutAttributes* attr in [collectionViewData layoutAttributesForElementsInRect:bounds]) { for (PSTCollectionViewLayoutAttributes* attr in [collectionViewData layoutAttributesForElementsInRect:bounds]) {
NSInteger index = [collectionViewData globalIndexForItemAtIndexPath:attr.indexPath]; if (attr.isCell) {

NSInteger index = [collectionViewData globalIndexForItemAtIndexPath:attr.indexPath];
index = [update[@"newToOldIndexMap"][index] intValue];
if(index != NSNotFound) { index = [update[@"newToOldIndexMap"][index] intValue];
PSTCollectionViewLayoutAttributes* finalAttrs = [attr copy]; if(index != NSNotFound) {
[finalAttrs setIndexPath:[update[@"oldModel"] indexPathForItemAtGlobalIndex:index]]; PSTCollectionViewLayoutAttributes* finalAttrs = [attr copy];
[finalAttrs setAlpha:0]; [finalAttrs setIndexPath:[update[@"oldModel"] indexPathForItemAtGlobalIndex:index]];
_finalAnimationLayoutAttributesDict[[PSTCollectionViewItemKey collectionItemKeyForLayoutAttributes:finalAttrs]] = finalAttrs; [finalAttrs setAlpha:0];
_finalAnimationLayoutAttributesDict[[PSTCollectionViewItemKey collectionItemKeyForLayoutAttributes:finalAttrs]] = finalAttrs;
}
} }
} }


Expand Down Expand Up @@ -388,7 +391,14 @@ - (PSTCollectionViewLayoutAttributes *)finalLayoutAttributesForDisappearingItemA
} }


- (PSTCollectionViewLayoutAttributes *)initialLayoutAttributesForInsertedSupplementaryElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)elementIndexPath { - (PSTCollectionViewLayoutAttributes *)initialLayoutAttributesForInsertedSupplementaryElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)elementIndexPath {
return nil; PSTCollectionViewLayoutAttributes* attrs = _initialAnimationLayoutAttributesDict[[PSTCollectionViewItemKey collectionItemKeyForCellWithIndexPath:elementIndexPath]];

if([_insertedSectionsSet containsIndex:[elementIndexPath section]]) {
attrs = [attrs copy];
[attrs setAlpha:0];
}
return attrs;

} }


- (PSTCollectionViewLayoutAttributes *)finalLayoutAttributesForDeletedSupplementaryElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)elementIndexPath { - (PSTCollectionViewLayoutAttributes *)finalLayoutAttributesForDeletedSupplementaryElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)elementIndexPath {
Expand Down

0 comments on commit f5c6dfa

Please sign in to comment.