Skip to content

Commit

Permalink
add bridge between layout provider and adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
CalvinChangCC committed May 22, 2024
1 parent 7bfeea1 commit 8fabf99
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ extension ListCollectionViewAdapter {
extension ListCollectionViewAdapter {
@inlinable
public func flowDelegate(at indexPath: IndexPath) -> SectionFlowDelegate? {
controller(at: indexPath)?.flowDelegate
(controller(at: indexPath) as? FlowLayoutSectionController)?.flowDelegate
}

@inlinable
public func flowDelegate(at index: Int) -> SectionFlowDelegate? {
controller(at: index)?.flowDelegate
(controller(at: index) as? FlowLayoutSectionController)?.flowDelegate
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ open class ListCollectionViewAdapter: NSObject, CollectionViewAdapter {
collectionView.dragDelegate = self
collectionView.dropDelegate = self
}
if #available(iOS 13.0, *),
let layout = collectionView.collectionViewLayout as? SectionKitCompositionalLayout {
layout.sections = { [weak self] in
self?.collectionViewSections ?? []
}
}
}

/**
Expand Down Expand Up @@ -90,6 +96,12 @@ open class ListCollectionViewAdapter: NSObject, CollectionViewAdapter {
collectionView.dragDelegate = self
collectionView.dropDelegate = self
}
if #available(iOS 13.0, *),
let layout = collectionView.collectionViewLayout as? SectionKitCompositionalLayout {
layout.sections = { [weak self] in
self?.collectionViewSections ?? []
}
}
}

public let context: CollectionViewContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ extension SingleSectionCollectionViewAdapter {
extension SingleSectionCollectionViewAdapter {
@inlinable
public func flowDelegate(at indexPath: IndexPath) -> SectionFlowDelegate? {
controller(at: indexPath)?.flowDelegate
(controller(at: indexPath) as? FlowLayoutSectionController)?.flowDelegate
}

@inlinable
public func flowDelegate(at index: Int) -> SectionFlowDelegate? {
controller(at: index)?.flowDelegate
(controller(at: index) as? FlowLayoutSectionController)?.flowDelegate
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import UIKit

@available(iOS 13.0, *)
extension NSCollectionLayoutSection {
static let empty: NSCollectionLayoutSection = {
let layoutSize = NSCollectionLayoutSize(
widthDimension: .absolute(0),
heightDimension: .absolute(0)
)
return NSCollectionLayoutSection(
group: .vertical(
layoutSize: layoutSize,
subitem: .init(layoutSize: layoutSize),
count: 1
)
)
}()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import UIKit

/// This compositional layout is designed for the section kit.
/// It ensures that the layout provider utilizes the layout section provided by the compositional layout section controller.
@MainActor
@available(iOS 13.0, *)
final public class SectionKitCompositionalLayout: UICollectionViewCompositionalLayout {
// Use internally to bridge the sections from the adapter to the section provider.
var sections: (() -> [Section])?

public init() {
var sections: (() -> [Section])?
super.init { index, environment in
guard let sections = sections?() else {
assertionFailure("The section provider doesn't set up correctly, please use the `CollectionViewCompositionalLayoutAdapter`")
return .empty
}
guard sections.count > index else {
assertionFailure("Section index out of bound")
return .empty
}
guard let compositionalLayoutSectionController = sections[index].controller
as? BaseCompositionalLayoutSectionController else {
assertionFailure("Please use the `CompositionalLayoutSectionControler`")
return .empty
}
return compositionalLayoutSectionController.layoutSection(
layoutEnvironment: environment
)
}
sections = { [weak self] in
self?.sections?() ?? []
}
}

@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

@available(*, unavailable)
override init(
sectionProvider: @escaping UICollectionViewCompositionalLayoutSectionProvider,
configuration: UICollectionViewCompositionalLayoutConfiguration
) {
fatalError("init(sectionProvider:configuration:) has not been implemented")
}

@available(*, unavailable)
override init(sectionProvider: @escaping UICollectionViewCompositionalLayoutSectionProvider) {
fatalError("init(sectionProvider:) has not been implemented")
}
}

0 comments on commit 8fabf99

Please sign in to comment.