Skip to content

Commit

Permalink
Merge pull request #128 from kazuhiro4949/feature/willbegin_delegate
Browse files Browse the repository at this point in the history
call delegate only when scrolling another page
  • Loading branch information
kazuhiro4949 authored Jan 5, 2021
2 parents 4ca9e09 + 9a9de75 commit 1ccc9fa
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 5 deletions.
19 changes: 16 additions & 3 deletions Sources/PagingContentViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public protocol PagingContentViewControllerDelegate: class {


/// Tells the delegate when the view controller is trying to start paging the content.
/// If it is the same page as the current, this delegate is not called.
///
/// - Parameters:
/// - viewController: The view controller object in which the scrolling occurred.
Expand All @@ -59,6 +60,7 @@ public protocol PagingContentViewControllerDelegate: class {
func contentViewController(viewController: PagingContentViewController, willBeginPagingAt index: Int, animated: Bool)

/// Tells the delegate when the view controller is trying to finish paging the content.
/// If it is the same page as the current, this delegate is not called.
///
/// - Parameters:
/// - viewController: The view controller object in which the scrolling occurred.
Expand All @@ -67,6 +69,7 @@ public protocol PagingContentViewControllerDelegate: class {
func contentViewController(viewController: PagingContentViewController, willFinishPagingAt index: Int, animated: Bool)

/// Tells the delegate when the view controller was finished to paging the content.
/// If it is the same page as the current, this delegate is not called.
///
/// - Parameters:
/// - viewController: The view controller object in which the scrolling occurred.
Expand Down Expand Up @@ -204,7 +207,11 @@ public class PagingContentViewController: UIViewController {


private func scroll(to page: Int, needsCallAppearance: Bool, animated: Bool, completion: ((Bool) -> Void)? = nil) {
delegate?.contentViewController(viewController: self, willBeginPagingAt: leftSidePageIndex, animated: animated)
let isScrollingToAnotherPage = leftSidePageIndex != page

if isScrollingToAnotherPage {
delegate?.contentViewController(viewController: self, willBeginPagingAt: leftSidePageIndex, animated: animated)
}

if needsCallAppearance {
appearanceHandler.beginDragging(at: leftSidePageIndex)
Expand All @@ -213,16 +220,22 @@ public class PagingContentViewController: UIViewController {
loadPagesIfNeeded(page: page)
leftSidePageIndex = page

delegate?.contentViewController(viewController: self, willFinishPagingAt: leftSidePageIndex, animated: animated)
if isScrollingToAnotherPage {
delegate?.contentViewController(viewController: self, willFinishPagingAt: leftSidePageIndex, animated: animated)
}

move(to: page, animated: animated) { [weak self] (finished) in
guard let _self = self, finished else { return }

if needsCallAppearance {
_self.appearanceHandler.stopScrolling(at: _self.leftSidePageIndex)
}

if isScrollingToAnotherPage {
_self.delegate?.contentViewController(viewController: _self, didFinishPagingAt: _self.leftSidePageIndex, animated: animated)
}

completion?(finished)
_self.delegate?.contentViewController(viewController: _self, didFinishPagingAt: _self.leftSidePageIndex, animated: animated)
}
}

Expand Down
54 changes: 52 additions & 2 deletions Tests/PagingContentViewControllerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -349,18 +349,63 @@ extension PagingContentViewControllerTests {
UIApplication.shared.keyWindow?.rootViewController = self.pagingContentViewController

XCTAssertNil(delegate.willBeginPagingAt_args)
XCTAssertNil(delegate.willFinishPagingAt_args)
XCTAssertNil(delegate.didFinishPagingAt_args)
XCTAssertNil(self.contentAppearanceHandler.beginDragging_args)
XCTAssertNil(self.contentAppearanceHandler.stopScrolling_args)

XCUIDevice.shared.orientation = .landscapeLeft

XCTAssertNotNil(delegate.willBeginPagingAt_args)
XCTAssertNil(delegate.willBeginPagingAt_args)
XCTAssertNil(delegate.willFinishPagingAt_args)
XCTAssertNil(delegate.didFinishPagingAt_args)
XCTAssertNil(self.contentAppearanceHandler.beginDragging_args)
XCTAssertNil(self.contentAppearanceHandler.stopScrolling_args)
expectation.fulfill()

wait(for: [expectation], timeout: 1)
}

func test_ScrollingTheSamePage_callingDelegate() {
let expectation = XCTestExpectation(description: "finish reloadData")
let dataSource = PagingContentVcDataSourceSpy()
let delegate = PagingContentVcDelegateSpy()
pagingContentViewController?.dataSource = dataSource
pagingContentViewController?.delegate = delegate
pagingContentViewController?.loadViewIfNeeded()
pagingContentViewController?.reloadData(with: 3) { [unowned self] in
self.pagingContentViewController?.scroll(to: 3, animated: false)

XCTAssertEqual(delegate.willBeginPagingAt_callCount, 0)
XCTAssertEqual(delegate.willFinishPagingAt_callCount, 0)
XCTAssertEqual(delegate.didFinishPagingAt_callCount, 0)

expectation.fulfill()
}

wait(for: [expectation], timeout: 0.2)
}

func test_ScrollingAnotherPage_callingDelegate() {
let expectation = XCTestExpectation(description: "finish reloadData")
let dataSource = PagingContentVcDataSourceSpy()
let delegate = PagingContentVcDelegateSpy()
pagingContentViewController?.dataSource = dataSource
pagingContentViewController?.delegate = delegate
pagingContentViewController?.loadViewIfNeeded()
pagingContentViewController?.reloadData(with: 3) { [unowned self] in
self.pagingContentViewController?.scroll(to: 4, animated: false) { _ in

XCTAssertEqual(delegate.willBeginPagingAt_callCount, 1)
XCTAssertEqual(delegate.willFinishPagingAt_callCount, 1)
XCTAssertEqual(delegate.didFinishPagingAt_callCount, 1)

expectation.fulfill()
}
}

wait(for: [expectation], timeout: 0.2)
}
}


Expand Down Expand Up @@ -416,19 +461,24 @@ class PagingContentVcDelegateSpy: NSObject, PagingContentViewControllerDelegate
didEndManualScrollOn_args = (viewController, index)
}

var willBeginPagingAt_callCount = 0
var willBeginPagingAt_args: (PagingContentViewController, Int, Bool)?
func contentViewController(viewController: PagingContentViewController, willBeginPagingAt index: Int, animated: Bool) {
willBeginPagingAt_args = (viewController, index, animated)
willBeginPagingAt_callCount += 1
}

var willFinishPagingAt_callCount = 0
var willFinishPagingAt_args: (PagingContentViewController, Int, Bool)?
func contentViewController(viewController: PagingContentViewController, willFinishPagingAt index: Int, animated: Bool) {
willFinishPagingAt_args = (viewController, index, animated)
willFinishPagingAt_callCount += 1
}

var didFinishPagingAt_callCount = 0
var didFinishPagingAt_args: (PagingContentViewController, Int, Bool)?
func contentViewController(viewController: PagingContentViewController, didFinishPagingAt index: Int, animated: Bool) {
didFinishPagingAt_args = (viewController, index, animated)
didFinishPagingAt_callCount += 1
}
}

Expand Down

0 comments on commit 1ccc9fa

Please sign in to comment.