Merge pull request #47 from maxsokolov/generic_improvements

Generic improvements
This commit is contained in:
Max Sokolov 2016-10-06 11:37:23 +03:00 committed by GitHub
commit 7eca499a59
9 changed files with 74 additions and 43 deletions

27
CHANGELOG.md Normal file
View File

@ -0,0 +1,27 @@
# Change Log
All notable changes to this project will be documented in this file.
## [2.0.0](https://github.com/maxsokolov/TableKit/releases/tag/1.4.0)
Released on 2016-09-06. Breaking changes in 2.0.0:
<br/>The signatures of `TableRow` and `TableRowAction` classes were changed from
```swift
let action = TableRowAction<String, StringTableViewCell>(.click) { (data) in
}
let row = TableRow<String, StringTableViewCell>(item: "some string", actions: [action])
```
to
```swift
let action = TableRowAction<StringTableViewCell>(.click) { (data) in
}
let row = TableRow<StringTableViewCell>(item: "some string", actions: [action])
```
This is the great improvement that comes from the community. Thanks a lot!
## [1.3.0](https://github.com/maxsokolov/TableKit/releases/tag/1.3.0)
Released on 2016-09-04. Swift 3.0 support.
## [0.1.0](https://github.com/maxsokolov/TableKit/releases/tag/0.1.0)
Released on 2015-11-15. Initial release called Tablet.

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

@ -4,7 +4,7 @@
<a href="https://travis-ci.org/maxsokolov/TableKit"><img src="https://api.travis-ci.org/maxsokolov/TableKit.svg" alt="Build Status" /></a>
<a href="https://developer.apple.com/swift"><img src="https://img.shields.io/badge/Swift_3.0-compatible-4BC51D.svg?style=flat" alt="Swift 3.0 compatible" /></a>
<a href="https://github.com/Carthage/Carthage"><img src="https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat" alt="Carthage compatible" /></a>
<a href="https://cocoapods.org/pods/tablekit"><img src="https://img.shields.io/badge/pod-1.3.1-blue.svg" alt="CocoaPods compatible" /></a>
<a href="https://cocoapods.org/pods/tablekit"><img src="https://img.shields.io/badge/pod-2.0.0-blue.svg" alt="CocoaPods compatible" /></a>
<img src="https://img.shields.io/badge/platform-iOS-blue.svg?style=flat" alt="Platform iOS" />
<a href="https://raw.githubusercontent.com/maxsokolov/tablekit/master/LICENSE"><img src="http://img.shields.io/badge/license-MIT-blue.svg?style=flat" alt="License: MIT" /></a>
</p>
@ -36,9 +36,9 @@ Create your rows:
```swift
import TableKit
let row1 = TableRow<String, StringTableViewCell>(item: "1")
let row2 = TableRow<Int, IntTableViewCell>(item: 2)
let row3 = TableRow<User, UserTableViewCell>(item: User(name: "John Doe", rating: 5))
let row1 = TableRow<StringTableViewCell>(item: "1")
let row2 = TableRow<IntTableViewCell>(item: 2)
let row3 = TableRow<UserTableViewCell>(item: User(name: "John Doe", rating: 5))
```
Put rows into section:
```swift
@ -84,20 +84,20 @@ You could have as many rows and sections as you need.
It nice to have some actions that related to your cells:
```swift
let action = TableRowAction<String, StringTableViewCell>(.click) { (data) in
let action = TableRowAction<StringTableViewCell>(.click) { (data) in
// you could access any useful information that relates to the action
// data.cell - StringTableViewCell?
// data.item - String
// data.indexPath - NSIndexPath
// data.indexPath - IndexPath
}
let row = TableRow<String, StringTableViewCell>(item: "some", actions: [action])
let row = TableRow<StringTableViewCell>(item: "some", actions: [action])
```
Or, using nice chaining approach:
```swift
let row = TableRow<String, StringTableViewCell>(item: "some")
let row = TableRow<StringTableViewCell>(item: "some")
.action(.click) { (data) in
}
@ -126,7 +126,7 @@ class MyTableViewCell: UITableViewCell, ConfigurableCell {
```
And handle them accordingly:
```swift
let myAction = TableRowAction<Void, MyTableViewCell>(.custom(MyActions.ButtonClicked)) { (data) in
let myAction = TableRowAction<MyTableViewCell>(.custom(MyActions.ButtonClicked)) { (data) in
}
```
@ -173,11 +173,11 @@ It's never been so easy to deal with table views.
```swift
let users = /* some users array */
let click = TableRowAction<String, UserTableViewCell>(.click) {
let click = TableRowAction<UserTableViewCell>(.click) {
}
let rows = users.filter({ $0.state == .active }).map({ TableRow<String, UserTableViewCell>(item: $0.name, actions: [click]) })
let rows = users.filter({ $0.state == .active }).map({ TableRow<UserTableViewCell>(item: $0.name, actions: [click]) })
tableDirector += rows
```
@ -215,6 +215,10 @@ Clone the repo and drag files from `Sources` folder into your Xcode project.
- iOS 8.0
- Xcode 8.0
# Changelog
Keep eye on [changes](CHANGELOG.md).
# License
TableKit is available under the MIT license. See LICENSE for details.

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)
}
}

View File

@ -2,7 +2,7 @@ Pod::Spec.new do |s|
s.name = 'TableKit'
s.module_name = 'TableKit'
s.version = '1.3.1'
s.version = '2.0.0'
s.homepage = 'https://github.com/maxsokolov/TableKit'
s.summary = 'Type-safe declarative table views with Swift.'

View File

@ -94,7 +94,7 @@ class TabletTests: XCTestCase {
let data = TestData(title: "title")
let row = TableRow<TestData, TestTableViewCell>(item: data)
let row = TableRow<TestTableViewCell>(item: data)
testController.tableDirector += row
testController.tableView.reloadData()
@ -111,7 +111,7 @@ class TabletTests: XCTestCase {
let data = [TestData(title: "1"), TestData(title: "2"), TestData(title: "3")]
let rows: [Row] = data.map({ TableRow<TestData, TestTableViewCell>(item: $0) })
let rows: [Row] = data.map({ TableRow<TestTableViewCell>(item: $0) })
testController.tableDirector += rows
testController.tableView.reloadData()
@ -129,7 +129,7 @@ class TabletTests: XCTestCase {
func testTableSectionCreatesSectionWithHeaderAndFooterTitles() {
let row = TableRow<TestData, TestTableViewCell>(item: TestData(title: "title"))
let row = TableRow<TestTableViewCell>(item: TestData(title: "title"))
let sectionHeaderTitle = "Header Title"
let sectionFooterTitle = "Footer Title"
@ -148,7 +148,7 @@ class TabletTests: XCTestCase {
func testTableSectionCreatesSectionWithHeaderAndFooterViews() {
let row = TableRow<TestData, TestTableViewCell>(item: TestData(title: "title"))
let row = TableRow<TestTableViewCell>(item: TestData(title: "title"))
let sectionHeaderView = UIView()
let sectionFooterView = UIView()
@ -170,7 +170,7 @@ class TabletTests: XCTestCase {
let expectation = self.expectation(description: "cell action")
let row = TableRow<TestData, TestTableViewCell>(item: TestData(title: "title"))
let row = TableRow<TestTableViewCell>(item: TestData(title: "title"))
.action(TableRowAction(.custom(TestTableViewCellOptions.CellAction)) { (data) in
XCTAssertNotNil(data.cell, "Action data should have a cell")