Skip to content

Commit

Permalink
Fix target content offset edge case for small content (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
bryankeller authored Feb 21, 2024
1 parent d7bb3ff commit bfc6077
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
13 changes: 11 additions & 2 deletions MagazineLayout/LayoutCore/Types/TargetContentOffsetAnchor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,19 @@ enum TargetContentOffsetAnchor: Equatable {
{
let top = (-topInset).alignedToPixel(forScreenWithScale: scale)
let bottom = (contentHeight + bottomInset - bounds.height).alignedToPixel(forScreenWithScale: scale)
let isAtTop = bounds.minY <= top
let isAtBottom = bounds.minY >= bottom
let position: Position
if bounds.minY <= top {
if isAtTop, isAtBottom {
switch verticalLayoutDirection {
case .topToBottom:
position = .atTop
case .bottomToTop:
position = .atBottom
}
} else if isAtTop {
position = .atTop
} else if bounds.minY >= bottom {
} else if isAtBottom {
position = .atBottom
} else {
position = .inMiddle
Expand Down
32 changes: 31 additions & 1 deletion Tests/TargetContentOffsetAnchorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,21 @@ final class TargetContentOffsetAnchorTests: XCTestCase {
XCTAssert(anchor == .topItem(id: "6", itemEdge: .top, distanceFromTop: 20))
}

func testAnchor_TopToBottom_SmallContentHeight() throws {
let anchor = TargetContentOffsetAnchor.targetContentOffsetAnchor(
verticalLayoutDirection: .topToBottom,
topInset: 50,
bottomInset: 30,
bounds: CGRect(x: 0, y: -50, width: 300, height: 400),
contentHeight: 50,
scale: 1,
firstVisibleItemID: "0",
lastVisibleItemID: "1",
firstVisibleItemFrame: CGRect(x: 0, y: 0, width: 300, height: 20),
lastVisibleItemFrame: CGRect(x: 0, y: 30, width: 300, height: 20))
XCTAssert(anchor == .top)
}

// MARK: Bottom-to-Top Anchor Tests

func testAnchor_BottomToTop_ScrolledToTop() throws {
Expand Down Expand Up @@ -113,7 +128,22 @@ final class TargetContentOffsetAnchorTests: XCTestCase {
XCTAssert(anchor == .bottom)
}

// MARK: To-to-Bottom Target Content Offset Tests
func testAnchor_BottomToTop_SmallContentHeight() throws {
let anchor = TargetContentOffsetAnchor.targetContentOffsetAnchor(
verticalLayoutDirection: .bottomToTop,
topInset: 50,
bottomInset: 30,
bounds: CGRect(x: 0, y: -50, width: 300, height: 400),
contentHeight: 50,
scale: 1,
firstVisibleItemID: "0",
lastVisibleItemID: "1",
firstVisibleItemFrame: CGRect(x: 0, y: 0, width: 300, height: 20),
lastVisibleItemFrame: CGRect(x: 0, y: 30, width: 300, height: 20))
XCTAssert(anchor == .bottom)
}

// MARK: Top-to-Bottom Target Content Offset Tests

func testOffset_TopToBottom_ScrolledToTop() {
let anchor = TargetContentOffsetAnchor.top
Expand Down

0 comments on commit bfc6077

Please sign in to comment.