Skip to content

Commit

Permalink
Fix data source index OOB crash (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
bryankeller committed Aug 4, 2022
1 parent 8cb3e72 commit d2eb5c5
Showing 1 changed file with 32 additions and 15 deletions.
47 changes: 32 additions & 15 deletions MagazineLayout/Public/MagazineLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -323,17 +323,19 @@ public final class MagazineLayout: UICollectionViewLayout {
let headerLocation = headerLocationFramePair.elementLocation
let headerFrame = headerLocationFramePair.frame

let layoutAttributes = headerLayoutAttributes(for: headerLocation, frame: headerFrame)
layoutAttributesInRect.append(layoutAttributes)
if let layoutAttributes = headerLayoutAttributes(for: headerLocation, frame: headerFrame) {
layoutAttributesInRect.append(layoutAttributes)
}
}

let footerLocationFramePairs = modelState.footerLocationFramePairs(forFootersIn: rect)
for footerLocationFramePair in footerLocationFramePairs {
let footerLocation = footerLocationFramePair.elementLocation
let footerFrame = footerLocationFramePair.frame

let layoutAttributes = footerLayoutAttributes(for: footerLocation, frame: footerFrame)
layoutAttributesInRect.append(layoutAttributes)
if let layoutAttributes = footerLayoutAttributes(for: footerLocation, frame: footerFrame) {
layoutAttributesInRect.append(layoutAttributes)
}
}

let backgroundLocationFramePairs = modelState.backgroundLocationFramePairs(
Expand All @@ -342,19 +344,23 @@ public final class MagazineLayout: UICollectionViewLayout {
let backgroundLocation = backgroundLocationFramePair.elementLocation
let backgroundFrame = backgroundLocationFramePair.frame

let layoutAttributes = backgroundLayoutAttributes(
for: backgroundLocation,
frame: backgroundFrame)
layoutAttributesInRect.append(layoutAttributes)
if
let layoutAttributes = backgroundLayoutAttributes(
for: backgroundLocation,
frame: backgroundFrame)
{
layoutAttributesInRect.append(layoutAttributes)
}
}

let itemLocationFramePairs = modelState.itemLocationFramePairs(forItemsIn: rect)
for itemLocationFramePair in itemLocationFramePairs {
let itemLocation = itemLocationFramePair.elementLocation
let itemFrame = itemLocationFramePair.frame

let layoutAttributes = itemLayoutAttributes(for: itemLocation, frame: itemFrame)
layoutAttributesInRect.append(layoutAttributes)
if let layoutAttributes = itemLayoutAttributes(for: itemLocation, frame: itemFrame) {
layoutAttributesInRect.append(layoutAttributes)
}
}

return layoutAttributesInRect
Expand Down Expand Up @@ -1223,8 +1229,10 @@ private extension MagazineLayout {
func headerLayoutAttributes(
for headerLocation: ElementLocation,
frame: CGRect)
-> UICollectionViewLayoutAttributes
-> UICollectionViewLayoutAttributes?
{
guard headerLocation.sectionIndex < currentCollectionView.numberOfSections else { return nil }

let layoutAttributes: MagazineLayoutCollectionViewLayoutAttributes
if
let cachedLayoutAttributes = headerLayoutAttributes[headerLocation],
Expand Down Expand Up @@ -1259,8 +1267,10 @@ private extension MagazineLayout {
func footerLayoutAttributes(
for footerLocation: ElementLocation,
frame: CGRect)
-> UICollectionViewLayoutAttributes
-> UICollectionViewLayoutAttributes?
{
guard footerLocation.sectionIndex < currentCollectionView.numberOfSections else { return nil }

let layoutAttributes: MagazineLayoutCollectionViewLayoutAttributes
if
let cachedLayoutAttributes = footerLayoutAttributes[footerLocation],
Expand Down Expand Up @@ -1295,8 +1305,12 @@ private extension MagazineLayout {
func backgroundLayoutAttributes(
for backgroundLocation: ElementLocation,
frame: CGRect)
-> UICollectionViewLayoutAttributes
-> UICollectionViewLayoutAttributes?
{
guard backgroundLocation.sectionIndex < currentCollectionView.numberOfSections else {
return nil
}

let layoutAttributes: MagazineLayoutCollectionViewLayoutAttributes
if
let cachedLayoutAttributes = backgroundLayoutAttributes[backgroundLocation],
Expand All @@ -1322,8 +1336,12 @@ private extension MagazineLayout {
func itemLayoutAttributes(
for itemLocation: ElementLocation,
frame: CGRect)
-> UICollectionViewLayoutAttributes
-> UICollectionViewLayoutAttributes?
{
guard itemLocation.sectionIndex < currentCollectionView.numberOfSections else { return nil }
let numberOfItems = currentCollectionView.numberOfItems(inSection: itemLocation.sectionIndex)
guard itemLocation.elementIndex < numberOfItems else { return nil }

let layoutAttributes: MagazineLayoutCollectionViewLayoutAttributes
if
let cachedLayoutAttributes = itemLayoutAttributes[itemLocation],
Expand All @@ -1344,7 +1362,6 @@ private extension MagazineLayout {
layoutAttributes.shouldVerticallySelfSize = true
}

let numberOfItems = currentCollectionView.numberOfItems(inSection: itemLocation.sectionIndex)
layoutAttributes.zIndex = numberOfItems - itemLocation.elementIndex

itemLayoutAttributes[itemLocation] = layoutAttributes
Expand Down

0 comments on commit d2eb5c5

Please sign in to comment.