Skip to content

Commit b08e51a

Browse files
committed
Updated files struct.
1 parent a006d02 commit b08e51a

13 files changed

+434
-332
lines changed

SPDiffable.podspec

+1-1
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 = '2.1.2'
4+
s.version = '2.2.0'
55
s.summary = 'Extension of Diffable API which allow not duplicate code and use less models. Included example for SideBar.'
66
s.homepage = 'https://github.com/ivanvorobei/SPDiffable'
77
s.source = { :git => 'https://github.com/ivanvorobei/SPDiffable.git', :tag => s.version }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
@available(iOS 13.0, *)
25+
extension SPDiffableCollectionDataSource {
26+
27+
/**
28+
SPDiffable: Get item by index path.
29+
*/
30+
public func item(for indexPath: IndexPath) -> SPDiffableItem? {
31+
return itemIdentifier(for: indexPath)
32+
}
33+
34+
/**
35+
SPDiffable: Get sections.
36+
*/
37+
public func sections() -> [SPDiffableSection] {
38+
return snapshot().sectionIdentifiers
39+
}
40+
41+
/**
42+
SPDiffable: Get section by index.
43+
*/
44+
public func section(for index: Int) -> SPDiffableSection? {
45+
let snapshot = snapshot()
46+
guard index < snapshot.sectionIdentifiers.count else { return nil }
47+
return snapshot.sectionIdentifiers[index]
48+
}
49+
50+
/**
51+
SPDiffable: Get index path for item by identifier.
52+
*/
53+
public func indexPath(for itemID: SPDiffableItem.Identifier) -> IndexPath? {
54+
return indexPath(for: SPDiffableItem(id: itemID))
55+
}
56+
57+
/**
58+
SPDiffable: Get cell specific type `T` by indetifier.
59+
*/
60+
public func cell<T: UICollectionViewCell>(_ type: T.Type, for itemID: SPDiffableItem.Identifier) -> T? {
61+
guard let indexPath = indexPath(for: itemID) else { return nil }
62+
guard let cell = self.collectionView?.cellForItem(at: indexPath) as? T else { return nil }
63+
return cell
64+
}
65+
}

Sources/SPDiffable/Collection/SPDiffableCollectionDataSource.swift Sources/SPDiffable/Collection/DataSource/SPDiffableCollectionDataSource+Set.swift

+1-91
Original file line numberDiff line numberDiff line change
@@ -21,51 +21,8 @@
2121

