Skip to content

Commit

Permalink
Merge pull request #16 from rubensousa/dev
Browse files Browse the repository at this point in the history
1.3.0
  • Loading branch information
rubensousa committed Jan 7, 2021
2 parents 182307d + 1f16d18 commit 4d68afb
Show file tree
Hide file tree
Showing 21 changed files with 625 additions and 234 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
with:
java-version: 1.8
- name: Install Android SDK
uses: malinskiy/action-android/install-sdk@release/0.0.5
uses: malinskiy/action-android/install-sdk@release/0.1.0
- name: Run unit tests
run: ./gradlew testDebugUnitTest --stacktrace -Pbintray_user=${{ secrets.BINTRAY_USER }} -Pbintray_apikey=${{ secrets.BINTRAY_KEY }}
- name: Assemble library
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
with:
java-version: 1.8
- name: Install Android SDK
uses: malinskiy/action-android/install-sdk@release/0.0.5
uses: malinskiy/action-android/install-sdk@release/0.1.0
- name: Run unit tests
run: ./gradlew testDebugUnitTest --stacktrace -Pbintray_user=${{ secrets.BINTRAY_USER }} -Pbintray_apikey=${{ secrets.BINTRAY_KEY }}
- name: Assemble library
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pull_requests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
with:
java-version: 1.8
- name: Install Android SDK
uses: malinskiy/action-android/install-sdk@release/0.0.5
uses: malinskiy/action-android/install-sdk@release/0.1.0
- name: Run unit tests
run: ./gradlew testDebugUnitTest --stacktrace -Pbintray_user=${{ secrets.BINTRAY_USER }} -Pbintray_apikey=${{ secrets.BINTRAY_KEY }}
- name: Assemble library
Expand Down
24 changes: 23 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
# 1.3.0

#### New DecorationLookup defaults

- Added `MergeDecorationLookup` to combine multiple `DecorationLookup`
- Added `SingleItemDecorationLookup` to disable decorations when there's only a single item

#### LinearDividerDecoration

