|
| 1 | +// The MIT License (MIT) |
| 2 | +// Copyright © 2020 Ivan Vorobei (hello@ivanvorobei.by) |
| 3 | +// |
| 4 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +// of this software and associated documentation files (the "Software"), to deal |
| 6 | +// in the Software without restriction, including without limitation the rights |
| 7 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +// copies of the Software, and to permit persons to whom the Software is |
| 9 | +// furnished to do so, subject to the following conditions: |
| 10 | +// |
| 11 | +// The above copyright notice and this permission notice shall be included in all |
| 12 | +// copies or substantial portions of the Software. |
| 13 | +// |
| 14 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | +// SOFTWARE. |
| 21 | + |
| 22 | +import UIKit |
| 23 | + |
| 24 | +/** |
| 25 | + SPDiffable: Diffable collecton data source. |
| 26 | + |
| 27 | + Using array cell providers for get view for each model. |
| 28 | + Need pass all cell providers which will be using in collection view and data source all by order each and try get view. |
| 29 | + */ |
| 30 | +@available(iOS 13.0, *) |
| 31 | +open class SPDiffableCollectionDataSource: UICollectionViewDiffableDataSource<SPDiffableSection, SPDiffableItem> { |
| 32 | + |
| 33 | + /** |
| 34 | + SPDiffable: Make section and use header model like first cell. |
| 35 | + |
| 36 | + If you don't use composition layout, no way say to collection system about header elements. Its using random, sometimes its cell provider call, sometimes it call supplementary. In this case we shoudn't set header to section snapshot. For this case it condition only. |
| 37 | + */ |
| 38 | + public let headerAsFirstCell: Bool |
| 39 | + |
| 40 | + // Using for get cells or update its. |
| 41 | + internal weak var collectionView: UICollectionView? |
| 42 | + |
| 43 | + // MARK: - Init |
| 44 | + |
| 45 | + public init(collectionView: UICollectionView, cellProviders: [SPDiffableCollectionCellProvider], supplementaryViewProviders: [SupplementaryViewProvider] = [], headerAsFirstCell: Bool = true) { |
| 46 | + |
| 47 | + self.headerAsFirstCell = headerAsFirstCell |
| 48 | + self.collectionView = collectionView |
| 49 | + |
| 50 | + super.init(collectionView: collectionView) { (collectionView, indexPath, item) -> UICollectionViewCell? in |
| 51 | + for provider in cellProviders { |
| 52 | + if let cell = provider.clouser(collectionView, indexPath, item) { |
| 53 | + return cell |
| 54 | + } |
| 55 | + } |
| 56 | + return nil |
| 57 | + } |
| 58 | + |
| 59 | + if !supplementaryViewProviders.isEmpty { |
| 60 | + supplementaryViewProvider = { (collectionView, kind, indexPath) -> UICollectionReusableView? in |
| 61 | + for provider in supplementaryViewProviders { |
| 62 | + if let view = provider(collectionView, kind, indexPath) { |
| 63 | + return view |
| 64 | + } |
| 65 | + } |
| 66 | + return nil |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + SPDiffable: Wrapper of collection supplementary view provider. |
| 74 | + */ |
| 75 | +@available(iOS 13.0, *) |
| 76 | +public typealias SPDiffableCollectionSupplementaryViewProvider = SPDiffableCollectionDataSource.SupplementaryViewProvider |
0 commit comments