Skip to content

Commit

Permalink
1.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
landarskiy authored Jul 13, 2023
2 parents c0f1a57 + d9a2212 commit e11af9e
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 53 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Change Log
==========

## Version 1.1.2

_2023-07-13_

* **Fix**: size distribution algorithm
* Dependency updates

## Version 1.1.1

_2023-05-23_
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ with a horizontal direction from start to end.

```kotlin
GridPad(
cells = GridPadCells(rowCount = 3, columnCount = 4)
cells = GridPadCells(rowCount = 4, columnCount = 3)
) {
item {
// 1-st item, row = 0, column = 0
Expand Down Expand Up @@ -213,7 +213,7 @@ GridPad(
) {
item(row = 1, column = 3, rowSpan = 1, columnSpan = 3) {
// will be skipped in a drawing process because the item is placed in the column range [3;5]
// but the maximum allowable 3
// but the maximum allowable is 3
}
}
```
Expand Down
20 changes: 10 additions & 10 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
[versions]
androidGradlePlugin = "8.0.1"
androidxActivity = "1.7.1"
androidxComposeBom = "2023.05.01"
androidGradlePlugin = "8.0.2"
androidxActivity = "1.7.2"
androidxComposeBom = "2023.06.01"
# @keep for plugin, not used directly
androidxComposeCompiler = "1.4.7"
androidxComposeCompiler = "1.4.8"
androidxCore = "1.10.1"
androidxEspresso = "3.5.1"
androidxLifecycleRuntime = "2.6.1"
androidxTestExt = "1.1.5"
detekt = "1.22.0"
dokka = "1.8.10"
detekt = "1.23.0"
dokka = "1.8.20"
junit4 = "4.13.2"
# @pin to use with compose
kotlin = "1.8.21"
kotlinCompatibilityValidator = "0.13.1"
kotlin = "1.8.22"
kotlinCompatibilityValidator = "0.13.2"
kotlinxCollectionsImmutable = "0.3.5"
kover = "0.7.0"
kover = "0.7.2"
nexusPublish = "1.3.0"
robolectric = "4.10.3"
versionsChecker = "0.46.0"
versionsChecker = "0.47.0"
versionsUpdater = "0.8.0"

[libraries]
Expand Down
2 changes: 1 addition & 1 deletion gridpad/artifact.properties
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
# Artifact related properties
groupId=com.touchlane
artifactId=gridpad
version=1.1.1
version=1.1.2
publicationName=gridpad
pomDescription=Jetpack Compose layout
pomUrl=https://github.com/touchlane/gridpad-android
Expand Down
41 changes: 11 additions & 30 deletions gridpad/src/main/kotlin/com/touchlane/gridpad/GridPadDsl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,7 @@ private fun MeasureScope.calculateCellPlaces(
}

