Skip to content

Commit 707d56a

Browse files
committed
Added supplementary view providers logic.
1 parent c358f05 commit 707d56a

File tree

5 files changed

+40
-6
lines changed

5 files changed

+40
-6
lines changed

SPDiffable.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Pod::Spec.new do |s|
22

33
s.name = 'SPDiffable'
4-
s.version = '1.2.5'
4+
s.version = '1.2.6'
55
s.summary = 'Extenshion of Diffable API which allow not duplicate code and use less models.'
66
s.homepage = 'https://github.com/ivanvorobei/SPDiffable'
77
s.source = { :git => 'https://github.com/ivanvorobei/SPDiffable.git', :tag => s.version }

Sources/SPDiffable/Collection/SPDiffableCollectionController.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,16 @@ open class SPDiffableCollectionController: UICollectionViewController {
4343
- parameter providers: Cell Providers with valid order for processing.
4444
- parameter sections: Content as array of `SPDiffableSection`.
4545
*/
46-
open func setCellProviders( _ providers: [SPDiffableCollectionCellProvider], sections: [SPDiffableSection]) {
47-
diffableDataSource = SPDiffableCollectionDataSource(collectionView: collectionView, cellProviders: providers)
46+
open func setCellProviders(
47+
_ cellProviders: [SPDiffableCollectionCellProvider],
48+
supplementaryViewProviders: [SPDiffableCollectionSupplementaryViewProvider] = [],
49+
sections: [SPDiffableSection])
50+
{
51+
diffableDataSource = SPDiffableCollectionDataSource(
52+
collectionView: collectionView,
53+
cellProviders: cellProviders,
54+
supplementaryViewProviders: supplementaryViewProviders
55+
)
4856
diffableDataSource?.apply(sections, animated: false)
4957
}
5058

Sources/SPDiffable/Collection/SPDiffableCollectionView.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,16 @@ open class SPDiffableCollectionView: UICollectionView, UICollectionViewDelegate
5959
- parameter providers: Cell Providers with valid order for processing.
6060
- parameter sections: Content as array of `SPDiffableSection`.
6161
*/
62-
open func setCellProviders( _ providers: [SPDiffableCollectionCellProvider], sections: [SPDiffableSection]) {
63-
diffableDataSource = SPDiffableCollectionDataSource(collectionView: self, cellProviders: providers)
62+
open func setCellProviders(
63+
_ cellProviders: [SPDiffableCollectionCellProvider],
64+
supplementaryViewProviders: [SPDiffableCollectionSupplementaryViewProvider] = [],
65+
sections: [SPDiffableSection])
66+
{
67+
diffableDataSource = SPDiffableCollectionDataSource(
68+
collectionView: self,
69+
cellProviders: cellProviders,
70+
supplementaryViewProviders: supplementaryViewProviders
71+
)
6472
diffableDataSource?.apply(sections, animated: false)
6573
}
6674

Sources/SPDiffable/DataSource/SPDiffableCollectionDataSource.swift

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ open class SPDiffableCollectionDataSource: UICollectionViewDiffableDataSource<SP
3434

3535
// MARK: - Init
3636

37-
public init(collectionView: UICollectionView, cellProviders: [CellProvider]) {
37+
public init(collectionView: UICollectionView, cellProviders: [CellProvider], supplementaryViewProviders: [SupplementaryViewProvider]) {
38+
3839
super.init(collectionView: collectionView) { (collectionView, indexPath, item) -> UICollectionViewCell? in
3940
for provider in cellProviders {
4041
if let cell = provider(collectionView, indexPath, item) {
@@ -43,6 +44,17 @@ open class SPDiffableCollectionDataSource: UICollectionViewDiffableDataSource<SP
4344
}
4445
return nil
4546
}
47+
48+
if !supplementaryViewProviders.isEmpty {
49+
supplementaryViewProvider = { (collectionView, kind, indexPath) -> UICollectionReusableView? in
50+
for provider in supplementaryViewProviders {
51+
if let view = provider(collectionView, kind, indexPath) {
52+
return view
53+
}
54+
}
55+
return nil
56+
}
57+
}
4658
}
4759

4860
// MARK: - Apply Content
@@ -134,3 +146,9 @@ open class SPDiffableCollectionDataSource: UICollectionViewDiffableDataSource<SP
134146
*/
135147
@available(iOS 13.0, *)
136148
public typealias SPDiffableCollectionCellProvider = SPDiffableCollectionDataSource.CellProvider
149+
150+
/**
151+
SPDiffable: Wrapper of collection supplementary view provider.
152+
*/
153+
@available(iOS 13.0, *)
154+
public typealias SPDiffableCollectionSupplementaryViewProvider = SPDiffableCollectionDataSource.SupplementaryViewProvider

0 commit comments

Comments
 (0)