Skip to content

Commit

Permalink
Merge branch 'merge/5'
Browse files Browse the repository at this point in the history
  • Loading branch information
ishkawa committed Sep 14, 2018
2 parents b2d1dcc + b4c8e14 commit 3580555
Show file tree
Hide file tree
Showing 21 changed files with 205 additions and 19 deletions.
12 changes: 12 additions & 0 deletions DataSourceKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
objects = {

/* Begin PBXBuildFile section */
553ACE5D21461F1700710FDF /* C.swift in Sources */ = {isa = PBXBuildFile; fileRef = 553ACE5C21461F1700710FDF /* C.swift */; };
553ACE5F21461F4B00710FDF /* CCollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 553ACE5E21461F4B00710FDF /* CCollectionViewCell.swift */; };
553ACE6121461F7300710FDF /* CTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 553ACE6021461F7300710FDF /* CTableViewCell.swift */; };
7F59442B213FA22E007300FC /* TableViewDataSource.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F59442A213FA22E007300FC /* TableViewDataSource.swift */; };
7F9AEDAD213BBBA10092854F /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F9AEDAC213BBBA10092854F /* AppDelegate.swift */; };
7F9AEDB4213BBBA30092854F /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 7F9AEDB3213BBBA30092854F /* Assets.xcassets */; };
Expand Down Expand Up @@ -89,6 +92,9 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
553ACE5C21461F1700710FDF /* C.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = C.swift; sourceTree = "<group>"; };
553ACE5E21461F4B00710FDF /* CCollectionViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CCollectionViewCell.swift; sourceTree = "<group>"; };
553ACE6021461F7300710FDF /* CTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CTableViewCell.swift; sourceTree = "<group>"; };
7F59442A213FA22E007300FC /* TableViewDataSource.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TableViewDataSource.swift; sourceTree = "<group>"; };
7F9AEDAA213BBBA10092854F /* Demo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Demo.app; sourceTree = BUILT_PRODUCTS_DIR; };
7F9AEDAC213BBBA10092854F /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -290,6 +296,9 @@
7FB64C1E213B5BC80071FC26 /* BCollectionViewCell.swift */,
7FC38D2E213FA8B3009FCEB1 /* BTableViewCell.xib */,
7FC38D30213FA8BC009FCEB1 /* BTableViewCell.swift */,
553ACE5C21461F1700710FDF /* C.swift */,
553ACE5E21461F4B00710FDF /* CCollectionViewCell.swift */,
553ACE6021461F7300710FDF /* CTableViewCell.swift */,
);
path = Example;
sourceTree = "<group>";
Expand Down Expand Up @@ -530,8 +539,11 @@
7FC38D29213FA6B7009FCEB1 /* B.swift in Sources */,
7FC38D33213FA908009FCEB1 /* TableViewDataSourceTests.swift in Sources */,
7FB64C1F213B5BC80071FC26 /* BCollectionViewCell.swift in Sources */,
553ACE5D21461F1700710FDF /* C.swift in Sources */,
7FB64C1D213B5BC20071FC26 /* ACollectionViewCell.swift in Sources */,
7FB64C1B213B59300071FC26 /* TestCollectionView.swift in Sources */,
553ACE6121461F7300710FDF /* CTableViewCell.swift in Sources */,
553ACE5F21461F4B00710FDF /* CCollectionViewCell.swift in Sources */,
7FC38D35213FA917009FCEB1 /* TestTableView.swift in Sources */,
7FC38D31213FA8BC009FCEB1 /* BTableViewCell.swift in Sources */,
7FC38D27213FA585009FCEB1 /* A.swift in Sources */,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
18 changes: 15 additions & 3 deletions DataSourceKit/CellBinder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,20 @@
import Foundation

