-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add bridge between layout provider and adapter
- Loading branch information
1 parent
7bfeea1
commit 8fabf99
Showing
5 changed files
with
87 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
SectionKit/Sources/Utility/CompositionalLayout/NSCollectionLayoutSection+Empty.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
) | ||
}() | ||
} |
53 changes: 53 additions & 0 deletions
53
SectionKit/Sources/Utility/CompositionalLayout/SectionKitCompositionalLayout.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |