Skip to content

Commit 4b62b13

Browse files
committed
Updated table and collections data sources.
1 parent e74d420 commit 4b62b13

File tree

40 files changed

+790
-1127
lines changed

40 files changed

+790
-1127
lines changed

Example Apps/iOS Example/Controllers/DiffableTableController.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ class DiffableTableController: SPDiffableTableController, SPDiffableTableMediato
3636
override func viewDidLoad() {
3737
super.viewDidLoad()
3838
diffableDataSource?.mediator = self
39-
setCellProviders(SPDiffableTableCellProvider.default, sections: content)
39+
configureDiffable(sections: content, cellProviders: SPDiffableTableDataSource.CellProvider.default)
4040
}
4141

4242
var content: [SPDiffableSection] {
4343
return []
4444
}
4545

4646
internal func updateContent(animated: Bool) {
47-
diffableDataSource?.apply(content, animated: animated)
47+
diffableDataSource?.set(content, animated: animated)
4848
}
4949
}

Example Apps/iOS Example/Controllers/RootController.swift

-2
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,6 @@ class RootController: DiffableTableController {
149149
didSet {
150150
print("Stepper value is: \(stepperValue)")
151151
updateContent(animated: true)
152-
guard let indexPath = diffableDataSource?.indexPath(for: stepperValueIdentifier), let cell = tableView.cellForRow(at: indexPath) else { return }
153-
cell.detailTextLabel?.text = "\(Int(stepperValue))"
154152
}
155153
}
156154

Example Apps/iOS Example/Controllers/SideBarController.swift

+2-3
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(SPDiffableCollectionCellProvider.sideBar, sections: content)
30+
configureDiffable(sections: content, cellProviders: SPDiffableCollectionDataSource.CellProvider.sideBar)
3131
}
3232

