Skip to content

Commit

Permalink
make the layout provider nonoptional
Browse files Browse the repository at this point in the history
  • Loading branch information
CalvinChangCC committed May 22, 2024
1 parent 1545811 commit 9e1437f
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ final class NamesSectionController: ListSectionController<
NamesSectionViewModelType,
String
> {
override var layoutProvider: SectionLayoutProvider? {
override var layoutProvider: SectionLayoutProvider {
let layoutSize = NSCollectionLayoutSize(
widthDimension: .fractionalWidth(1),
heightDimension: .estimated(50)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ extension ListCollectionViewAdapter {
@inlinable
public func flowDelegate(at indexPath: IndexPath) -> SectionFlowDelegate? {
if #available(iOS 13.0, *) {
return controller(at: indexPath)?.layoutProvider?.flowLayoutProvider
return controller(at: indexPath)?.layoutProvider.flowLayoutProvider
} else {
return controller(at: indexPath)?.flowDelegate
}
Expand All @@ -68,7 +68,7 @@ extension ListCollectionViewAdapter {
@inlinable
public func flowDelegate(at index: Int) -> SectionFlowDelegate? {
if #available(iOS 13.0, *) {
return controller(at: index)?.layoutProvider?.flowLayoutProvider
return controller(at: index)?.layoutProvider.flowLayoutProvider
} else {
return controller(at: index)?.flowDelegate
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ extension SingleSectionCollectionViewAdapter {
@inlinable
public func flowDelegate(at indexPath: IndexPath) -> SectionFlowDelegate? {
if #available(iOS 13.0, *) {
return controller(at: indexPath)?.layoutProvider?.flowLayoutProvider
return controller(at: indexPath)?.layoutProvider.flowLayoutProvider
} else {
return controller(at: indexPath)?.flowDelegate
}
Expand All @@ -68,7 +68,7 @@ extension SingleSectionCollectionViewAdapter {
@inlinable
public func flowDelegate(at index: Int) -> SectionFlowDelegate? {
if #available(iOS 13.0, *) {
return controller(at: index)?.layoutProvider?.flowLayoutProvider
return controller(at: index)?.layoutProvider.flowLayoutProvider
} else {
return controller(at: index)?.flowDelegate
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ open class BaseSectionController: SectionController,
open var dropDelegate: SectionDropDelegate? { self }

@available(iOS 13.0, *)
open var layoutProvider: SectionLayoutProvider? { .flowLayout(self) }
open var layoutProvider: SectionLayoutProvider { .flowLayout(self) }

open func didUpdate(model: Any) { }

Expand Down
7 changes: 2 additions & 5 deletions SectionKit/Sources/SectionController/SectionController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public protocol SectionController: AnyObject {
var dropDelegate: SectionDropDelegate? { get }

@available(iOS 13.0, *)
var layoutProvider: SectionLayoutProvider? { get }
var layoutProvider: SectionLayoutProvider { get }

/// The model of this section controller changed.
func didUpdate(model: Any)
Expand All @@ -47,14 +47,11 @@ extension SectionController {

@available(iOS 11.0, *)
public var dropDelegate: SectionDropDelegate? { nil }

@available(iOS 13.0, *)
public var layoutProvider: SectionLayoutProvider? { nil }
}

@available(iOS 13.0, *)
public enum SectionLayoutProvider {
case flowLayout(FlowLayoutProvider)
case flowLayout(FlowLayoutProvider?)
case compositionalLayout(CompositionalLayoutProvider)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@
import UIKit
import XCTest

internal class BaseCollectionViewAdapterUICollectionViewDelegateFlowLayoutTests: XCTestCase {
class BaseCollectionViewAdapterUICollectionViewDelegateFlowLayoutTests: XCTestCase {
override func setUpWithError() throws {
try super.setUpWithError()
continueAfterFailure = false
try skipIfNeeded()
}

internal func skipIfNeeded() throws {
func skipIfNeeded() throws {
guard Self.self === BaseCollectionViewAdapterUICollectionViewDelegateFlowLayoutTests.self else { return }
throw XCTSkip("Tests from base class are skipped")
}

internal func createCollectionView(
func createCollectionView(
frame: CGRect = .zero,
collectionViewLayout layout: UICollectionViewLayout? = nil
) -> UICollectionView {
UICollectionView(frame: frame, collectionViewLayout: layout ?? UICollectionViewFlowLayout())
}

@MainActor
internal func createCollectionViewAdapter(
func createCollectionViewAdapter(
collectionView: UICollectionView,
sections: [Section] = [],
viewController: UIViewController? = nil,
Expand All @@ -34,7 +34,7 @@ internal class BaseCollectionViewAdapterUICollectionViewDelegateFlowLayoutTests:

// MARK: - sizeForItem
@MainActor
internal func testSizeForItemWithDelegate() throws {
func testSizeForItemWithDelegate() throws {
let testExpectation = expectation(description: "Should invoke flow layout delegate")
let mockLayout = UICollectionViewFlowLayout()
let collectionView = createCollectionView(collectionViewLayout: mockLayout)
Expand Down Expand Up @@ -67,7 +67,7 @@ internal class BaseCollectionViewAdapterUICollectionViewDelegateFlowLayoutTests:
}

@MainActor
internal func testSizeForItemWithoutDelegateButWithFlowLayout() throws {
func testSizeForItemWithoutDelegateButWithFlowLayout() throws {
let itemSize = CGSize(width: 1, height: 2)
let layout = UICollectionViewFlowLayout()
layout.itemSize = itemSize
Expand All @@ -78,7 +78,7 @@ internal class BaseCollectionViewAdapterUICollectionViewDelegateFlowLayoutTests:
sections: [
Section(id: "", model: "", controller: {
let sectionController = MockSectionController()
sectionController.layoutProvider = nil
sectionController.layoutProvider = .flowLayout(nil)
return sectionController
})
]
Expand All @@ -90,16 +90,16 @@ internal class BaseCollectionViewAdapterUICollectionViewDelegateFlowLayoutTests:
}

@MainActor
internal func testSizeForItemWithoutDelegateAndFlowLayout() throws {
let layout = UICollectionViewLayout()
func testSizeForItemWithoutDelegateAndFlowLayout() throws {
let layout = UICollectionViewFlowLayout()
let collectionView = createCollectionView(collectionViewLayout: layout)
let itemIndexPath = IndexPath(item: 0, section: 0)
let adapter = try createCollectionViewAdapter(
collectionView: collectionView,
sections: [
Section(id: "", model: "", controller: {
let sectionController = MockSectionController()
sectionController.layoutProvider = nil
sectionController.layoutProvider = .flowLayout(nil)
return sectionController
})
]
Expand All @@ -113,7 +113,7 @@ internal class BaseCollectionViewAdapterUICollectionViewDelegateFlowLayoutTests:
// MARK: - inset

@MainActor
internal func testInsetWithDelegate() throws {
func testInsetWithDelegate() throws {
let testExpectation = expectation(description: "Should invoke flow layout delegate")
let mockLayout = UICollectionViewFlowLayout()
let collectionView = createCollectionView(collectionViewLayout: mockLayout)
Expand Down Expand Up @@ -144,7 +144,7 @@ internal class BaseCollectionViewAdapterUICollectionViewDelegateFlowLayoutTests:
}

@MainActor
internal func testInsetWithoutDelegateButWithFlowLayout() throws {
func testInsetWithoutDelegateButWithFlowLayout() throws {
let sectionInset = UIEdgeInsets(top: 1, left: 2, bottom: 4, right: 8)
let layout = UICollectionViewFlowLayout()
layout.sectionInset = sectionInset
Expand All @@ -154,7 +154,7 @@ internal class BaseCollectionViewAdapterUICollectionViewDelegateFlowLayoutTests:
sections: [
Section(id: "", model: "", controller: {
let sectionController = MockSectionController()
sectionController.layoutProvider = nil
sectionController.layoutProvider = .flowLayout(nil)
return sectionController
})
]
Expand All @@ -166,15 +166,15 @@ internal class BaseCollectionViewAdapterUICollectionViewDelegateFlowLayoutTests:
}

@MainActor
internal func testInsetWithoutDelegateAndFlowLayout() throws {
func testInsetWithoutDelegateAndFlowLayout() throws {
let layout = UICollectionViewLayout()
let collectionView = createCollectionView(collectionViewLayout: layout)
let adapter = try createCollectionViewAdapter(
collectionView: collectionView,
sections: [
Section(id: "", model: "", controller: {
let sectionController = MockSectionController()
sectionController.layoutProvider = nil
sectionController.layoutProvider = .flowLayout(nil)
return sectionController
})
]
Expand All @@ -188,7 +188,7 @@ internal class BaseCollectionViewAdapterUICollectionViewDelegateFlowLayoutTests:
// MARK: - minimumLineSpacing

@MainActor
internal func testMinimumLineSpacingWithDelegate() throws {
func testMinimumLineSpacingWithDelegate() throws {
let testExpectation = expectation(description: "Should invoke flow layout delegate")
let mockLayout = UICollectionViewFlowLayout()
let collectionView = createCollectionView(collectionViewLayout: mockLayout)
Expand Down Expand Up @@ -219,7 +219,7 @@ internal class BaseCollectionViewAdapterUICollectionViewDelegateFlowLayoutTests:
}

@MainActor
internal func testMinimumLineSpacingWithoutDelegateButWithFlowLayout() throws {
func testMinimumLineSpacingWithoutDelegateButWithFlowLayout() throws {
let lineSpacing: CGFloat = 1
let layout = UICollectionViewFlowLayout()
layout.minimumLineSpacing = lineSpacing
Expand All @@ -229,7 +229,7 @@ internal class BaseCollectionViewAdapterUICollectionViewDelegateFlowLayoutTests:
sections: [
Section(id: "", model: "", controller: {
let sectionController = MockSectionController()
sectionController.layoutProvider = nil
sectionController.layoutProvider = .flowLayout(nil)
return sectionController
})
]
Expand All @@ -241,15 +241,15 @@ internal class BaseCollectionViewAdapterUICollectionViewDelegateFlowLayoutTests:
}

@MainActor
internal func testMinimumLineSpacingWithoutDelegateAndFlowLayout() throws {
func testMinimumLineSpacingWithoutDelegateAndFlowLayout() throws {
let layout = UICollectionViewLayout()
let collectionView = createCollectionView(collectionViewLayout: layout)
let adapter = try createCollectionViewAdapter(
collectionView: collectionView,
sections: [
Section(id: "", model: "", controller: {
let sectionController = MockSectionController()
sectionController.layoutProvider = nil
sectionController.layoutProvider = .flowLayout(nil)
return sectionController
})
]
Expand All @@ -263,7 +263,7 @@ internal class BaseCollectionViewAdapterUICollectionViewDelegateFlowLayoutTests:
// MARK: - minimumInteritemSpacing

@MainActor
internal func testMinimumInteritemSpacingWithDelegate() throws {
func testMinimumInteritemSpacingWithDelegate() throws {
let testExpectation = expectation(description: "Should invoke flow layout delegate")
let mockLayout = UICollectionViewFlowLayout()
let collectionView = createCollectionView(collectionViewLayout: mockLayout)
Expand Down Expand Up @@ -294,7 +294,7 @@ internal class BaseCollectionViewAdapterUICollectionViewDelegateFlowLayoutTests:
}

@MainActor
internal func testMinimumInteritemSpacingWithoutDelegateButWithFlowLayout() throws {
func testMinimumInteritemSpacingWithoutDelegateButWithFlowLayout() throws {
let interitemSpacing: CGFloat = 1
let layout = UICollectionViewFlowLayout()
layout.minimumInteritemSpacing = interitemSpacing
Expand All @@ -304,7 +304,7 @@ internal class BaseCollectionViewAdapterUICollectionViewDelegateFlowLayoutTests:
sections: [
Section(id: "", model: "", controller: {
let sectionController = MockSectionController()
sectionController.layoutProvider = nil
sectionController.layoutProvider = .flowLayout(nil)
return sectionController
})
]
Expand All @@ -316,15 +316,15 @@ internal class BaseCollectionViewAdapterUICollectionViewDelegateFlowLayoutTests:
}

@MainActor
internal func testMinimumInteritemSpacingWithoutDelegateAndFlowLayout() throws {
func testMinimumInteritemSpacingWithoutDelegateAndFlowLayout() throws {
let layout = UICollectionViewLayout()
let collectionView = createCollectionView(collectionViewLayout: layout)
let adapter = try createCollectionViewAdapter(
collectionView: collectionView,
sections: [
Section(id: "", model: "", controller: {
let sectionController = MockSectionController()
sectionController.layoutProvider = nil
sectionController.layoutProvider = .flowLayout(nil)
return sectionController
})
]
Expand All @@ -338,7 +338,7 @@ internal class BaseCollectionViewAdapterUICollectionViewDelegateFlowLayoutTests:
// MARK: - referenceSizeForHeader

@MainActor
internal func testReferenceSizeForHeaderWithDelegate() throws {
func testReferenceSizeForHeaderWithDelegate() throws {
let testExpectation = expectation(description: "Should invoke flow layout delegate")
let mockLayout = UICollectionViewFlowLayout()
let collectionView = createCollectionView(collectionViewLayout: mockLayout)
Expand Down Expand Up @@ -369,7 +369,7 @@ internal class BaseCollectionViewAdapterUICollectionViewDelegateFlowLayoutTests:
}

@MainActor
internal func testReferenceSizeForHeaderWithoutDelegateButWithFlowLayout() throws {
func testReferenceSizeForHeaderWithoutDelegateButWithFlowLayout() throws {
let headerSize = CGSize(width: 1, height: 2)
let layout = UICollectionViewFlowLayout()
layout.headerReferenceSize = headerSize
Expand All @@ -379,7 +379,7 @@ internal class BaseCollectionViewAdapterUICollectionViewDelegateFlowLayoutTests:
sections: [
Section(id: "", model: "", controller: {
let sectionController = MockSectionController()
sectionController.layoutProvider = nil
sectionController.layoutProvider = .flowLayout(nil)
return sectionController
})
]
Expand All @@ -391,15 +391,15 @@ internal class BaseCollectionViewAdapterUICollectionViewDelegateFlowLayoutTests:
}

@MainActor
internal func testReferenceSizeForHeaderWithoutDelegateAndFlowLayout() throws {
func testReferenceSizeForHeaderWithoutDelegateAndFlowLayout() throws {
let layout = UICollectionViewLayout()
let collectionView = createCollectionView(collectionViewLayout: layout)
let adapter = try createCollectionViewAdapter(
collectionView: collectionView,
sections: [
Section(id: "", model: "", controller: {
let sectionController = MockSectionController()
sectionController.layoutProvider = nil
sectionController.layoutProvider = .flowLayout(nil)
return sectionController
})
]
Expand All @@ -413,7 +413,7 @@ internal class BaseCollectionViewAdapterUICollectionViewDelegateFlowLayoutTests:
// MARK: - referenceSizeForFooter

@MainActor
internal func testReferenceSizeForFooterWithDelegate() throws {
func testReferenceSizeForFooterWithDelegate() throws {
let testExpectation = expectation(description: "Should invoke flow layout delegate")
let mockLayout = UICollectionViewFlowLayout()
let collectionView = createCollectionView(collectionViewLayout: mockLayout)
Expand Down Expand Up @@ -444,7 +444,7 @@ internal class BaseCollectionViewAdapterUICollectionViewDelegateFlowLayoutTests:
}

@MainActor
internal func testReferenceSizeForFooterWithoutDelegateButWithFlowLayout() throws {
func testReferenceSizeForFooterWithoutDelegateButWithFlowLayout() throws {
let footerSize = CGSize(width: 1, height: 2)
let layout = UICollectionViewFlowLayout()
layout.footerReferenceSize = footerSize
Expand All @@ -454,7 +454,7 @@ internal class BaseCollectionViewAdapterUICollectionViewDelegateFlowLayoutTests:
sections: [
Section(id: "", model: "", controller: {
let sectionController = MockSectionController()
sectionController.layoutProvider = nil
sectionController.layoutProvider = .flowLayout(nil)
return sectionController
})
]
Expand All @@ -466,15 +466,15 @@ internal class BaseCollectionViewAdapterUICollectionViewDelegateFlowLayoutTests:
}

@MainActor
internal func testReferenceSizeForFooterWithoutDelegateAndFlowLayout() throws {
func testReferenceSizeForFooterWithoutDelegateAndFlowLayout() throws {
let layout = UICollectionViewLayout()
let collectionView = createCollectionView(collectionViewLayout: layout)
let adapter = try createCollectionViewAdapter(
collectionView: collectionView,
sections: [
Section(id: "", model: "", controller: {
let sectionController = MockSectionController()
sectionController.layoutProvider = nil
sectionController.layoutProvider = .flowLayout(nil)
return sectionController
})
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ final class ProtocolDefaultValuesSectionControllerTests: XCTestCase {
super.setUp()
class DefaultSectionController: SectionController {
var context: CollectionViewContext?
var dataSource: SectionDataSource = MockSectionDataSource()
let dataSource: SectionDataSource = MockSectionDataSource()
let layoutProvider: SectionLayoutProvider = .flowLayout(nil)
func didUpdate(model: Any) { }
}
sut = DefaultSectionController()
Expand Down
Loading

0 comments on commit 9e1437f

Please sign in to comment.