Skip to content

Commit

Permalink
Fix end and bottom divider detection on GridLayoutManager (#108)
Browse files Browse the repository at this point in the history
* Fix end and bottom divider detection

* Fix lint
  • Loading branch information
fondesa authored May 26, 2020
1 parent 927dea8 commit 919f4fa
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,9 @@ data class Divider(
val isBottomDivider: Boolean
get() {
if (orientation.isVertical) return false
val lastY = if (isGridVertical) grid.linesCount else grid.lines[originX].cellsCount
return originY == lastY
if (isGridVertical) return originY == grid.linesCount
val line = grid.lines[originX]
return originY == line.cellsCount && line.isFilled
}

/**
Expand All @@ -136,8 +137,9 @@ data class Divider(
val isEndDivider: Boolean
get() {
if (orientation.isHorizontal) return false
val lastX = if (isGridVertical) grid.lines[originY].cellsCount else grid.linesCount
return originX == lastX
if (isGridHorizontal) return originX == grid.linesCount
val line = grid.lines[originY]
return originX == line.cellsCount && line.isFilled
}

/**
Expand Down Expand Up @@ -167,4 +169,8 @@ data class Divider(
val isSideDivider: Boolean get() = if (isGridVertical) isStartDivider || isEndDivider else isTopDivider || isBottomDivider

private val isGridVertical: Boolean get() = grid.orientation.isVertical

private val isGridHorizontal: Boolean get() = grid.orientation.isHorizontal

private val Line.isFilled: Boolean get() = cells.sumBy { it.spanSize } == grid.spanCount
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class DividerTest {
}

@Test
fun `isBottomDivider - divider horizontal, grid horizontal, originY equal to cells count in given line - returns false`() {
fun `isBottomDivider - divider horizontal, grid horizontal, originY not equal to cells count in given line - returns false`() {
val divider = Divider(
grid = dummyGrid(orientation = Orientation.HORIZONTAL, cellsInLines = intArrayOf(3, 2, 3)),
originX = 2,
Expand All @@ -126,11 +126,23 @@ class DividerTest {
}

@Test
fun `isBottomDivider - divider horizontal, grid horizontal, originY equal to cells count in given line - returns true`() {
fun `isBottomDivider - divider horizontal, grid horizontal, originY equal to cells count in line, line not filled - returns false`() {
val divider = Divider(
grid = dummyGrid(orientation = Orientation.HORIZONTAL, cellsInLines = intArrayOf(3, 2, 3)),
grid = dummyGrid(orientation = Orientation.HORIZONTAL, cellsInLines = intArrayOf(3, 2, 2), fillLine = false),
originX = 2,
originY = 3,
originY = 2,
orientation = Orientation.HORIZONTAL
)

assertFalse(divider.isBottomDivider)
}

@Test
fun `isBottomDivider - divider horizontal, grid horizontal, originY equal to cells count in line, line filled - returns true`() {
val divider = Divider(
grid = dummyGrid(orientation = Orientation.HORIZONTAL, cellsInLines = intArrayOf(3, 2, 2)),
originX = 2,
originY = 2,
orientation = Orientation.HORIZONTAL
)

Expand Down Expand Up @@ -222,7 +234,19 @@ class DividerTest {
}

@Test
fun `isEndDivider - divider vertical, grid vertical, originX equal to cells count in given line - returns true`() {
fun `isEndDivider - divider vertical, grid vertical, originX equal to cells count in given line, line not filled - returns false`() {
val divider = Divider(
grid = dummyGrid(orientation = Orientation.VERTICAL, cellsInLines = intArrayOf(3, 2, 2), fillLine = false),
originX = 2,
originY = 2,
orientation = Orientation.VERTICAL
)

assertFalse(divider.isEndDivider)
}

@Test
fun `isEndDivider - divider vertical, grid vertical, originX equal to cells count in given line, line filled - returns true`() {
val divider = Divider(
grid = dummyGrid(orientation = Orientation.VERTICAL, cellsInLines = intArrayOf(3, 2, 3)),
originX = 3,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,15 @@ import com.fondesa.recyclerviewdivider.Orientation
*
* @param orientation the grid's orientation.
* @param cellsInLines the array of cells count for each line. The lines count is defined with the array size.
* @param fillLine true if the sum of the span sizes of the cells in each line should be equal to the span count.
* E.g. the array [2, 5, 3] creates a grid with 3 lines having 2 cells in the first line, 5 cells in the second line and 3 in the third.
* @return the grid used in test methods.
*/
internal fun dummyGrid(orientation: Orientation = Orientation.VERTICAL, cellsInLines: IntArray = intArrayOf()): Grid {
internal fun dummyGrid(
orientation: Orientation = Orientation.VERTICAL,
cellsInLines: IntArray = intArrayOf(),
fillLine: Boolean = true
): Grid {
val spanCount = cellsInLines.max() ?: 1
return Grid(
spanCount = spanCount,
Expand All @@ -44,7 +49,7 @@ internal fun dummyGrid(orientation: Orientation = Orientation.VERTICAL, cellsInL
),
lines = cellsInLines.map { cellsCount ->
val cells = List(cellsCount) { index ->
val spanSize = if (index == cellsCount - 1) spanCount - cellsCount + 1 else 1
val spanSize = if (index == cellsCount - 1 && fillLine) spanCount - cellsCount + 1 else 1
Cell(spanSize = spanSize)
}
Line(cells = cells)
Expand Down

0 comments on commit 919f4fa

Please sign in to comment.