Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 1 addition & 17 deletions Example/MagazineLayoutExample/GridDemoViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -291,23 +291,7 @@ extension GridDemoViewController: UICollectionViewDelegateMagazineLayout {

private struct GridItem: Hashable {

// MARK: Lifecycle

init(
id: UUID = UUID(),
text: String,
color: UIColor,
widthMode: MagazineLayoutItemWidthMode)
{
self.id = id
self.text = text
self.color = color
self.widthMode = widthMode
}

// MARK: Internal

let id: UUID
let id = UUID()
let text: String
let color: UIColor
let widthMode: MagazineLayoutItemWidthMode
Expand Down
18 changes: 1 addition & 17 deletions Example/MagazineLayoutExample/ListDemoViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -383,23 +383,7 @@ private struct ListSection: Hashable {

private struct ListItem: Hashable {

// MARK: Lifecycle

init(
id: UUID = UUID(),
title: String,
subtitle: String,
color: UIColor)
{
self.id = id
self.title = title
self.subtitle = subtitle
self.color = color
}

// MARK: Internal

let id: UUID
let id = UUID()
let title: String
let subtitle: String
let color: UIColor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,20 +302,18 @@ private struct Message: Hashable {
// MARK: Lifecycle

init(
id: UUID = UUID(),
text: String,
isSent: Bool,
timestamp: Date = Date())
{
self.id = id
self.text = text
self.isSent = isSent
self.timestamp = timestamp
}

// MARK: Internal

let id: UUID
let id = UUID()
let text: String
let isSent: Bool
let timestamp: Date
Expand Down
4 changes: 3 additions & 1 deletion MagazineLayout/LayoutCore/LayoutState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ struct LayoutState {
var targetContentOffsetAnchor: TargetContentOffsetAnchor {
var visibleItemLocationFramePairs = [ElementLocationFramePair]()
for itemLocationFramePair in modelState.itemLocationFramePairs(forItemsIn: bounds) {
// Only consider fully-visible items
guard bounds.contains(itemLocationFramePair.frame) else { continue }
visibleItemLocationFramePairs.append(itemLocationFramePair)
}
visibleItemLocationFramePairs.sort { $0.elementLocation < $1.elementLocation }
Expand All @@ -74,7 +76,7 @@ struct LayoutState {
modelState.isItemHeightSettled(indexPath: $0.elementLocation.indexPath)
} ?? visibleItemLocationFramePairs.first // fallback to the first item if we can't find one with a settled height

let lastVisibleItemLocationFramePair = visibleItemLocationFramePairs.reversed().first {
let lastVisibleItemLocationFramePair = visibleItemLocationFramePairs.last {
// When scrolling down, only calculate a target content offset based on visible, already-sized
// cells. Otherwise, scrolling will be jumpy.
modelState.isItemHeightSettled(indexPath: $0.elementLocation.indexPath)
Expand Down
12 changes: 6 additions & 6 deletions Tests/LayoutStateTargetContentOffsetTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ final class LayoutStateTargetContentOffsetTests: XCTestCase {
contentInset: UIEdgeInsets(top: 50, left: 0, bottom: 30, right: 0),
scale: 1,
verticalLayoutDirection: .topToBottom)
let id = layoutState.modelState.idForItemModel(at: IndexPath(item: 5, section: 0))!
XCTAssert(layoutState.targetContentOffsetAnchor == .topItem(id: id, distanceFromTop: -160))
let id = layoutState.modelState.idForItemModel(at: IndexPath(item: 6, section: 0))!
XCTAssert(layoutState.targetContentOffsetAnchor == .topItem(id: id, distanceFromTop: -25))
}

func testAnchor_TopToBottom_ScrolledToBottom() throws {
Expand All @@ -61,8 +61,8 @@ final class LayoutStateTargetContentOffsetTests: XCTestCase {
contentInset: measurementLayoutState.contentInset,
scale: measurementLayoutState.scale,
verticalLayoutDirection: measurementLayoutState.verticalLayoutDirection)
let id = layoutState.modelState.idForItemModel(at: IndexPath(item: 7, section: 0))!
XCTAssert(layoutState.targetContentOffsetAnchor == .topItem(id: id, distanceFromTop: -80))
let id = layoutState.modelState.idForItemModel(at: IndexPath(item: 9, section: 0))!
XCTAssert(layoutState.targetContentOffsetAnchor == .topItem(id: id, distanceFromTop: 25))
}

// MARK: Bottom-to-Top Anchor Tests
Expand All @@ -87,8 +87,8 @@ final class LayoutStateTargetContentOffsetTests: XCTestCase {
contentInset: UIEdgeInsets(top: 50, left: 0, bottom: 30, right: 0),
scale: 1,
verticalLayoutDirection: .bottomToTop)
let id = layoutState.modelState.idForItemModel(at: IndexPath(item: 12, section: 0))!
XCTAssert(layoutState.targetContentOffsetAnchor == .bottomItem(id: id, distanceFromBottom: 190))
let id = layoutState.modelState.idForItemModel(at: IndexPath(item: 10, section: 0))!
XCTAssert(layoutState.targetContentOffsetAnchor == .bottomItem(id: id, distanceFromBottom: -10))
}

func testAnchor_BottomToTop_ScrolledToBottom() throws {
Expand Down