Skip to content

Commit 5135a00

Browse files
committed
Moved delegates to data source object. Updated providers models and naming.
1 parent 15827b7 commit 5135a00

18 files changed

+317
-204
lines changed

Readme.md

Lines changed: 73 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,15 @@ Apple's diffable API requerid models for each object type. If you want use it in
1414
- [Swift Package Manager](#swift-package-manager)
1515
- [CocoaPods](#cocoapods)
1616
- [Manually](#manually)
17+
- [Ready Use](#ready-use)
18+
- [Example](#ready-use)
19+
- [Available Classes](#available-classes)
1720
- [Usage](#usage)
1821
- [How it work](#usage)
1922
- [Set Content](#set-content)
2023
- [Get Content](#get-content)
21-
- [Mediator](#mediator)
22-
- [Diffable Delegate](#diffable-delegate)
23-
- [Sidebar](#sidebar)
24-
- [Ready Use](#ready-use)
25-
- [Example](#ready-use)
26-
- [List classes](#ready-use-classes)
2724
- [Wrapper](#wrapper)
25+
- [Sidebar](#sidebar)
2826
- [Russian Community](#russian-community)
2927

3028
## Installation
@@ -55,6 +53,72 @@ pod 'SPDiffable'
5553

5654
If you prefer not to use any of dependency managers, you can integrate manually. Put `Sources/SPDiffable` folder in your Xcode project. Make sure to enable `Copy items if needed` and `Create groups`.
5755

56+
## Ready Use
57+
58+
For example you need simple table with native cells. You need create content with `SPDiffableTableRow`:
59+
60+
```swift
61+
let section = SPDiffableSection(
62+
id: "example section",
63+
header: SPDiffableTextHeaderFooter(text: "Header"),
64+
footer: SPDiffableTextHeaderFooter(text: "Footer"),
65+
items: [
66+
SPDiffableTableRow(text: "First Cell", accessoryType: .disclosureIndicator, selectionStyle: .default, action: { [weak self] indexPath in
67+
guard let self = self else { return }
68+
self.tableView.deselectRow(at: indexPath, animated: true)
69+
}),
70+
SPDiffableTableRow(text: "Second Cell", accessoryType: .disclosureIndicator, selectionStyle: .default, action: { [weak self] indexPath in
71+
guard let self = self else { return }
72+
self.tableView.deselectRow(at: indexPath, animated: true)
73+
}),
74+
]
75+
)
76+
```
77+
78+
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.default`.
79+
80+
```swift
81+
setCellProviders(SPDiffableTableCellProvider.default, sections: [section])
82+
```
83+
84+
Now project's models automatically converting to cell. No need any additional work. That all code.
85+
If you use custom table view or table controller, don't forget register cells classes. For `SPDiffableTableController` all cells already registered.
86+
87+
## Available Classes
88+
89+
It list models which you can use now, it shoud close your task without code. Of couse you can create your models.
90+
Now in project you can find this ready-use models:
91+
92+
- `SPDiffableItem` it basic class. All item models shoud be extend from it model. Header and footer also.
93+
- `SPDiffableSection` section class. Included footer and header properties, also items (cells).
94+
- `SPDiffableTextHeaderFooter` header or footer class with text.
95+
96+
#### For Table:
97+
98+
Here provided models:
99+
100+
- `SPDiffableTableRow` it native item for table cell. Support all basic styles and action for tap event.
101+
- `SPDiffableTableRowSubtitle` it native item for table cell with subtitle. Support all as before.
102+
- `SPDiffableTableRowButton` item for table in style as button. Support table styles and action for tap.
103+
- `SPDiffableTableRowStepper` item for table cell with stepper. Has maximum value and minimum, also incuded action with passed value.
104+
- `SPDiffableTableRowSwitch` item for table with switch, included default state and action for change event.
105+
106+
Here provided cells:
107+
108+
- `SPDiffableTableViewCell` basic table cell with detail text right side.
109+
- `SPDiffableSubtitleTableViewCell` basic table cell with two lines of texts.
110+
- `SPDiffableStepper` cell with stepper control. Using with `SPDiffableTableRowStepper` model.
111+
- `SPDiffableSwitch` cell with switch. Using with `SPDiffableTableRowSwitch` model.
112+
- `SPDiffableSlider` cell with slider.
113+
114+
#### For Collection:
115+
116+
Provided only models, becouse for most items using list registration and no need specific cell class.
117+
118+
- `SPDiffableSideBarItem` menu item in side bar. Support accessories and actions.
119+
- `SPDiffableSideBarButton` button item in side bar. Color of title similar to tint.
120+
- `SPDiffableSideBarHeader` header model for side bar item.
121+
58122
## Usage
59123

60124
Before read it, highly recomded check `Example` target in project. It examle show all features, like use stepper and switch, like process actions, create custom models and many other. Also you can skip full undestand logic and read [Ready-use section](https://github.com/ivanvorobei/SPDiffable#ready-use) with minimum of code for start.
@@ -148,7 +212,6 @@ diffableDataSource.reload(content)
148212

149213
// Reload only specific items
150214
diffableDataSource.reload(items) // or for 1 item `reload(item)`
151-
152215
```
153216

154217
Changes apply without animation and like deep reload.
@@ -165,51 +228,12 @@ func section(for index: Int) -> SPDiffableSection? {}
165228
func cell<T: UITableViewCell>(_ type: T.Type, for itemID: SPDiffableItem.Identifier) -> T? {}
166229
```
167230

168-
### Mediator
169-
170-
Some methods in diffable data source can't ovveride without custom data source. It solved with mediator delegate. It simple. Next example for table. Set delegate `SPTableDiffableMediator`, all method optional:
171-
172-
```swift
173-
class DiffableTableController: SPDiffableTableController, SPTableDiffableMediator {
174-
175-
override func viewDidLoad() {
176-
super.viewDidLoad()
177-
setCellProviders([cellProvider], sections: content)
178-
diffableDataSource?.mediator = self
179-
}
180-
}
181-
```
182-
183-
Now you can implemented requerid methods, for example title of header:
231+
### Wrapper
184232

185-
```swift
186-
func diffableTableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
187-
return "Overridden in of diffable mediator"
188-
}
189-
```
190-
191-
In protocol you can find more methods, like `canEdit` and other.
192-
193-
### Diffable Delegate
194-
195-
For handle some useful extensions, you can use delegates `SPDiffableTableDelegate`, `SPDiffableCollectionDelegate`. For example, when you need get which model did select, use this:
233+
In project you can find class `SPDiffableWrapperItem`. Using it, when you don't want create custom item model for you diffable struct. You can pass any your model and uwrap it later in cell provider.
196234

197235
```swift
198-
class DiffableTableController: SPDiffableTableController, SPDiffableTableDelegate {
199-
200-
override func viewDidLoad() {
201-
super.viewDidLoad()
202-
203-
// Hidded code for cell providers and content
204-
205-
setCellProviders([cellProvider], sections: content)
206-
diffableDelegate = self
207-
}
208-
209-
func diffableTableView(_ tableView: UITableView, didSelectItem item: SPDiffableItem) {
210-
// Here you get model, which did select.
211-
}
212-
}
236+
let item = SPDiffableWrapperItem(id: "uniq-identifier", model: LocationRowModel(city: "Minsk"))
213237
```
214238

215239
### Sidebar
@@ -245,80 +269,6 @@ SPDiffableSection(
245269
)
246270
```
247271

248-
## Ready Use
249-
250-
You can save time and count lines of code using ready-use classes. In project available models and views. For example you need simple table with native cells. You need create content with `SPDiffableTableRow`:
251-
252-
```swift
253-
let section = SPDiffableSection(
254-
id: "example section",
255-
header: SPDiffableTextHeaderFooter(text: "Header"),
256-
footer: SPDiffableTextHeaderFooter(text: "Footer"),
257-
items: [
258-
SPDiffableTableRow(text: "First Cell", accessoryType: .disclosureIndicator, selectionStyle: .default, action: { [weak self] indexPath in
259-
guard let self = self else { return }
260-
self.tableView.deselectRow(at: indexPath, animated: true)
261-
}),
262-
SPDiffableTableRow(text: "Second Cell", accessoryType: .disclosureIndicator, selectionStyle: .default, action: { [weak self] indexPath in
263-
guard let self = self else { return }
264-
self.tableView.deselectRow(at: indexPath, animated: true)
265-
}),
266-
]
267-
)
268-
```
269-
270-
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`.
271-
272-
```swift
273-
setCellProviders(SPDiffableTableCellProvider.default, sections: [section])
274-
```
275-
276-
Now project's models automatically converting to cell. No need any additional work. That all code.
277-
If you use custom table view or table controller, don't forget register cells classes. For `SPDiffableTableController` all cells already registered.
278-
279-
## Ready-use classes
280-
281-
It list models which you can use now, it shoud close your task without code. Of couse you can create your models.
282-
Now in project you can find this ready-use models:
283-
284-
- `SPDiffableItem` it basic class. All item models shoud be extend from it model. Header and footer also.
285-
- `SPDiffableSection` section class. Included footer and header properties, also items (cells).
286-
- `SPDiffableTextHeaderFooter` header or footer class with text.
287-
288-
#### For Table:
289-
290-
Here provided models:
291-
292-
- `SPDiffableTableRow` it native item for table cell. Support all basic styles and action for tap event.
293-
- `SPDiffableTableRowSubtitle` it native item for table cell with subtitle. Support all as before.
294-
- `SPDiffableTableRowButton` item for table in style as button. Support table styles and action for tap.
295-
- `SPDiffableTableRowStepper` item for table cell with stepper. Has maximum value and minimum, also incuded action with passed value.
296-
- `SPDiffableTableRowSwitch` item for table with switch, included default state and action for change event.
297-
298-
Here provided cells:
299-
300-
- `SPDiffableTableViewCell` basic table cell with detail text right side.
301-
- `SPDiffableSubtitleTableViewCell` basic table cell with two lines of texts.
302-
- `SPDiffableStepper` cell with stepper control. Using with `SPDiffableTableRowStepper` model.
303-
- `SPDiffableSwitch` cell with switch. Using with `SPDiffableTableRowSwitch` model.
304-
- `SPDiffableSlider` cell with slider.
305-
306-
#### For Collection:
307-
308-
Provided only models, becouse for most items using list registration and no need specific cell class.
309-
310-
- `SPDiffableSideBarItem` menu item in side bar. Support accessories and actions.
311-
- `SPDiffableSideBarButton` button item in side bar. Color of title similar to tint.
312-
- `SPDiffableSideBarHeader` header model for side bar item.
313-
314-
## Wrapper
315-
316-
In project you can find class `SPDiffableWrapperItem`. Using it, when you don't want create custom item model for you diffable struct. You can pass any your model and uwrap it later in cell provider.
317-
318-
```swift
319-
let item = SPDiffableWrapperItem(id: "uniq-identifier", model: LocationRowModel(city: "Minsk"))
320-
```
321-
322272
## Russian Community
323273

324274
Я веду [телеграм-канал](https://sparrowcode.by/telegram), там публикую новости и туториалы.<br>

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 = '2.2.0'
4+
s.version = '3.0.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/DataSource/SPDiffableCollectionDataSource+Set.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ extension SPDiffableCollectionDataSource {
158158

159159
/**
160160
SPDiffable: Update layout.
161-
161+
162162
- parameter animating: Shoud update layout with animation or not.
163163
- parameter completion: A closure to execute when the updating complete.
164164
*/
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// The MIT License (MIT)
2+
// Copyright © 2020 Ivan Vorobei ([email protected])
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: UICollectionViewDelegate {
26+
27+
// Process actions after tap.
28+
open func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
29+
guard let item = item(for: indexPath) else { return }
30+
diffableDelegate?.diffableCollectionView?(collectionView, didSelectItem: item, indexPath: indexPath)
31+
switch item {
32+
case let model as SPDiffableActionableItem:
33+
model.action?(model, indexPath)
34+
default:
35+
break
36+
}
37+
}
38+
}

Sources/SPDiffable/Collection/DataSource/SPDiffableCollectionDataSource.swift

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,22 @@ open class SPDiffableCollectionDataSource: UICollectionViewDiffableDataSource<SP
3737
*/
3838
public let headerAsFirstCell: Bool
3939

40+
/**
41+
SPDiffable: Mirror of `UICollectionViewDelegate`.
42+
*/
43+
open weak var diffableDelegate: SPDiffableCollectionDelegate?
44+
4045
// Using for get cells or update its.
4146
internal weak var collectionView: UICollectionView?
4247

4348
// MARK: - Init
4449

45-
public init(collectionView: UICollectionView, cellProviders: [SPDiffableCollectionCellProvider], supplementaryViewProviders: [SupplementaryViewProvider] = [], headerAsFirstCell: Bool = true) {
46-
50+
public init(
51+
collectionView: UICollectionView,
52+
cellProviders: [SPDiffableCollectionCellProvider],
53+
headerFooterProviders: [SPDiffableCollectionHeaderFooterProvider] = [],
54+
headerAsFirstCell: Bool = true
55+
) {
4756
self.headerAsFirstCell = headerAsFirstCell
4857
self.collectionView = collectionView
4958

@@ -55,22 +64,18 @@ open class SPDiffableCollectionDataSource: UICollectionViewDiffableDataSource<SP
5564
}
5665
return nil
5766
}
58-
59-
if !supplementaryViewProviders.isEmpty {
67+
68+
if !headerFooterProviders.isEmpty {
6069
supplementaryViewProvider = { (collectionView, kind, indexPath) -> UICollectionReusableView? in
61-
for provider in supplementaryViewProviders {
62-
if let view = provider(collectionView, kind, indexPath) {
70+
for provider in headerFooterProviders {
71+
if let view = provider.clouser(collectionView, kind, indexPath) {
6372
return view
6473
}
6574
}
6675
return nil
6776
}
6877
}
78+
79+
collectionView.delegate = self
6980
}
7081
}
71-
72-
/**
73-
SPDiffable: Wrapper of collection supplementary view provider.
74-
*/
75-
@available(iOS 13.0, *)
76-
public typealias SPDiffableCollectionSupplementaryViewProvider = SPDiffableCollectionDataSource.SupplementaryViewProvider

Sources/SPDiffable/Collection/Protocols/SPDiffableCollectionDelegate.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import UIKit
2323

2424
/**
25-
SPDiffable: Protocol with collection events.
25+
SPDiffable: Protocol for mirrir `UICollectionViewDelegate`.
2626
*/
2727
@available(iOS 13.0, *)
2828
@objc public protocol SPDiffableCollectionDelegate: AnyObject {

Sources/SPDiffable/Collection/SPDiffableCollectionCellProvider.swift renamed to Sources/SPDiffable/Collection/Providers/SPDiffableCollectionCellProvider.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@
2121

2222
import UIKit
2323

24-
import UIKit
25-
2624
/**
27-
SPDiffable: Wrapper of collection cell provider.
25+
SPDiffable: Cell Provider.
2826
*/
2927
@available(iOS 13.0, *)
3028
open class SPDiffableCollectionCellProvider {

0 commit comments

Comments
 (0)