Skip to content

Commit d072685

Browse files
committed
Add SimpleGridCells.FixedSize
1 parent 5de159c commit d072685

16 files changed

+779
-0
lines changed

grid/api/android/grid.api

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,13 @@ public final class com/cheonjaeung/compose/grid/SimpleGridCells$Fixed : com/cheo
9191
public fun hashCode ()I
9292
}
9393

94+
public final class com/cheonjaeung/compose/grid/SimpleGridCells$FixedSize : com/cheonjaeung/compose/grid/SimpleGridCells {
95+
public static final field $stable I
96+
public synthetic fun <init> (FZILkotlin/jvm/internal/DefaultConstructorMarker;)V
97+
public synthetic fun <init> (FZLkotlin/jvm/internal/DefaultConstructorMarker;)V
98+
public fun calculateCrossAxisCellSizes (Landroidx/compose/ui/unit/Density;II)Ljava/util/List;
99+
public fun equals (Ljava/lang/Object;)Z
100+
public fun fillCellSize ()Z
101+
public fun hashCode ()I
102+
}
103+

grid/api/jvm/grid.api

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,13 @@ public final class com/cheonjaeung/compose/grid/SimpleGridCells$Fixed : com/cheo
9191
public fun hashCode ()I
9292
}
9393

94+
public final class com/cheonjaeung/compose/grid/SimpleGridCells$FixedSize : com/cheonjaeung/compose/grid/SimpleGridCells {
95+
public static final field $stable I
96+
public synthetic fun <init> (FZILkotlin/jvm/internal/DefaultConstructorMarker;)V
97+
public synthetic fun <init> (FZLkotlin/jvm/internal/DefaultConstructorMarker;)V
98+
public fun calculateCrossAxisCellSizes (Landroidx/compose/ui/unit/Density;II)Ljava/util/List;
99+
public fun equals (Ljava/lang/Object;)Z
100+
public fun fillCellSize ()Z
101+
public fun hashCode ()I
102+
}
103+

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,63 @@ interface SimpleGridCells {
134134
return hash
135135
}
136136
}
137+
138+
/**
139+
* Make grid to have as many rows or columns as possible and each cell has exactly [size].
140+
* If [size] is bigger than container's size, the cell will have the same size to the container.
141+
*
142+
* For example, `FixedSize(20.dp)` for `VerticalGrid(Modifier.width(66.dp)` means that there
143+
* will be 3 columns and each cell will have 20dp width and 6dp is remained.
144+
*
145+
* In other case, `FixedSize(150.dp)` for `VerticalGrid(Modifier.width(100.dp)` means that there
146+
* will be only one column and the cell will have 100dp width.
147+
*
148+
* @param size The size which each cell should have.
149+
* @param fill When `true`, item composable fill cell's width or height.
150+
*/
151+
@ExperimentalGridApi
152+
class FixedSize(
153+
private val size: Dp,
154+
private val fill: Boolean = true,
155+
) : SimpleGridCells {
156+
init {
157+
if (size <= 0.dp) {
158+
throw IllegalArgumentException("FixedSize size must be a positive, but $size")
159+
}
160+
}
161+
162+
override fun Density.calculateCrossAxisCellSizes(
163+
availableSize: Int,
164+
spacing: Int
165+
): List<Int> {
166+
val cellSize = size.roundToPx()
167+
val availableSizeWithSpacing = availableSize + spacing
168+
val cellSizeWithSpacing = cellSize + spacing
169+
170+
return if (cellSizeWithSpacing < availableSizeWithSpacing) {
171+
val count = availableSizeWithSpacing / cellSizeWithSpacing
172+
return List(count) { cellSize }
173+
} else {
174+
List(1) { availableSize }
175+
}
176+
}
177+
178+
override fun fillCellSize(): Boolean {
179+
return fill
180+
}
181+
182+
override fun equals(other: Any?): Boolean {
183+
if (other !is FixedSize) return false
184+
if (this.size != other.size) return false
185+
if (this.fill != other.fill) return false
186+
return true
187+
}
188+
189+
override fun hashCode(): Int {
190+
var hash = 1
191+
hash = hash * 31 + size.hashCode()
192+
hash = hash * 31 + fill.hashCode()
193+
return hash
194+
}
195+
}
137196
}

0 commit comments

Comments
 (0)