Skip to content

Commit 3c6541e

Browse files
andkulikovGerrit Code Review
authored and
Gerrit Code Review
committed
Merge "Add contentType for lazy list, grid and staggered grid layout item info" into androidx-main
2 parents d0dfda7 + f70cd50 commit 3c6541e

18 files changed

+192
-15
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
// Baseline format: 1.0
2+
AddedAbstractMethod: androidx.compose.foundation.lazy.grid.LazyGridItemInfo#getContentType():
3+
Added method androidx.compose.foundation.lazy.grid.LazyGridItemInfo.getContentType()
4+
5+
26
InvalidNullConversion: androidx.compose.foundation.MutatorMutex#mutateWith(T, androidx.compose.foundation.MutatePriority, kotlin.jvm.functions.Function2<? super T,? super kotlin.coroutines.Continuation<? super R>,?>, kotlin.coroutines.Continuation<? super R>) parameter #0:
37
Attempted to change parameter from @Nullable to @NonNull: incompatible change for parameter receiver in androidx.compose.foundation.MutatorMutex.mutateWith(T receiver, androidx.compose.foundation.MutatePriority priority, kotlin.jvm.functions.Function2<? super T,? super kotlin.coroutines.Continuation<? super R>,?> block, kotlin.coroutines.Continuation<? super R> arg4)

compose/foundation/foundation/api/current.txt

+6
Original file line numberDiff line numberDiff line change
@@ -403,10 +403,12 @@ package androidx.compose.foundation.lazy {
403403
}
404404

