Skip to content

Commit 507f820

Browse files
authored
Merge pull request #58 from cheonjaeung/refactor-utils
Refactor utilities
2 parents bd25b87 + c531ac7 commit 507f820

File tree

4 files changed

+71
-53
lines changed

4 files changed

+71
-53
lines changed

grid/src/commonMain/kotlin/com/cheonjaeung/compose/grid/BoxGridMeasurePolicy.kt

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -248,22 +248,6 @@ private class BoxGridMeasureHelper(
248248
}
249249
}
250250

251-
private fun <T> mutableListOfNulls(size: Int): MutableList<T?> {
252-
return MutableList(size) { null }
253-
}
254-
255-
private fun Placeable.size(): IntSize {
256-
return IntSize(width = width, height = height)
257-
}
258-
259-
private fun <T> Iterable<T>.sumOfIndexed(selector: (Int, T) -> Int): Int {
260-
var sum = 0
261-
for ((index, element) in this.withIndex()) {
262-
sum += selector(index, element)
263-
}
264-
return sum
265-
}
266-
267251
class GridMeasureResult(
268252
val layoutSize: Size,
269253
val placeableMeasureInfos: List<PlaceableMeasureInfo?>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.cheonjaeung.compose.grid
2+
3+
import kotlin.math.max
4+
5+
/**
6+
* Returns a new [IntArray] with specified [size] filled with 0.
7+
*/
8+
@Suppress("NOTHING_TO_INLINE")
9+
internal inline fun intArrayOfZeros(size: Int): IntArray {
10+
return IntArray(size) { 0 }
11+
}
12+
13+
/**
14+
* Returns a new [MutableList] with specified [size] filled with `null`.
15+
*/
16+
@Suppress("NOTHING_TO_INLINE")
17+
internal inline fun <T> mutableListOfNulls(size: Int): MutableList<T?> {
18+
return MutableList(size) { null }
19+
}
20+
21+
/**
22+
* Returns the largest element or 0 if there are no elements.
23+
*/
24+
internal fun IntArray.maxOrZero(): Int {
25+
if (this.isEmpty()) {
26+
return 0
27+
}
28+
var maxValue = this[0]
29+
for (i in 1 until this.size) {
30+
maxValue = max(maxValue, this[i])
31+
}
32+
return maxValue
33+
}
34+
35+
/**
36+
* Returns the sum of the all values produced by [selector] function applied to each element.
37+
*/
38+
internal inline fun <T> Iterable<T>.sumOfIndexed(selector: (Int, T) -> Int): Int {
39+
var sum = 0
40+
for ((index, element) in this.withIndex()) {
41+
sum += selector(index, element)
42+
}
43+
return sum
44+
}

grid/src/commonMain/kotlin/com/cheonjaeung/compose/grid/HorizontalVerticalGridMeasurePolicy.kt

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ private class HorizontalVerticalGridMeasureHelper(
204204
crossAxisPlacedSpace += crossAxisCellConstraints + crossAxisSpaceAfterLast
205205
placeableMainAxisSizeMax = max(
206206
placeableMainAxisSizeMax,
207-
placeable.mainAxisSize()
207+
placeable.mainAxisSize(orientation)
208208
)
209209
crossAxisLineLayoutSize = max(crossAxisLineLayoutSize, crossAxisPlacedSpace)
210210
crossAxisIndex += span
@@ -255,15 +255,15 @@ private class HorizontalVerticalGridMeasureHelper(
255255
val crossAxisCount = measureResult.crossAxisCount
256256
val mainAxisLayoutSize = measureResult.mainAxisLayoutSize
257257
val crossAxisLayoutSize = measureResult.crossAxisLayoutSize
258-
val mainAxisPositions = IntArray(mainAxisCount) { 0 }
259-
val crossAxisPositions = IntArray(crossAxisCount) { 0 }
258+
val mainAxisPositions = intArrayOfZeros(mainAxisCount)
259+
val crossAxisPositions = intArrayOfZeros(crossAxisCount)
260260

261-
val mainAxisBiggestChildrenSizes = IntArray(mainAxisCount) { 0 }
261+
val mainAxisBiggestChildrenSizes = intArrayOfZeros(mainAxisCount)
262262
for (m in 0 until mainAxisCount) {
263263
val currentLinePlaceables = placeableMeasureInfoTable[m]
264264
val currentLineChildrenSizes = IntArray(currentLinePlaceables.size) { index ->
265265
val placeable = currentLinePlaceables[index].placeable
266-
placeable.mainAxisSize()
266+
placeable.mainAxisSize(orientation)
267267
}
268268
mainAxisBiggestChildrenSizes[m] = currentLineChildrenSizes.maxOrZero()
269269
}
@@ -356,38 +356,6 @@ private class HorizontalVerticalGridMeasureHelper(
356356
}
357357
}
358358

359-
/**
360-
* Returns main axis size of this [Placeable] by current orientation.
361-
*/
362-
private fun Placeable.mainAxisSize(): Int {
363-
return if (orientation == LayoutOrientation.Horizontal) {
364-
width
365-
} else {
366-
height
367-
}
368-
}
369-
370-
/**
371-
* Returns the size of this [Placeable].
372-
*/
373-
private fun Placeable.size(): IntSize {
374-
return IntSize(width = width, height = height)
375-
}
376-
377-
/**
378-
* Returns the largest element or 0 if there are no elements.
379-
*/
380-
private fun IntArray.maxOrZero(): Int {
381-
if (this.isEmpty()) {
382-
return 0
383-
}
384-
var maxValue = this[0]
385-
for (i in 1 until this.size) {
386-
maxValue = max(maxValue, this[i])
387-
}
388-
return maxValue
389-
}
390-
391359
/**
392360
* Result data of [HorizontalVerticalGridMeasureHelper.measure].
393361
*
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.cheonjaeung.compose.grid
2+
3+
import androidx.compose.ui.layout.Placeable
4+
import androidx.compose.ui.unit.IntSize
5+
6+
/**
7+
* Returns an [IntSize] containing the width and height size of the [Placeable].
8+
*/
9+
internal fun Placeable.size(): IntSize {
10+
return IntSize(width, height)
11+
}
12+
13+
/**
14+
* Returns the size of the main axis of the [Placeable].
15+
*/
16+
internal fun Placeable.mainAxisSize(orientation: LayoutOrientation): Int {
17+
return if (orientation == LayoutOrientation.Horizontal) {
18+
width
19+
} else {
20+
height
21+
}
22+
}

0 commit comments

Comments
 (0)