Skip to content

Commit 0e98b7a

Browse files
authored
Fix invalidation after horizontal insets change (#164)
1 parent b6c137e commit 0e98b7a

1 file changed

Lines changed: 14 additions & 7 deletions

File tree

MagazineLayout/Public/MagazineLayout.swift

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,10 @@ public final class MagazineLayout: UICollectionViewLayout {
8585
_currentCollectionView = collectionView
8686
_delegateMagazineLayout = currentCollectionView.delegate as? UICollectionViewDelegateMagazineLayout
8787

88-
// Save the previous collection view width if necessary
89-
if prepareActions.contains(.cachePreviousWidth) {
88+
// Save the previous collection view width and horizontal insets if necessary
89+
if prepareActions.contains(.cachePreviousHorizontalMetrics) {
9090
cachedCollectionViewWidth = currentCollectionView.bounds.width
91+
cachedHorizontalContentInset = contentInset.left + contentInset.right
9192
}
9293

9394
if
@@ -905,14 +906,19 @@ public final class MagazineLayout: UICollectionViewLayout {
905906
prepareActions.formUnion(.recreateSectionModels)
906907
}
907908

908-
// Checking `cachedCollectionViewWidth != collectionView?.bounds.size.width` is necessary
909-
// because the collection view's width can change without a `contentSizeAdjustment` occurring.
909+
// Manually checking for a different width or different horizontal insets is necessary because
910+
// these values can change without a `contentSizeAdjustment` occurring.
910911
let isSameWidth = collectionView?.bounds.size.width.isEqual(
911912
to: cachedCollectionViewWidth ?? -.greatestFiniteMagnitude,
912913
screenScale: scale)
913914
?? false
914-
if !isSameWidth {
915-
prepareActions.formUnion(.cachePreviousWidth)
915+
let horizontalContentInset = (collectionView?.adjustedContentInset.left ?? 0) +
916+
(collectionView?.adjustedContentInset.right ?? 0)
917+
let isSameHorizontalContentInset = horizontalContentInset.isEqual(
918+
to: cachedHorizontalContentInset ?? -.greatestFiniteMagnitude,
919+
screenScale: scale)
920+
if !isSameWidth || !isSameHorizontalContentInset {
921+
prepareActions.formUnion(.cachePreviousHorizontalMetrics)
916922
prepareActions.formUnion(.updateLayoutMetrics)
917923
}
918924

@@ -994,7 +1000,7 @@ public final class MagazineLayout: UICollectionViewLayout {
9941000

9951001
static let recreateSectionModels = PrepareActions(rawValue: 1 << 0)
9961002
static let updateLayoutMetrics = PrepareActions(rawValue: 1 << 1)
997-
static let cachePreviousWidth = PrepareActions(rawValue: 1 << 2)
1003+
static let cachePreviousHorizontalMetrics = PrepareActions(rawValue: 1 << 2)
9981004
}
9991005
private var prepareActions: PrepareActions = []
10001006

@@ -1004,6 +1010,7 @@ public final class MagazineLayout: UICollectionViewLayout {
10041010
private var hasDataSourceCountInvalidationBeforeReceivingUpdateItems = false
10051011

10061012
private var cachedCollectionViewWidth: CGFloat?
1013+
private var cachedHorizontalContentInset: CGFloat?
10071014
private var previousContentInset: UIEdgeInsets?
10081015

10091016
// Unowned unsafe references to avoid weak reference overhead.

0 commit comments

Comments
 (0)