405405
public interface LazyListItemInfo {
406+
method public default Object? getContentType();
406407
method public int getIndex();
407408
method public Object getKey();
408409
method public int getOffset();
409410
method public int getSize();
411+
property public default Object? contentType;
410412
property public abstract int index;
411413
property public abstract Object key;
412414
property public abstract int offset;
@@ -514,12 +516,14 @@ package androidx.compose.foundation.lazy.grid {
514516

515517
public sealed interface LazyGridItemInfo {
516518
method public int getColumn();
519+
method public Object? getContentType();
517520
method public int getIndex();
518521
method public Object getKey();
519522
method public long getOffset();
520523
method public int getRow();
521524
method public long getSize();
522525
property public abstract int column;
526+
property public abstract Object? contentType;
523527
property public abstract int index;
524528
property public abstract Object key;
525529
property public abstract long offset;
@@ -624,11 +628,13 @@ package androidx.compose.foundation.lazy.staggeredgrid {
624628
}
625629

626630
public sealed interface LazyStaggeredGridItemInfo {
631+
method public Object? getContentType();
627632
method public int getIndex();
628633
method public Object getKey();
629634
method public int getLane();
630635
method public long getOffset();
631636
method public long getSize();
637+
property public abstract Object? contentType;
632638
property public abstract int index;
633639
property public abstract Object key;
634640
property public abstract int lane;

compose/foundation/foundation/api/public_plus_experimental_current.txt

+6
Original file line numberDiff line numberDiff line change
@@ -537,10 +537,12 @@ package androidx.compose.foundation.lazy {
537537
}
538538

539539
public interface LazyListItemInfo {
540+
method public default Object? getContentType();
540541
method public int getIndex();
541542
method public Object getKey();
542543
method public int getOffset();
543544
method public int getSize();
545+
property public default Object? contentType;
544546
property public abstract int index;
545547
property public abstract Object key;
546548
property public abstract int offset;
@@ -650,12 +652,14 @@ package androidx.compose.foundation.lazy.grid {
650652

651653
public sealed interface LazyGridItemInfo {
652654
method public int getColumn();
655+
method public Object? getContentType();
653656
method public int getIndex();
654657
method public Object getKey();
655658
method public long getOffset();
656659
method public int getRow();
657660
method public long getSize();
658661
property public abstract int column;
662+
property public abstract Object? contentType;
659663
property public abstract int index;
660664
property public abstract Object key;
661665
property public abstract long offset;
@@ -868,11 +872,13 @@ package androidx.compose.foundation.lazy.staggeredgrid {
868872
}
869873

870874
public sealed interface LazyStaggeredGridItemInfo {
875+
method public Object? getContentType();
871876
method public int getIndex();
872877
method public Object getKey();
873878
method public int getLane();
874879
method public long getOffset();
875880
method public long getSize();
881+
property public abstract Object? contentType;
876882
property public abstract int index;
877883
property public abstract Object key;
878884
property public abstract int lane;
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
// Baseline format: 1.0
2+
AddedAbstractMethod: androidx.compose.foundation.lazy.grid.LazyGridItemInfo#getContentType():
3+
Added method androidx.compose.foundation.lazy.grid.LazyGridItemInfo.getContentType()
4+
5+
26
InvalidNullConversion: androidx.compose.foundation.MutatorMutex#mutateWith(T, androidx.compose.foundation.MutatePriority, kotlin.jvm.functions.Function2<? super T,? super kotlin.coroutines.Continuation<? super R>,?>, kotlin.coroutines.Continuation<? super R>) parameter #0:
37
Attempted to change parameter from @Nullable to @NonNull: incompatible change for parameter receiver in androidx.compose.foundation.MutatorMutex.mutateWith(T receiver, androidx.compose.foundation.MutatePriority priority, kotlin.jvm.functions.Function2<? super T,? super kotlin.coroutines.Continuation<? super R>,?> block, kotlin.coroutines.Continuation<? super R> arg4)

compose/foundation/foundation/api/restricted_current.txt

+6
Original file line numberDiff line numberDiff line change
@@ -403,10 +403,12 @@ package androidx.compose.foundation.lazy {
403403
}
404404

405405
public interface LazyListItemInfo {
406+
method public default Object? getContentType();
406407
method public int getIndex();
407408
method public Object getKey();
408409
method public int getOffset();
409410
method public int getSize();
411+
property public default Object? contentType;
410412
property public abstract int index;
411413
property public abstract Object key;
412414
property public abstract int offset;
@@ -514,12 +516,14 @@ package androidx.compose.foundation.lazy.grid {
514516

515517
public sealed interface LazyGridItemInfo {
516518
method public int getColumn();
519+
method public Object? getContentType();
517520
method public int getIndex();
518521
method public Object getKey();
519522
method public long getOffset();
520523
method public int getRow();
521524
method public long getSize();
522525
property public abstract int column;
526+
property public abstract Object? contentType;
523527
property public abstract int index;
524528
property public abstract Object key;
525529
property public abstract long offset;
@@ -624,11 +628,13 @@ package androidx.compose.foundation.lazy.staggeredgrid {
624628
}
625629

626630
public sealed interface LazyStaggeredGridItemInfo {
631+
method public Object? getContentType();
627632
method public int getIndex();
628633
method public Object getKey();
629634
method public int getLane();
630635
method public long getOffset();
631636
method public long getSize();
637+
property public abstract Object? contentType;
632638
property public abstract int index;
633639
property public abstract Object key;
634640
property public abstract int lane;

compose/foundation/foundation/src/androidAndroidTest/kotlin/androidx/compose/foundation/lazy/grid/LazyGridLayoutInfoTest.kt

+28
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import androidx.test.filters.MediumTest
3636
import com.google.common.truth.Truth.assertThat
3737
import com.google.common.truth.Truth.assertWithMessage
3838
import kotlinx.coroutines.runBlocking
39+
import org.junit.Assume
3940
import org.junit.Before
4041
import org.junit.Test
4142
import org.junit.runner.RunWith
@@ -493,6 +494,33 @@ class LazyGridLayoutInfoTest(
493494
}
494495
}
495496

497+
@Test
498+
fun contentTypeIsCorrect() {
499+
// no reasons to run the test in all variants
500+
Assume.assumeTrue(vertical && !reverseLayout)
501+
502+
val state = LazyGridState()
503+
rule.setContent {
504+
LazyGrid(
505+
cells = 1,
506+
state = state,
507+
modifier = Modifier.requiredSize(itemSizeDp * 3f)
508+
) {
509+
items(2, contentType = { it }) {
510+
Box(Modifier.requiredSize(itemSizeDp))
511+
}
512+
item {
513+
Box(Modifier.requiredSize(itemSizeDp))
514+
}
515+
}
516+
}
517+
518+
rule.runOnIdle {
519+
assertThat(state.layoutInfo.visibleItemsInfo.map { it.contentType })
520+
.isEqualTo(listOf(0, 1, null))
521+
}
522+
}
523+
496524
fun LazyGridLayoutInfo.assertVisibleItems(
497525
count: Int,
498526
cells: Int,

compose/foundation/foundation/src/androidAndroidTest/kotlin/androidx/compose/foundation/lazy/list/LazyListLayoutInfoTest.kt

+27
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import androidx.test.filters.MediumTest
3737
import com.google.common.truth.Truth.assertThat
3838
import com.google.common.truth.Truth.assertWithMessage
3939
import kotlinx.coroutines.runBlocking
40+
import org.junit.Assume.assumeTrue
4041
import org.junit.Before
4142
import org.junit.Test
4243
import org.junit.runner.RunWith
@@ -431,6 +432,32 @@ class LazyListLayoutInfoTest(
431432
}
432433
}
433434

435+
@Test
436+
fun contentTypeIsCorrect() {
437+
// no reasons to run the test in all variants
438+
assumeTrue(vertical && !reverseLayout)
439+
440+
lateinit var state: LazyListState
441+
rule.setContent {
442+
LazyColumnOrRow(
443+
state = rememberLazyListState().also { state = it },
444+
modifier = Modifier.requiredSize(itemSizeDp * 3f)
445+
) {
446+
items(2, contentType = { it }) {
447+
Box(Modifier.requiredSize(itemSizeDp))
448+
}
449+
item {
450+
Box(Modifier.requiredSize(itemSizeDp))
451+
}
452+
}
453+
}
454+
455+
rule.runOnIdle {
456+
assertThat(state.layoutInfo.visibleItemsInfo.map { it.contentType })
457+
.isEqualTo(listOf(0, 1, null))
458+
}
459+
}
460+
434461
@Test
435462
fun viewportOffsetsSmallContentReverseArrangement() {
436463
val state = LazyListState()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2023 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package androidx.compose.foundation.lazy.staggeredgrid
18+
19+
import androidx.compose.foundation.gestures.Orientation
20+
import androidx.compose.foundation.layout.Box
21+
import androidx.compose.foundation.layout.requiredSize
22+
import androidx.compose.foundation.layout.size
23+
import androidx.compose.ui.Modifier
24+
import androidx.compose.ui.unit.dp
25+
import androidx.test.filters.MediumTest
26+
import com.google.common.truth.Truth
27+
import org.junit.Test
28+
29+
@MediumTest
30+
class LazyStaggeredGridLayoutInfoTest : BaseLazyStaggeredGridWithOrientation(Orientation.Vertical) {
31+
32+
@Test
33+
fun contentTypeIsCorrect() {
34+
val state = LazyStaggeredGridState()
35+
rule.setContent {
36+
LazyStaggeredGrid(
37+
lanes = 1,
38+
state = state,
39+
modifier = Modifier.requiredSize(30.dp)
40+
) {
41+
items(2, contentType = { it }) {
42+
Box(Modifier.size(10.dp))
43+
}
44+
item {
45+
Box(Modifier.size(10.dp))
46+
}
47+
}
48+
}
49+
50+
rule.runOnIdle {
51+
Truth.assertThat(state.layoutInfo.visibleItemsInfo.map { it.contentType })
52+
.isEqualTo(listOf(0, 1, null))
53+
}
54+
}
55+
}

compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/lazy/LazyList.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ private fun rememberLazyListMeasurePolicy(
259259
isVertical,
260260
itemProvider,
261261
this
262-
) { index, key, placeables ->
262+
) { index, key, contentType, placeables ->
263263
// we add spaceBetweenItems as an extra spacing for all items apart from the last one so
264264
// the lazy list measuring logic will take it into account.
265265
val spacing = if (index.value == itemsCount - 1) 0 else spaceBetweenItems
@@ -275,7 +275,8 @@ private fun rememberLazyListMeasurePolicy(
275275
afterContentPadding = afterContentPadding,
276276
spacing = spacing,
277277
visualOffset = visualItemOffset,
278-
key = key
278+
key = key,
279+
contentType = contentType
279280
)
280281
}
281282
state.premeasureConstraints = measuredItemProvider.childConstraints

compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/lazy/LazyListItemInfo.kt

+5
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,9 @@ interface LazyListItemInfo {
4343
* slot for the item then this size will be calculated as the sum of their sizes.
4444
*/
4545
val size: Int
46+
47+
/**
48+
* The content type of the item which was passed to the item() or items() function.
49+
*/
50+
val contentType: Any? get() = null
4651
}

compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/lazy/LazyListMeasuredItem.kt

+5-2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ internal class LazyListMeasuredItem @ExperimentalFoundationApi constructor(
4949
*/
5050
private val visualOffset: IntOffset,
5151
val key: Any,
52+
private val contentType: Any?
5253
) {
5354
/**
5455
* Sum of the main axis sizes of all the inner placeables.
@@ -116,7 +117,8 @@ internal class LazyListMeasuredItem @ExperimentalFoundationApi constructor(
116117
wrappers = wrappers,
117118
visualOffset = visualOffset,
118119
reverseLayout = reverseLayout,
119-
mainAxisLayoutSize = mainAxisLayoutSize
120+
mainAxisLayoutSize = mainAxisLayoutSize,
121+
contentType = contentType
120122
)
121123
}
122124
}
@@ -132,7 +134,8 @@ internal class LazyListPositionedItem(
132134
private val wrappers: List<LazyListPlaceableWrapper>,
133135
private val visualOffset: IntOffset,
134136
private val reverseLayout: Boolean,
135-
private val mainAxisLayoutSize: Int
137+
private val mainAxisLayoutSize: Int,
138+
override val contentType: Any?
136139
) : LazyListItemInfo {
137140
val placeablesCount: Int get() = wrappers.size
138141

compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/lazy/LazyListMeasuredItemProvider.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ internal class LazyListMeasuredItemProvider @ExperimentalFoundationApi construct
4545
*/
4646
fun getAndMeasure(index: DataIndex): LazyListMeasuredItem {
4747
val key = itemProvider.getKey(index.value)
48+
val contentType = itemProvider.getContentType(index.value)
4849
val placeables = measureScope.measure(index.value, childConstraints)
49-
return measuredItemFactory.createItem(index, key, placeables)
50+
return measuredItemFactory.createItem(index, key, contentType, placeables)
5051
}
5152

5253
/**
@@ -61,6 +62,7 @@ internal fun interface MeasuredItemFactory {
6162
fun createItem(
6263
index: DataIndex,
6364
key: Any,
65+
contentType: Any?,
6466
placeables: List<Placeable>
6567
): LazyListMeasuredItem
6668
}

compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/lazy/grid/LazyGrid.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ private fun rememberLazyGridMeasurePolicy(
256256
itemProvider,
257257
this,
258258
spaceBetweenLines
259-
) { index, key, crossAxisSize, mainAxisSpacing, placeables ->
259+
) { index, key, contentType, crossAxisSize, mainAxisSpacing, placeables ->
260260
LazyGridMeasuredItem(
261261
index = index,
262262
key = key,
@@ -268,7 +268,8 @@ private fun rememberLazyGridMeasurePolicy(
268268
beforeContentPadding = beforeContentPadding,
269269
afterContentPadding = afterContentPadding,
270270
visualOffset = visualItemOffset,
271-
placeables = placeables
271+
placeables = placeables,
272+
contentType = contentType
272273
)
273274
}
274275
val measuredLineProvider = LazyGridMeasuredLineProvider(

compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/lazy/grid/LazyGridItemInfo.kt

+5
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ sealed interface LazyGridItemInfo {
6060
*/
6161
val size: IntSize
6262

63+
/**
64+
* The content type of the item which was passed to the item() or items() function.
65+
*/
66+
val contentType: Any?
67+
6368
companion object {
6469
/**
6570
* Possible value for [row], when they are unknown. This can happen when the item is

0 commit comments

Comments
 (0)