Skip to content

Commit

Permalink
Revert "Fix target content offset edge case (#115)"
Browse files Browse the repository at this point in the history
This reverts commit c3f346b.
  • Loading branch information
bryankeller committed Feb 8, 2024
1 parent c3f346b commit b0ebcbc
Showing 1 changed file with 12 additions and 42 deletions.
54 changes: 12 additions & 42 deletions MagazineLayout/Public/MagazineLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -265,32 +265,26 @@ public final class MagazineLayout: UICollectionViewLayout {
supplementaryViewLayoutAttributesForPendingAnimations.removeAll()

targetContentOffsetAnchor = nil
preInvalidationContentSize = nil
preInvalidationContentInset = nil

super.finalizeCollectionViewUpdates()
}

override public func prepare(forAnimatedBoundsChange oldBounds: CGRect) {
super.prepare(forAnimatedBoundsChange: oldBounds)

let contentSize: CGSize
let contentInset: UIEdgeInsets
if let metricsSnapshot = metricsSnapshots.first(where: { $0.bounds == oldBounds }) {
contentSize = metricsSnapshot.contentSize
contentInset = metricsSnapshot.contentInset
} else {
contentSize = collectionViewContentSize
contentInset = self.contentInset
}

targetContentOffsetAnchor = targetContentOffsetAnchor(
bounds: oldBounds,
contentHeight: contentSize.height,
topInset: contentInset.top,
bottomInset: contentInset.bottom)
contentHeight: preInvalidationContentSize?.height ?? collectionViewContentSize.height,
topInset: preInvalidationContentInset?.top ?? contentInset.top,
bottomInset: preInvalidationContentInset?.bottom ?? contentInset.bottom)
}

override public func finalizeAnimatedBoundsChange() {
targetContentOffsetAnchor = nil
preInvalidationContentSize = nil
preInvalidationContentInset = nil

super.finalizeAnimatedBoundsChange()
}
Expand Down Expand Up @@ -760,29 +754,10 @@ public final class MagazineLayout: UICollectionViewLayout {
return
}

// We need to save a few content size and content inset values for different bounds. This
// allows us to compute the correct target content offset in `prepareForAnimatedBoundsChange`.
// The root issue is that in the aforementioned function, we need a way to know what the content
// size and content inset _were_ for a given bounds value.
let metricsSnapshot = MetricsSnapshot(
bounds: currentCollectionView.bounds,
contentSize: collectionViewContentSize,
contentInset: lastContentInset ?? contentInset)

// Replace existing snapshot if the bounds is the same
let indexOfExistingMetricsSnapshot = metricsSnapshots.firstIndex {
$0.bounds == metricsSnapshot.bounds
}
if let indexOfExistingMetricsSnapshot {
metricsSnapshots[indexOfExistingMetricsSnapshot] = metricsSnapshot
} else {
metricsSnapshots.append(metricsSnapshot)
}

// Limit to 3 snapshots
if metricsSnapshots.count > 3 {
metricsSnapshots.removeFirst()
if collectionViewContentSize.width > 0, collectionViewContentSize.height > 0 {
preInvalidationContentSize = preInvalidationContentSize ?? collectionViewContentSize
}
preInvalidationContentInset = preInvalidationContentInset ?? lastContentInset ?? contentInset

let shouldInvalidateLayoutMetrics = !context.invalidateEverything &&
!context.invalidateDataSourceCounts
Expand Down Expand Up @@ -856,13 +831,8 @@ public final class MagazineLayout: UICollectionViewLayout {

private var lastContentInset: UIEdgeInsets?
private var cachedCollectionViewWidth: CGFloat?

private struct MetricsSnapshot {
let bounds: CGRect
let contentSize: CGSize
let contentInset: UIEdgeInsets
}
private var metricsSnapshots = [MetricsSnapshot]()
private var preInvalidationContentSize: CGSize?
private var preInvalidationContentInset: UIEdgeInsets?

// These properties are used to prevent scroll jumpiness due to self-sizing after rotation; see
// comment in `invalidationContext(forPreferredLayoutAttributes:withOriginalAttributes:)` for more
Expand Down

0 comments on commit b0ebcbc

Please sign in to comment.