Skip to content

Commit

Permalink
Merge pull request #25 from rubensousa/dev
Browse files Browse the repository at this point in the history
2.0.2
  • Loading branch information
rubensousa committed Jun 6, 2022
2 parents c473435 + 293cbd4 commit 3d4d9e3
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 108 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 2.0.2

- Moved `setDecorationLookup` to `AbstractMarginDecoration`
- Added new `shouldApplyDecorationAt` to `AbstractMarginDecoration`

# 2.0.1

- Added support for `ConcatAdapter`. `DecorationLookup` now receives the absolute adapter position.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Decorator is a library that helps creating composable margins and dividers in Re
## Install

```groovy
implementation 'com.rubensousa:decorator:2.0.1'
implementation 'com.rubensousa:decorator:2.0.2'
```

## How to use
Expand Down
2 changes: 1 addition & 1 deletion decorator/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
LIBRARY_VERSION=2.0.1
LIBRARY_VERSION=2.0.2
LIBRARY_GROUP=com.rubensousa
LIBRARY_ARTIFACT=decorator
# POM info
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import androidx.recyclerview.widget.RecyclerView
* A base [RecyclerView.ItemDecoration] that checks if item offsets should be applied
* for a given position using [DecorationLookup]
*/
abstract class AbstractMarginDecoration(private val decorationLookup: DecorationLookup?) :
abstract class AbstractMarginDecoration(private var decorationLookup: DecorationLookup?) :
RecyclerView.ItemDecoration() {

final override fun getItemOffsets(
Expand All @@ -36,14 +36,31 @@ abstract class AbstractMarginDecoration(private val decorationLookup: Decoration
val lm = parent.layoutManager as RecyclerView.LayoutManager
val layoutParams = view.layoutParams as RecyclerView.LayoutParams
val position = layoutParams.absoluteAdapterPosition
if (position != RecyclerView.NO_POSITION &&
(decorationLookup == null ||
decorationLookup.shouldApplyDecoration(position, lm.itemCount))
) {
if (shouldApplyDecorationAt(position, lm.itemCount)) {
getItemOffsets(outRect, view, position, parent, state, lm)
}
}

/**
* @param decorationLookup an optional [DecorationLookup] to filter positions
* that shouldn't have this decoration applied to
*/
fun setDecorationLookup(decorationLookup: DecorationLookup?) {
this.decorationLookup = decorationLookup
}

/**
* @return true if decoration will be applied for [position]
* or false if position is not valid
* or [decorationLookup] doesn't allow decoration for this [position]
*/
fun shouldApplyDecorationAt(position: Int, itemCount: Int): Boolean {
if (position == RecyclerView.NO_POSITION) {
return false
}
return decorationLookup?.shouldApplyDecoration(position, itemCount) ?: true
}

abstract fun getItemOffsets(
outRect: Rect,
view: View,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class GridBoundsMarginDecoration(
private var columnProvider: ColumnProvider,
private var orientation: Int = RecyclerView.VERTICAL,
private var inverted: Boolean = false,
private var decorationLookup: DecorationLookup? = null
decorationLookup: DecorationLookup? = null
) : AbstractMarginDecoration(decorationLookup) {

companion object {
Expand Down Expand Up @@ -194,10 +194,6 @@ class GridBoundsMarginDecoration(

fun isInverted() = inverted

fun setDecorationLookup(decorationLookup: DecorationLookup?) {
this.decorationLookup = decorationLookup
}

override fun getItemOffsets(
outRect: Rect,
view: View,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class GridDividerDecoration(
private val columnProvider: ColumnProvider,
private var orientation: Int = RecyclerView.VERTICAL,
private var inverted: Boolean = false,
private var decorationLookup: DecorationLookup? = null
decorationLookup: DecorationLookup? = null
) : AbstractMarginDecoration(decorationLookup) {

companion object {
Expand Down Expand Up @@ -125,14 +125,6 @@ class GridDividerDecoration(

fun isInverted() = inverted

/**
* @param decorationLookup an optional [DecorationLookup] to filter positions
* that shouldn't have this decoration applied to
*/
fun setDecorationLookup(decorationLookup: DecorationLookup?) {
this.decorationLookup = decorationLookup
}

override fun getItemOffsets(
outRect: Rect,
view: View,
Expand All @@ -158,27 +150,16 @@ class GridDividerDecoration(
for (i in 0 until parent.childCount) {
val child = parent.getChildAt(i)
val adapterPosition = parent.getChildAdapterPosition(child)
if (canApplyDecorationAt(adapterPosition, itemCount)) {
if (shouldApplyDecorationAt(adapterPosition, itemCount)) {
if (orientation == RecyclerView.VERTICAL) {
drawVertical(c, child, adapterPosition, itemCount, columns, layoutManager)
drawVertical(c, child, adapterPosition, itemCount, columns)
} else {
drawHorizontal(c, child, adapterPosition, itemCount, columns, layoutManager)
drawHorizontal(c, child, adapterPosition, itemCount, columns)
}
}
}
}

private fun canApplyDecorationAt(position: Int, itemCount: Int): Boolean {
if (position == RecyclerView.NO_POSITION) {
return false
}
if (decorationLookup == null) {
return true
}

return decorationLookup!!.shouldApplyDecoration(position, itemCount)
}

private fun applyVerticalOffsets(outRect: Rect, position: Int, columns: Int, itemCount: Int) {
val columnIndex = position.rem(columns)
val lines = Math.ceil(itemCount / columns.toDouble()).toInt()
Expand Down Expand Up @@ -297,15 +278,14 @@ class GridDividerDecoration(
view: View,
position: Int,
itemCount: Int,
columns: Int,
layoutManager: RecyclerView.LayoutManager
columns: Int
) {
val bottomPosition = if (!inverted) {
getBottomPosition(position, columns, itemCount)
} else {
getTopPosition(position, columns)
}
if (bottomPosition != null && canApplyDecorationAt(bottomPosition, itemCount)) {
if (bottomPosition != null && shouldApplyDecorationAt(bottomPosition, itemCount)) {
canvas.drawRect(
view.left.toFloat() + heightMargin,
view.bottom.toFloat() + widthMargin,
Expand All @@ -316,7 +296,7 @@ class GridDividerDecoration(
}

val rightPosition = getRightPosition(position, columns, itemCount)
if (rightPosition != null && canApplyDecorationAt(rightPosition, itemCount)) {
if (rightPosition != null && shouldApplyDecorationAt(rightPosition, itemCount)) {
canvas.drawRect(
view.right.toFloat() + widthMargin,
view.top.toFloat() + heightMargin,
Expand All @@ -332,15 +312,14 @@ class GridDividerDecoration(
view: View,
position: Int,
itemCount: Int,
columns: Int,
layoutManager: RecyclerView.LayoutManager
columns: Int
) {
val bottomPosition = if (!inverted) {
getBottomPosition(position, columns, itemCount)
} else {
getTopPosition(position, columns)
}
if (bottomPosition != null && canApplyDecorationAt(bottomPosition, itemCount)) {
if (bottomPosition != null && shouldApplyDecorationAt(bottomPosition, itemCount)) {
canvas.drawRect(
view.right.toFloat() + widthMargin,
view.top.toFloat() + heightMargin,
Expand All @@ -351,7 +330,7 @@ class GridDividerDecoration(
}

val rightPosition = getRightPosition(position, columns, itemCount)
if (rightPosition != null && canApplyDecorationAt(rightPosition, itemCount)) {
if (rightPosition != null && shouldApplyDecorationAt(rightPosition, itemCount)) {
canvas.drawRect(
view.left.toFloat() + heightMargin,
view.bottom.toFloat() + widthMargin,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class GridMarginDecoration(
private var columnProvider: ColumnProvider,
private var orientation: Int = RecyclerView.VERTICAL,
private var inverted: Boolean = false,
private var decorationLookup: DecorationLookup? = null
decorationLookup: DecorationLookup? = null
) : AbstractMarginDecoration(decorationLookup) {

companion object {
Expand Down Expand Up @@ -160,14 +160,6 @@ class GridMarginDecoration(
this.inverted = isInverted
}

/**
* @param decorationLookup an optional [DecorationLookup] to filter positions
* that shouldn't have this decoration applied to
*/
fun setDecorationLookup(decorationLookup: DecorationLookup?) {
this.decorationLookup = decorationLookup
}

override fun getItemOffsets(
outRect: Rect,
view: View,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class GridSpanBoundsMarginDecoration(
@Px private var rightMargin: Int = 0,
@Px private var bottomMargin: Int = 0,
private var gridLayoutManager: GridLayoutManager,
private var decorationLookup: DecorationLookup? = null
decorationLookup: DecorationLookup? = null
) : AbstractMarginDecoration(decorationLookup) {

fun setMargin(margin: Int) {
Expand Down Expand Up @@ -78,15 +78,6 @@ class GridSpanBoundsMarginDecoration(

fun getBottomMargin() = bottomMargin

/**
* @param decorationLookup an optional [DecorationLookup] to filter positions
* that shouldn't have this decoration applied to
*/
fun setDecorationLookup(decorationLookup: DecorationLookup?) {
this.decorationLookup = decorationLookup
}


override fun getItemOffsets(
outRect: Rect,
view: View,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class GridSpanMarginDecoration(
@Px private var horizontalMargin: Int,
@Px private var verticalMargin: Int,
private var gridLayoutManager: GridLayoutManager,
private var decorationLookup: DecorationLookup? = null
decorationLookup: DecorationLookup? = null
) : AbstractMarginDecoration(decorationLookup) {

companion object {
Expand Down Expand Up @@ -117,14 +117,6 @@ class GridSpanMarginDecoration(
gridLayoutManager = layoutManager
}

/**
* @param decorationLookup an optional [DecorationLookup] to filter positions
* that shouldn't have this decoration applied to
*/
fun setDecorationLookup(decorationLookup: DecorationLookup?) {
this.decorationLookup = decorationLookup
}

override fun getItemOffsets(
outRect: Rect,
view: View,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class LinearBoundsMarginDecoration(
@Px private var bottomMargin: Int = 0,
private var orientation: Int = RecyclerView.VERTICAL,
private var inverted: Boolean = false,
private var decorationLookup: DecorationLookup? = null
decorationLookup: DecorationLookup? = null
) : AbstractMarginDecoration(decorationLookup) {

companion object {
Expand Down Expand Up @@ -155,14 +155,6 @@ class LinearBoundsMarginDecoration(
this.inverted = inverted
}

/**
* @param decorationLookup an optional [DecorationLookup] to filter positions
* that shouldn't have this decoration applied to
*/
fun setDecorationLookup(decorationLookup: DecorationLookup?) {
this.decorationLookup = decorationLookup
}

override fun getItemOffsets(
outRect: Rect,
view: View,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class LinearDividerDecoration(
private var inverted: Boolean = false,
private var addBeforeFirstPosition: Boolean = false,
private var addAfterLastPosition: Boolean = false,
private var decorationLookup: DecorationLookup? = null
decorationLookup: DecorationLookup? = null
) : AbstractMarginDecoration(decorationLookup) {

companion object {
Expand Down Expand Up @@ -175,14 +175,6 @@ class LinearDividerDecoration(

fun isInverted() = inverted

/**
* @param decorationLookup an optional [DecorationLookup] to filter positions
* that shouldn't have this decoration applied to
*/
fun setDecorationLookup(decorationLookup: DecorationLookup?) {
this.decorationLookup = decorationLookup
}

override fun getItemOffsets(
outRect: Rect,
view: View,
Expand Down Expand Up @@ -216,16 +208,6 @@ class LinearDividerDecoration(
}
}

private fun shouldApplyDecorationAt(position: Int, itemCount: Int): Boolean {
if (position == RecyclerView.NO_POSITION) {
return false
}
if (decorationLookup == null) {
return true
}
return decorationLookup!!.shouldApplyDecoration(position, itemCount)
}

private fun applyVerticalOffsets(outRect: Rect, position: Int, itemCount: Int) {
// Add space for the divider from the bottom if not inverted
if (!inverted) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class LinearMarginDecoration(
private var inverted: Boolean = false,
private var addBeforeFirstPosition: Boolean = true,
private var addAfterLastPosition: Boolean = true,
private var decorationLookup: DecorationLookup? = null
decorationLookup: DecorationLookup? = null
) : AbstractMarginDecoration(decorationLookup) {

companion object {
Expand Down Expand Up @@ -193,14 +193,6 @@ class LinearMarginDecoration(
this.inverted = inverted
}

/**
* @param decorationLookup an optional [DecorationLookup] to filter positions
* that shouldn't have this decoration applied to
*/
fun setDecorationLookup(decorationLookup: DecorationLookup?) {
this.decorationLookup = decorationLookup
}

override fun getItemOffsets(
outRect: Rect,
view: View,
Expand Down

0 comments on commit 3d4d9e3

Please sign in to comment.