2222
import UIKit
2323

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-
*/
3024
@available(iOS 13.0, *)
31-
open class SPDiffableCollectionDataSource: UICollectionViewDiffableDataSource<SPDiffableSection, SPDiffableItem> {
32-
33-
private weak var collectionView: UICollectionView?
34-
35-
/**
36-
SPDiffable: Make section and use header model like first cell.
37-
38-
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.
39-
*/
40-
public let headerAsFirstCell: Bool
41-
42-
// MARK: - Init
43-
44-
public init(collectionView: UICollectionView, cellProviders: [SPDiffableCollectionCellProvider], supplementaryViewProviders: [SupplementaryViewProvider] = [], headerAsFirstCell: Bool = true) {
45-
46-
self.headerAsFirstCell = headerAsFirstCell
47-
self.collectionView = collectionView
48-
49-
super.init(collectionView: collectionView) { (collectionView, indexPath, item) -> UICollectionViewCell? in
50-
for provider in cellProviders {
51-
if let cell = provider.clouser(collectionView, indexPath, item) {
52-
return cell
53-
}
54-
}
55-
return nil
56-
}
57-
58-
if !supplementaryViewProviders.isEmpty {
59-
supplementaryViewProvider = { (collectionView, kind, indexPath) -> UICollectionReusableView? in
60-
for provider in supplementaryViewProviders {
61-
if let view = provider(collectionView, kind, indexPath) {
62-
return view
63-
}
64-
}
65-
return nil
66-
}
67-
}
68-
}
25+
extension SPDiffableCollectionDataSource {
6926

7027
// MARK: - Apply Content
7128

@@ -209,51 +166,4 @@ open class SPDiffableCollectionDataSource: UICollectionViewDiffableDataSource<SP
209166
let snapshot = self.snapshot()
210167
apply(snapshot, animated: animated, completion: completion)
211168
}
212-
213-
// MARK: - Get Content
214-
215-
/**
216-
SPDiffable: Get item by index path.
217-
*/
218-
public func item(for indexPath: IndexPath) -> SPDiffableItem? {
219-
return itemIdentifier(for: indexPath)
220-
}
221-
222-
/**
223-
SPDiffable: Get sections.
224-
*/
225-
public func sections() -> [SPDiffableSection] {
226-
return snapshot().sectionIdentifiers
227-
}
228-
229-
/**
230-
SPDiffable: Get section by index.
231-
*/
232-
public func section(for index: Int) -> SPDiffableSection? {
233-
let snapshot = snapshot()
234-
guard index < snapshot.sectionIdentifiers.count else { return nil }
235-
return snapshot.sectionIdentifiers[index]
236-
}
237-
238-
/**
239-
SPDiffable: Get index path for item by identifier.
240-
*/
241-
public func indexPath(for itemID: SPDiffableItem.Identifier) -> IndexPath? {
242-
return indexPath(for: SPDiffableItem(id: itemID))
243-
}
244-
245-
/**
246-
SPDiffable: Get cell specific type `T` by indetifier.
247-
*/
248-
public func cell<T: UICollectionViewCell>(_ type: T.Type, for itemID: SPDiffableItem.Identifier) -> T? {
249-
guard let indexPath = indexPath(for: itemID) else { return nil }
250-
guard let cell = self.collectionView?.cellForItem(at: indexPath) as? T else { return nil }
251-
return cell
252-
}
253169
}
254-
255-
/**
256-
SPDiffable: Wrapper of collection supplementary view provider.
257-
*/
258-
@available(iOS 13.0, *)
259-
public typealias SPDiffableCollectionSupplementaryViewProvider = SPDiffableCollectionDataSource.SupplementaryViewProvider
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
@available(iOS 13.0, *)
25+
extension SPDiffableTableDataSource {
26+
27+
/**
28+
SPDiffable: Get item by index path.
29+
*/
30+
public func item(for indexPath: IndexPath) -> SPDiffableItem? {
31+
return itemIdentifier(for: indexPath)
32+
}
33+
34+
/**
35+
SPDiffable: Get sections.
36+
*/
37+
public func sections() -> [SPDiffableSection] {
38+
return snapshot().sectionIdentifiers
39+
}
40+
41+
/**
42+
SPDiffable: Get section by index.
43+
*/
44+
public func section(for index: Int) -> SPDiffableSection? {
45+
let snapshot = snapshot()
46+
guard index < snapshot.sectionIdentifiers.count else { return nil }
47+
return snapshot.sectionIdentifiers[index]
48+
}
49+
50+
/**
51+
SPDiffable: Get index path for item by identifier.
52+
*/
53+
public func indexPath(for itemID: SPDiffableItem.Identifier) -> IndexPath? {
54+
return indexPath(for: SPDiffableItem(id: itemID))
55+
}
56+
57+
/**
58+
SPDiffable: Get cell specific type `T` by indetifier.
59+
*/
60+
public func cell<T: UITableViewCell>(_ type: T.Type, for itemID: SPDiffableItem.Identifier) -> T? {
61+
guard let indexPath = indexPath(for: itemID) else { return nil }
62+
guard let cell = self.tableView?.cellForRow(at: indexPath) as? T else { return nil }
63+
return cell
64+
}
65+
66+
// MARK: - Helpers
67+
68+
public func convertToSnapshot(_ sections: [SPDiffableSection]) -> SPDiffableSnapshot {
69+
var snapshot = SPDiffableSnapshot()
70+
snapshot.appendSections(sections)
71+
for section in sections {
72+
snapshot.appendItems(section.items, toSection: section)
73+
}
74+
return snapshot
75+
}
76+
}

0 commit comments

Comments
 (0)