Skip to content

Commit

Permalink
Merge pull request #86 from kazuhiro4949/feature/bugfix_rectForItem
Browse files Browse the repository at this point in the history
added guard statement when numberOfItem is zero
  • Loading branch information
kazuhiro4949 authored Jan 20, 2019
2 parents 7598316 + 9511e46 commit d29cdee
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 8 deletions.
21 changes: 16 additions & 5 deletions PagingKit/PagingMenuView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,11 @@ open class PagingMenuView: UIScrollView {
/// space setting between cells
open var cellSpacing: CGFloat = 0

/// total space between cells
open var totalSpacing: CGFloat {
return cellSpacing * numberOfCellSpacing
}

/// The object that acts as the data source of the paging menu view.
open weak var dataSource: PagingMenuViewDataSource?

Expand Down Expand Up @@ -346,12 +351,22 @@ open class PagingMenuView: UIScrollView {
/// - Parameter index: An index that identifies a item by its index.
/// - Returns: A rectangle defining the area in which the table view draws the row or right edge rect if index is over the number of items.
open func rectForItem(at index: Int) -> CGRect {
guard 0 < widths.count else {
return CGRect(x: 0, y: 0, width: 0, height: bounds.height)
}

guard index < widths.count else {
let rightEdge = widths.reduce(CGFloat(0)) { (sum, width) in sum + width }
let rightEdge = widths.reduce(CGFloat(0)) { (sum, width) in sum + width } + totalSpacing
let mostRightWidth = widths[widths.endIndex - 1]
return CGRect(x: rightEdge, y: 0, width: mostRightWidth, height: bounds.height)
}

guard 0 <= index else {
let leftEdge = -widths[0]
let mostLeftWidth = widths[0]
return CGRect(x: leftEdge, y: 0, width: mostLeftWidth, height: bounds.height)
}

var x = (0..<index).reduce(0) { (sum, idx) in
return sum + widths[idx]
}
Expand Down Expand Up @@ -515,10 +530,6 @@ open class PagingMenuView: UIScrollView {
return max(CGFloat(numberOfItem - 1), 0)
}

private var totalSpacing: CGFloat {
return cellSpacing * numberOfCellSpacing
}

private func recenterIfNeeded() {
let currentOffset = contentOffset
let contentWidth = contentSize.width
Expand Down
129 changes: 126 additions & 3 deletions PagingKitTests/PagingMenuViewTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,132 @@ class PagingMenuViewTests: XCTestCase {
pagingMenuView?.dataSource = dataSource
pagingMenuView?.reloadData()

let rect = pagingMenuView?.rectForItem(at: 3)
XCTAssertEqual(rect,
CGRect(x: 300, y: 0, width: 100, height: 44), "get correct rect")

do {
let rect = pagingMenuView?.rectForItem(at: -2)
XCTAssertEqual(rect,
CGRect(x: -100, y: 0, width: 100, height: 44), "get correct rect")
}

do {
let rect = pagingMenuView?.rectForItem(at: -1)
XCTAssertEqual(rect,
CGRect(x: -100, y: 0, width: 100, height: 44), "get correct rect")
}

do {
// first index
let rect = pagingMenuView?.rectForItem(at: 0)
XCTAssertEqual(rect,
CGRect(x: 0, y: 0, width: 100, height: 44), "get correct rect")
}

do {
let rect = pagingMenuView?.rectForItem(at: 3)
XCTAssertEqual(rect,
CGRect(x: 300, y: 0, width: 100, height: 44), "get correct rect")
}

do {
// last index
let rect = pagingMenuView?.rectForItem(at: 19)
XCTAssertEqual(rect,
CGRect(x: 1900, y: 0, width: 100, height: 44), "get correct rect")
}

do {
let rect = pagingMenuView?.rectForItem(at: 20)
XCTAssertEqual(rect,
CGRect(x: 2000, y: 0, width: 100, height: 44), "get correct rect")
}

do {
let rect = pagingMenuView?.rectForItem(at: 21)
XCTAssertEqual(rect,
CGRect(x: 2000, y: 0, width: 100, height: 44), "get correct rect")
}
}

func testRectForItemWithSpacing() {
let dataSource = MenuViewDataSourceSpy()
dataSource.widthForItem = 100
dataSource.registerNib(to: pagingMenuView)
dataSource.data = Array(repeating: "foo", count: 20)
pagingMenuView?.dataSource = dataSource
pagingMenuView?.cellSpacing = 10
pagingMenuView?.reloadData()


do {
let rect = pagingMenuView?.rectForItem(at: -2)
XCTAssertEqual(rect,
CGRect(x: -100, y: 0, width: 100, height: 44), "get correct rect")
}

do {
let rect = pagingMenuView?.rectForItem(at: -1)
XCTAssertEqual(rect,
CGRect(x: -100, y: 0, width: 100, height: 44), "get correct rect")
}

do {
// first index
let rect = pagingMenuView?.rectForItem(at: 0)
XCTAssertEqual(rect,
CGRect(x: 0, y: 0, width: 100, height: 44), "get correct rect")
}

do {
let rect = pagingMenuView?.rectForItem(at: 3)
XCTAssertEqual(rect,
CGRect(x: 330, y: 0, width: 100, height: 44), "get correct rect")
}

do {
// last index
let rect = pagingMenuView?.rectForItem(at: 19)
XCTAssertEqual(rect,
CGRect(x: 2090, y: 0, width: 100, height: 44), "get correct rect")
}

do {
let rect = pagingMenuView?.rectForItem(at: 20)
XCTAssertEqual(rect,
CGRect(x: 2190, y: 0, width: 100, height: 44), "get correct rect")
}

do {
let rect = pagingMenuView?.rectForItem(at: 21)
XCTAssertEqual(rect,
CGRect(x: 2190, y: 0, width: 100, height: 44), "get correct rect")
}
}

func testRectForItemWhenDataSourceHasNoElement() {
let dataSource = MenuViewDataSourceSpy()
dataSource.widthForItem = 100
dataSource.registerNib(to: pagingMenuView)
dataSource.data = []
pagingMenuView?.dataSource = dataSource
pagingMenuView?.reloadData()

do {
let rect = pagingMenuView?.rectForItem(at: -1)
XCTAssertEqual(rect,
CGRect(x: 0, y: 0, width: 0, height: 44), "get correct rect")
}

do {
let rect = pagingMenuView?.rectForItem(at: 0)
XCTAssertEqual(rect,
CGRect(x: 0, y: 0, width: 0, height: 44), "get correct rect")
}

do {
let rect = pagingMenuView?.rectForItem(at: 1)
XCTAssertEqual(rect,
CGRect(x: 0, y: 0, width: 0, height: 44), "get correct rect")
}
}

func testRectForItemToEdgeCase() {
Expand Down

0 comments on commit d29cdee

Please sign in to comment.