store actions

This commit is contained in:
Max Sokolov 2016-06-10 00:32:00 +03:00
parent 708f164c96
commit 96b2a19f1d
5 changed files with 48 additions and 55 deletions

View File

@ -50,9 +50,8 @@ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate
// MARK: Public
public func invoke(action action: ActionType, cell: UITableViewCell?, indexPath: NSIndexPath) -> AnyObject? {
return nil
public func invoke(action action: TableRowActionType, cell: UITableViewCell?, indexPath: NSIndexPath) -> Any? {
return sections[indexPath.section].items[indexPath.row].invoke(action, cell: cell, path: indexPath)
}
public override func respondsToSelector(selector: Selector) -> Bool {

View File

@ -28,6 +28,7 @@ public protocol RowConfigurable {
public protocol RowActionable {
func invoke(action: TableRowActionType, cell: UITableViewCell?, path: NSIndexPath) -> Any?
func hasAction(action: TableRowActionType) -> Bool
}
public protocol Row: RowConfigurable, RowActionable {
@ -40,7 +41,7 @@ public protocol Row: RowConfigurable, RowActionable {
public class TableRow<ItemType, CellType: ConfigurableCell where CellType.T == ItemType, CellType: UITableViewCell>: Row {
public let item: ItemType
private var actions = [RowAction]()
private var actions = [String: RowAction]()
public var reusableIdentifier: String {
return CellType.reusableIdentifier()
@ -67,14 +68,18 @@ public class TableRow<ItemType, CellType: ConfigurableCell where CellType.T == I
// MARK: - RowActionable -
public func invoke(action: TableRowActionType, cell: UITableViewCell?, path: NSIndexPath) -> Any? {
return nil
return actions[action.key]?.invoke()
}
public func hasAction(action: TableRowActionType) -> Bool {
return actions[action.key] != nil
}
// MARK: - actions -
public func action(action: TableRowAction<ItemType, CellType>) -> Self {
actions[action.type.key] = action
return self
}
}

View File

@ -22,21 +22,47 @@ import UIKit
public enum TableRowActionType {
case Click
case Custom(String)
case click
case select
case deselect
case willSelect
case configure
case willDisplay
case shouldHighlight
case height
case custom(String)
var key: String {
switch (self) {
case .custom(let key):
return key
default:
return "_\(self)"
}
}
}
protocol RowAction {
func invoke() -> Any?
}
public class TableRowAction<ItemType, CellType: ConfigurableCell where CellType.T == ItemType, CellType: UITableViewCell>: RowAction {
public init(_ action: ActionType, handler: (row: TableRow<ItemType, CellType>) -> Void) {
public let type: TableRowActionType
public init(_ type: TableRowActionType, handler: (row: TableRow<ItemType, CellType>) -> Void) {
self.type = type
}
public init<T>(_ action: ActionType, handler: (row: TableRow<ItemType, CellType>) -> T) {
public init<T>(_ type: TableRowActionType, handler: (row: TableRow<ItemType, CellType>) -> T) {
self.type = type
}
// MARK: - RowAction -
func invoke() -> Any? {
return nil
}
}

View File

@ -24,31 +24,6 @@ struct TabletNotifications {
static let CellAction = "TabletNotificationsCellAction"
}
/**
The actions that Tablet provides.
*/
public enum ActionType {
case click
case select
case deselect
case willSelect
case configure
case willDisplay
case shouldHighlight
case height
case custom(String)
var key: String {
switch (self) {
case .custom(let key):
return key
default:
return "_\(self)"
}
}
}
public class ActionData<DataType, CellType> {
@ -68,22 +43,6 @@ public class ActionData<DataType, CellType> {
}
}
enum ActionHandler<DataType, CellType> {
case Handler((data: ActionData<DataType, CellType>) -> Void)
case ValueHandler((data: ActionData<DataType, CellType>) -> AnyObject?)
func invoke(data: ActionData<DataType, CellType>) -> ReturnValue {
switch (self) {
case .Handler(let handler):
handler(data: data)
return nil
case .ValueHandler(let handler):
return handler(data: data)
}
}
}
/**
A custom action that you can trigger from your cell.

View File

@ -33,6 +33,7 @@ class MainController: UIViewController {
let a = TableRowAction<String, StoryboardImageTableViewCell>(.click) {
(row) in
print("3")
}
@ -42,13 +43,16 @@ class MainController: UIViewController {
let row3 = TableRow<String, StoryboardImageTableViewCell>(item: "3", actions: [a])
row1
.action(TableRowAction(.click) { (row) in
print("1")
})
.action(TableRowAction(.click) { (row) -> String in
print("2")
return ""
})