From 96b2a19f1d5e12b11d2f9691058b6c201d6f7e47 Mon Sep 17 00:00:00 2001 From: Max Sokolov Date: Fri, 10 Jun 2016 00:32:00 +0300 Subject: [PATCH] store actions --- Tablet/TableDirector.swift | 5 +-- Tablet/TableRow.swift | 13 ++++-- Tablet/TableRowAction.swift | 38 ++++++++++++++--- Tablet/Tablet.swift | 41 ------------------- .../Controllers/MainController.swift | 6 ++- 5 files changed, 48 insertions(+), 55 deletions(-) diff --git a/Tablet/TableDirector.swift b/Tablet/TableDirector.swift index 1679148..e77a671 100644 --- a/Tablet/TableDirector.swift +++ b/Tablet/TableDirector.swift @@ -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 { diff --git a/Tablet/TableRow.swift b/Tablet/TableRow.swift index 86338d4..620342a 100644 --- a/Tablet/TableRow.swift +++ b/Tablet/TableRow.swift @@ -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: 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 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) -> Self { - + + actions[action.type.key] = action return self } } \ No newline at end of file diff --git a/Tablet/TableRowAction.swift b/Tablet/TableRowAction.swift index 00cfd18..9c73c79 100644 --- a/Tablet/TableRowAction.swift +++ b/Tablet/TableRowAction.swift @@ -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: RowAction { - public init(_ action: ActionType, handler: (row: TableRow) -> Void) { + public let type: TableRowActionType + public init(_ type: TableRowActionType, handler: (row: TableRow) -> Void) { + self.type = type } - public init(_ action: ActionType, handler: (row: TableRow) -> T) { - + public init(_ type: TableRowActionType, handler: (row: TableRow) -> T) { + self.type = type + } + + // MARK: - RowAction - + + func invoke() -> Any? { + return nil } } \ No newline at end of file diff --git a/Tablet/Tablet.swift b/Tablet/Tablet.swift index bf5e653..fb22bde 100644 --- a/Tablet/Tablet.swift +++ b/Tablet/Tablet.swift @@ -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 { @@ -68,22 +43,6 @@ public class ActionData { } } -enum ActionHandler { - - case Handler((data: ActionData) -> Void) - case ValueHandler((data: ActionData) -> AnyObject?) - - func invoke(data: ActionData) -> 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. diff --git a/TabletDemo/Classes/Presentation/Controllers/MainController.swift b/TabletDemo/Classes/Presentation/Controllers/MainController.swift index b055272..0a803a5 100644 --- a/TabletDemo/Classes/Presentation/Controllers/MainController.swift +++ b/TabletDemo/Classes/Presentation/Controllers/MainController.swift @@ -33,6 +33,7 @@ class MainController: UIViewController { let a = TableRowAction(.click) { (row) in + print("3") } @@ -42,13 +43,16 @@ class MainController: UIViewController { let row3 = TableRow(item: "3", actions: [a]) + row1 .action(TableRowAction(.click) { (row) in - + print("1") }) .action(TableRowAction(.click) { (row) -> String in + print("2") + return "" })