public struct CellBinder {
public let nib: UINib

public enum RegistrationMethod {
case nib(UINib)
case `class`(AnyClass)
case none
}

public let registrationMethod: RegistrationMethod
public let reuseIdentifier: String

internal let configureCell: (Any) -> Void

public init<Cell>(cellType: Cell.Type, nib: UINib, reuseIdentifier: String, configureCell: @escaping (Cell) -> Void) {
self.nib = nib
public init<Cell>(cellType: Cell.Type, registrationMethod: RegistrationMethod, reuseIdentifier: String, configureCell: @escaping (Cell) -> Void) {
self.registrationMethod = registrationMethod
self.reuseIdentifier = reuseIdentifier
self.configureCell = { cell in
guard let cell = cell as? Cell else {
Expand All @@ -25,4 +32,9 @@ public struct CellBinder {
configureCell(cell)
}
}

@available(*, deprecated, message: "Use `init<Cell>(cellType: Cell.Type, registrationMethod: RegistrationMethod, reuseIdentifier: String, configureCell: @escaping (Cell) -> Void)` instead")
public init<Cell>(cellType: Cell.Type, nib: UINib, reuseIdentifier: String, configureCell: @escaping (Cell) -> Void) {
self.init(cellType: cellType, registrationMethod: .nib(nib), reuseIdentifier: reuseIdentifier, configureCell: configureCell)
}
}
9 changes: 8 additions & 1 deletion DataSourceKit/CollectionViewDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@ public class CollectionViewDataSource<CellDeclaration>: NSObject, UICollectionVi
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cellBinder = binderFromDeclaration(cellDeclarations[indexPath.item])
if !registeredReuseIdentifiers.contains(cellBinder.reuseIdentifier) {
collectionView.register(cellBinder.nib, forCellWithReuseIdentifier: cellBinder.reuseIdentifier)
switch cellBinder.registrationMethod {
case let .nib(nib):
collectionView.register(nib, forCellWithReuseIdentifier: cellBinder.reuseIdentifier)
case let .class(cellClass):
collectionView.register(cellClass, forCellWithReuseIdentifier: cellBinder.reuseIdentifier)
case .none:
break
}
registeredReuseIdentifiers.append(cellBinder.reuseIdentifier)
}

Expand Down
11 changes: 8 additions & 3 deletions DataSourceKit/TableViewDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,18 @@ public class TableViewDataSource<CellDeclaration>: NSObject, UITableViewDataSour
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellBinder = binderFromDeclaration(cellDeclarations[indexPath.item])
if !registeredReuseIdentifiers.contains(cellBinder.reuseIdentifier) {
tableView.register(cellBinder.nib, forCellReuseIdentifier: cellBinder.reuseIdentifier)
switch cellBinder.registrationMethod {
case let .nib(nib):
tableView.register(nib, forCellReuseIdentifier: cellBinder.reuseIdentifier)
case let .class(cellClass):
tableView.register(cellClass, forCellReuseIdentifier: cellBinder.reuseIdentifier)
case .none:
break
}
registeredReuseIdentifiers.append(cellBinder.reuseIdentifier)
}

let cell = tableView.dequeueReusableCell(withIdentifier: cellBinder.reuseIdentifier, for: indexPath)
cellBinder.configureCell(cell)

return cell
}
}
17 changes: 15 additions & 2 deletions DataSourceKitTests/CollectionViewDataSourceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ class CollectionViewDataSourceTests: XCTestCase {
enum CellDeclaration {
case a(A)
case b(B)
case c(C)
}

struct Data: CellsDeclarator {
var a: [A]
var b: [B]
var c: [C]

func declareCells(_ cell: (CellDeclaration) -> Void) {
for a in a {
Expand All @@ -27,6 +29,9 @@ class CollectionViewDataSourceTests: XCTestCase {
for b in b {
cell(.b(b))
}
for c in c {
cell(.c(c))
}
}
}

Expand All @@ -42,6 +47,8 @@ class CollectionViewDataSourceTests: XCTestCase {
return ACollectionViewCell.makeBinder(value: a)
case .b(let b):
return BCollectionViewCell.makeBinder(value: b)
case .c(let c):
return CCollectionViewCell.makeBinder(value: c)
}
}

Expand All @@ -50,13 +57,14 @@ class CollectionViewDataSourceTests: XCTestCase {
}

func test() {
let data = Data(a: [A(id: 1), A(id: 2)], b: [B(id: 1)])
let data = Data(a: [A(id: 1), A(id: 2)], b: [B(id: 1)], c: [C(id: 1)])
dataSource.cellDeclarations = data.cellDeclarations
collectionView.reloadData()
collectionView.layoutIfNeeded()

XCTAssertEqual(dataSource.collectionView(collectionView, numberOfItemsInSection: 0), 3)
XCTAssertEqual(dataSource.collectionView(collectionView, numberOfItemsInSection: 0), 4)
XCTAssertEqual(collectionView.nibRegistrations.map({ $0.reuseIdentifier }), ["ACollectionViewCell", "BCollectionViewCell"])
XCTAssertEqual(collectionView.classRegistrations.map({ $0.reuseIdentifier }), ["CCollectionViewCell"])

let verifiers: [(UICollectionViewCell?) -> Void] = [
{ cell in
Expand All @@ -74,6 +82,11 @@ class CollectionViewDataSourceTests: XCTestCase {
XCTAssertNotNil(cell)
XCTAssertEqual(cell?.idLabel.text, "1")
},
{ cell in
let cell = cell as? CCollectionViewCell
XCTAssertNotNil(cell)
XCTAssertEqual(cell?.idLabel.text, "1")
}
]

for (index, verifier) in verifiers.enumerated() {
Expand Down
2 changes: 1 addition & 1 deletion DataSourceKitTests/Example/ACollectionViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ extension ACollectionViewCell: BindableCell {
static func makeBinder(value: A) -> CellBinder {
return CellBinder(
cellType: ACollectionViewCell.self,
nib: UINib(nibName: "ACollectionViewCell", bundle: Bundle(for: ACollectionViewCell.self)),
registrationMethod: .nib(UINib(nibName: "ACollectionViewCell", bundle: Bundle(for: ACollectionViewCell.self))),
reuseIdentifier: "ACollectionViewCell",
configureCell: { cell in
cell.idLabel.text = String(value.id)
Expand Down
2 changes: 1 addition & 1 deletion DataSourceKitTests/Example/ATableViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ extension ATableViewCell: BindableCell {
static func makeBinder(value: A) -> CellBinder {
return CellBinder(
cellType: ATableViewCell.self,
nib: UINib(nibName: "ATableViewCell", bundle: Bundle(for: ATableViewCell.self)),
registrationMethod: .nib(UINib(nibName: "ATableViewCell", bundle: Bundle(for: ATableViewCell.self))),
reuseIdentifier: "ATableViewCell",
configureCell: { cell in
cell.idLabel.text = String(value.id)
Expand Down
2 changes: 1 addition & 1 deletion DataSourceKitTests/Example/BCollectionViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ extension BCollectionViewCell: BindableCell {
static func makeBinder(value: B) -> CellBinder {
return CellBinder(
cellType: BCollectionViewCell.self,
nib: UINib(nibName: "BCollectionViewCell", bundle: Bundle(for: BCollectionViewCell.self)),
registrationMethod: .nib(UINib(nibName: "BCollectionViewCell", bundle: Bundle(for: BCollectionViewCell.self))),
reuseIdentifier: "BCollectionViewCell",
configureCell: { cell in
cell.idLabel.text = String(value.id)
Expand Down
2 changes: 1 addition & 1 deletion DataSourceKitTests/Example/BTableViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ extension BTableViewCell: BindableCell {
static func makeBinder(value: B) -> CellBinder {
return CellBinder(
cellType: BTableViewCell.self,
nib: UINib(nibName: "BTableViewCell", bundle: Bundle(for: BTableViewCell.self)),
registrationMethod: .nib(UINib(nibName: "BTableViewCell", bundle: Bundle(for: BTableViewCell.self))),
reuseIdentifier: "BTableViewCell",
configureCell: { cell in
cell.idLabel.text = String(value.id)
Expand Down
13 changes: 13 additions & 0 deletions DataSourceKitTests/Example/C.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// C.swift
// DataSourceKitTests
//
// Created by zhzh liu on 2018/9/10.
// Copyright © 2018年 Yosuke Ishikawa. All rights reserved.
//

import Foundation

struct C {
let id: Int64
}
26 changes: 26 additions & 0 deletions DataSourceKitTests/Example/CCollectionViewCell.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// CCollectionViewCell.swift
// DataSourceKitTests
//
// Created by zhzh liu on 2018/9/10.
// Copyright © 2018年 Yosuke Ishikawa. All rights reserved.
//

import UIKit
import DataSourceKit

class CCollectionViewCell: UICollectionViewCell {
var idLabel = UILabel(frame: .zero)
}

extension CCollectionViewCell: BindableCell {
static func makeBinder(value: C) -> CellBinder {
return CellBinder(
cellType: CCollectionViewCell.self,
registrationMethod: .class(CCollectionViewCell.self),
reuseIdentifier: "CCollectionViewCell",
configureCell: { (cell) in
cell.idLabel.text = "\(value.id)"
})
}
}
40 changes: 40 additions & 0 deletions DataSourceKitTests/Example/CTableViewCell.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// CTableViewCell.swift
// DataSourceKitTests
//
// Created by zhzh liu on 2018/9/10.
// Copyright © 2018年 Yosuke Ishikawa. All rights reserved.
//

import UIKit
import DataSourceKit

class CTableViewCell: UITableViewCell {
var idLabel = UILabel(frame: .zero)

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
contentView.addSubview(idLabel)
idLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor)
idLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor)
idLabel.heightAnchor.constraint(equalToConstant: 44.0)
idLabel.topAnchor.constraint(equalTo: contentView.topAnchor)
idLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor)
}

required init?(coder aDecoder: NSCoder) {
fatalError()
}
}

extension CTableViewCell: BindableCell {
static func makeBinder(value: C) -> CellBinder {
return CellBinder(
cellType: CTableViewCell.self,
registrationMethod: .class(CTableViewCell.self),
reuseIdentifier: "CTableViewCell",
configureCell: { (cell) in
cell.idLabel.text = "\(value.id)"
})
}
}
17 changes: 15 additions & 2 deletions DataSourceKitTests/TableViewDataSourceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ class TableViewDataSourceTests: XCTestCase {
enum CellDeclaration {
case a(A)
case b(B)
case c(C)
}

struct Data: CellsDeclarator {
var a: [A]
var b: [B]
var c: [C]

func declareCells(_ cell: (CellDeclaration) -> Void) {
for a in a {
Expand All @@ -27,6 +29,9 @@ class TableViewDataSourceTests: XCTestCase {
for b in b {
cell(.b(b))
}
for c in c {
cell(.c(c))
}
}
}

Expand All @@ -42,6 +47,8 @@ class TableViewDataSourceTests: XCTestCase {
return ATableViewCell.makeBinder(value: a)
case .b(let b):
return BTableViewCell.makeBinder(value: b)
case .c(let c):
return CTableViewCell.makeBinder(value: c)
}
}

Expand All @@ -50,13 +57,14 @@ class TableViewDataSourceTests: XCTestCase {
}

func test() {
let data = Data(a: [A(id: 1), A(id: 2)], b: [B(id: 1)])
let data = Data(a: [A(id: 1), A(id: 2)], b: [B(id: 1)], c: [C(id: 1)])
dataSource.cellDeclarations = data.cellDeclarations
tableView.reloadData()
tableView.layoutIfNeeded()

XCTAssertEqual(dataSource.tableView(tableView, numberOfRowsInSection: 0), 3)
XCTAssertEqual(dataSource.tableView(tableView, numberOfRowsInSection: 0), 4)
XCTAssertEqual(tableView.nibRegistrations.map({ $0.reuseIdentifier }), ["ATableViewCell", "BTableViewCell"])
XCTAssertEqual(tableView.classRegistrations.map({ $0.reuseIdentifier }), ["CTableViewCell"])

let verifiers: [(UITableViewCell?) -> Void] = [
{ cell in
Expand All @@ -74,6 +82,11 @@ class TableViewDataSourceTests: XCTestCase {
XCTAssertNotNil(cell)
XCTAssertEqual(cell?.idLabel.text, "1")
},
{ cell in
let cell = cell as? CTableViewCell
XCTAssertNotNil(cell)
XCTAssertEqual(cell?.idLabel.text, "1")
}
]

for (index, verifier) in verifiers.enumerated() {
Expand Down
Loading

0 comments on commit 3580555

Please sign in to comment.