Skip to content

Commit fa079b5

Browse files
committed
Added SPDiffableItemActionable protocol.
1 parent d00c426 commit fa079b5

File tree

7 files changed

+22
-21
lines changed

7 files changed

+22
-21
lines changed

Example Apps/iOS Example/Controllers/RootController.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ class RootController: DiffableTableController {
7070
header: SPDiffableTextHeaderFooter(text: "Presenter"),
7171
footer: SPDiffableTextHeaderFooter(text: "Push in navigation processing by table controller. Sometimes you need manually deselect cell."),
7272
items: [
73-
SPDiffableTableRow(text: "Deselect", accessoryType: .disclosureIndicator, selectionStyle: .default, action: { [weak self] indexPath in
73+
SPDiffableTableRow(text: "Deselect", accessoryType: .disclosureIndicator, selectionStyle: .default, action: { [weak self] model, indexPath in
7474
guard let self = self else { return }
7575
self.tableView.deselectRow(at: indexPath, animated: true)
7676
}),
77-
SPDiffableTableRow(text: "Push", accessoryType: .disclosureIndicator, selectionStyle: .default, action: { [weak self] indexPath in
77+
SPDiffableTableRow(text: "Push", accessoryType: .disclosureIndicator, selectionStyle: .default, action: { [weak self] model, indexPath in
7878
guard let self = self else { return }
7979
let controller = UIViewController()
8080
controller.view.backgroundColor = .secondarySystemGroupedBackground
@@ -99,7 +99,7 @@ class RootController: DiffableTableController {
9999
identifier: Section.sidebar.identifier,
100100
footer: SPDiffableTextHeaderFooter(text: "Example of new Side Bar."),
101101
items: [
102-
SPDiffableTableRow(text: "Present Side Bar", accessoryType: .disclosureIndicator, selectionStyle: .default, action: { [weak self] indexPath in
102+
SPDiffableTableRow(text: "Present Side Bar", accessoryType: .disclosureIndicator, selectionStyle: .default, action: { [weak self] model, indexPath in
103103
guard let self = self else { return }
104104
self.tableView.deselectRow(at: indexPath, animated: true)
105105
let sideBarController = SideBarSplitController()
@@ -115,7 +115,7 @@ class RootController: DiffableTableController {
115115
identifier: Section.checkmark.identifier,
116116
footer: SPDiffableTextHeaderFooter(text: "Example how usage search by models and change checkmark without reload table."),
117117
items: [
118-
SPDiffableTableRow(text: "Chekmarks", accessoryType: .disclosureIndicator, selectionStyle: .default, action: { [weak self] indexPath in
118+
SPDiffableTableRow(text: "Chekmarks", accessoryType: .disclosureIndicator, selectionStyle: .default, action: { [weak self] model, indexPath in
119119
guard let self = self else { return }
120120
self.tableView.deselectRow(at: indexPath, animated: true)
121121
})
@@ -127,7 +127,7 @@ class RootController: DiffableTableController {
127127
identifier: Section.customCellProvider.identifier,
128128
footer: SPDiffableTextHeaderFooter(text: "Also you can add more providers for specific controller, and use default and custom specially for some contorllers."),
129129
items: [
130-
SPDiffableTableRow(text: "Custom Cell Provider", accessoryType: .disclosureIndicator, selectionStyle: .default, action: { [weak self] indexPath in
130+
SPDiffableTableRow(text: "Custom Cell Provider", accessoryType: .disclosureIndicator, selectionStyle: .default, action: { [weak self] model, indexPath in
131131
guard let self = self else { return }
132132
self.tableView.deselectRow(at: indexPath, animated: true)
133133
})

Sources/SPDiffable/Models/SPDiffableItem.swift

+11
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,14 @@ open class SPDiffableItem: NSObject, NSCopying {
6767

6868
public typealias Identifier = String
6969
}
70+
71+
/**
72+
SPDiffable: Protocol for items which shoud have actions.
73+
In default controllers will be handled.
74+
*/
75+
public protocol SPDiffableItemActionable: AnyObject {
76+
77+
var action: Action? { get set }
78+
79+
typealias Action = (_ item: SPDiffableItem, _ indexPath: IndexPath) -> Void
80+
}

Sources/SPDiffable/Models/SPDiffableWrapperItem.swift

+1-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import UIKit
2929

3030
Also need pass uniq `identifier`.
3131
*/
32-
open class SPDiffableWrapperItem: SPDiffableItem {
32+
open class SPDiffableWrapperItem: SPDiffableItem, SPDiffableItemActionable {
3333

3434
/**
3535
SPDiffable: You can pass any object as model.
@@ -42,6 +42,4 @@ open class SPDiffableWrapperItem: SPDiffableItem {
4242
self.model = model
4343
super.init(identifier: identifier)
4444
}
45-
46-
public typealias Action = (_ indexPath: IndexPath) -> Void
4745
}

Sources/SPDiffable/Table/Models/SPDiffableTableRow.swift

+2-4
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ import UIKit
2828
By default if action is nil, selection style set to `.none`.
2929
If accessory is control, you can find reay-use class for it.
3030
*/
31-
open class SPDiffableTableRow: SPDiffableItem {
31+
open class SPDiffableTableRow: SPDiffableItem, SPDiffableItemActionable {
3232

3333
open var text: String
3434
open var detail: String? = nil
3535
open var icon: UIImage? = nil
3636
open var selectionStyle: UITableViewCell.SelectionStyle
3737
open var accessoryType: UITableViewCell.AccessoryType
38-
open var action: Action?
38+
public var action: Action?
3939

4040
public init(identifier: String? = nil, text: String, detail: String? = nil, icon: UIImage? = nil, accessoryType: UITableViewCell.AccessoryType = .none, selectionStyle: UITableViewCell.SelectionStyle = .none, action: Action? = nil) {
4141
self.text = text
@@ -46,6 +46,4 @@ open class SPDiffableTableRow: SPDiffableItem {
4646
self.action = action
4747
super.init(identifier: identifier ?? text)
4848
}
49-
50-
public typealias Action = (_ indexPath: IndexPath) -> Void
5149
}

Sources/SPDiffable/Table/Models/SPDiffableTableRowSubtitle.swift

+1-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import UIKit
2828
By default if action is nil, selection style set to `.none`.
2929
If accessory is control, you can find reay-use class for it.
3030
*/
31-
open class SPDiffableTableRowSubtitle: SPDiffableItem {
31+
open class SPDiffableTableRowSubtitle: SPDiffableItem, SPDiffableItemActionable {
3232

3333
open var text: String
3434
open var subtitle: String? = nil
@@ -46,6 +46,4 @@ open class SPDiffableTableRowSubtitle: SPDiffableItem {
4646
self.action = action
4747
super.init(identifier: identifier ?? text)
4848
}
49-
50-
public typealias Action = (_ indexPath: IndexPath) -> Void
5149
}

Sources/SPDiffable/Table/SPDiffableTableController.swift

+2-6
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,8 @@ open class SPDiffableTableController: UITableViewController {
6565
diffableDelegate?.diffableTableView?(tableView, didSelectItem: item, indexPath: indexPath)
6666

6767
switch item {
68-
case let model as SPDiffableTableRow:
69-
model.action?(indexPath)
70-
case let model as SPDiffableTableRowSubtitle:
71-
model.action?(indexPath)
72-
case let model as SPDiffableWrapperItem:
73-
model.action?(indexPath)
68+
case let model as SPDiffableItemActionable:
69+
model.action?(item, indexPath)
7470
default:
7571
break
7672
}

0 commit comments

Comments
 (0)