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
4 changes: 2 additions & 2 deletions MagazineLayout/LayoutCore/ItemModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ struct ItemModel {
// MARK: Lifecycle

init(sizeMode: MagazineLayoutItemSizeMode, height: CGFloat) {
id = NSUUID().uuidString
id = UUID()
self.sizeMode = sizeMode
originInSection = .zero
size = CGSize(width: 0, height: height)
}

// MARK: Internal

let id: String
let id: UUID

var sizeMode: MagazineLayoutItemSizeMode
var originInSection: CGPoint
Expand Down
8 changes: 4 additions & 4 deletions MagazineLayout/LayoutCore/ModelState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ final class ModelState {
return sectionModels[sectionIndex].numberOfItems
}

func idForItemModel(at indexPath: IndexPath, _ batchUpdateStage: BatchUpdateStage) -> String? {
func idForItemModel(at indexPath: IndexPath, _ batchUpdateStage: BatchUpdateStage) -> UUID? {
let sectionModels = self.sectionModels(for: batchUpdateStage)

guard
Expand All @@ -67,7 +67,7 @@ final class ModelState {
}

func indexPathForItemModel(
withID id: String,
withID id: UUID,
_ batchUpdateStage: BatchUpdateStage)
-> IndexPath?
{
Expand All @@ -83,7 +83,7 @@ final class ModelState {
return nil
}

func idForSectionModel(atIndex index: Int, _ batchUpdateStage: BatchUpdateStage) -> String? {
func idForSectionModel(atIndex index: Int, _ batchUpdateStage: BatchUpdateStage) -> UUID? {
let sectionModels = self.sectionModels(for: batchUpdateStage)

guard index < sectionModels.count else {
Expand All @@ -94,7 +94,7 @@ final class ModelState {
return sectionModels[index].id
}

func indexForSectionModel(withID id: String, _ batchUpdateStage: BatchUpdateStage) -> Int? {
func indexForSectionModel(withID id: UUID, _ batchUpdateStage: BatchUpdateStage) -> Int? {
let sectionModels = self.sectionModels(for: batchUpdateStage)

for sectionIndex in 0..<sectionModels.count {
Expand Down
8 changes: 4 additions & 4 deletions MagazineLayout/LayoutCore/SectionModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct SectionModel {
backgroundModel: BackgroundModel?,
metrics: MagazineLayoutSectionMetrics)
{
id = NSUUID().uuidString
id = UUID()
self.itemModels = itemModels
self.headerModel = headerModel
self.footerModel = footerModel
Expand All @@ -43,7 +43,7 @@ struct SectionModel {

// MARK: Internal

let id: String
let id: UUID

private(set) var headerModel: HeaderModel?
private(set) var footerModel: FooterModel?
Expand All @@ -55,11 +55,11 @@ struct SectionModel {
return itemModels.count
}

func idForItemModel(atIndex index: Int) -> String {
func idForItemModel(atIndex index: Int) -> UUID {
return itemModels[index].id
}

func indexForItemModel(withID id: String) -> Int? {
func indexForItemModel(withID id: UUID) -> Int? {
return itemModels.firstIndex { $0.id == id }
}

Expand Down
10 changes: 5 additions & 5 deletions MagazineLayout/LayoutCore/Types/TargetContentOffsetAnchor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import UIKit
enum TargetContentOffsetAnchor: Equatable {
case top
case bottom
case topItem(id: String, distanceFromTop: CGFloat)
case bottomItem(id: String, distanceFromBottom: CGFloat)
case topItem(id: UUID, distanceFromTop: CGFloat)
case bottomItem(id: UUID, distanceFromBottom: CGFloat)

static func targetContentOffsetAnchor(
verticalLayoutDirection: MagazineLayoutVerticalLayoutDirection,
Expand All @@ -32,8 +32,8 @@ enum TargetContentOffsetAnchor: Equatable {
bounds: CGRect,
contentHeight: CGFloat,
scale: CGFloat,
firstVisibleItemID: String,
lastVisibleItemID: String,
firstVisibleItemID: UUID,
lastVisibleItemID: UUID,
firstVisibleItemFrame: CGRect,
lastVisibleItemFrame: CGRect)
-> Self
Expand Down Expand Up @@ -89,7 +89,7 @@ enum TargetContentOffsetAnchor: Equatable {
bottomInset: CGFloat,
bounds: CGRect,
contentHeight: CGFloat,
indexPathForItemID: (_ id: String) -> IndexPath?,
indexPathForItemID: (_ id: UUID) -> IndexPath?,
frameForItemAtIndexPath: (_ indexPath: IndexPath) -> CGRect)
-> CGFloat
{
Expand Down
52 changes: 28 additions & 24 deletions Tests/TargetContentOffsetAnchorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,41 +29,43 @@ final class TargetContentOffsetAnchorTests: XCTestCase {
bounds: CGRect(x: 0, y: -50, width: 300, height: 400),
contentHeight: 2000,
scale: 1,
firstVisibleItemID: "0",
lastVisibleItemID: "4",
firstVisibleItemID: UUID(),
lastVisibleItemID: UUID(),
firstVisibleItemFrame: CGRect(x: 0, y: 0, width: 300, height: 20),
lastVisibleItemFrame: CGRect(x: 0, y: 290, width: 300, height: 20))
XCTAssert(anchor == .top)
}

func testAnchor_TopToBottom_ScrolledToMiddle() throws {
let topID = UUID()
let anchor = TargetContentOffsetAnchor.targetContentOffsetAnchor(
verticalLayoutDirection: .topToBottom,
topInset: 50,
bottomInset: 30,
bounds: CGRect(x: 0, y: 500, width: 300, height: 400),
contentHeight: 2000,
scale: 1,
firstVisibleItemID: "2",
lastVisibleItemID: "6",
firstVisibleItemID: topID,
lastVisibleItemID: UUID(),
firstVisibleItemFrame: CGRect(x: 0, y: 560, width: 300, height: 20),
lastVisibleItemFrame: CGRect(x: 0, y: 800, width: 300, height: 20))
XCTAssert(anchor == .topItem(id: "2", distanceFromTop: 10))
XCTAssert(anchor == .topItem(id: topID, distanceFromTop: 10))
}

func testAnchor_TopToBottom_ScrolledToBottom() throws {
let topID = UUID()
let anchor = TargetContentOffsetAnchor.targetContentOffsetAnchor(
verticalLayoutDirection: .topToBottom,
topInset: 50,
bottomInset: 30,
bounds: CGRect(x: 0, y: 1630, width: 300, height: 400),
contentHeight: 2000,
scale: 1,
firstVisibleItemID: "6",
lastVisibleItemID: "10",
firstVisibleItemID: topID,
lastVisibleItemID: UUID(),
firstVisibleItemFrame: CGRect(x: 0, y: 1700, width: 300, height: 20),
lastVisibleItemFrame: CGRect(x: 0, y: 1950, width: 300, height: 20))
XCTAssert(anchor == .topItem(id: "6", distanceFromTop: 20))
XCTAssert(anchor == .topItem(id: topID, distanceFromTop: 20))
}

func testAnchor_TopToBottom_SmallContentHeight() throws {
Expand All @@ -74,8 +76,8 @@ final class TargetContentOffsetAnchorTests: XCTestCase {
bounds: CGRect(x: 0, y: -50, width: 300, height: 400),
contentHeight: 50,
scale: 1,
firstVisibleItemID: "0",
lastVisibleItemID: "1",
firstVisibleItemID: UUID(),
lastVisibleItemID: UUID(),
firstVisibleItemFrame: CGRect(x: 0, y: 0, width: 300, height: 20),
lastVisibleItemFrame: CGRect(x: 0, y: 30, width: 300, height: 20))
XCTAssert(anchor == .top)
Expand All @@ -84,33 +86,35 @@ final class TargetContentOffsetAnchorTests: XCTestCase {
// MARK: Bottom-to-Top Anchor Tests

func testAnchor_BottomToTop_ScrolledToTop() throws {
let bottomID = UUID()
let anchor = TargetContentOffsetAnchor.targetContentOffsetAnchor(
verticalLayoutDirection: .bottomToTop,
topInset: 50,
bottomInset: 30,
bounds: CGRect(x: 0, y: -50, width: 300, height: 400),
contentHeight: 2000,
scale: 1,
firstVisibleItemID: "0",
lastVisibleItemID: "4",
firstVisibleItemID: UUID(),
lastVisibleItemID: bottomID,
firstVisibleItemFrame: CGRect(x: 0, y: 0, width: 300, height: 20),
lastVisibleItemFrame: CGRect(x: 0, y: 290, width: 300, height: 20))
XCTAssert(anchor == .bottomItem(id: "4", distanceFromBottom: -10))
XCTAssert(anchor == .bottomItem(id: bottomID, distanceFromBottom: -10))
}

func testAnchor_BottomToTop_ScrolledToMiddle() throws {
let bottomID = UUID()
let anchor = TargetContentOffsetAnchor.targetContentOffsetAnchor(
verticalLayoutDirection: .bottomToTop,
topInset: 50,
bottomInset: 30,
bounds: CGRect(x: 0, y: 500, width: 300, height: 400),
contentHeight: 2000,
scale: 1,
firstVisibleItemID: "2",
lastVisibleItemID: "6",
firstVisibleItemID: UUID(),
lastVisibleItemID: bottomID,
firstVisibleItemFrame: CGRect(x: 0, y: 560, width: 300, height: 20),
lastVisibleItemFrame: CGRect(x: 0, y: 800, width: 300, height: 20))
XCTAssert(anchor == .bottomItem(id: "6", distanceFromBottom: -50))
XCTAssert(anchor == .bottomItem(id: bottomID, distanceFromBottom: -50))
}

func testAnchor_BottomToTop_ScrolledToBottom() throws {
Expand All @@ -121,8 +125,8 @@ final class TargetContentOffsetAnchorTests: XCTestCase {
bounds: CGRect(x: 0, y: 1630, width: 300, height: 400),
contentHeight: 2000,
scale: 1,
firstVisibleItemID: "6",
lastVisibleItemID: "10",
firstVisibleItemID: UUID(),
lastVisibleItemID: UUID(),
firstVisibleItemFrame: CGRect(x: 0, y: 1700, width: 300, height: 20),
lastVisibleItemFrame: CGRect(x: 0, y: 1950, width: 300, height: 20))
XCTAssert(anchor == .bottom)
Expand All @@ -136,8 +140,8 @@ final class TargetContentOffsetAnchorTests: XCTestCase {
bounds: CGRect(x: 0, y: -50, width: 300, height: 400),
contentHeight: 50,
scale: 1,
firstVisibleItemID: "0",
lastVisibleItemID: "1",
firstVisibleItemID: UUID(),
lastVisibleItemID: UUID(),
firstVisibleItemFrame: CGRect(x: 0, y: 0, width: 300, height: 20),
lastVisibleItemFrame: CGRect(x: 0, y: 30, width: 300, height: 20))
XCTAssert(anchor == .bottom)
Expand All @@ -158,7 +162,7 @@ final class TargetContentOffsetAnchorTests: XCTestCase {
}

func testOffset_TopToBottom_ScrolledToMiddle() {
let anchor = TargetContentOffsetAnchor.topItem(id: "2", distanceFromTop: 10)
let anchor = TargetContentOffsetAnchor.topItem(id: UUID(), distanceFromTop: 10)
let offset = anchor.yOffset(
topInset: 50,
bottomInset: 30,
Expand All @@ -170,7 +174,7 @@ final class TargetContentOffsetAnchorTests: XCTestCase {
}

func testOffset_TopToBottom_ScrolledToBottom() {
let anchor = TargetContentOffsetAnchor.topItem(id: "2", distanceFromTop: 10)
let anchor = TargetContentOffsetAnchor.topItem(id: UUID(), distanceFromTop: 10)
let offset = anchor.yOffset(
topInset: 50,
bottomInset: 30,
Expand All @@ -184,7 +188,7 @@ final class TargetContentOffsetAnchorTests: XCTestCase {
// MARK: Bottom-to-Top Target Content Offset Tests

func testOffset_BottomToTop_ScrolledToTop() {
let anchor = TargetContentOffsetAnchor.bottomItem(id: "4", distanceFromBottom: -10)
let anchor = TargetContentOffsetAnchor.bottomItem(id: UUID(), distanceFromBottom: -10)
let offset = anchor.yOffset(
topInset: 50,
bottomInset: 30,
Expand All @@ -196,7 +200,7 @@ final class TargetContentOffsetAnchorTests: XCTestCase {
}

func testOffset_BottomToTop_ScrolledToMiddle() {
let anchor = TargetContentOffsetAnchor.bottomItem(id: "6", distanceFromBottom: -50)
let anchor = TargetContentOffsetAnchor.bottomItem(id: UUID(), distanceFromBottom: -50)
let offset = anchor.yOffset(
topInset: 50,
bottomInset: 30,
Expand Down