Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix didSelectItem delegate being called before actual selection #676

Merged
merged 1 commit into from
Apr 6, 2023
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: 4 additions & 0 deletions Parchment.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
3EA04A641C53BFF40054E5E0 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 3EA04A631C53BFF40054E5E0 /* Assets.xcassets */; };
3EA04A671C53BFF40054E5E0 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 3EA04A651C53BFF40054E5E0 /* LaunchScreen.storyboard */; };
3EFEFBF71C80B8820023C949 /* PagingIndicatorLayoutAttributesTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3EFEFBF61C80B8820023C949 /* PagingIndicatorLayoutAttributesTests.swift */; };
9509917129DED89E001BAA09 /* PagingViewControllerDelegateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9509917029DED89E001BAA09 /* PagingViewControllerDelegateTests.swift */; };
950ABE422437BD4D00CAD458 /* PagingNavigationOrientation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 950ABE412437BD4D00CAD458 /* PagingNavigationOrientation.swift */; };
950ABE452438975300CAD458 /* PageViewExampleViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 950ABE442438975300CAD458 /* PageViewExampleViewController.swift */; };
951E163720A21D3A0055E9D4 /* PagingViewControllerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 954842601F4251F90072038C /* PagingViewControllerTests.swift */; };
Expand Down Expand Up @@ -256,6 +257,7 @@
3EA04A681C53BFF40054E5E0 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
3EC0184C1C95F993005421AD /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; name = Info.plist; path = Resources/Info.plist; sourceTree = "<group>"; };
3EFEFBF61C80B8820023C949 /* PagingIndicatorLayoutAttributesTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PagingIndicatorLayoutAttributesTests.swift; sourceTree = "<group>"; };
9509917029DED89E001BAA09 /* PagingViewControllerDelegateTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PagingViewControllerDelegateTests.swift; sourceTree = "<group>"; };
950ABE412437BD4D00CAD458 /* PagingNavigationOrientation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PagingNavigationOrientation.swift; sourceTree = "<group>"; };
950ABE442438975300CAD458 /* PageViewExampleViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PageViewExampleViewController.swift; sourceTree = "<group>"; };
952D802E1E37CC09003DCB18 /* PagingTransition.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PagingTransition.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -491,6 +493,7 @@
3EFEFBF61C80B8820023C949 /* PagingIndicatorLayoutAttributesTests.swift */,
3E5E93E21CE7D899000762A1 /* PagingStateTests.swift */,
954842601F4251F90072038C /* PagingViewControllerTests.swift */,
9509917029DED89E001BAA09 /* PagingViewControllerDelegateTests.swift */,
955D02432434E56900416E71 /* PagingViewTests.swift */,
9566F55E257982B500CCA8FC /* UIColorInterpolationTests.swift */,
95D78FE5228728FD00E6EE7C /* Mocks */,
Expand Down Expand Up @@ -1029,6 +1032,7 @@
95D78FDB2287138F00E6EE7C /* MockCollectionView.swift in Sources */,
9575BEB72463490B002403F6 /* PagingDistanceCenteredTests.swift in Sources */,
9566F55F257982B500CCA8FC /* UIColorInterpolationTests.swift in Sources */,
9509917129DED89E001BAA09 /* PagingViewControllerDelegateTests.swift in Sources */,
954E7DEE1F48AE6E00342ECF /* Item.swift in Sources */,
954842631F4252070072038C /* PagingDiffTests.swift in Sources */,
95D78FDD228713B800E6EE7C /* MockCollectionViewLayout.swift in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion Parchment/Classes/PagingViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -611,8 +611,8 @@ open class PagingViewController:

open func collectionView(_: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let pagingItem = pagingController.visibleItems.pagingItem(for: indexPath)
delegate?.pagingViewController(self, didSelectItem: pagingItem)
pagingController.select(indexPath: indexPath, animated: true)
delegate?.pagingViewController(self, didSelectItem: pagingItem)
}

open func collectionView(_: UICollectionView, targetContentOffsetForProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
Expand Down
82 changes: 82 additions & 0 deletions ParchmentTests/PagingViewControllerDelegateTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import Foundation
@testable import Parchment
import UIKit
import XCTest

final class PagingViewControllerDelegateTests: XCTestCase {
func testDidSelectItem() {
let viewController0 = UIViewController()
let viewController1 = UIViewController()
let pagingViewController = PagingViewController(viewControllers: [
viewController0,
viewController1
])

let delegate = Delegate()
let window = UIWindow(frame: UIScreen.main.bounds)
window.rootViewController = pagingViewController
window.makeKeyAndVisible()
pagingViewController.view.layoutIfNeeded()
pagingViewController.delegate = delegate

let expectation = XCTestExpectation()

delegate.didSelectItem = { item in
let upcomingItem = pagingViewController.state.upcomingPagingItem as? PagingIndexItem
let item = item as! PagingIndexItem
XCTAssertEqual(item.index, 1)
XCTAssertEqual(upcomingItem, item)
expectation.fulfill()
}

let indexPath = IndexPath(item: 1, section: 0)
pagingViewController.collectionView.delegate?.collectionView?(
pagingViewController.collectionView,
didSelectItemAt: indexPath
)

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

func testDidScrollToItem() {
let viewController0 = UIViewController()
let viewController1 = UIViewController()
let pagingViewController = PagingViewController(viewControllers: [
viewController0,
viewController1
])

let delegate = Delegate()
let window = UIWindow(frame: UIScreen.main.bounds)
window.rootViewController = pagingViewController
window.makeKeyAndVisible()
pagingViewController.view.layoutIfNeeded()
pagingViewController.delegate = delegate

let expectation = XCTestExpectation()

delegate.didSelectItem = { item in
let upcomingItem = pagingViewController.state.upcomingPagingItem as? PagingIndexItem
let item = item as! PagingIndexItem
XCTAssertEqual(item.index, 1)
XCTAssertEqual(upcomingItem, item)
expectation.fulfill()
}

let indexPath = IndexPath(item: 1, section: 0)
pagingViewController.collectionView.delegate?.collectionView?(
pagingViewController.collectionView,
didSelectItemAt: indexPath
)

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

private final class Delegate: PagingViewControllerDelegate {
var didSelectItem: ((PagingItem) -> Void)?

func pagingViewController(_ pagingViewController: PagingViewController, didSelectItem pagingItem: PagingItem) {
didSelectItem?(pagingItem)
}
}