diff --git a/recycler-view-divider/src/main/kotlin/com/fondesa/recyclerviewdivider/Divider.kt b/recycler-view-divider/src/main/kotlin/com/fondesa/recyclerviewdivider/Divider.kt index 46c28985..46664bf7 100644 --- a/recycler-view-divider/src/main/kotlin/com/fondesa/recyclerviewdivider/Divider.kt +++ b/recycler-view-divider/src/main/kotlin/com/fondesa/recyclerviewdivider/Divider.kt @@ -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 } /** @@ -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 } /** @@ -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 } diff --git a/recycler-view-divider/src/test/kotlin/com/fondesa/recyclerviewdivider/DividerTest.kt b/recycler-view-divider/src/test/kotlin/com/fondesa/recyclerviewdivider/DividerTest.kt index 1ecbd59e..f3341f95 100644 --- a/recycler-view-divider/src/test/kotlin/com/fondesa/recyclerviewdivider/DividerTest.kt +++ b/recycler-view-divider/src/test/kotlin/com/fondesa/recyclerviewdivider/DividerTest.kt @@ -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, @@ -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 ) @@ -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, diff --git a/recycler-view-divider/src/test/kotlin/com/fondesa/recyclerviewdivider/test/CreateDummyGrid.kt b/recycler-view-divider/src/test/kotlin/com/fondesa/recyclerviewdivider/test/CreateDummyGrid.kt index de4b30e5..aa6ccff7 100644 --- a/recycler-view-divider/src/test/kotlin/com/fondesa/recyclerviewdivider/test/CreateDummyGrid.kt +++ b/recycler-view-divider/src/test/kotlin/com/fondesa/recyclerviewdivider/test/CreateDummyGrid.kt @@ -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, @@ -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)