/**
* Calculate sizes for row or column.
* The remaining pixels should be distributed equally between the last cells in cases where
* the first pass did not distribute them. For example, we have to split 101px among 3 cells
* with a weight equal to 1. This means that after the first pass the cell sizes
* would be [33, 33, 33] with the remainder size equal to 2. In a second pass,
* the remaining 2 pixels will be distributed between the last 2 cells and the final
* result will be [33, 34, 34].
* Calculate sizes in pixels for each row or column.
*
* @param availableSize parent size that available for distribution
* @param cellSizes sizes for rows or columns
Expand All @@ -173,38 +167,25 @@ private fun MeasureScope.calculateCellPlaces(
private fun MeasureScope.calculateSizesForDimension(
availableSize: Int, cellSizes: ImmutableList<GridPadCellSize>, totalSize: TotalSize
): List<Int> {
val availableWeight = availableSize - totalSize.fixed.roundToPx()

//First pass calculation
var distributedSize = 0
val sizes = cellSizes.map { cellSize ->
val availableWeight = availableSize - totalSize.fixed.toPx()
var reminder = 0f
return cellSizes.map { cellSize ->
when (cellSize) {
is GridPadCellSize.Fixed -> {
val size = cellSize.size.roundToPx()
distributedSize += size
val floatSize = cellSize.size.toPx() + reminder
val size = floatSize.roundToInt()
reminder = floatSize - size
size
}

is GridPadCellSize.Weight -> {
val size = (availableWeight * cellSize.size / totalSize.weight).toInt()
distributedSize += size
val floatSize = availableWeight * cellSize.size / totalSize.weight + reminder
val size = floatSize.roundToInt()
reminder = floatSize - size
size
}
}
}.toMutableList()

//Distribute remaining pixels
var notDistributedSize = availableSize - distributedSize
cellSizes.reversed().mapIndexed { index, gridPadCellSize ->
if (notDistributedSize <= 0) {
return@mapIndexed
}
val originalIndex = cellSizes.size - index - 1
if (gridPadCellSize is GridPadCellSize.Weight) {
sizes[originalIndex] = sizes[originalIndex] + 1
notDistributedSize--
}
}
return sizes
}

/**
Expand Down
46 changes: 36 additions & 10 deletions gridpad/src/test/kotlin/com/touchlane/gridpad/GridPadScopeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner


@RunWith(RobolectricTestRunner::class)
class GridPadScopeTest : LoggerTest() {

Expand Down Expand Up @@ -149,13 +150,13 @@ class GridPadScopeTest : LoggerTest() {
val bounds00 = boundsForNodeWithText("0:0")
assertEquals(Rect(0f, 0f, 33f, 33f), bounds00)
val bounds01 = boundsForNodeWithText("0:1")
assertEquals(Rect(33f, 0f, 66f, 33f), bounds01)
assertEquals(Rect(33f, 0f, 67f, 33f), bounds01)
val bounds02 = boundsForNodeWithText("0:2")
assertEquals(Rect(66f, 0f, 100f, 33f), bounds02)
assertEquals(Rect(67f, 0f, 100f, 33f), bounds02)
val bounds10 = boundsForNodeWithText("1:0")
assertEquals(Rect(0f, 33f, 33f, 66f), bounds10)
assertEquals(Rect(0f, 33f, 33f, 67f), bounds10)
val bounds20 = boundsForNodeWithText("2:0")
assertEquals(Rect(0f, 66f, 33f, 100f), bounds20)
assertEquals(Rect(0f, 67f, 33f, 100f), bounds20)
}

@Test
Expand All @@ -170,9 +171,9 @@ class GridPadScopeTest : LoggerTest() {
}
}
val bounds00 = boundsForNodeWithText("0:0")
assertEquals(Rect(0f, 0f, 33f, 10f), bounds00)
assertEquals(Rect(0f, 0f, 34f, 10f), bounds00)
val bounds01 = boundsForNodeWithText("0:1")
assertEquals(Rect(33f, 0f, 67f, 10f), bounds01)
assertEquals(Rect(34f, 0f, 67f, 10f), bounds01)
val bounds02 = boundsForNodeWithText("0:2")
assertEquals(Rect(67f, 0f, 101f, 10f), bounds02)
}
Expand All @@ -193,13 +194,13 @@ class GridPadScopeTest : LoggerTest() {
}
}
val bounds00 = boundsForNodeWithText("0:0")
assertEquals(Rect(0f, 0f, 10f, 10f), bounds00)
assertEquals(Rect(0f, 0f, 11f, 10f), bounds00)
val bounds01 = boundsForNodeWithText("0:1")
assertEquals(Rect(10f, 0f, 21f, 10f), bounds01)
assertEquals(Rect(11f, 0f, 22f, 10f), bounds01)
val bounds02 = boundsForNodeWithText("0:2")
assertEquals(Rect(21f, 0f, 32f, 10f), bounds02)
assertEquals(Rect(22f, 0f, 33f, 10f), bounds02)
val bounds03 = boundsForNodeWithText("0:3")
assertEquals(Rect(32f, 0f, 43f, 10f), bounds03)
assertEquals(Rect(33f, 0f, 43f, 10f), bounds03)
val bounds04 = boundsForNodeWithText("0:4")
assertEquals(Rect(43f, 0f, 54f, 10f), bounds04)
val bounds05 = boundsForNodeWithText("0:5")
Expand All @@ -208,6 +209,31 @@ class GridPadScopeTest : LoggerTest() {
assertEquals(Rect(65f, 0f, 76f, 10f), bounds06)
}

@Test
fun `Check size distribution 108x10 with spans 1x5`() = with(composeTestRule) {
setContent {
Box(modifier = Modifier.size(108.dp, 10.dp)) {
GridPad(cells = GridPadCells(rowCount = 1, columnCount = 50)) {
repeat(10) {
item(columnSpan = 5) { MaxSizeText(text = "0:$it") }
}
}
}
}
val bounds00 = boundsForNodeWithText("0:0")
assertEquals(Rect(0f, 0f, 11f, 10f), bounds00)
val bounds01 = boundsForNodeWithText("0:1")
assertEquals(Rect(11f, 0f, 22f, 10f), bounds01)
val bounds02 = boundsForNodeWithText("0:2")
assertEquals(Rect(22f, 0f, 32f, 10f), bounds02)
val bounds03 = boundsForNodeWithText("0:3")
assertEquals(Rect(32f, 0f, 43f, 10f), bounds03)
val bounds04 = boundsForNodeWithText("0:4")
assertEquals(Rect(43f, 0f, 54f, 10f), bounds04)
val bounds05 = boundsForNodeWithText("0:5")
assertEquals(Rect(54f, 0f, 65f, 10f), bounds05)
}

@Test
fun `Check placement mainH horSE verTB LTR`() = with(composeTestRule) {
placeItems(
Expand Down

0 comments on commit e11af9e

Please sign in to comment.