Skip to content
Open
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
12 changes: 4 additions & 8 deletions IBPCollectionViewCompositionalLayout.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -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 = "<group>"; };
14B9585223B8DE3A00DF3B7F /* ListViewTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ListViewTests.swift; sourceTree = "<group>"; };
14B9585423B8E07B00DF3B7F /* IBPCollectionViewCompositionalLayoutInteroperability.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IBPCollectionViewCompositionalLayoutInteroperability.swift; sourceTree = "<group>"; };
14B9585623B8E12400DF3B7F /* DiffableDataSourceInteroperability.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DiffableDataSourceInteroperability.swift; sourceTree = "<group>"; };
14B9585F23B8E27F00DF3B7F /* UIKitInteroperability.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIKitInteroperability.swift; sourceTree = "<group>"; };
14B9586323B8E35C00DF3B7F /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
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 = "<group>"; };
14B9587423B8E60900DF3B7F /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
Expand Down Expand Up @@ -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 = "<group>";
Expand Down Expand Up @@ -540,7 +537,6 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
14B9586423B8E35C00DF3B7F /* Assets.xcassets in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -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;
};
Expand Down

This file was deleted.

This file was deleted.

Binary file not shown.

This file was deleted.

Binary file not shown.
189 changes: 189 additions & 0 deletions Tests/IBPCollectionViewCompositionalLayoutTests/GridViewTests.swift
Original file line number Diff line number Diff line change
@@ -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<Section, Int>! = 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<Section, Int>(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<Section, Int>()
snapshot.appendSections([.main])
snapshot.appendItems(Array(0..<94))
dataSource.apply(snapshot, animatingDifferences: false)
}
}
Loading