support multiple actions with same type

This commit is contained in:
Max Sokolov 2016-10-15 01:12:51 +03:00
parent 3de586c4f6
commit 3da097f1f1
2 changed files with 18 additions and 15 deletions

View File

@ -23,7 +23,7 @@ import UIKit
open class TableRow<CellType: ConfigurableCell>: Row where CellType: UITableViewCell { open class TableRow<CellType: ConfigurableCell>: Row where CellType: UITableViewCell {
open let item: CellType.T open let item: CellType.T
private lazy var actions = [String: RowAction]() private lazy var actions = [String: [RowAction]]()
private(set) open var editingActions: [UITableViewRowAction]? private(set) open var editingActions: [UITableViewRowAction]?
open var hashValue: Int { open var hashValue: Int {
@ -46,11 +46,11 @@ open class TableRow<CellType: ConfigurableCell>: Row where CellType: UITableView
return CellType.self return CellType.self
} }
public init(item: CellType.T, actions: [RowAction]? = nil, editingActions: [UITableViewRowAction]? = nil) { public init(item: CellType.T, actions: [TableRowAction<CellType>]? = nil, editingActions: [UITableViewRowAction]? = nil) {
self.item = item self.item = item
self.editingActions = editingActions self.editingActions = editingActions
actions?.forEach { self.actions[$0.type.key] = $0 } actions?.forEach { on($0) }
} }
// MARK: - RowConfigurable - // MARK: - RowConfigurable -
@ -62,7 +62,8 @@ open class TableRow<CellType: ConfigurableCell>: Row where CellType: UITableView
// MARK: - RowActionable - // MARK: - RowActionable -
open func invoke(_ action: TableRowActionType, cell: UITableViewCell?, path: IndexPath) -> Any? { open func invoke(_ action: TableRowActionType, cell: UITableViewCell?, path: IndexPath) -> Any? {
return actions[action.key]?.invokeActionOn(cell: cell, item: item, path: path)
return actions[action.key]?.flatMap({ $0.invokeActionOn(cell: cell, item: item, path: path) }).last
} }
open func hasAction(_ action: TableRowActionType) -> Bool { open func hasAction(_ action: TableRowActionType) -> Bool {
@ -80,17 +81,20 @@ open class TableRow<CellType: ConfigurableCell>: Row where CellType: UITableView
// MARK: - actions - // MARK: - actions -
@discardableResult @discardableResult
open func on(_ action: RowAction) -> Self { open func on(_ action: TableRowAction<CellType>) -> Self {
if actions[action.type.key] == nil {
actions[action.type.key] = [RowAction]()
}
actions[action.type.key]?.append(action)
actions[action.type.key] = action
return self return self
} }
@discardableResult @discardableResult
open func on<T>(_ type: TableRowActionType, handler: @escaping (_ data: TableRowActionData<CellType>) -> T) -> Self { open func on<T>(_ type: TableRowActionType, handler: @escaping (_ data: TableRowActionData<CellType>) -> T) -> Self {
actions[type.key] = TableRowAction<CellType>(type, handler: handler) return on(TableRowAction<CellType>(type, handler: handler))
return self
} }
// MARK: - deprecated actions - // MARK: - deprecated actions -
@ -99,15 +103,13 @@ open class TableRow<CellType: ConfigurableCell>: Row where CellType: UITableView
@discardableResult @discardableResult
open func action(_ action: TableRowAction<CellType>) -> Self { open func action(_ action: TableRowAction<CellType>) -> Self {
actions[action.type.key] = action return on(action)
return self
} }
@available(*, deprecated, message: "Use 'on' method instead") @available(*, deprecated, message: "Use 'on' method instead")
@discardableResult @discardableResult
open func action<T>(_ type: TableRowActionType, handler: @escaping (_ data: TableRowActionData<CellType>) -> T) -> Self { open func action<T>(_ type: TableRowActionType, handler: @escaping (_ data: TableRowActionData<CellType>) -> T) -> Self {
actions[type.key] = TableRowAction<CellType>(type, handler: handler) return on(TableRowAction<CellType>(type, handler: handler))
return self
} }
} }

View File

@ -45,7 +45,8 @@ private enum TableRowActionHandler<CellType: ConfigurableCell> where CellType: U
switch self { switch self {
case .voidAction(let handler): case .voidAction(let handler):
return handler(actionData) handler(actionData)
return nil
case .action(let handler): case .action(let handler):
return handler(actionData) return handler(actionData)
} }