implement RowAction protocol

This commit is contained in:
Max Sokolov 2016-10-14 21:16:21 +03:00
parent 7f65f0c4e0
commit 3de586c4f6
3 changed files with 16 additions and 30 deletions

View File

@ -74,6 +74,8 @@ public enum TableRowActionType {
}
public protocol RowAction {
func invokeActionOn<CellType: ConfigurableCell>(cell: CellType, item: CellType.T, path: IndexPath) -> Any? where CellType: UITableViewCell
var type: TableRowActionType { get }
func invokeActionOn(cell: UITableViewCell?, item: Any, path: IndexPath) -> Any?
}

View File

@ -20,26 +20,10 @@
import UIKit
open class TableRowActionX<ActionCellType: ConfigurableCell>: RowAction where ActionCellType: UITableViewCell {
var handler: ((TableRowActionData<ActionCellType>) -> Void)?
public func invokeActionOn<CellType: ConfigurableCell>(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<CellType: ConfigurableCell>: Row where CellType: UITableViewCell {
open let item: CellType.T
private lazy var actions = [String: TableRowAction<CellType>]()
private lazy var actions = [String: RowAction]()
private(set) open var editingActions: [UITableViewRowAction]?
open var hashValue: Int {
@ -62,7 +46,7 @@ open class TableRow<CellType: ConfigurableCell>: Row where CellType: UITableView
return CellType.self
}
public init(item: CellType.T, actions: [TableRowAction<CellType>]? = 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<CellType: ConfigurableCell>: 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<CellType: ConfigurableCell>: Row where CellType: UITableView
// MARK: - actions -
@discardableResult
open func on(_ action: TableRowAction<CellType>) -> Self {
open func on(_ action: RowAction) -> Self {
actions[action.type.key] = action
return self

View File

@ -20,8 +20,6 @@
import UIKit
open class TableRowActionData<CellType: ConfigurableCell> where CellType: UITableViewCell {
open let item: CellType.T
@ -43,18 +41,18 @@ private enum TableRowActionHandler<CellType: ConfigurableCell> where CellType: U
case voidAction((TableRowActionData<CellType>) -> Void)
case action((TableRowActionData<CellType>) -> Any?)
func invoke(item: CellType.T, cell: UITableViewCell?, path: IndexPath) -> Any? {
func invoke(withActionData actionData: TableRowActionData<CellType>) -> 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<CellType: ConfigurableCell> where CellType: UITableViewCell {
open class TableRowAction<CellType: ConfigurableCell>: RowAction where CellType: UITableViewCell {
open let type: TableRowActionType
private let handler: TableRowActionHandler<CellType>
@ -71,7 +69,9 @@ open class TableRowAction<CellType: ConfigurableCell> 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))
}
}