reduce first generic parameter

This commit is contained in:
Max Sokolov 2016-10-06 01:45:39 +03:00
parent 38230a9a57
commit 6f441b2f7d
5 changed files with 26 additions and 26 deletions

View File

@ -30,10 +30,10 @@ class AutolayoutCellsController: UIViewController {
while rows <= 1000 {
rows += 1
let row = TableRow<Void, AutolayoutTableViewCell>(item: ())
let row = TableRow<AutolayoutTableViewCell>(item: ())
section += row
}
tableDirector += section
}
}
}

View File

@ -23,7 +23,7 @@ class MainController: UIViewController {
title = "TableKit"
let clickAction = TableRowAction<String, ConfigurableTableViewCell>(.click) { [weak self] (data) in
let clickAction = TableRowAction<ConfigurableTableViewCell>(.click) { [weak self] (data) in
switch (data.indexPath as NSIndexPath).row {
case 0:
@ -37,8 +37,8 @@ class MainController: UIViewController {
let rows = [
TableRow<String, ConfigurableTableViewCell>(item: "Autolayout cells", actions: [clickAction]),
TableRow<String, ConfigurableTableViewCell>(item: "Nib cells", actions: [clickAction])
TableRow<ConfigurableTableViewCell>(item: "Autolayout cells", actions: [clickAction]),
TableRow<ConfigurableTableViewCell>(item: "Nib cells", actions: [clickAction])
]
// automatically creates a section, also could be used like tableDirector.append(rows: rows)

View File

@ -22,11 +22,11 @@ class NibCellsController: UITableViewController {
let numbers = [1000, 2000, 3000, 4000, 5000]
let shouldHighlightAction = TableRowAction<Int, NibTableViewCell>(.shouldHighlight) { (_) -> Bool in
let shouldHighlightAction = TableRowAction<NibTableViewCell>(.shouldHighlight) { (_) -> Bool in
return false
}
let rows: [Row] = numbers.map { TableRow<Int, NibTableViewCell>(item: $0, actions: [shouldHighlightAction]) }
let rows = numbers.map { TableRow<NibTableViewCell>(item: $0, actions: [shouldHighlightAction]) }
tableDirector.append(rows: rows)
}

View File

@ -48,10 +48,10 @@ public protocol Row: RowConfigurable, RowActionable, RowHashable {
var defaultHeight: CGFloat? { get }
}
open class TableRow<ItemType, CellType: ConfigurableCell>: Row where CellType.T == ItemType, CellType: UITableViewCell {
open class TableRow<CellType: ConfigurableCell>: Row where CellType: UITableViewCell {
open let item: ItemType
private lazy var actions = [String: TableRowAction<ItemType, CellType>]()
open let item: CellType.T
private lazy var actions = [String: TableRowAction<CellType>]()
private(set) open var editingActions: [UITableViewRowAction]?
open var hashValue: Int {
@ -74,7 +74,7 @@ open class TableRow<ItemType, CellType: ConfigurableCell>: Row where CellType.T
return CellType.self
}
public init(item: ItemType, actions: [TableRowAction<ItemType, CellType>]? = nil, editingActions: [UITableViewRowAction]? = nil) {
public init(item: CellType.T, actions: [TableRowAction<CellType>]? = nil, editingActions: [UITableViewRowAction]? = nil) {
self.item = item
self.editingActions = editingActions
@ -108,16 +108,16 @@ open class TableRow<ItemType, CellType: ConfigurableCell>: Row where CellType.T
// MARK: - actions -
@discardableResult
open func action(_ action: TableRowAction<ItemType, CellType>) -> Self {
open func action(_ action: TableRowAction<CellType>) -> Self {
actions[action.type.key] = action
return self
}
@discardableResult
open func action<T>(_ type: TableRowActionType, handler: @escaping (_ data: TableRowActionData<ItemType, CellType>) -> T) -> Self {
open func action<T>(_ type: TableRowActionType, handler: @escaping (_ data: TableRowActionData<CellType>) -> T) -> Self {
actions[type.key] = TableRowAction<ItemType, CellType>(type, handler: handler)
actions[type.key] = TableRowAction<CellType>(type, handler: handler)
return self
}
}

View File

@ -45,14 +45,14 @@ public enum TableRowActionType {
}
}
open class TableRowActionData<ItemType, CellType: ConfigurableCell> where CellType.T == ItemType, CellType: UITableViewCell {
open class TableRowActionData<CellType: ConfigurableCell> where CellType: UITableViewCell {
open let item: ItemType
open let item: CellType.T
open let cell: CellType?
open let indexPath: IndexPath
open let userInfo: [AnyHashable: Any]?
init(item: ItemType, cell: CellType?, path: IndexPath, userInfo: [AnyHashable: Any]?) {
init(item: CellType.T, cell: CellType?, path: IndexPath, userInfo: [AnyHashable: Any]?) {
self.item = item
self.cell = cell
@ -61,12 +61,12 @@ open class TableRowActionData<ItemType, CellType: ConfigurableCell> where CellTy
}
}
private enum TableRowActionHandler<ItemType, CellType: ConfigurableCell> where CellType.T == ItemType, CellType: UITableViewCell {
private enum TableRowActionHandler<CellType: ConfigurableCell> where CellType: UITableViewCell {
case voidAction((TableRowActionData<ItemType, CellType>) -> Void)
case action((TableRowActionData<ItemType, CellType>) -> Any?)
case voidAction((TableRowActionData<CellType>) -> Void)
case action((TableRowActionData<CellType>) -> Any?)
func invoke(item: ItemType, cell: UITableViewCell?, path: IndexPath) -> Any? {
func invoke(item: CellType.T, cell: UITableViewCell?, path: IndexPath) -> Any? {
switch self {
case .voidAction(let handler):
@ -77,24 +77,24 @@ private enum TableRowActionHandler<ItemType, CellType: ConfigurableCell> where C
}
}
open class TableRowAction<ItemType, CellType: ConfigurableCell> where CellType.T == ItemType, CellType: UITableViewCell {
open class TableRowAction<CellType: ConfigurableCell> where CellType: UITableViewCell {
open let type: TableRowActionType
private let handler: TableRowActionHandler<ItemType, CellType>
private let handler: TableRowActionHandler<CellType>
public init(_ type: TableRowActionType, handler: @escaping (_ data: TableRowActionData<ItemType, CellType>) -> Void) {
public init(_ type: TableRowActionType, handler: @escaping (_ data: TableRowActionData<CellType>) -> Void) {
self.type = type
self.handler = .voidAction(handler)
}
public init<T>(_ type: TableRowActionType, handler: @escaping (_ data: TableRowActionData<ItemType, CellType>) -> T) {
public init<T>(_ type: TableRowActionType, handler: @escaping (_ data: TableRowActionData<CellType>) -> T) {
self.type = type
self.handler = .action(handler)
}
func invoke(item: ItemType, cell: UITableViewCell?, path: IndexPath) -> Any? {
func invoke(item: CellType.T, cell: UITableViewCell?, path: IndexPath) -> Any? {
return handler.invoke(item: item, cell: cell, path: path)
}
}