Skip to content

Commit 3aa0c4e

Browse files
committed
Updated provider syntax.
1 parent 3e6ab67 commit 3aa0c4e

File tree

10 files changed

+71
-49
lines changed

10 files changed

+71
-49
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Bucket
3+
uuid = "3BC838C7-4F4D-4BCA-9D2A-159B4E874E76"
4+
type = "1"
5+
version = "2.0">
6+
</Bucket>

Example Apps/iOS Example/Controllers/DiffableTableController.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class DiffableTableController: SPDiffableTableController, SPDiffableTableMediato
3636
override func viewDidLoad() {
3737
super.viewDidLoad()
3838
diffableDataSource?.mediator = self
39-
setCellProviders(SPDiffableTableCellProviders.default, sections: content)
39+
setCellProviders([.default], sections: content)
4040
}
4141

4242
var content: [SPDiffableSection] {

Example Apps/iOS Example/Controllers/SideBarController.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class SidebarController: SPDiffableSideBarController {
2727

2828
override func viewDidLoad() {
2929
super.viewDidLoad()
30-
setCellProviders(SPDiffableCollectionCellProviders.sideBar, sections: content)
30+
setCellProviders([.sideBar], sections: content)
3131
}
3232

3333
enum Section: String {

Readme.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class DiffableTableController: SPDiffableTableController {
8484
tableView.register(LocationTableCell.self, forCellReuseIdentifier: "LocationTableCell")
8585

8686
// Cell provider for `LocationRowModel`
87-
let locationCellProvider: SPDiffableTableCellProvider = { (tableView, indexPath, model) -> UITableViewCell? in
87+
let locationCellProvider = SPDiffableTableCellProvider() { (tableView, indexPath, model) -> UITableViewCell? in
8888
switch model {
8989
case let model as TableRowModel:
9090
let cell = tableView.dequeueReusableCell(withIdentifier: "LocationTableCell", for: indexPath) as! LocationTableCell
@@ -270,7 +270,7 @@ let section = SPDiffableSection(
270270
You init cell model and pass action, choose selection style and other. As you see, model describe native table cell. Next, you need set cell provider, but it also already available, for get it call `SPDiffableTableController.defaultCellProvider`.
271271

272272
```swift
273-
setCellProviders(SPDiffableTableCellProviders.default, sections: [section])
273+
setCellProviders([.default], sections: [section])
274274
```
275275

276276
Now project's models automatically converting to cell. No need any additional work. That all code.

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.0.0'
4+
s.version = '2.1.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 }

Sources/SPDiffable/Collection/SPDiffableCollectionCellProviders.swift Sources/SPDiffable/Collection/SPDiffableCollectionCellProvider.swift

+28-9
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,37 @@
2121

2222
import UIKit
2323

24-
public enum SPDiffableCollectionCellProviders {
24+
import UIKit
25+
26+
/**
27+
SPDiffable: Wrapper of collection cell provider.
28+
*/
29+
@available(iOS 13.0, *)
30+
open class SPDiffableCollectionCellProvider {
31+
32+
open var clouser: SPDiffableCollectionDataSource.CellProvider
33+
34+
public init(clouser: @escaping SPDiffableCollectionDataSource.CellProvider) {
35+
self.clouser = clouser
36+
}
37+
38+
// MARK: - Ready Use
2539

2640
/**
2741
SPDiffable: Defaults cell provider, which can help you doing side bar faster.
2842
You can do your providers and ise its with more flexible.
2943
*/
3044
@available(iOS 14, *)
31-
public static var sideBar: [SPDiffableCollectionCellProvider] {
32-
return [sideBarItem, sideBarButton, sideBarHeader]
45+
public static var sideBar: SPDiffableCollectionCellProvider {
46+
return SPDiffableCollectionCellProvider() { (collectionView, indexPath, item) -> UICollectionViewCell? in
47+
let providers = [sideBarItem, sideBarButton, sideBarHeader]
48+
for provider in providers {
49+
if let cell = provider.clouser(collectionView, indexPath, item) {
50+
return cell
51+
}
52+
}
53+
return nil
54+
}
3355
}
3456

3557
@available(iOS 14, *)
@@ -41,11 +63,10 @@ public enum SPDiffableCollectionCellProviders {
4163
cell.contentConfiguration = content
4264
cell.accessories = item.accessories
4365
}
44-
let cellProvider: SPDiffableCollectionCellProvider = { (collectionView, indexPath, item) -> UICollectionViewCell? in
66+
return SPDiffableCollectionCellProvider() { (collectionView, indexPath, item) -> UICollectionViewCell? in
4567
guard let item = item as? SPDiffableSideBarItem else { return nil }
4668
return collectionView.dequeueConfiguredReusableCell(using: cellRegistration, for: indexPath, item: item)
4769
}
48-
return cellProvider
4970
}
5071

5172
@available(iOS 14, *)
@@ -54,11 +75,10 @@ public enum SPDiffableCollectionCellProviders {
5475
cell.updateWithItem(item)
5576
cell.accessories = item.accessories
5677
}
57-
let cellProvider: SPDiffableCollectionCellProvider = { (collectionView, indexPath, item) -> UICollectionViewCell? in
78+
return SPDiffableCollectionCellProvider() { (collectionView, indexPath, item) -> UICollectionViewCell? in
5879
guard let item = item as? SPDiffableSideBarButton else { return nil }
5980
return collectionView.dequeueConfiguredReusableCell(using: cellRegistration, for: indexPath, item: item)
6081
}
61-
return cellProvider
6282
}
6383

6484
@available(iOS 14, *)
@@ -70,10 +90,9 @@ public enum SPDiffableCollectionCellProviders {
7090
cell.contentConfiguration = content
7191
cell.accessories = item.accessories
7292
}
73-
let cellProvider: SPDiffableCollectionCellProvider = { (collectionView, indexPath, item) -> UICollectionViewCell? in
93+
return SPDiffableCollectionCellProvider() { (collectionView, indexPath, item) -> UICollectionViewCell? in
7494
guard let item = item as? SPDiffableSideBarHeader else { return nil }
7595
return collectionView.dequeueConfiguredReusableCell(using: cellRegistration, for: indexPath, item: item)
7696
}
77-
return cellProvider
7897
}
7998
}

Sources/SPDiffable/Collection/SPDiffableCollectionDataSource.swift

+2-8
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ open class SPDiffableCollectionDataSource: UICollectionViewDiffableDataSource<SP
4141

4242
// MARK: - Init
4343

44-
public init(collectionView: UICollectionView, cellProviders: [CellProvider], supplementaryViewProviders: [SupplementaryViewProvider] = [], headerAsFirstCell: Bool = true) {
44+
public init(collectionView: UICollectionView, cellProviders: [SPDiffableCollectionCellProvider], supplementaryViewProviders: [SupplementaryViewProvider] = [], headerAsFirstCell: Bool = true) {
4545

4646
self.headerAsFirstCell = headerAsFirstCell
4747
self.collectionView = collectionView
4848

4949
super.init(collectionView: collectionView) { (collectionView, indexPath, item) -> UICollectionViewCell? in
5050
for provider in cellProviders {
51-
if let cell = provider(collectionView, indexPath, item) {
51+
if let cell = provider.clouser(collectionView, indexPath, item) {
5252
return cell
5353
}
5454
}
@@ -252,12 +252,6 @@ open class SPDiffableCollectionDataSource: UICollectionViewDiffableDataSource<SP
252252
}
253253
}
254254

255-
/**
256-
SPDiffable: Wrapper of collection cell provider.
257-
*/
258-
@available(iOS 13.0, *)
259-
public typealias SPDiffableCollectionCellProvider = SPDiffableCollectionDataSource.CellProvider
260-
261255
/**
262256
SPDiffable: Wrapper of collection supplementary view provider.
263257
*/

Sources/SPDiffable/Table/SPDiffableTableCellProviders.swift Sources/SPDiffable/Table/SPDiffableTableCellProvider.swift

+27-19
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,34 @@
2121

2222
import UIKit
2323

24+
/**
25+
SPDiffable: Wrapper of table cell provider.
26+
*/
2427
@available(iOS 13.0, *)
25-
public enum SPDiffableTableCellProviders {
28+
open class SPDiffableTableCellProvider {
2629

27-
/**
28-
SPDiffable: Return cell providers, which process for project models cells.
29-
No need additional configure.
30-
31-
For change style of cells requerid register new cell provider.
32-
*/
33-
public static var `default`: [SPDiffableTableCellProvider] {
34-
return [rowDetail, rowSubtitle, `switch`, stepper, customisable]
30+
open var clouser: SPDiffableTableDataSource.CellProvider
31+
32+
public init(clouser: @escaping SPDiffableTableDataSource.CellProvider) {
33+
self.clouser = clouser
34+
}
35+
36+
// MARK: - Ready Use
37+
38+
public static var `default`: SPDiffableTableCellProvider {
39+
return SPDiffableTableCellProvider() { (tableView, indexPath, item) -> UITableViewCell? in
40+
let providers = [rowDetail, rowSubtitle, `switch`, stepper, customisable]
41+
for provider in providers {
42+
if let cell = provider.clouser(tableView, indexPath, item) {
43+
return cell
44+
}
45+
}
46+
return nil
47+
}
3548
}
3649

3750
public static var rowDetail: SPDiffableTableCellProvider {
38-
let cellProvider: SPDiffableTableCellProvider = { (tableView, indexPath, item) -> UITableViewCell? in
51+
return SPDiffableTableCellProvider() { (tableView, indexPath, item) -> UITableViewCell? in
3952
guard let item = item as? SPDiffableTableRow else { return nil }
4053
let cell = tableView.dequeueReusableCell(withIdentifier: SPDiffableTableViewCell.reuseIdentifier, for: indexPath) as! SPDiffableTableViewCell
4154
cell.textLabel?.text = item.text
@@ -45,11 +58,10 @@ public enum SPDiffableTableCellProviders {
4558
cell.selectionStyle = item.selectionStyle
4659
return cell
4760
}
48-
return cellProvider
4961
}
5062

5163
public static var rowSubtitle: SPDiffableTableCellProvider {
52-
let cellProvider: SPDiffableTableCellProvider = { (tableView, indexPath, item) -> UITableViewCell? in
64+
return SPDiffableTableCellProvider() { (tableView, indexPath, item) -> UITableViewCell? in
5365
guard let item = item as? SPDiffableTableRowSubtitle else { return nil }
5466
let cell = tableView.dequeueReusableCell(withIdentifier: SPDiffableSubtitleTableViewCell.reuseIdentifier, for: indexPath) as! SPDiffableSubtitleTableViewCell
5567
cell.textLabel?.text = item.text
@@ -59,11 +71,10 @@ public enum SPDiffableTableCellProviders {
5971
cell.selectionStyle = item.selectionStyle
6072
return cell
6173
}
62-
return cellProvider
6374
}
6475

6576
public static var `switch`: SPDiffableTableCellProvider {
66-
let cellProvider: SPDiffableTableCellProvider = { (tableView, indexPath, item) -> UITableViewCell? in
77+
return SPDiffableTableCellProvider() { (tableView, indexPath, item) -> UITableViewCell? in
6778
guard let item = item as? SPDiffableTableRowSwitch else { return nil }
6879
let cell = tableView.dequeueReusableCell(withIdentifier: SPDiffableTableViewCell.reuseIdentifier, for: indexPath) as! SPDiffableTableViewCell
6980
cell.textLabel?.text = item.text
@@ -74,11 +85,10 @@ public enum SPDiffableTableCellProviders {
7485
cell.selectionStyle = .none
7586
return cell
7687
}
77-
return cellProvider
7888
}
7989

8090
public static var stepper: SPDiffableTableCellProvider {
81-
let cellProvider: SPDiffableTableCellProvider = { (tableView, indexPath, item) -> UITableViewCell? in
91+
return SPDiffableTableCellProvider() { (tableView, indexPath, item) -> UITableViewCell? in
8292
guard let item = item as? SPDiffableTableRowStepper else { return nil }
8393
let cell = tableView.dequeueReusableCell(withIdentifier: SPDiffableTableViewCell.reuseIdentifier, for: indexPath) as! SPDiffableTableViewCell
8494
cell.textLabel?.text = item.text
@@ -92,11 +102,10 @@ public enum SPDiffableTableCellProviders {
92102
cell.selectionStyle = .none
93103
return cell
94104
}
95-
return cellProvider
96105
}
97106

98107
public static var customisable: SPDiffableTableCellProvider {
99-
let cellProvider: SPDiffableTableCellProvider = { (tableView, indexPath, item) -> UITableViewCell? in
108+
return SPDiffableTableCellProvider() { (tableView, indexPath, item) -> UITableViewCell? in
100109
guard let item = item as? SPDiffableCustomTableRow else { return nil }
101110
let cell = tableView.dequeueReusableCell(withIdentifier: SPDiffableCustomTableViewCell.reuseIdentifier, for: indexPath) as! SPDiffableCustomTableViewCell
102111
cell.textLabel?.text = item.text
@@ -108,6 +117,5 @@ public enum SPDiffableTableCellProviders {
108117
cell.higlightStyle = item.higlightStyle
109118
return cell
110119
}
111-
return cellProvider
112120
}
113121
}

Sources/SPDiffable/Table/SPDiffableTableDataSource.swift

+3-8
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,19 @@ open class SPDiffableTableDataSource: UITableViewDiffableDataSource<SPDiffableSe
4343

4444
// MARK: - Init
4545

46-
public init(tableView: UITableView, cellProviders: [CellProvider]) {
46+
public init(tableView: UITableView, cellProviders: [SPDiffableTableCellProvider]) {
4747
self.tableView = tableView
4848
super.init(tableView: tableView, cellProvider: { (tableView, indexPath, item) -> UITableViewCell? in
4949
for provider in cellProviders {
50-
if let cell = provider(tableView, indexPath, item) {
50+
if let cell = provider.clouser(tableView, indexPath, item) {
5151
return cell
5252
}
5353
}
5454
return nil
5555
})
5656
}
5757

58+
5859
// MARK: - Apply Content
5960

6061
/**
@@ -236,9 +237,3 @@ open class SPDiffableTableDataSource: UITableViewDiffableDataSource<SPDiffableSe
236237
mediator?.diffableTableView?(tableView, moveRowAt: sourceIndexPath, to: destinationIndexPath)
237238
}
238239
}
239-
240-
/**
241-
SPDiffable: Wrapper of table cell provider.
242-
*/
243-
@available(iOS 13.0, *)
244-
public typealias SPDiffableTableCellProvider = SPDiffableTableDataSource.CellProvider

0 commit comments

Comments
 (0)