diff --git a/Sources/TableKit.swift b/Sources/TableKit.swift index 1f349b9..eebc57d 100644 --- a/Sources/TableKit.swift +++ b/Sources/TableKit.swift @@ -74,6 +74,8 @@ public enum TableRowActionType { } public protocol RowAction { - - func invokeActionOn(cell: CellType, item: CellType.T, path: IndexPath) -> Any? where CellType: UITableViewCell + + var type: TableRowActionType { get } + + func invokeActionOn(cell: UITableViewCell?, item: Any, path: IndexPath) -> Any? } diff --git a/Sources/TableRow.swift b/Sources/TableRow.swift index 128123c..5135ea9 100644 --- a/Sources/TableRow.swift +++ b/Sources/TableRow.swift @@ -20,26 +20,10 @@ import UIKit -open class TableRowActionX: RowAction where ActionCellType: UITableViewCell { - - var handler: ((TableRowActionData) -> Void)? - - public func invokeActionOn(cell: CellType, item: CellType.T, path: IndexPath) -> Any? where CellType: UITableViewCell { - - guard let item = item as? ActionCellType.T else { return nil } - - let x = TableRowActionData(item: item, cell: cell as? ActionCellType, path: path, userInfo: nil) - - handler?(x) - - return nil - } -} - open class TableRow: Row where CellType: UITableViewCell { open let item: CellType.T - private lazy var actions = [String: TableRowAction]() + private lazy var actions = [String: RowAction]() private(set) open var editingActions: [UITableViewRowAction]? open var hashValue: Int { @@ -62,7 +46,7 @@ open class TableRow: Row where CellType: UITableView return CellType.self } - public init(item: CellType.T, actions: [TableRowAction]? = nil, editingActions: [UITableViewRowAction]? = nil) { + public init(item: CellType.T, actions: [RowAction]? = nil, editingActions: [UITableViewRowAction]? = nil) { self.item = item self.editingActions = editingActions @@ -78,7 +62,7 @@ open class TableRow: Row where CellType: UITableView // MARK: - RowActionable - open func invoke(_ action: TableRowActionType, cell: UITableViewCell?, path: IndexPath) -> Any? { - return actions[action.key]?.invoke(item: item, cell: cell, path: path) + return actions[action.key]?.invokeActionOn(cell: cell, item: item, path: path) } open func hasAction(_ action: TableRowActionType) -> Bool { @@ -96,7 +80,7 @@ open class TableRow: Row where CellType: UITableView // MARK: - actions - @discardableResult - open func on(_ action: TableRowAction) -> Self { + open func on(_ action: RowAction) -> Self { actions[action.type.key] = action return self diff --git a/Sources/TableRowAction.swift b/Sources/TableRowAction.swift index 9c99d2d..1698226 100644 --- a/Sources/TableRowAction.swift +++ b/Sources/TableRowAction.swift @@ -20,8 +20,6 @@ import UIKit - - open class TableRowActionData where CellType: UITableViewCell { open let item: CellType.T @@ -43,18 +41,18 @@ private enum TableRowActionHandler where CellType: U case voidAction((TableRowActionData) -> Void) case action((TableRowActionData) -> Any?) - func invoke(item: CellType.T, cell: UITableViewCell?, path: IndexPath) -> Any? { + func invoke(withActionData actionData: TableRowActionData) -> Any? { switch self { case .voidAction(let handler): - return handler(TableRowActionData(item: item, cell: cell as? CellType, path: path, userInfo: nil)) + return handler(actionData) case .action(let handler): - return handler(TableRowActionData(item: item, cell: cell as? CellType, path: path, userInfo: nil)) + return handler(actionData) } } } -open class TableRowAction where CellType: UITableViewCell { +open class TableRowAction: RowAction where CellType: UITableViewCell { open let type: TableRowActionType private let handler: TableRowActionHandler @@ -71,7 +69,9 @@ open class TableRowAction where CellType: UITableVie self.handler = .action(handler) } - func invoke(item: CellType.T, cell: UITableViewCell?, path: IndexPath) -> Any? { - return handler.invoke(item: item, cell: cell, path: path) + public func invokeActionOn(cell: UITableViewCell?, item: Any, path: IndexPath) -> Any? { + guard let item = item as? CellType.T else { return nil } + + return handler.invoke(withActionData: TableRowActionData(item: item, cell: cell as? CellType, path: path, userInfo: nil)) } }