Skip to content

Commit

Permalink
Use @discardableResult attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
xai3 committed Sep 29, 2016
1 parent 863ff6a commit 142967e
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 61 deletions.
18 changes: 9 additions & 9 deletions Classes/Section.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ extension Section {
return rowFor((indexPath as NSIndexPath).row)
}

public func removeRow(_ index: Int) -> RowType {
@discardableResult public func removeRow(_ index: Int) -> RowType {
return rows.remove(at: index)
}

Expand All @@ -42,47 +42,47 @@ extension Section {
}

extension Section {
public func add(row: RowType) -> Self {
@discardableResult public func add(row: RowType) -> Self {
rows.append(row)
return self
}

public func add(rows: [RowType]) -> Self {
@discardableResult public func add(rows: [RowType]) -> Self {
self.rows.append(contentsOf: rows)
return self
}

public func createRow<T>(_ closure: ((Row<T>) -> Void)) -> Self {
@discardableResult public func createRow<T>(_ closure: ((Row<T>) -> Void)) -> Self {
return add(row: Row<T>() { closure($0) })
}

public func createRows<T, E>(for elements: [E], closure: ((E, Row<T>) -> Void)) -> Self {
@discardableResult public func createRows<T, E>(for elements: [E], closure: ((E, Row<T>) -> Void)) -> Self {
return add(rows:
elements.map { element -> Row<T> in
return Row<T>() { closure(element, $0) }
}.map { $0 as RowType }
)
}

public func createRows<T>(for count: UInt, closure: ((UInt, Row<T>) -> Void)) -> Self {
@discardableResult public func createRows<T>(for count: UInt, closure: ((UInt, Row<T>) -> Void)) -> Self {
return createRows(for: [UInt](0..<count), closure: closure)
}

public func createHeader(_ closure: ((SectionHeaderFooter<HeaderType>) -> Void)) -> Self {
@discardableResult public func createHeader(_ closure: ((SectionHeaderFooter<HeaderType>) -> Void)) -> Self {
return createHeaderFooter { (header: SectionHeaderFooter<HeaderType>) in
self.header = header
closure(header)
}
}

public func createFooter(_ closure: ((SectionHeaderFooter<FooterType>) -> Void)) -> Self {
@discardableResult public func createFooter(_ closure: ((SectionHeaderFooter<FooterType>) -> Void)) -> Self {
return createHeaderFooter { (footer: SectionHeaderFooter<FooterType>) in
self.footer = footer
closure(footer)
}
}

fileprivate func createHeaderFooter<T>(_ closure: ((SectionHeaderFooter<T>) -> Void)) -> Self {
@discardableResult fileprivate func createHeaderFooter<T>(_ closure: ((SectionHeaderFooter<T>) -> Void)) -> Self {
closure(SectionHeaderFooter<T>())
return self
}
Expand Down
2 changes: 1 addition & 1 deletion Classes/SectionType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ public protocol SectionType {
func rowFor(_ row: Int) -> RowType
func rowFor(_ indexPath: IndexPath) -> RowType

func removeRow(_ index: Int) -> RowType
@discardableResult func removeRow(_ index: Int) -> RowType
func insertRow(_ row: RowType, index: Int)
}
12 changes: 6 additions & 6 deletions Classes/Source.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,29 @@ open class Source: NSObject {
return sections.count > (indexPath as NSIndexPath).section && sections[(indexPath as NSIndexPath).section].rows.count > (indexPath as NSIndexPath).row
}

open func add(section: SectionType) -> Self {
@discardableResult open func add(section: SectionType) -> Self {
sections.append(section)
return self
}

open func add(sections: [SectionType]) -> Self {
@discardableResult open func add(sections: [SectionType]) -> Self {
self.sections.append(contentsOf: sections)
return self
}

open func createSection<H, F>(closure: ((Section<H, F>) -> Void)) -> Self {
@discardableResult open func createSection<H, F>(closure: ((Section<H, F>) -> Void)) -> Self {
return add(section: Section<H, F>() { closure($0) })
}

open func createSections<H, F, E>(for elements: [E], closure: ((E, Section<H, F>) -> Void)) -> Self {
@discardableResult open func createSections<H, F, E>(for elements: [E], closure: ((E, Section<H, F>) -> Void)) -> Self {
return add(sections:
elements.map { element -> Section<H, F> in
return Section<H, F>() { closure(element, $0) }
}.map { $0 as SectionType }
)
}

open func createSections<H, F>(for count: UInt, closure: ((UInt, Section<H, F>) -> Void)) -> Self {
@discardableResult open func createSections<H, F>(for count: UInt, closure: ((UInt, Section<H, F>) -> Void)) -> Self {
return createSections(for: [UInt](0..<count), closure: closure)
}

Expand Down Expand Up @@ -154,7 +154,7 @@ extension Source: UITableViewDataSource {

switch editingStyle {
case .delete:
_ = self.section(for: indexPath).removeRow((indexPath as NSIndexPath).row)
self.section(for: indexPath).removeRow((indexPath as NSIndexPath).row)
let animation = delegate.willRemove(tableView, indexPath: indexPath)
tableView.deleteRows(at: [indexPath], with: animation)
delegate.didRemove(tableView, indexPath: indexPath)
Expand Down
8 changes: 4 additions & 4 deletions ShoyuExample/TableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ class TableViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableView.source = Source().createSection { (section: Section<HeaderTableViewCell, FooterTableViewCell>) in
let _ = section.createHeader { header in
section.createHeader { header in
header.reuseIdentifier = "Header"
header.height = 32
header.configureView = { headerCell, _ in
headerCell.contentView.backgroundColor = UIColor.blue
}
}
.createFooter { footer in
section.createFooter { footer in
footer.createView = { [weak self] _ in
return self?.createViewForFooterCell()
}
Expand All @@ -43,7 +43,7 @@ class TableViewController: UIViewController {
return 32
}
}
.createRows(for: members) { (member: Member, row: Row<DefaultTableViewCell>) in
section.createRows(for: members) { (member: Member, row: Row<DefaultTableViewCell>) in
row.height = 52
row.configureCell = configureMemberCell(member: member)
row.didSelect = didSelectMember(member: member)
Expand All @@ -64,7 +64,7 @@ class TableViewController: UIViewController {
print(event.row)
}
}
.createRows(for: 5) { (index: UInt, row: Row<DefaultTableViewCell>) -> Void in
section.createRows(for: 5) { (index: UInt, row: Row<DefaultTableViewCell>) -> Void in
row.heightFor = { _ -> CGFloat? in
return 44
}
Expand Down
50 changes: 25 additions & 25 deletions ShoyuTests/SectionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,86 +25,86 @@ class SectionTests: XCTestCase {

func testAddMultiRow() {
let section = Section()
_ = section.add(row: Row()).add(row: Row())
section.add(row: Row()).add(row: Row())
XCTAssertEqual(section.rows.count, 2)

// Method chain
_ = section.add(row: Row()).add(row: Row()).add(row: Row())
section.add(row: Row()).add(row: Row()).add(row: Row())
XCTAssertEqual(section.rows.count, 5)
}

func testAddRows() {
let section = Section()
_ = section.add(rows: [Row(), Row()])
section.add(rows: [Row(), Row()])
XCTAssertEqual(section.rows.count, 2)

// Method chain
_ = section.add(rows: [Row(), Row(), Row()])
section.add(rows: [Row(), Row(), Row()])
XCTAssertEqual(section.rows.count, 5)
}

func testCreateSingleRow() {
let section = Section() { section in
_ = section.createRow { _ in }
section.createRow { _ in }
}
XCTAssertEqual(section.rows.count, 1)
}

func testCreateMultiRow() {
let section = Section() { section in
_ = section.createRow { _ in }
_ = section.createRow { _ in }
section.createRow { _ in }
section.createRow { _ in }
}
XCTAssertEqual(section.rows.count, 2)
}

func testCreateCountRows() {
let count = UInt(10)
let section = Section() { section in
_ = section.createRows(for: count) { _ in }
section.createRows(for: count) { _ in }
}
XCTAssertEqual(section.rows.count, Int(count))
}

func testCreateMapArrayRows() {
let items = [1, 2, 3]
let section = Section() { section in
_ = section.createRows(for: items) { _ in }
section.createRows(for: items) { _ in }
}
XCTAssertEqual(section.rows.count, items.count)
}

func testCreateHeader() {
let section = Section() { section in
_ = section.createHeader { _ in }
section.createHeader { _ in }
}
XCTAssertNotNil(section.header)
}

func testCreateFooter() {
let section = Section() { section in
_ = section.createFooter { _ in }
section.createFooter { _ in }
}
XCTAssertNotNil(section.footer)
}

func testCreateHeaderFooter() {
let section = Section() { section in
_ = section.createHeader { _ in }
_ = section.createFooter { _ in }
section.createHeader { _ in }
section.createFooter { _ in }
}
XCTAssertNotNil(section.header)
XCTAssertNotNil(section.footer)
}

func testCreateHeaderGeneric() {
let section = Section() { (section: Section<UILabel, UIButton>) in
_ = section.createHeader { header -> Void in
section.createHeader { header -> Void in
header.configureView = { label, info in
label.text = "label"
}
}
_ = section.createFooter { footer -> Void in
section.createFooter { footer -> Void in
footer.configureView = { button, info in
button.titleLabel!.text = "button"
}
Expand All @@ -118,7 +118,7 @@ class SectionTests: XCTestCase {
let count = UInt(2)
let items = [1, 2, 3]
let section = Section() { section in
_ = section
section
.createRow { _ in }
.createRows(for: count) { _ in }
.createRows(for: items) { _ in }
Expand All @@ -133,7 +133,7 @@ class SectionTests: XCTestCase {
func testRemoveRow() {
let count = 10
let section = Section() { section in
_ = section.createRows(for: UInt(count)) { index, row in
section.createRows(for: UInt(count)) { index, row in
row.reuseIdentifier = String(index)
}
}
Expand All @@ -155,7 +155,7 @@ class SectionTests: XCTestCase {
func testInsertRow() {
let count = 10
let section = Section() { section in
_ = section.createRows(for: UInt(count)) { _, _ in }
section.createRows(for: UInt(count)) { _, _ in }
}
XCTAssertEqual(section.rowCount, count)

Expand All @@ -176,7 +176,7 @@ class SectionTests: XCTestCase {
XCTAssertEqual((section.footer as? SectionHeaderFooterDelegateType)?.heightFor(UITableView(), section: 0), nil)

// Constant
_ = section
section
.createHeader { header in
header.height = 10
}
Expand All @@ -187,7 +187,7 @@ class SectionTests: XCTestCase {
XCTAssertEqual((section.footer as? SectionHeaderFooterDelegateType)?.heightFor(UITableView(), section: 0), 11)

// Configure height
_ = section
section
.createHeader { header in
header.heightFor = { _ -> CGFloat? in
return 20
Expand All @@ -202,7 +202,7 @@ class SectionTests: XCTestCase {
XCTAssertEqual((section.footer as? SectionHeaderFooterDelegateType)?.heightFor(UITableView(), section: 0), 21)

// Configure nil height
_ = section
section
.createHeader { header in
header.height = 30
header.heightFor = { _ -> CGFloat? in
Expand Down Expand Up @@ -232,7 +232,7 @@ class SectionTests: XCTestCase {
XCTAssertEqual((section.footer as? SectionHeaderFooterDelegateType)?.titleFor(UITableView(), section: 0), nil)

// Constant
_ = section
section
.createHeader { header in
header.title = constantHeaderTitle
}.createFooter { footer in
Expand All @@ -242,7 +242,7 @@ class SectionTests: XCTestCase {
XCTAssertEqual((section.footer as? SectionHeaderFooterDelegateType)?.titleFor(UITableView(), section: 0), constantFooterTitle)

// Title for
_ = section
section
.createHeader { header in
header.titleFor = { _ -> String? in
return variableHeaderTitle
Expand All @@ -257,7 +257,7 @@ class SectionTests: XCTestCase {
XCTAssertEqual((section.footer as? SectionHeaderFooterDelegateType)?.titleFor(UITableView(), section: 0), variableFooterTitle)

// Both
_ = section
section
.createHeader { header in
header.title = constantHeaderTitle
header.titleFor = { _ -> String? in
Expand All @@ -274,7 +274,7 @@ class SectionTests: XCTestCase {
XCTAssertEqual((section.footer as? SectionHeaderFooterDelegateType)?.titleFor(UITableView(), section: 0), variableFooterTitle)

// Title for nil
_ = section
section
.createHeader { header in
header.title = constantHeaderTitle
header.titleFor = { _ -> String? in
Expand Down
Loading

0 comments on commit 142967e

Please sign in to comment.