Skip to content

Commit 867f1d9

Browse files
committed
Updated docs and protector lever for some properties. Added SPDiffableCollectionActionableItem.
1 parent fbf413d commit 867f1d9

32 files changed

+279
-159
lines changed

Example iOS/Controllers/DiffableTableController.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
// SOFTWARE.
2121

2222
import UIKit
23+
import SPDiffable
2324

2425
@available(iOS 13.0, *)
2526
class DiffableTableController: SPDiffableTableController, SPDiffableTableMediator {

Example iOS/Controllers/RootController.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
// SOFTWARE.
2121

2222
import UIKit
23+
import SPDiffable
2324

2425
@available(iOS 13.0, *)
2526
class RootController: DiffableTableController {
@@ -69,13 +70,15 @@ class RootController: DiffableTableController {
6970
header: SPDiffableTextHeaderFooter(text: "Presenter"),
7071
footer: SPDiffableTextHeaderFooter(text: "Push in navigation processing by table controller. Sometimes you need manually deselect cell."),
7172
items: [
72-
SPDiffableTableRow(text: "Basic Deselect", accessoryType: .disclosureIndicator, selectionStyle: .default, action: { [weak self] indexPath in
73+
SPDiffableTableRow(text: "Deselect", accessoryType: .disclosureIndicator, selectionStyle: .default, action: { [weak self] indexPath in
7374
guard let self = self else { return }
7475
self.tableView.deselectRow(at: indexPath, animated: true)
7576
}),
76-
SPDiffableTableRow(text: "Basic Push", accessoryType: .disclosureIndicator, selectionStyle: .default, action: { [weak self] indexPath in
77+
SPDiffableTableRow(text: "Push", accessoryType: .disclosureIndicator, selectionStyle: .default, action: { [weak self] indexPath in
7778
guard let self = self else { return }
78-
self.tableView.deselectRow(at: indexPath, animated: true)
79+
let controller = UIViewController()
80+
controller.view.backgroundColor = .secondarySystemGroupedBackground
81+
self.navigationController?.pushViewController(controller, animated: true)
7982
})
8083
]
8184
)

Example iOS/Controllers/SideBarController.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@
2020
// SOFTWARE.
2121

2222
import UIKit
23+
import SPDiffable
2324

2425
@available(iOS 14, *)
2526
class SidebarController: SPDiffableSideBarController {
2627

2728
override func viewDidLoad() {
2829
super.viewDidLoad()
29-
setCellProviders([SPDiffableCollectionCellProviders.sideBar], sections: content)
30+
setCellProviders(SPDiffableCollectionCellProviders.sideBar, sections: content)
3031
}
3132

3233
enum Section: String {

Readme.md

Lines changed: 71 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ If you like the project, don't forget to `put star ★` and follow me on GitHub:
1717
- [How it work](#usage)
1818
- [Apply content](#apply-content)
1919
- [Mediator](#mediator)
20+
- [Diffable Delegate](#diffable-delegate)
2021
- [Sidebar](#sidebar)
2122
- [Ready Use](#ready-use)
2223
- [Example](#ready-use)
@@ -62,15 +63,10 @@ For work with diffable need create model (inside project you found some ready-us
6263

6364
New model shoud extend from basic class `SPDiffableItem`:
6465

65-
```swift
66-
class LocationRowModel: SPDiffableItem {}
67-
```
68-
69-
After it add properties, which you want use. For example:
70-
7166
```swift
7267
class LocationRowModel: SPDiffableItem {
7368

69+
// Add properties, which you need
7470
public var city: String
7571
public var adress: String?
7672
}
@@ -79,59 +75,67 @@ class LocationRowModel: SPDiffableItem {
7975
Last step, create table controller class and extend of `SPDiffableTableController`. Create custom cell provider, it doing convert it data to table cell:
8076

8177
```swift
78+
class DiffableTableController: SPDiffableTableController {
8279

83-
override func viewDidLoad() {
84-
super.viewDidLoad()
85-
86-
// Register cell for usage it in table view
87-
tableView.register(LocationTableCell.self, forCellReuseIdentifier: "LocationTableCell")
88-
89-
// Cell provider for `LocationRowModel`
90-
let locationCellProvider: SPDiffableTableCellProvider = { (tableView, indexPath, model) -> UITableViewCell? in
91-
switch model {
92-
case let model as TableRowModel:
93-
let cell = tableView.dequeueReusableCell(withIdentifier: "LocationTableCell", for: indexPath) as! LocationTableCell
94-
cell.textLabel?.text = model.city
95-
cell.detailTextLabel?.text = model.adress
96-
return cell
97-
default:
98-
return nil
80+
override func viewDidLoad() {
81+
super.viewDidLoad()
82+
83+
// Register cell for usage it in table view
84+
tableView.register(LocationTableCell.self, forCellReuseIdentifier: "LocationTableCell")
85+
86+
// Cell provider for `LocationRowModel`
87+
let locationCellProvider: SPDiffableTableCellProvider = { (tableView, indexPath, model) -> UITableViewCell? in
88+
switch model {
89+
case let model as TableRowModel:
90+
let cell = tableView.dequeueReusableCell(withIdentifier: "LocationTableCell", for: indexPath) as! LocationTableCell
91+
cell.textLabel?.text = model.city
92+
cell.detailTextLabel?.text = model.adress
93+
return cell
94+
default:
95+
return nil
96+
}
9997
}
98+
99+
// Pass cell provider and content.
100+
// About content you can read in next section.
101+
setCellProviders([locationCellProvider], sections: content)
100102
}
101-
102-
// Pass cell provider and content.
103-
// About content you can read next.
104-
setCellProviders([locationCellProvider], sections: content)
105103
}
106104
```
107-
In project available models for like `SPDiffableTableRow` and other with ready-use properties. Also you can use default cell provider if using project's models. For get it call `SPDiffableTableCellProviders.default`.
108105

109-
### Apply Content
106+
Now ready model and convert it to views. Time to add content. Read next section.
107+
108+
#### Apply Content
110109

111-
Now table support models and custom cell provider. We can apply diffable content with animation (or not).
112-
Create section class:
110+
Now table support models and custom cell provider. You can apply diffable content with animation (or not).
111+
Create content:
113112

114113
```swift
115-
let section = SPDiffableSection(
116-
identifier: "example section",
117-
header: SPDiffableTextHeaderFooter(text: "Header"),
118-
footer: SPDiffableTextHeaderFooter(text: "Footer"),
119-
items: [
120-
LocationRowModel(city: "Minsk", adress: "Frunze Pr., bld. 47, appt. 7"),
121-
LocationRowModel(city: "Shanghai", adress: "Ting Wei Gong Lu 9299long 168hao"),
122-
LocationRowModel(city: "London", adress: "94 Whitby Road")
123-
]
124-
)
125114

126-
let content = [section]
115+
var content: [SPDiffableSection] {
116+
let section = SPDiffableSection(
117+
identifier: "example section",
118+
header: SPDiffableTextHeaderFooter(text: "Header"),
119+
footer: SPDiffableTextHeaderFooter(text: "Footer"),
120+
items: [
121+
LocationRowModel(city: "Minsk", adress: "Frunze Pr., bld. 47, appt. 7"),
122+
LocationRowModel(city: "Shanghai", adress: "Ting Wei Gong Lu 9299long 168hao"),
123+
LocationRowModel(city: "London", adress: "94 Whitby Road")
124+
]
125+
)
126+
127+
let content = [section]
128+
return content
129+
}
127130
```
128131

129-
You can add more cells or sections. Last step - apply:
132+
You can add more items or sections. Last step - apply:
130133

131134
```swift
132135
diffableDataSource?.apply(sections: content, animating: true)
133136
```
134137

138+
Call this when you need update content. When you call `setCellProviders`, it set content by default without animation.
135139
That all. You can each time create new order or count cells and it automatically show with diffable animation.
136140

137141
### Mediator
@@ -159,6 +163,28 @@ func diffableTableView(_ tableView: UITableView, titleForHeaderInSection section
159163

160164
In protocol you can find more methods, like `canEdit` and other.
161165

166+
### Diffable Delegate
167+
168+
For handle some useful extensions, you can use delegates `SPDiffableTableDelegate`, `SPDiffableCollectionDelegate`. For example, when you need get which model did select, use this:
169+
170+
```swift
171+
class DiffableTableController: SPDiffableTableController, SPDiffableTableDelegate {
172+
173+
override func viewDidLoad() {
174+
super.viewDidLoad()
175+
176+
// Hidded code for cell providers and content
177+
178+
setCellProviders([cellProvider], sections: content)
179+
diffableDelegate = self
180+
}
181+
182+
func diffableTableView(_ tableView: UITableView, didSelectItem item: SPDiffableItem) {
183+
// Here you get model, which did select.
184+
}
185+
}
186+
```
187+
162188
### Sidebar
163189

164190
Create new controller and extend from `SPDiffableSideBarController`. Remember, it available only from iOS 14.
@@ -194,7 +220,7 @@ SPDiffableSection(
194220

195221
## Ready Use
196222

197-
You can save time and count 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`:
223+
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`:
198224

199225
```swift
200226
let section = SPDiffableSection(
@@ -221,18 +247,11 @@ setCellProviders(SPDiffableTableCellProviders.default, sections: [section])
221247
```
222248

223249
Now project's models automatically converting to cell. No need any additional work. That all code.
224-
225-
For update table shoud using `apply()` method:
226-
227-
```swift
228-
diffableDataSource?.apply(sections: [section], animating: true)
229-
```
230-
231250
If you use custom table view or table controller, don't forget register cells classes. For `SPDiffableTableController` all cells already registered.
232251

233252
## Ready-use classes
234253

235-
It models which you can use now, it shoud close your task without code. Of couse you can create your models.
254+
It list models which you can use now, it shoud close your task without code. Of couse you can create your models.
236255
Now in project you can find this ready-use models:
237256

238257
- `SPDiffableItem` it basic class. All item models shoud be extend from it model. Header and footer also.
@@ -252,6 +271,7 @@ Now in project you can find this ready-use models:
252271

253272
#### For Collection:
254273

274+
- `SPDiffableCollectionActionableItem` actionable item for collection view.
255275
- `SPDiffableSideBarItem` menu item in side bar. Support accessories and actions.
256276
- `SPDiffableSideBarButton` button item in side bar. Color of title similar to tint.
257277
- `SPDiffableSideBarHeader` header model for side bar item.

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 = '1.1.4'
4+
s.version = '1.1.5'
55
s.summary = 'Extenshion of Diffable API which allow not duplicate code and use less models.'
66
s.homepage = 'https://github.com/varabeis/SPDiffable'
77
s.source = { :git => 'https://github.com/varabeis/SPDiffable.git', :tag => s.version }

SPDiffable.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
F4920A6424F3BFFA007F7EE9 /* SPDiffableWrapperItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4920A6324F3BFFA007F7EE9 /* SPDiffableWrapperItem.swift */; };
1818
F4920A6924F447D6007F7EE9 /* SPDiffableTableCellProviders.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4920A6824F447D6007F7EE9 /* SPDiffableTableCellProviders.swift */; };
1919
F4920A6C24F44834007F7EE9 /* SPDiffableCollectionCellProviders.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4920A6B24F44834007F7EE9 /* SPDiffableCollectionCellProviders.swift */; };
20+
F4D77FAC260B2FFC004660B8 /* SPDiffableCollectionActionableItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4D77FAB260B2FFC004660B8 /* SPDiffableCollectionActionableItem.swift */; };
2021
F4FBB993260912A50036E67D /* SPDiffableCollectionDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4FBB98726090FFE0036E67D /* SPDiffableCollectionDelegate.swift */; };
2122
F4FBB995260912A70036E67D /* SPDiffableTableDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4FBB98B260910D40036E67D /* SPDiffableTableDelegate.swift */; };
2223
F4FBB99C260912E20036E67D /* SPDiffable.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = "SPDiffable::SPDiffable::Product" /* SPDiffable.framework */; platformFilter = ios; };
@@ -86,6 +87,7 @@
8687
F4977BC6252E079A00B20771 /* Example iOS.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = "Example iOS.entitlements"; sourceTree = "<group>"; };
8788
F4D6FB12256646FC0046A328 /* FUNDING.yml */ = {isa = PBXFileReference; lastKnownFileType = text.yaml; path = FUNDING.yml; sourceTree = "<group>"; };
8889
F4D6FB16256646FD0046A328 /* .gitignore */ = {isa = PBXFileReference; lastKnownFileType = text; path = .gitignore; sourceTree = "<group>"; };
90+
F4D77FAB260B2FFC004660B8 /* SPDiffableCollectionActionableItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SPDiffableCollectionActionableItem.swift; sourceTree = "<group>"; };
8991
F4FBB98726090FFE0036E67D /* SPDiffableCollectionDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SPDiffableCollectionDelegate.swift; sourceTree = "<group>"; };
9092
F4FBB98B260910D40036E67D /* SPDiffableTableDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SPDiffableTableDelegate.swift; sourceTree = "<group>"; };
9193
OBJ_10 /* SPDiffableCollectionController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SPDiffableCollectionController.swift; sourceTree = "<group>"; };
@@ -230,6 +232,7 @@
230232
OBJ_16 /* SPDiffableSideBarButton.swift */,
231233
OBJ_17 /* SPDiffableSideBarHeader.swift */,
232234
OBJ_18 /* SPDiffableSideBarItem.swift */,
235+
F4D77FAB260B2FFC004660B8 /* SPDiffableCollectionActionableItem.swift */,
233236
);
234237
path = Models;
235238
sourceTree = "<group>";
@@ -451,6 +454,7 @@
451454
OBJ_69 /* SPDiffableStepper.swift in Sources */,
452455
OBJ_70 /* SPDiffableSubtitleTableViewCell.swift in Sources */,
453456
OBJ_71 /* SPDiffableSwitch.swift in Sources */,
457+
F4D77FAC260B2FFC004660B8 /* SPDiffableCollectionActionableItem.swift in Sources */,
454458
F4FBB995260912A70036E67D /* SPDiffableTableDelegate.swift in Sources */,
455459
OBJ_72 /* SPDiffableTableViewCell.swift in Sources */,
456460
OBJ_73 /* SPDiffableTableController.swift in Sources */,

Sources/SPDiffable/CellProvider/Models/SPDiffableItem.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@
2222
import UIKit
2323

2424
/**
25-
Basic class for items and sections.
25+
SPDiffable: Basic class for items and sections.
2626

2727
All next model's classes shoud be extend from it class.
2828
*/
2929
open class SPDiffableItem: NSObject, NSCopying {
3030

3131
/**
32-
Identifier help for detect uniq cell and doing diffable work and animations.
32+
SPDiffable: Identifier help for detect uniq cell and doing diffable work and animations.
3333

3434
Always shoud be uniq. But if it changed, diffable system remove old and insert new (not reload).
3535
Identifier uses in `Hashable` and `Equatable` protocols.

Sources/SPDiffable/CellProvider/Models/SPDiffableSection.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,23 @@
2222
import UIKit
2323

2424
/**
25-
Basic section class. Can set header, footer and items in it section.
25+
SPDiffable: Basic section class. Can set header, footer and items in it section.
2626

2727
Using in diffable work. All sections shoud be inhert from it.
2828
Can init with empty models if need configure later.
2929
*/
3030
open class SPDiffableSection: NSObject, NSCopying {
3131

32+
// MARK: - Properties
33+
3234
/**
33-
Identifier help for detect uniq section and doing diffable work and animations.
35+
SPDiffable: Identifier help for detect uniq section and doing diffable work and animations.
3436

3537
Always shoud be uniq. But if it changed, diffable system remove old and insert new (not reload).
3638
Identifier uses in `Hashable` and `Equatable` protocols.
3739
*/
3840
public var identifier: SectionIdentifier
41+
3942
public var header: SPDiffableItem?
4043
public var footer: SPDiffableItem?
4144

Sources/SPDiffable/CellProvider/Models/SPDiffableSnapshot.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@
2222
import UIKit
2323

2424
/**
25-
Wrapper of `NSDiffableDataSourceSnapshot` with basic `SPDiffableSection` & `SPDiffableItem`.
25+
SPDiffable: Wrapper of `NSDiffableDataSourceSnapshot` with basic `SPDiffableSection` & `SPDiffableItem`.
2626
*/
2727
@available(iOS 13.0, *)
2828
public typealias SPDiffableSnapshot = NSDiffableDataSourceSnapshot<SPDiffableSection, SPDiffableItem>
2929

3030
/**
31-
Wrapper of NSDiffableDataSourceSectionSnapshot with basic `SPDiffableItem`.
31+
SPDiffable: Wrapper of NSDiffableDataSourceSectionSnapshot with basic `SPDiffableItem`.
3232
*/
3333
@available(iOS 14, *)
3434
public typealias SPDiffableSectionSnapshot = NSDiffableDataSourceSectionSnapshot<SPDiffableItem>

Sources/SPDiffable/CellProvider/Models/SPDiffableTextHeaderFooter.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-
Header & Footer model class with simple text.
25+
SPDiffable: Header & Footer model class with simple text.
2626
*/
2727
open class SPDiffableTextHeaderFooter: SPDiffableItem {
2828

0 commit comments

Comments
 (0)