Skip to content

Commit 8087602

Browse files
committed
Move utility functions to separated files
1 parent bd25b87 commit 8087602

File tree

4 files changed

+60
-50
lines changed

4 files changed

+60
-50
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: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.cheonjaeung.compose.grid
2+
3+
import kotlin.math.max
4+
5+
/**
6+
* Returns a new [MutableList] with specified [size] filled with `null`.
7+
*/
8+
@Suppress("NOTHING_TO_INLINE")
9+
internal inline fun <T> mutableListOfNulls(size: Int): MutableList<T?> {
10+
return MutableList(size) { null }
11+
}
12+
13+
/**
14+
* Returns the largest element or 0 if there are no elements.
15+
*/
16+
internal fun IntArray.maxOrZero(): Int {
17+
if (this.isEmpty()) {
18+
return 0
19+
}
20+
var maxValue = this[0]
21+
for (i in 1 until this.size) {
22+
maxValue = max(maxValue, this[i])
23+
}
24+
return maxValue
25+
}
26+
27+
/**
28+
* Returns the sum of the all values produced by [selector] function applied to each element.
29+
*/
30+
internal inline fun <T> Iterable<T>.sumOfIndexed(selector: (Int, T) -> Int): Int {
31+
var sum = 0
32+
for ((index, element) in this.withIndex()) {
33+
sum += selector(index, element)
34+
}
35+
return sum
36+
}

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

Lines changed: 2 additions & 34 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
@@ -263,7 +263,7 @@ private class HorizontalVerticalGridMeasureHelper(
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)