Skip to content

Commit

Permalink
Merge pull request #50 from DeluxeAlonso/feature/current-page-index-c…
Browse files Browse the repository at this point in the history
…hange

Adds currentPageIndexDidChange property for DLAutoSlidePageViewController
  • Loading branch information
DeluxeAlonso authored Jan 18, 2024
2 parents f0b96c3 + e3a95af commit 8de1839
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
9 changes: 8 additions & 1 deletion Sources/DLAutoSlidePageViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,14 @@ open class DLAutoSlidePageViewController: UIPageViewController {
private (set) public var pages: [UIViewController] = []
private (set) public var configuration: AutoSlideConfiguration = DefaultAutoSlideConfiguration.shared

private var currentPageIndex: Int = 0
private var currentPageIndex: Int = 0 {
didSet {
currentPageIndexDidChange?(oldValue, currentPageIndex)
}
}
/// Closure that takes as parameter the previous current page index and the new current page index.
public var currentPageIndexDidChange: ((Int, Int) -> Void)?

private var nextPageIndex: Int = 0
private var timer: Timer?

Expand Down
26 changes: 26 additions & 0 deletions Tests/DLAutoSlidePageViewControllerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ struct TestConfiguration: AutoSlideConfiguration {
var hidePageControl: Bool {
testHidesPageControl
}

var testTimeInterval: TimeInterval = 1.0
var timeInterval: TimeInterval {
testTimeInterval
}
}

final class DLAutoSlidePageViewControllerTests: XCTestCase {
Expand Down Expand Up @@ -54,4 +59,25 @@ final class DLAutoSlidePageViewControllerTests: XCTestCase {
XCTAssertEqual(presentationIndex, 0)
}

func testCurrentPageIndexDidChange() {
// Arrange
var testConfiguration = TestConfiguration()
testConfiguration.testTimeInterval = 0.0
let firstViewController = UIViewController()
let secondViewController = UIViewController()
let pages = [firstViewController, secondViewController]
let pageViewController = DLAutoSlidePageViewController(pages: pages, configuration: testConfiguration)
let expectation = XCTestExpectation(description: "currentPageIndexDidChange closure should be called.")
// Act
pageViewController.currentPageIndexDidChange = { previousIndex, newIndex in
XCTAssertEqual(previousIndex, 0)
XCTAssertEqual(newIndex, 1)
expectation.fulfill()
}
pageViewController.pageViewController(pageViewController, willTransitionTo: [secondViewController])
pageViewController.pageViewController(pageViewController, didFinishAnimating: true, previousViewControllers: [], transitionCompleted: true)
// Assert
wait(for: [expectation], timeout: 1)
}

}

0 comments on commit 8de1839

Please sign in to comment.