ActionType improved

This commit is contained in:
Max Sokolov 2015-11-14 01:25:23 +03:00
parent 73ea2cf573
commit 2cb9078375
5 changed files with 44 additions and 31 deletions

View File

@ -25,7 +25,7 @@ import Foundation
Responsible for table view's datasource and delegate.
*/
public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate {
private weak var tableView: UITableView!
private var sections = [TableSectionBuilder]()
@ -72,7 +72,7 @@ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate
private func triggerAction(action: ActionType, cell: UITableViewCell?, indexPath: NSIndexPath) -> AnyObject? {
let builder = builderAtIndexPath(indexPath)
return builder.0.triggerAction(action.rawValue, cell: cell, indexPath: indexPath, itemIndex: builder.1)
return builder.0.triggerAction(action, cell: cell, indexPath: indexPath, itemIndex: builder.1)
}
internal func didReceiveAction(notification: NSNotification) {
@ -80,7 +80,7 @@ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate
if let action = notification.object as? Action, indexPath = tableView.indexPathForCell(action.cell) {
let builder = builderAtIndexPath(indexPath)
builder.0.triggerAction(action.key, cell: action.cell, indexPath: indexPath, itemIndex: builder.1)
builder.0.triggerAction(.custom(action.key), cell: action.cell, indexPath: indexPath, itemIndex: builder.1)
}
}
@ -102,7 +102,7 @@ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate
let cell = tableView.dequeueReusableCellWithIdentifier(builder.0.reusableIdentifier, forIndexPath: indexPath)
builder.0.triggerAction(ActionType.configure.rawValue, cell: cell, indexPath: indexPath, itemIndex: builder.1)
builder.0.triggerAction(.configure, cell: cell, indexPath: indexPath, itemIndex: builder.1)
return cell
}
@ -144,7 +144,7 @@ public class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate
// MARK: UITableViewDelegate - actions
public func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath)
if triggerAction(.click, cell: cell, indexPath: indexPath) != nil {

View File

@ -21,12 +21,12 @@
import UIKit
import Foundation
internal enum ActionHandler<T, E> {
internal enum ActionHandler<I, C> {
case actionBlock((data: ActionData<T, E>) -> Void)
case actionReturnBlock((data: ActionData<T, E>) -> AnyObject)
case actionBlock((data: ActionData<I, C>) -> Void)
case actionReturnBlock((data: ActionData<I, C>) -> AnyObject)
func call(data: ActionData<T, E>) -> AnyObject {
func call(data: ActionData<I, C>) -> AnyObject {
switch (self) {
case .actionBlock(let closure):
@ -67,6 +67,8 @@ public class TableRowBuilder<I, C where C: UITableViewCell> : RowBuilder {
self.items.appendContentsOf(items!)
}
}
// MARK: Items manipulation
public func appendItems(items: [I]) {
@ -86,23 +88,23 @@ public class TableRowBuilder<I, C where C: UITableViewCell> : RowBuilder {
return self
}
public func action(key: ActionType, action: (data: ActionData<I, C>) -> Void) -> Self {
public func action(actionType: ActionType, action: (data: ActionData<I, C>) -> Void) -> Self {
actions[key.rawValue] = .actionBlock(action)
actions[actionType.key] = .actionBlock(action)
return self
}
public func action(key: ActionType, action: (data: ActionData<I, C>) -> AnyObject) -> Self {
public func action(actionType: ActionType, action: (data: ActionData<I, C>) -> AnyObject) -> Self {
actions[key.rawValue] = .actionReturnBlock(action)
actions[actionType.key] = .actionReturnBlock(action)
return self
}
// MARK: Triggers
public func triggerAction(key: String, cell: UITableViewCell?, indexPath: NSIndexPath, itemIndex: Int) -> AnyObject? {
public func triggerAction(actionType: ActionType, cell: UITableViewCell?, indexPath: NSIndexPath, itemIndex: Int) -> AnyObject? {
if let action = actions[key] {
if let action = actions[actionType.key] {
return action.call(ActionData(cell: cell as? C, indexPath: indexPath, item: items[itemIndex], itemIndex: itemIndex))
}
return nil
@ -122,10 +124,10 @@ public class TableConfigurableRowBuilder<I, C: ConfigurableCell where C.Item ==
super.init(items: items, id: C.reusableIdentifier())
}
public override func triggerAction(key: String, cell: UITableViewCell?, indexPath: NSIndexPath, itemIndex: Int) -> AnyObject? {
public override func triggerAction(actionType: ActionType, cell: UITableViewCell?, indexPath: NSIndexPath, itemIndex: Int) -> AnyObject? {
(cell as? C)?.configureWithItem(items[itemIndex])
return super.triggerAction(key, cell: cell, indexPath: indexPath, itemIndex: itemIndex)
return super.triggerAction(actionType, cell: cell, indexPath: indexPath, itemIndex: itemIndex)
}
}

View File

@ -24,20 +24,31 @@ import Foundation
internal let kActionPerformedNotificationKey = "_action"
/**
Built in actions that Tablet provides.
The actions that Tablet provides.
*/
public enum ActionType : String {
public enum ActionType {
case click = "_click"
case select = "_select"
case deselect = "_deselect"
case configure = "_configure"
case willDisplay = "_willDisplay"
case shouldHighlight = "_shouldHighlight"
case height = "_height"
case click
case select
case deselect
case configure
case willDisplay
case shouldHighlight
case height
case custom(String)
var key: String {
switch (self) {
case .custom(let str):
return str
default:
return "_\(self)"
}
}
}
public struct ActionData<I, C> {
public class ActionData<I, C> {
public let cell: C?
public let item: I
@ -83,7 +94,7 @@ public class Action {
/**
If you want to delegate your cell configuration logic to cell itself (with your view model or even model) than
just provide an implementation of this protocol for your cell. Enjoy strong typisation.
just provide an implementation of this protocol for your cell. Enjoy safe-typisation.
*/
public protocol ConfigurableCell {
@ -102,5 +113,5 @@ public protocol RowBuilder {
var numberOfRows: Int { get }
var reusableIdentifier: String { get }
func triggerAction(key: String, cell: UITableViewCell?, indexPath: NSIndexPath, itemIndex: Int) -> AnyObject?
func triggerAction(actionType: ActionType, cell: UITableViewCell?, indexPath: NSIndexPath, itemIndex: Int) -> AnyObject?
}

View File

@ -37,7 +37,7 @@ class ViewController: UIViewController {
data.cell!.textLabel?.text = ""
print("custom action indexPath: \(data.indexPath), item: \(data.item)")
print("click action indexPath: \(data.indexPath), item: \(data.item)")
}
.action(kConfigurableTableViewCellButtonClickedAction) { data in