diff --git a/IBPCollectionViewCompositionalLayout.xcodeproj/project.pbxproj b/IBPCollectionViewCompositionalLayout.xcodeproj/project.pbxproj index e448941..a59e913 100644 --- a/IBPCollectionViewCompositionalLayout.xcodeproj/project.pbxproj +++ b/IBPCollectionViewCompositionalLayout.xcodeproj/project.pbxproj @@ -67,11 +67,10 @@ 147537BF230A190F00337C1B /* IBPNSCollectionLayoutSpacing_Private.h in Headers */ = {isa = PBXBuildFile; fileRef = 147537BD230A190F00337C1B /* IBPNSCollectionLayoutSpacing_Private.h */; settings = {ATTRIBUTES = (Private, ); }; }; 147537C7230A6A1300337C1B /* IBPNSCollectionLayoutAnchor_Private.h in Headers */ = {isa = PBXBuildFile; fileRef = 147537C5230A6A1300337C1B /* IBPNSCollectionLayoutAnchor_Private.h */; settings = {ATTRIBUTES = (Private, ); }; }; 1477B43023B906DE00373474 /* DiffableDataSources.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1477B42F23B906DE00373474 /* DiffableDataSources.framework */; }; + 1477B43823B9182000373474 /* GridViewTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1477B43723B9182000373474 /* GridViewTests.swift */; }; 14B9585323B8DE3A00DF3B7F /* ListViewTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 14B9585223B8DE3A00DF3B7F /* ListViewTests.swift */; }; 14B9585523B8E07B00DF3B7F /* IBPCollectionViewCompositionalLayoutInteroperability.swift in Sources */ = {isa = PBXBuildFile; fileRef = 14B9585423B8E07B00DF3B7F /* IBPCollectionViewCompositionalLayoutInteroperability.swift */; }; 14B9585E23B8E21000DF3B7F /* DiffableDataSourceInteroperability.swift in Sources */ = {isa = PBXBuildFile; fileRef = 14B9585623B8E12400DF3B7F /* DiffableDataSourceInteroperability.swift */; }; - 14B9586023B8E27F00DF3B7F /* UIKitInteroperability.swift in Sources */ = {isa = PBXBuildFile; fileRef = 14B9585F23B8E27F00DF3B7F /* UIKitInteroperability.swift */; }; - 14B9586423B8E35C00DF3B7F /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 14B9586323B8E35C00DF3B7F /* Assets.xcassets */; }; 14B9586C23B8E60700DF3B7F /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 14B9586B23B8E60700DF3B7F /* AppDelegate.swift */; }; 14B9587523B8E60900DF3B7F /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 14B9587423B8E60900DF3B7F /* Assets.xcassets */; }; 14B9587823B8E60900DF3B7F /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 14B9587623B8E60900DF3B7F /* LaunchScreen.storyboard */; }; @@ -166,11 +165,10 @@ 1477B42D23B906B800373474 /* DiffableDataSources.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = DiffableDataSources.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 1477B42F23B906DE00373474 /* DiffableDataSources.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = DiffableDataSources.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 1477B43123B906DE00373474 /* DifferenceKit.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = DifferenceKit.framework; sourceTree = BUILT_PRODUCTS_DIR; }; + 1477B43723B9182000373474 /* GridViewTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GridViewTests.swift; sourceTree = ""; }; 14B9585223B8DE3A00DF3B7F /* ListViewTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ListViewTests.swift; sourceTree = ""; }; 14B9585423B8E07B00DF3B7F /* IBPCollectionViewCompositionalLayoutInteroperability.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IBPCollectionViewCompositionalLayoutInteroperability.swift; sourceTree = ""; }; 14B9585623B8E12400DF3B7F /* DiffableDataSourceInteroperability.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DiffableDataSourceInteroperability.swift; sourceTree = ""; }; - 14B9585F23B8E27F00DF3B7F /* UIKitInteroperability.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIKitInteroperability.swift; sourceTree = ""; }; - 14B9586323B8E35C00DF3B7F /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 14B9586923B8E60700DF3B7F /* HostApp.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = HostApp.app; sourceTree = BUILT_PRODUCTS_DIR; }; 14B9586B23B8E60700DF3B7F /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 14B9587423B8E60900DF3B7F /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; @@ -360,10 +358,9 @@ isa = PBXGroup; children = ( 14B9585223B8DE3A00DF3B7F /* ListViewTests.swift */, + 1477B43723B9182000373474 /* GridViewTests.swift */, 14B9585423B8E07B00DF3B7F /* IBPCollectionViewCompositionalLayoutInteroperability.swift */, 14B9585623B8E12400DF3B7F /* DiffableDataSourceInteroperability.swift */, - 14B9585F23B8E27F00DF3B7F /* UIKitInteroperability.swift */, - 14B9586323B8E35C00DF3B7F /* Assets.xcassets */, ); path = IBPCollectionViewCompositionalLayoutTests; sourceTree = ""; @@ -540,7 +537,6 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( - 14B9586423B8E35C00DF3B7F /* Assets.xcassets in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -589,9 +585,9 @@ buildActionMask = 2147483647; files = ( 14B9585523B8E07B00DF3B7F /* IBPCollectionViewCompositionalLayoutInteroperability.swift in Sources */, - 14B9586023B8E27F00DF3B7F /* UIKitInteroperability.swift in Sources */, 14B9585323B8DE3A00DF3B7F /* ListViewTests.swift in Sources */, 14B9585E23B8E21000DF3B7F /* DiffableDataSourceInteroperability.swift in Sources */, + 1477B43823B9182000373474 /* GridViewTests.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Tests/IBPCollectionViewCompositionalLayoutTests/Assets.xcassets/Contents.json b/Tests/IBPCollectionViewCompositionalLayoutTests/Assets.xcassets/Contents.json deleted file mode 100644 index da4a164..0000000 --- a/Tests/IBPCollectionViewCompositionalLayoutTests/Assets.xcassets/Contents.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "info" : { - "version" : 1, - "author" : "xcode" - } -} \ No newline at end of file diff --git a/Tests/IBPCollectionViewCompositionalLayoutTests/Assets.xcassets/chevron.left.imageset/Contents.json b/Tests/IBPCollectionViewCompositionalLayoutTests/Assets.xcassets/chevron.left.imageset/Contents.json deleted file mode 100644 index 485679c..0000000 --- a/Tests/IBPCollectionViewCompositionalLayoutTests/Assets.xcassets/chevron.left.imageset/Contents.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "images" : [ - { - "idiom" : "universal", - "filename" : "chevron.left.pdf" - } - ], - "info" : { - "version" : 1, - "author" : "xcode" - }, - "properties" : { - "preserves-vector-representation" : true - } -} \ No newline at end of file diff --git a/Tests/IBPCollectionViewCompositionalLayoutTests/Assets.xcassets/chevron.left.imageset/chevron.left.pdf b/Tests/IBPCollectionViewCompositionalLayoutTests/Assets.xcassets/chevron.left.imageset/chevron.left.pdf deleted file mode 100644 index 09887dc..0000000 Binary files a/Tests/IBPCollectionViewCompositionalLayoutTests/Assets.xcassets/chevron.left.imageset/chevron.left.pdf and /dev/null differ diff --git a/Tests/IBPCollectionViewCompositionalLayoutTests/Assets.xcassets/chevron.right.imageset/Contents.json b/Tests/IBPCollectionViewCompositionalLayoutTests/Assets.xcassets/chevron.right.imageset/Contents.json deleted file mode 100644 index c36b684..0000000 --- a/Tests/IBPCollectionViewCompositionalLayoutTests/Assets.xcassets/chevron.right.imageset/Contents.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "images" : [ - { - "idiom" : "universal", - "filename" : "chevron.right.pdf", - "scale" : "1x" - }, - { - "idiom" : "universal", - "scale" : "2x" - }, - { - "idiom" : "universal", - "scale" : "3x" - } - ], - "info" : { - "version" : 1, - "author" : "xcode" - } -} \ No newline at end of file diff --git a/Tests/IBPCollectionViewCompositionalLayoutTests/Assets.xcassets/chevron.right.imageset/chevron.right.pdf b/Tests/IBPCollectionViewCompositionalLayoutTests/Assets.xcassets/chevron.right.imageset/chevron.right.pdf deleted file mode 100644 index 572d5f3..0000000 Binary files a/Tests/IBPCollectionViewCompositionalLayoutTests/Assets.xcassets/chevron.right.imageset/chevron.right.pdf and /dev/null differ diff --git a/Tests/IBPCollectionViewCompositionalLayoutTests/GridViewTests.swift b/Tests/IBPCollectionViewCompositionalLayoutTests/GridViewTests.swift new file mode 100644 index 0000000..1047a62 --- /dev/null +++ b/Tests/IBPCollectionViewCompositionalLayoutTests/GridViewTests.swift @@ -0,0 +1,189 @@ +import XCTest +import IBPCollectionViewCompositionalLayout + +class GridViewTests: XCTestCase { + func testViewController() { + let viewController = GridViewController() + + let window = UIWindow(frame: UIScreen.main.bounds) + window.rootViewController = viewController + window.makeKeyAndVisible() + + RunLoop.main.run(until: Date(timeIntervalSinceNow: 0)) + + let collectionView = viewController.collectionView! + let fractionalWidth: CGFloat = 0.2 + let numberOfColumns: CGFloat = floor(1 / fractionalWidth) + let width: CGFloat = window.bounds.width * fractionalWidth + let height = width + let numberOfItems = collectionView.numberOfItems(inSection: 0) + + if #available(iOS 11.0, *) { + let safeAreaInsets = viewController.view.safeAreaInsets + + XCTAssertEqual(collectionView.adjustedContentInset, safeAreaInsets) + XCTAssertEqual(collectionView.contentOffset, CGPoint(x: -safeAreaInsets.left, y: -safeAreaInsets.top)) + XCTAssertEqual(collectionView.contentSize, CGSize(width: window.bounds.width, height: ceil(CGFloat(numberOfItems) / numberOfColumns) * window.bounds.width * fractionalWidth)) + + for case (let index, let cell as UICollectionViewCell) in collectionView.subviews.enumerated() { + XCTAssertEqual(cell.frame, CGRect(x: width * (CGFloat(index).truncatingRemainder(dividingBy: numberOfColumns)), y: height * floor(CGFloat(index) / numberOfColumns), width: width, height: height)) + } + } else { + + } + } + + func testNavigationController() { + let viewController = GridViewController() + + let window = UIWindow(frame: UIScreen.main.bounds) + let navigationController = UINavigationController(rootViewController: viewController) + window.rootViewController = navigationController + window.makeKeyAndVisible() + + RunLoop.main.run(until: Date(timeIntervalSinceNow: 0)) + + let collectionView = viewController.collectionView! + let fractionalWidth: CGFloat = 0.2 + let numberOfColumns: CGFloat = floor(1 / fractionalWidth) + let width: CGFloat = window.bounds.width * fractionalWidth + let height = width + let numberOfItems = collectionView.numberOfItems(inSection: 0) + + if #available(iOS 11.0, *) { + let safeAreaInsets = viewController.view.safeAreaInsets + + XCTAssertEqual(collectionView.adjustedContentInset, safeAreaInsets) + XCTAssertEqual(collectionView.contentOffset, CGPoint(x: -safeAreaInsets.left, y: -safeAreaInsets.top)) + XCTAssertEqual(collectionView.contentSize, CGSize(width: window.bounds.width, height: ceil(CGFloat(numberOfItems) / numberOfColumns) * window.bounds.width * fractionalWidth)) + + for case (let index, let cell as UICollectionViewCell) in collectionView.subviews.enumerated() { + XCTAssertEqual(cell.frame, CGRect(x: width * (CGFloat(index).truncatingRemainder(dividingBy: numberOfColumns)), y: height * floor(CGFloat(index) / numberOfColumns), width: width, height: height)) + } + } else { + + } + } + + func testTabBarController() { + let viewController = GridViewController() + + let window = UIWindow(frame: UIScreen.main.bounds) + let tabBarController = UITabBarController() + tabBarController.viewControllers = [viewController] + window.rootViewController = tabBarController + window.makeKeyAndVisible() + + RunLoop.main.run(until: Date(timeIntervalSinceNow: 0)) + + let collectionView = viewController.collectionView! + let fractionalWidth: CGFloat = 0.2 + let numberOfColumns: CGFloat = floor(1 / fractionalWidth) + let width: CGFloat = window.bounds.width * fractionalWidth + let height = width + let numberOfItems = collectionView.numberOfItems(inSection: 0) + + if #available(iOS 11.0, *) { + let safeAreaInsets = viewController.view.safeAreaInsets + + XCTAssertEqual(collectionView.adjustedContentInset, safeAreaInsets) + XCTAssertEqual(collectionView.contentOffset, CGPoint(x: -safeAreaInsets.left, y: -safeAreaInsets.top)) + XCTAssertEqual(collectionView.contentSize, CGSize(width: window.bounds.width, height: ceil(CGFloat(numberOfItems) / numberOfColumns) * window.bounds.width * fractionalWidth)) + + for case (let index, let cell as UICollectionViewCell) in collectionView.subviews.enumerated() { + XCTAssertEqual(cell.frame, CGRect(x: width * (CGFloat(index).truncatingRemainder(dividingBy: numberOfColumns)), y: height * floor(CGFloat(index) / numberOfColumns), width: width, height: height)) + } + } else { + + } + } + + func testNavigationControllerInTabBarController() { + let viewController = GridViewController() + + let window = UIWindow(frame: UIScreen.main.bounds) + let navigationController = UINavigationController(rootViewController: viewController) + let tabBarController = UITabBarController() + tabBarController.viewControllers = [navigationController] + window.rootViewController = tabBarController + window.makeKeyAndVisible() + + RunLoop.main.run(until: Date(timeIntervalSinceNow: 0)) + + let collectionView = viewController.collectionView! + let fractionalWidth: CGFloat = 0.2 + let numberOfColumns: CGFloat = floor(1 / fractionalWidth) + let width: CGFloat = window.bounds.width * fractionalWidth + let height = width + let numberOfItems = collectionView.numberOfItems(inSection: 0) + + if #available(iOS 11.0, *) { + let safeAreaInsets = viewController.view.safeAreaInsets + + XCTAssertEqual(collectionView.adjustedContentInset, safeAreaInsets) + XCTAssertEqual(collectionView.contentOffset, CGPoint(x: -safeAreaInsets.left, y: -safeAreaInsets.top)) + XCTAssertEqual(collectionView.contentSize, CGSize(width: window.bounds.width, height: ceil(CGFloat(numberOfItems) / numberOfColumns) * window.bounds.width * fractionalWidth)) + + for case (let index, let cell as UICollectionViewCell) in collectionView.subviews.enumerated() { + XCTAssertEqual(cell.frame, CGRect(x: width * (CGFloat(index).truncatingRemainder(dividingBy: numberOfColumns)), y: height * floor(CGFloat(index) / numberOfColumns), width: width, height: height)) + } + } else { + + } + } +} + +class GridViewController: UIViewController { + enum Section { + case main + } + + var dataSource: UICollectionViewDiffableDataSource! = nil + var collectionView: UICollectionView! = nil + + override func viewDidLoad() { + super.viewDidLoad() + navigationItem.title = "Grid" + configureHierarchy() + configureDataSource() + } +} + +extension GridViewController { + private func createLayout() -> UICollectionViewLayout { + let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.2), heightDimension: .fractionalHeight(1)) + let item = NSCollectionLayoutItem(layoutSize: itemSize) + + let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .fractionalWidth(0.2)) + let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item]) + + let section = NSCollectionLayoutSection(group: group) + + let layout = UICollectionViewCompositionalLayout(section: section) + return layout + } +} + +extension GridViewController { + private func configureHierarchy() { + collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: createLayout()) + collectionView.autoresizingMask = [.flexibleWidth, .flexibleHeight] + collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: String(describing: UICollectionViewCell.self)) + view.addSubview(collectionView) + } + + private func configureDataSource() { + dataSource = UICollectionViewDiffableDataSource(collectionView: collectionView) { (collectionView, indexPath, identifier) in + let cell = collectionView.dequeueReusableCell(withReuseIdentifier: String(describing: UICollectionViewCell.self), for: indexPath) + cell.contentView.backgroundColor = .white + cell.contentView.layer.borderWidth = 1 + cell.contentView.layer.borderColor = UIColor.black.withAlphaComponent(0.5).cgColor + return cell + } + + var snapshot = NSDiffableDataSourceSnapshot() + snapshot.appendSections([.main]) + snapshot.appendItems(Array(0..<94)) + dataSource.apply(snapshot, animatingDifferences: false) + } +} diff --git a/Tests/IBPCollectionViewCompositionalLayoutTests/ListViewTests.swift b/Tests/IBPCollectionViewCompositionalLayoutTests/ListViewTests.swift index 8cdb0de..e999c07 100644 --- a/Tests/IBPCollectionViewCompositionalLayoutTests/ListViewTests.swift +++ b/Tests/IBPCollectionViewCompositionalLayoutTests/ListViewTests.swift @@ -12,13 +12,19 @@ class ListViewTests: XCTestCase { RunLoop.main.run(until: Date(timeIntervalSinceNow: 0)) let collectionView = viewController.collectionView! + let height: CGFloat = 44 + let numberOfItems = collectionView.numberOfItems(inSection: 0) if #available(iOS 11.0, *) { let safeAreaInsets = viewController.view.safeAreaInsets XCTAssertEqual(collectionView.adjustedContentInset, safeAreaInsets) XCTAssertEqual(collectionView.contentOffset, CGPoint(x: -safeAreaInsets.left, y: -safeAreaInsets.top)) - XCTAssertEqual(collectionView.contentSize, CGSize(width: window.bounds.width, height: 44 * 94)) + XCTAssertEqual(collectionView.contentSize, CGSize(width: window.bounds.width, height: height * CGFloat(numberOfItems))) + + for case (let index, let cell as UICollectionViewCell) in collectionView.subviews.enumerated() { + XCTAssertEqual(cell.frame, CGRect(x: 0, y: CGFloat(index) * height, width: window.bounds.width, height: height)) + } } else { } @@ -35,13 +41,19 @@ class ListViewTests: XCTestCase { RunLoop.main.run(until: Date(timeIntervalSinceNow: 0)) let collectionView = viewController.collectionView! + let height: CGFloat = 44 + let numberOfItems = collectionView.numberOfItems(inSection: 0) if #available(iOS 11.0, *) { let safeAreaInsets = viewController.view.safeAreaInsets XCTAssertEqual(collectionView.adjustedContentInset, safeAreaInsets) XCTAssertEqual(collectionView.contentOffset, CGPoint(x: -safeAreaInsets.left, y: -safeAreaInsets.top)) - XCTAssertEqual(collectionView.contentSize, CGSize(width: window.bounds.width, height: 44 * 94)) + XCTAssertEqual(collectionView.contentSize, CGSize(width: window.bounds.width, height: height * CGFloat(numberOfItems))) + + for case (let index, let cell as UICollectionViewCell) in collectionView.subviews.enumerated() { + XCTAssertEqual(cell.frame, CGRect(x: 0, y: CGFloat(index) * height, width: window.bounds.width, height: height)) + } } else { } @@ -59,13 +71,19 @@ class ListViewTests: XCTestCase { RunLoop.main.run(until: Date(timeIntervalSinceNow: 0)) let collectionView = viewController.collectionView! + let height: CGFloat = 44 + let numberOfItems = collectionView.numberOfItems(inSection: 0) if #available(iOS 11.0, *) { let safeAreaInsets = viewController.view.safeAreaInsets XCTAssertEqual(collectionView.adjustedContentInset, safeAreaInsets) XCTAssertEqual(collectionView.contentOffset, CGPoint(x: -safeAreaInsets.left, y: -safeAreaInsets.top)) - XCTAssertEqual(collectionView.contentSize, CGSize(width: window.bounds.width, height: 44 * 94)) + XCTAssertEqual(collectionView.contentSize, CGSize(width: window.bounds.width, height: height * CGFloat(numberOfItems))) + + for case (let index, let cell as UICollectionViewCell) in collectionView.subviews.enumerated() { + XCTAssertEqual(cell.frame, CGRect(x: 0, y: CGFloat(index) * height, width: window.bounds.width, height: height)) + } } else { } @@ -84,13 +102,19 @@ class ListViewTests: XCTestCase { RunLoop.main.run(until: Date(timeIntervalSinceNow: 0)) let collectionView = viewController.collectionView! + let height: CGFloat = 44 + let numberOfItems = collectionView.numberOfItems(inSection: 0) if #available(iOS 11.0, *) { let safeAreaInsets = viewController.view.safeAreaInsets XCTAssertEqual(collectionView.adjustedContentInset, safeAreaInsets) XCTAssertEqual(collectionView.contentOffset, CGPoint(x: -safeAreaInsets.left, y: -safeAreaInsets.top)) - XCTAssertEqual(collectionView.contentSize, CGSize(width: window.bounds.width, height: 44 * 94)) + XCTAssertEqual(collectionView.contentSize, CGSize(width: window.bounds.width, height: height * CGFloat(numberOfItems))) + + for case (let index, let cell as UICollectionViewCell) in collectionView.subviews.enumerated() { + XCTAssertEqual(cell.frame, CGRect(x: 0, y: CGFloat(index) * height, width: window.bounds.width, height: height)) + } } else { } @@ -115,14 +139,11 @@ class ListViewController: UIViewController { extension ListViewController { private func createLayout() -> UICollectionViewLayout { - let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), - heightDimension: .fractionalHeight(1)) + let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .fractionalHeight(1)) let item = NSCollectionLayoutItem(layoutSize: itemSize) - let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), - heightDimension: .absolute(44)) - let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, - subitems: [item]) + let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .absolute(44)) + let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item]) let section = NSCollectionLayoutSection(group: group) @@ -135,15 +156,16 @@ extension ListViewController { private func configureHierarchy() { collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: createLayout()) collectionView.autoresizingMask = [.flexibleWidth, .flexibleHeight] - collectionView.backgroundColor = .systemBackground - collectionView.register(ListCell.self, forCellWithReuseIdentifier: ListCell.reuseIdentifier) + collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: String(describing: UICollectionViewCell.self)) view.addSubview(collectionView) - collectionView.delegate = self } + private func configureDataSource() { dataSource = UICollectionViewDiffableDataSource(collectionView: collectionView) { (collectionView, indexPath, identifier) in - guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ListCell.reuseIdentifier, for: indexPath) as? ListCell else { fatalError("Cannot create new cell") } - cell.label.text = "\(identifier)" + let cell = collectionView.dequeueReusableCell(withReuseIdentifier: String(describing: UICollectionViewCell.self), for: indexPath) + cell.contentView.backgroundColor = .white + cell.contentView.layer.borderWidth = 1 + cell.contentView.layer.borderColor = UIColor.black.withAlphaComponent(0.5).cgColor return cell } @@ -153,68 +175,3 @@ extension ListViewController { dataSource.apply(snapshot, animatingDifferences: false) } } - -extension ListViewController: UICollectionViewDelegate { - func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { - collectionView.deselectItem(at: indexPath, animated: true) - } -} - -class ListCell: UICollectionViewCell { - static let reuseIdentifier = "list-cell-reuse-identifier" - let label = UILabel() - let accessoryImageView = UIImageView() - let seperatorView = UIView() - - override init(frame: CGRect) { - super.init(frame: frame) - configure() - } - - required init?(coder: NSCoder) { - fatalError("not implemented") - } -} - -extension ListCell { - func configure() { - seperatorView.translatesAutoresizingMaskIntoConstraints = false - seperatorView.backgroundColor = .lightGray - contentView.addSubview(seperatorView) - - label.translatesAutoresizingMaskIntoConstraints = false - label.adjustsFontForContentSizeCategory = true - label.font = .preferredFont(forTextStyle: .body) - contentView.addSubview(label) - - accessoryImageView.translatesAutoresizingMaskIntoConstraints = false - contentView.addSubview(accessoryImageView) - - selectedBackgroundView = UIView() - selectedBackgroundView?.backgroundColor = UIColor.lightGray.withAlphaComponent(0.3) - - let rtl = effectiveUserInterfaceLayoutDirection == .rightToLeft - let chevronImageName = rtl ? "chevron.left" : "chevron.right" - let chevronImage = UIImage(systemName: chevronImageName) - accessoryImageView.image = chevronImage - accessoryImageView.tintColor = UIColor.lightGray.withAlphaComponent(0.7) - - let inset: CGFloat = 10 - NSLayoutConstraint.activate([ - label.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: inset), - label.topAnchor.constraint(equalTo: contentView.topAnchor, constant: inset), - label.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -inset), - label.trailingAnchor.constraint(equalTo: accessoryImageView.leadingAnchor, constant: -inset), - - accessoryImageView.centerYAnchor.constraint(equalTo: contentView.centerYAnchor), - accessoryImageView.widthAnchor.constraint(equalToConstant: 13), - accessoryImageView.heightAnchor.constraint(equalToConstant: 20), - accessoryImageView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -inset), - - seperatorView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: inset), - seperatorView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor), - seperatorView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -inset), - seperatorView.heightAnchor.constraint(equalToConstant: 0.5) - ]) - } -} diff --git a/Tests/IBPCollectionViewCompositionalLayoutTests/UIKitInteroperability.swift b/Tests/IBPCollectionViewCompositionalLayoutTests/UIKitInteroperability.swift deleted file mode 120000 index 32c09d5..0000000 --- a/Tests/IBPCollectionViewCompositionalLayoutTests/UIKitInteroperability.swift +++ /dev/null @@ -1 +0,0 @@ -../../Example/Example/UIKitInteroperability.swift \ No newline at end of file