From 5a1ece4f80219b349e9eca12fb64638c9a7ae412 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Mlynari=C4=8D?= Date: Mon, 23 May 2022 14:52:03 +0200 Subject: [PATCH] Add ForYou TopicSelection scrolling state Change-Id: Icb85b612b1aa45fc5579acea9358a2da70de15d6 --- .../feature/foryou/AuthorsCarousel.kt | 2 +- .../feature/foryou/ForYouScreen.kt | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/feature-foryou/src/main/java/com/google/samples/apps/nowinandroid/feature/foryou/AuthorsCarousel.kt b/feature-foryou/src/main/java/com/google/samples/apps/nowinandroid/feature/foryou/AuthorsCarousel.kt index 81024276d..bf1f5d5dc 100644 --- a/feature-foryou/src/main/java/com/google/samples/apps/nowinandroid/feature/foryou/AuthorsCarousel.kt +++ b/feature-foryou/src/main/java/com/google/samples/apps/nowinandroid/feature/foryou/AuthorsCarousel.kt @@ -77,7 +77,7 @@ fun AuthorsCarousel( } }.collect() } - + LazyRow(modifier, lazyListState) { items(items = authors, key = { item -> item.author.id }) { followableAuthor -> AuthorItem( diff --git a/feature-foryou/src/main/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouScreen.kt b/feature-foryou/src/main/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouScreen.kt index b29a3b6e0..8a3782a8b 100644 --- a/feature-foryou/src/main/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouScreen.kt +++ b/feature-foryou/src/main/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouScreen.kt @@ -44,6 +44,7 @@ import androidx.compose.foundation.lazy.LazyListScope import androidx.compose.foundation.lazy.grid.GridCells import androidx.compose.foundation.lazy.grid.LazyHorizontalGrid import androidx.compose.foundation.lazy.grid.items +import androidx.compose.foundation.lazy.grid.rememberLazyGridState import androidx.compose.foundation.lazy.items import androidx.compose.foundation.lazy.rememberLazyListState import androidx.compose.foundation.shape.CornerSize @@ -320,7 +321,25 @@ private fun TopicSelection( onTopicCheckedChanged: (String, Boolean) -> Unit, modifier: Modifier = Modifier ) { + val lazyGridState = rememberLazyGridState() + JankMetricEffect(lazyGridState) { metricsHolder -> + combine( + snapshotFlow { lazyGridState.isScrollInProgress }, + snapshotFlow { lazyGridState.firstVisibleItemIndex }, + ) { isScrollInProgress, firstVisibleItemIndex -> + if (isScrollInProgress) { + metricsHolder.state?.addState( + "ForYou:TopicSelection:Scrolling", + "index=${firstVisibleItemIndex}" + ) + } else { + metricsHolder.state?.removeState("ForYou:TopicSelection:Scrolling") + } + }.collect() + } + LazyHorizontalGrid( + state = lazyGridState, rows = GridCells.Fixed(3), horizontalArrangement = Arrangement.spacedBy(12.dp), verticalArrangement = Arrangement.spacedBy(12.dp),