3333
enum Section: String {
@@ -47,10 +47,9 @@ class SidebarController: SPDiffableSideBarController {
4747
SPDiffableSideBarItem(title: "Browse", image: UIImage(systemName: "square.grid.2x2"), action: { _, _ in }),
4848
SPDiffableSideBarItem(title: "Radio", image: UIImage(systemName: "dot.radiowaves.left.and.right"), action: { _, _ in }),
4949
SPDiffableSideBarItem(title: "Search", image: UIImage(systemName: "magnifyingglass"), action: { _, _ in
50-
print("action call 2")
50+
5151
}),
5252
SPDiffableSideBarButton(title: "Button", image: UIImage(systemName: "plus.square.fill"), action: { [weak self] _, _ in
53-
print("action call")
5453
guard let self = self else { return }
5554
let controller = UIViewController()
5655
controller.view.backgroundColor = .systemBackground

Readme.md

-225
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,6 @@ 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)
20-
- [Usage](#usage)
21-
- [How it work](#usage)
22-
- [Set Content](#set-content)
23-
- [Get Content](#get-content)
24-
- [Wrapper](#wrapper)
25-
- [Sidebar](#sidebar)
2617
- [Russian Community](#russian-community)
2718

2819
## Installation
@@ -53,222 +44,6 @@ pod 'SPDiffable'
5344

5445
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`.
5546

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-
122-
## Usage
123-
124-
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.
125-
126-
For work with diffable need create model (inside project you found some ready-use models) and do cell provider, which convert data-model to `UITableViewCell` or `UICollectionViewCell`. Next example for table, but all methods and class names available for collections.
127-
128-
New model shoud extend from basic class `SPDiffableItem`:
129-
130-
```swift
131-
class LocationRowModel: SPDiffableItem {
132-
133-
// Add properties, which you need
134-
public var city: String
135-
public var adress: String?
136-
}
137-
```
138-
139-
Last step, create table controller class and extend of `SPDiffableTableController`. Create custom cell provider, it doing convert it data to table cell:
140-
141-
```swift
142-
class DiffableTableController: SPDiffableTableController {
143-
144-
override func viewDidLoad() {
145-
super.viewDidLoad()
146-
147-
// Register cell for usage it in table view
148-
tableView.register(LocationTableCell.self, forCellReuseIdentifier: "LocationTableCell")
149-
150-
// Cell provider for `LocationRowModel`
151-
let locationCellProvider = SPDiffableTableCellProvider() { (tableView, indexPath, model) -> UITableViewCell? in
152-
switch model {
153-
case let model as TableRowModel:
154-
let cell = tableView.dequeueReusableCell(withIdentifier: "LocationTableCell", for: indexPath) as! LocationTableCell
155-
cell.textLabel?.text = model.city
156-
cell.detailTextLabel?.text = model.adress
157-
return cell
158-
default:
159-
return nil
160-
}
161-
}
162-
163-
// Pass cell provider and content.
164-
// About content you can read in next section.
165-
setCellProviders([locationCellProvider], sections: content)
166-
}
167-
}
168-
```
169-
170-
Now ready model and convert it to views. Time to add content.
171-
172-
### Set Content
173-
174-
Now table support models and custom cell provider. You can apply diffable content with animation (or not).
175-
Create content:
176-
177-
```swift
178-
179-
var content: [SPDiffableSection] {
180-
let section = SPDiffableSection(
181-
id: "example section",
182-
header: SPDiffableTextHeaderFooter(text: "Header"),
183-
footer: SPDiffableTextHeaderFooter(text: "Footer"),
184-
items: [
185-
LocationRowModel(city: "Minsk", adress: "Frunze Pr., bld. 47, appt. 7"),
186-
LocationRowModel(city: "Shanghai", adress: "Ting Wei Gong Lu 9299long 168hao"),
187-
LocationRowModel(city: "London", adress: "94 Whitby Road")
188-
]
189-
)
190-
191-
let content = [section]
192-
return content
193-
}
194-
```
195-
196-
You can add more items or sections. Last step - apply:
197-
198-
```swift
199-
diffableDataSource?.apply(content, animating: true)
200-
```
201-
202-
Call this when you need update content. When you call `setCellProviders`, it set content by default without animation.
203-
That all. You can each time create new order or count cells and it automatically show with diffable animation.
204-
205-
#### Reload Content
206-
207-
If you need something like old function `.reloadData()` in collection and table, look at this method:
208-
209-
```swift
210-
// Reload fully content
211-
diffableDataSource.reload(content)
212-
213-
// Reload only specific items
214-
diffableDataSource.reload(items) // or for 1 item `reload(item)`
215-
```
216-
217-
Changes apply without animation and like deep reload.
218-
219-
### Get Content
220-
221-
You can get index path by item, item by id and many other funcs:
222-
223-
```swift
224-
func item(for indexPath: IndexPath) -> SPDiffableItem? {}
225-
func indexPath(for itemID: SPDiffableItem.Identifier) -> IndexPath? {}
226-
func sections() -> [SPDiffableSection] {}
227-
func section(for index: Int) -> SPDiffableSection? {}
228-
func cell<T: UITableViewCell>(_ type: T.Type, for itemID: SPDiffableItem.Identifier) -> T? {}
229-
```
230-
231-
### Wrapper
232-
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.
234-
235-
```swift
236-
let item = SPDiffableWrapperItem(id: "uniq-identifier", model: LocationRowModel(city: "Minsk"))
237-
```
238-
239-
### Sidebar
240-
241-
Create new controller and extend from `SPDiffableSideBarController`. Remember, it available only from iOS 14.
242-
243-
```swift
244-
class SidebarController: SPDiffableSideBarController {}
245-
```
246-
247-
In class available ready-use cell providers for menu item and header section. For get it shoud call:
248-
249-
```swift
250-
override func viewDidLoad() {
251-
super.viewDidLoad()
252-
setCellProviders(SPDiffableCollectionCellProvider.sideBar, sections: content)
253-
}
254-
```
255-
256-
Content it array of `SPDiffableSection`. For menu model need use model `SPDiffableSideBarItem` or `SPDiffableSideBarButton`. For header and footer will create `SPDiffableSideBarHeader` model.
257-
258-
```swift
259-
SPDiffableSection(
260-
id: Section.library.rawValue,
261-
header: SPDiffableSideBarHeader(text: "Library", accessories: [.outlineDisclosure()]),
262-
items: [
263-
SPDiffableSideBarItem(title: "Recently Added", image: UIImage(systemName: "clock"), action: { _ in }),
264-
SPDiffableSideBarItem(title: "Artists", image: UIImage(systemName: "music.mic"), action: { _ in }),
265-
SPDiffableSideBarItem(title: "Albums", image: UIImage(systemName: "rectangle.stack"), action: { _ in }),
266-
SPDiffableSideBarItem(title: "Songs", image: UIImage(systemName: "music.note"), action: { _ in }),
267-
SPDiffableSideBarButton(title: "Add New", image: UIImage(systemName: "plus.square.fill"), action: { _ in })
268-
]
269-
)
270-
```
271-
27247
## Russian Community
27348

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

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 = '3.0.0'
4+
s.version = '4.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 }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
open class AppleCollectionDiffableDataSource: UICollectionViewDiffableDataSource<SPDiffableSection, SPDiffableItem> {
26+
27+
// MARK: - Init
28+
29+
public init(
30+
collectionView: UICollectionView,
31+
cellProvider: @escaping CellProvider,
32+
headerFooterProvider: SupplementaryViewProvider?
33+
) {
34+
super.init(collectionView: collectionView, cellProvider: cellProvider)
35+
self.supplementaryViewProvider = supplementaryViewProvider
36+
}
37+
38+
// MARK: - Wrappers
39+
40+
typealias Snapshot = NSDiffableDataSourceSnapshot<SPDiffableSection, SPDiffableItem>
41+
42+
@available(iOS 14.0, *)
43+
typealias SectionSnapshot = NSDiffableDataSourceSectionSnapshot<SPDiffableItem>
44+
}

0 commit comments

Comments
 (0)