From 03186a2cf54580c8d1a8771f752b312aabbf5304 Mon Sep 17 00:00:00 2001 From: Max Sokolov Date: Sat, 30 Apr 2016 00:14:07 +0300 Subject: [PATCH] generic improvements --- Tablet/TableRowBuilder.swift | 94 +++++++++---------- Tablet/Tablet.swift | 10 +- .../Controllers/MainController.swift | 2 + 3 files changed, 52 insertions(+), 54 deletions(-) diff --git a/Tablet/TableRowBuilder.swift b/Tablet/TableRowBuilder.swift index e5a438d..6ed6869 100644 --- a/Tablet/TableRowBuilder.swift +++ b/Tablet/TableRowBuilder.swift @@ -67,23 +67,23 @@ public class RowBuilder : NSObject { /** Responsible for building cells of given type and passing items to them. */ -public class TableRowBuilder : RowBuilder { +public class TableRowBuilder : RowBuilder { - private var actions = Dictionary>() - private var items = [I]() + private var actions = Dictionary>() + private var items = [DataType]() public override var numberOfRows: Int { return items.count } - public init(item: I, id: String? = nil) { - super.init(id: id ?? NSStringFromClass(C).componentsSeparatedByString(".").last ?? "") + public init(item: DataType, id: String? = nil) { + super.init(id: id ?? String(CellType)) items.append(item) } - public init(items: [I]? = nil, id: String? = nil) { - super.init(id: id ?? NSStringFromClass(C).componentsSeparatedByString(".").last ?? "") + public init(items: [DataType]? = nil, id: String? = nil) { + super.init(id: id ?? String(CellType)) if items != nil { self.items.appendContentsOf(items!) @@ -92,19 +92,19 @@ public class TableRowBuilder : RowBuilder { // MARK: Chaining actions - public func action(key: String, closure: (data: ActionData) -> Void) -> Self { + public func action(key: String, closure: (data: ActionData) -> Void) -> Self { actions[key] = .actionBlock(closure) return self } - public func action(actionType: ActionType, closure: (data: ActionData) -> Void) -> Self { + public func action(actionType: ActionType, closure: (data: ActionData) -> Void) -> Self { actions[actionType.key] = .actionBlock(closure) return self } - public func action(actionType: ActionType, closure: (data: ActionData) -> ReturnValue) -> Self { + public func action(actionType: ActionType, closure: (data: ActionData) -> ReturnValue) -> Self { actions[actionType.key] = .actionReturnBlock(closure) return self @@ -115,7 +115,7 @@ public class TableRowBuilder : RowBuilder { override func invokeAction(actionType: ActionType, cell: UITableViewCell?, indexPath: NSIndexPath, itemIndex: Int, userInfo: [NSObject: AnyObject]?) -> AnyObject? { if let action = actions[actionType.key] { - return action.invoke(ActionData(cell: cell as? C, indexPath: indexPath, item: items[itemIndex], itemIndex: itemIndex, userInfo: userInfo)) + return action.invoke(ActionData(cell: cell as? CellType, indexPath: indexPath, item: items[itemIndex], itemIndex: itemIndex, userInfo: userInfo)) } return nil } @@ -126,9 +126,8 @@ public class TableRowBuilder : RowBuilder { return } - guard let resource = NSStringFromClass(C).componentsSeparatedByString(".").last else { return } - - let bundle = NSBundle(forClass: C.self) + let resource = String(CellType) + let bundle = NSBundle(forClass: CellType.self) if let _ = bundle.pathForResource(resource, ofType: "nib") { // existing cell @@ -136,44 +135,13 @@ public class TableRowBuilder : RowBuilder { } else { - tableView.registerClass(C.self, forCellReuseIdentifier: reusableIdentifier) + tableView.registerClass(CellType.self, forCellReuseIdentifier: reusableIdentifier) } } -} - -/** - Responsible for building configurable cells of given type and passing items to them. - */ -public class TableConfigurableRowBuilder : TableRowBuilder { - - public override var estimatedRowHeight: Float { - return C.estimatedHeight() - } - - public init(item: I) { - super.init(item: item, id: C.reusableIdentifier()) - } - - public init(items: [I]? = nil) { - super.init(items: items, id: C.reusableIdentifier()) - } - - override func invokeAction(actionType: ActionType, cell: UITableViewCell?, indexPath: NSIndexPath, itemIndex: Int, userInfo: [NSObject: AnyObject]?) -> AnyObject? { - - switch actionType { - case .configure: - (cell as? C)?.configure(items[itemIndex]) - default: break - } - return super.invokeAction(actionType, cell: cell, indexPath: indexPath, itemIndex: itemIndex, userInfo: userInfo) - } -} - -public extension TableRowBuilder { // MARK: Items manipulation - public func append(items items: [I]) { + public func append(items items: [DataType]) { self.items.appendContentsOf(items) } @@ -182,10 +150,38 @@ public extension TableRowBuilder { } } -public func +=(left: TableRowBuilder, right: I) { +/** + Responsible for building configurable cells of given type and passing items to them. + */ +public class TableConfigurableRowBuilder : TableRowBuilder { + + public override var estimatedRowHeight: Float { + return CellType.estimatedHeight() + } + + public init(item: DataType) { + super.init(item: item, id: CellType.reusableIdentifier()) + } + + public init(items: [DataType]? = nil) { + super.init(items: items, id: CellType.reusableIdentifier()) + } + + override func invokeAction(actionType: ActionType, cell: UITableViewCell?, indexPath: NSIndexPath, itemIndex: Int, userInfo: [NSObject: AnyObject]?) -> AnyObject? { + + switch actionType { + case .configure: + (cell as? CellType)?.configure(items[itemIndex]) + default: break + } + return super.invokeAction(actionType, cell: cell, indexPath: indexPath, itemIndex: itemIndex, userInfo: userInfo) + } +} + +public func +=(left: TableRowBuilder, right: DataType) { left.append(items: [right]) } -public func +=(left: TableRowBuilder, right: [I]) { +public func +=(left: TableRowBuilder, right: [DataType]) { left.append(items: right) } \ No newline at end of file diff --git a/Tablet/Tablet.swift b/Tablet/Tablet.swift index 597baac..46b8767 100644 --- a/Tablet/Tablet.swift +++ b/Tablet/Tablet.swift @@ -51,15 +51,15 @@ public enum ActionType { } } -public class ActionData { +public class ActionData { - public let cell: C? - public let item: I + public let cell: CellType? + public let item: DataType public let itemIndex: Int public let indexPath: NSIndexPath public let userInfo: [NSObject: AnyObject]? - init(cell: C?, indexPath: NSIndexPath, item: I, itemIndex: Int, userInfo: [NSObject: AnyObject]?) { + init(cell: CellType?, indexPath: NSIndexPath, item: DataType, itemIndex: Int, userInfo: [NSObject: AnyObject]?) { self.cell = cell self.indexPath = indexPath @@ -112,6 +112,6 @@ public protocol ConfigurableCell { public extension ConfigurableCell where Self: UITableViewCell { static func reusableIdentifier() -> String { - return NSStringFromClass(self).componentsSeparatedByString(".").last ?? "" + return String(self) } } \ No newline at end of file diff --git a/TabletDemo/Classes/Presentation/Controllers/MainController.swift b/TabletDemo/Classes/Presentation/Controllers/MainController.swift index f5213fe..36f107f 100644 --- a/TabletDemo/Classes/Presentation/Controllers/MainController.swift +++ b/TabletDemo/Classes/Presentation/Controllers/MainController.swift @@ -25,6 +25,8 @@ class MainController: UIViewController { .action(.click) { [unowned self] data -> Void in self.performSegueWithIdentifier("headerfooter", sender: nil) } + + print("", String(TableDirector.self)) tableDirector += rows }