Skip to content

Commit

Permalink
Fix pageIncrement in infinite pager
Browse files Browse the repository at this point in the history
  • Loading branch information
fermoya committed Apr 8, 2022
1 parent 974a75b commit 43b291c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 37 deletions.
14 changes: 7 additions & 7 deletions Sources/SwiftUIPager/PagerContent+Helper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -288,12 +288,12 @@ extension Pager.PagerContent {
}

/// Returns true if the item is the first or last element in memory. Just used when `isInfinitePager` is set to `true` to hide elements being resorted.
// func isEdgePage(_ item: PageWrapper<Element, ID>) -> Bool {
// guard data.count >= 3 else { return false }
// guard !isDragging else { return false }
// guard let index = dataDisplayed.firstIndex(of: item) else { return false }
// let limit = max(pageIncrement, 1)
// return index < limit || index > dataDisplayed.count - 1 - limit
// }
func isEdgePage(_ item: PageWrapper<Element, ID>) -> Bool {
guard data.count >= 3 else { return false }
guard !isDragging else { return false }
guard let index = dataDisplayed.firstIndex(of: item) else { return false }
let limit = max(pageIncrement, 1)
return index < limit || index > dataDisplayed.count - 1 - limit
}

}
32 changes: 15 additions & 17 deletions Sources/SwiftUIPager/PagerContent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -178,21 +178,20 @@ extension Pager {
var body: some View {
let stack = HStack(spacing: interactiveItemSpacing) {
ForEach(dataDisplayed, id: id) { item in
// Group {
// if self.isInifinitePager && self.isEdgePage(item) {
// EmptyView()
// } else {
// self.content(item.element)
// }
// }
self.content(item.element)
.frame(size: self.pageSize)
.scaleEffect(self.scale(for: item))
.rotation3DEffect((self.isHorizontal ? .zero : Angle(degrees: -90)) - self.scrollDirectionAngle,
axis: (0, 0, 1))
.rotation3DEffect(self.angle(for: item),
axis: self.axis)
.opacity(opacity(for: item))
Group {
if self.isInifinitePager && self.isEdgePage(item) {
EmptyView()
} else {
self.content(item.element)
.frame(size: self.pageSize)
.scaleEffect(self.scale(for: item))
.rotation3DEffect((self.isHorizontal ? .zero : Angle(degrees: -90)) - self.scrollDirectionAngle,
axis: (0, 0, 1))
.rotation3DEffect(self.angle(for: item),
axis: self.axis)
.opacity(opacity(for: item))
}
}
}
.offset(x: self.xOffset, y : self.yOffset)
}
Expand Down Expand Up @@ -393,8 +392,7 @@ extension Pager.PagerContent {
}
withAnimation(animation) {
self.pagerModel.draggingOffset = 0
let sign = page > newPage ? -1 : +1
self.pagerModel.pageIncrement += sign * pageIncrement
self.pagerModel.pageIncrement = pageIncrement
self.pagerModel.draggingVelocity = 0
self.pagerModel.lastDraggingValue = nil
self.pagerModel.index = newPage
Expand Down
27 changes: 14 additions & 13 deletions Tests/SwiftUIPagerTests/PagerContent+Helper_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -338,21 +338,22 @@ final class PagerContent_Helper_Tests: XCTestCase {
XCTAssertLessThan(angle.degrees, .zero)
}

// func test_GivenPagerDragging_WhenIsEdgePage_ThenFalse() {
// let pager = givenPager.pageOffset(0.1)
// let isEdgePage = pager.isEdgePage(.init(batchId: 1, keyPath: \.self, element: 0))
// XCTAssertFalse(isEdgePage)
// }
//
// func test_GivenPager_WhenIsEdgePage_ThenTrue() {
// let pager = givenPager.loopPages()
// XCTAssertTrue(pager.isEdgePage(.init(batchId: 1, keyPath: \.self, element: 18)))
// XCTAssertTrue(pager.isEdgePage(.init(batchId: 1, keyPath: \.self, element: 2)))
// XCTAssertFalse(pager.isEdgePage(.init(batchId: 1, keyPath: \.self, element: 1)))
// }
func test_GivenPagerDragging_WhenIsEdgePage_ThenFalse() {
let pager = givenPager.pageOffset(0.1)
let isEdgePage = pager.isEdgePage(.init(batchId: 1, keyPath: \.self, element: 0))
XCTAssertFalse(isEdgePage)
}

func test_GivenPager_WhenIsEdgePage_ThenTrue() {
let pager = givenPager.loopPages()
XCTAssertTrue(pager.isEdgePage(.init(batchId: 1, keyPath: \.self, element: 18)))
XCTAssertTrue(pager.isEdgePage(.init(batchId: 1, keyPath: \.self, element: 2)))
XCTAssertFalse(pager.isEdgePage(.init(batchId: 1, keyPath: \.self, element: 1)))
}

static var allTests = [
// ("test_GivenPagerDragging_WhenIsEdgePage_ThenFalse", test_GivenPagerDragging_WhenIsEdgePage_ThenFalse),
("test_GivenPager_WhenIsEdgePage_ThenTrue", test_GivenPager_WhenIsEdgePage_ThenTrue),
("test_GivenPagerDragging_WhenIsEdgePage_ThenFalse", test_GivenPagerDragging_WhenIsEdgePage_ThenFalse),
("test_GivenPagerWithRotation3DDraggingForward_WhenAngle_ThenGreaterThanZero", test_GivenPagerWithRotation3DDraggingForward_WhenAngle_ThenGreaterThanZero),
("test_GivenPagerWithRotation3DDraggingBackward_WhenAngle_ThenLessThanZero", test_GivenPagerWithRotation3DDraggingBackward_WhenAngle_ThenLessThanZero),
("test_GivenPager_WhenAngle_ThenZero", test_GivenPager_WhenAngle_ThenZero),
Expand Down

0 comments on commit 43b291c

Please sign in to comment.