- Added `addBeforeFirstPosition` and `addAfterLastPosition` parameter to control dividers at the first and last positions
- Fixed `LinearDividerDecoration` reporting wrong positions in its `DecorationLookup` [#14](https://github.com/rubensousa/Decorator/issues/14)
- Fixed `LinearDividerDecoration` not applying dividers correctly at the last position [#13](https://github.com/rubensousa/Decorator/issues/13)

#### LinearMarginDecoration

- Added `addBeforeFirstPosition` and `addAfterLastPosition` parameter to control margin at the first and last positions

#### GridSpanMarginDecoration & GridSpanBoundsMarginDecoration

- Added support for defining vertical and horizontal margins separately [#8](https://github.com/rubensousa/Decorator/issues/8) [#9](https://github.com/rubensousa/Decorator/issues/9)


# 1.2.0

- Fixed decorations not being applied correctly when the itemCount is 1
- Added `createVertical` and `createHorizontal` helpers to:
- `LinearMarginDecoration`
- `LinearBoundsMarginDecoration`
- `GridBoundsMarginDecoration
- `GridBoundsMarginDecoration`

# 1.1.0

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=1.2.0
LIBRARY_VERSION=1.3.0
LIBRARY_GROUP=io.cabriole
LIBRARY_ARTIFACT=decorator
# POM info
Expand Down
140 changes: 107 additions & 33 deletions decorator/src/main/java/io/cabriole/decorator/GridMarginDecoration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ import androidx.recyclerview.widget.RecyclerView
* This only works for grids with fixed span rows.
* For variable spanned rows, use [GridSpanMarginDecoration].
*
* @param margin margin to be applied to all sides of an item
* @param horizontalMargin margin to be applied to the start and end side of an item
*
* @param verticalMargin margin to be applied to the top and bottom sides of an item
*
* @param columnProvider a [ColumnProvider] that provides the number of columns
*
Expand All @@ -43,24 +45,96 @@ import androidx.recyclerview.widget.RecyclerView
* Any property change should be followed by [RecyclerView.invalidateItemDecorations]
*/
class GridMarginDecoration(
@Px private var margin: Int,
@Px private var horizontalMargin: Int,
@Px private var verticalMargin: Int,
private var columnProvider: ColumnProvider,
private var orientation: Int = RecyclerView.VERTICAL,
private var inverted: Boolean = false,
private var decorationLookup: DecorationLookup? = null
) : AbstractMarginDecoration(decorationLookup) {

fun getMargin() = margin
companion object {

/**
* Creates a [GridMarginDecoration] that applies the same margin to all sides
*/
@JvmStatic
fun create(
@Px margin: Int,
columnProvider: ColumnProvider,
orientation: Int = RecyclerView.VERTICAL,
inverted: Boolean = false,
decorationLookup: DecorationLookup? = null
): GridMarginDecoration {
return GridMarginDecoration(
verticalMargin = margin,
horizontalMargin = margin,
columnProvider = columnProvider,
orientation = orientation,
inverted = inverted,
decorationLookup = decorationLookup
)
}

@JvmStatic
fun createVertical(
@Px verticalMargin: Int,
columnProvider: ColumnProvider,
orientation: Int = RecyclerView.VERTICAL,
inverted: Boolean = false,
decorationLookup: DecorationLookup? = null
): GridMarginDecoration {
return GridMarginDecoration(
verticalMargin = verticalMargin,
horizontalMargin = 0,
columnProvider = columnProvider,
orientation = orientation,
inverted = inverted,
decorationLookup = decorationLookup
)
}

@JvmStatic
fun createHorizontal(
@Px horizontalMargin: Int,
columnProvider: ColumnProvider,
orientation: Int = RecyclerView.VERTICAL,
inverted: Boolean = false,
decorationLookup: DecorationLookup? = null
): GridMarginDecoration {
return GridMarginDecoration(
horizontalMargin = horizontalMargin,
verticalMargin = 0,
columnProvider = columnProvider,
orientation = orientation,
inverted = inverted,
decorationLookup = decorationLookup
)
}
}

fun getHorizontalMargin() = horizontalMargin

fun getVerticalMargin() = verticalMargin

fun getOrientation() = orientation

fun isInverted() = inverted

/**
* @param margin padding to be applied to all sides of an item
* @param margin margin to be applied to all sides of an item
*/
fun setMargin(margin: Int) {
this.margin = margin
this.horizontalMargin = margin
this.verticalMargin = margin
}

fun setHorizontalMargin(margin: Int) {
this.horizontalMargin = margin
}

fun setVerticalMargin(margin: Int) {
this.verticalMargin = margin
}

/**
Expand Down Expand Up @@ -122,38 +196,38 @@ class GridMarginDecoration(
lines: Int,
lineIndex: Int
) {
val startPadding = margin * ((columns - columnIndex) / columns.toFloat())
val endPadding = margin * ((columnIndex + 1) / columns.toFloat())
val startPadding = horizontalMargin * ((columns - columnIndex) / columns.toFloat())
val endPadding = horizontalMargin * ((columnIndex + 1) / columns.toFloat())
outRect.left = startPadding.toInt()
outRect.right = endPadding.toInt()

if (lineIndex == 0) {
if (!inverted) {
outRect.top = margin
outRect.top = verticalMargin
if (lines > 1) {
outRect.bottom = margin / 2
outRect.bottom = verticalMargin / 2
} else {
outRect.bottom = margin
outRect.bottom = verticalMargin
}
} else {
outRect.bottom = margin
outRect.bottom = verticalMargin
if (lines > 1) {
outRect.top = margin / 2
outRect.top = verticalMargin / 2
} else {
outRect.top = margin
outRect.top = verticalMargin
}
}
} else if (lineIndex == lines - 1) {
if (!inverted) {
outRect.top = margin / 2
outRect.bottom = margin
outRect.top = verticalMargin / 2
outRect.bottom = verticalMargin
} else {
outRect.top = margin
outRect.bottom = margin / 2
outRect.top = verticalMargin
outRect.bottom = verticalMargin / 2
}
} else {
outRect.top = margin / 2
outRect.bottom = margin / 2
outRect.top = verticalMargin / 2
outRect.bottom = verticalMargin / 2
}
}

Expand All @@ -164,39 +238,39 @@ class GridMarginDecoration(
lines: Int,
lineIndex: Int
) {
val startPadding = margin * ((columns - columnIndex) / columns.toFloat())
val endPadding = margin * ((columnIndex + 1) / columns.toFloat())
val startPadding = verticalMargin * ((columns - columnIndex) / columns.toFloat())
val endPadding = verticalMargin * ((columnIndex + 1) / columns.toFloat())

outRect.top = startPadding.toInt()
outRect.bottom = endPadding.toInt()

if (lineIndex == 0) {
if (!inverted) {
outRect.left = margin
outRect.left = horizontalMargin
if (lines > 1) {
outRect.right = margin / 2
outRect.right = horizontalMargin / 2
} else {
outRect.right = margin
outRect.right = horizontalMargin
}
} else {
outRect.right = margin
outRect.right = horizontalMargin
if (lines > 1) {
outRect.left = margin / 2
outRect.left = horizontalMargin / 2
} else {
outRect.left = margin
outRect.left = horizontalMargin
}
}
} else if (lineIndex == lines - 1) {
if (!inverted) {
outRect.left = margin / 2
outRect.right = margin
outRect.left = horizontalMargin / 2
outRect.right = horizontalMargin
} else {
outRect.left = margin
outRect.right = margin / 2
outRect.left = horizontalMargin
outRect.right = horizontalMargin / 2
}
} else {
outRect.left = margin / 2
outRect.right = margin / 2
outRect.left = horizontalMargin / 2
outRect.right = horizontalMargin / 2
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ import androidx.recyclerview.widget.RecyclerView
*
* Any property change should be followed by [RecyclerView.invalidateItemDecorations]
*
* Any property change in [margin] or [gridLayoutManager]
* should be followed by [RecyclerView.invalidateItemDecorations]
*/
class GridSpanBoundsMarginDecoration(
@Px private var leftMargin: Int = 0,
Expand Down

0 comments on commit 4d68afb

Please sign in to comment.