Skip to content

A fully functional Android app built entirely with Kotlin and Jetpack Compose

Merge remote-tracking branch 'origin/main'

YvesKalumeb887f0c03ce1
diff --cc feature-foryou/src/main/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouScreen.kt
index 02ac7fe40,f53525e36..42f6bfc29
--- 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
@@@ -38,13 -38,15 +39,16 @@@ import androidx.compose.foundation.layo
import androidx.compose.foundation.layout.windowInsetsBottomHeight
import androidx.compose.foundation.layout.windowInsetsPadding
import androidx.compose.foundation.layout.wrapContentSize
- import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyListScope
import androidx.compose.foundation.lazy.grid.GridCells
+ import androidx.compose.foundation.lazy.grid.GridCells.Adaptive
+ import androidx.compose.foundation.lazy.grid.GridItemSpan
+ import androidx.compose.foundation.lazy.grid.LazyGridScope
import androidx.compose.foundation.lazy.grid.LazyHorizontalGrid
+ import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.lazy.grid.rememberLazyGridState
+import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.shape.CornerSize
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.ExperimentalMaterial3Api
@@@ -154,77 -148,63 +150,68 @@@ fun ForYouScreen
},
containerColor = Color.Transparent
) { innerPadding ->
- // TODO: Replace with `LazyVerticalGrid` when blocking bugs are fixed:
- // https://issuetracker.google.com/issues/230514914
- // https://issuetracker.google.com/issues/231320714
- BoxWithConstraints(
- modifier = modifier
- .padding(innerPadding)
- .consumedWindowInsets(innerPadding)
- ) {
- val numberOfColumns = when (windowSizeClass.widthSizeClass) {
- WindowWidthSizeClass.Compact, WindowWidthSizeClass.Medium -> 1
- else -> floor(maxWidth / 300.dp).toInt().coerceAtLeast(1)
- }
-
- // Workaround to call Activity.reportFullyDrawn from Jetpack Compose.
- // This code should be called when the UI is ready for use
- // and relates to Time To Full Display.
- val interestsLoaded =
- interestsSelectionState !is ForYouInterestsSelectionUiState.Loading
- val feedLoaded = feedState !is NewsFeedUiState.Loading
+ // Workaround to call Activity.reportFullyDrawn from Jetpack Compose.
+ // This code should be called when the UI is ready for use
+ // and relates to Time To Full Display.
+ val interestsLoaded =
+ interestsSelectionState !is ForYouInterestsSelectionUiState.Loading
+ val feedLoaded = feedState !is NewsFeedUiState.Loading
- if (interestsLoaded && feedLoaded) {
- val localView = LocalView.current
- // We use Unit to call reportFullyDrawn only on the first recomposition,
- // however it will be called again if this composable goes out of scope.
- // Activity.reportFullyDrawn() has its own check for this
- // and is safe to call multiple times though.
- LaunchedEffect(Unit) {
- // We're leveraging the fact, that the current view is directly set as content of Activity.
- val activity = localView.context as? Activity ?: return@LaunchedEffect
- // To be sure not to call in the middle of a frame draw.
- localView.doOnPreDraw { activity.reportFullyDrawn() }
- }
+ if (interestsLoaded && feedLoaded) {
+ val localView = LocalView.current
+ // We use Unit to call reportFullyDrawn only on the first recomposition,
+ // however it will be called again if this composable goes out of scope.
+ // Activity.reportFullyDrawn() has its own check for this
+ // and is safe to call multiple times though.
+ LaunchedEffect(Unit) {
+ // We're leveraging the fact, that the current view is directly set as content of Activity.
+ val activity = localView.context as? Activity ?: return@LaunchedEffect
+ // To be sure not to call in the middle of a frame draw.
+ localView.doOnPreDraw { activity.reportFullyDrawn() }
}
+ }
- val tag = "forYou:feed"
+ val tag = "forYou:feed"
- val lazyListState = rememberLazyListState()
- TrackScrollJank(scrollableState = lazyListState, stateName = tag)
+ val lazyGridState = rememberLazyGridState()
+ TrackScrollJank(scrollableState = lazyGridState, stateName = tag)
- LazyColumn(
- modifier = Modifier
- .fillMaxSize()
- .testTag(tag),
- state = lazyListState,
- ) {
- InterestsSelection(
- interestsSelectionState = interestsSelectionState,
- showLoadingUIIfLoading = true,
- onAuthorCheckedChanged = onAuthorCheckedChanged,
- onTopicCheckedChanged = onTopicCheckedChanged,
- saveFollowedTopics = saveFollowedTopics
- )
+ LazyVerticalGrid(
+ columns = Adaptive(300.dp),
+ contentPadding = PaddingValues(16.dp),
+ horizontalArrangement = Arrangement.spacedBy(32.dp),
+ verticalArrangement = Arrangement.spacedBy(24.dp),
+ modifier = modifier
+ .padding(innerPadding)
+ .consumedWindowInsets(innerPadding)
+ .fillMaxSize()
+ .testTag("forYou:feed"),
+ state = lazyGridState
+ ) {
+ interestsSelection(
+ interestsSelectionState = interestsSelectionState,
+ onAuthorCheckedChanged = onAuthorCheckedChanged,
+ onTopicCheckedChanged = onTopicCheckedChanged,
+ saveFollowedTopics = saveFollowedTopics
+ )
- NewsFeed(
- feedState = feedState,
- // Avoid showing a second loading wheel if we already are for the interests
- // selection
- showLoadingUIIfLoading =
- interestsSelectionState !is ForYouInterestsSelectionUiState.Loading,
- numberOfColumns = numberOfColumns,
- onNewsResourcesCheckedChanged = onNewsResourcesCheckedChanged,
- loadingContentDescription = R.string.for_you_loading
- )
+ newsFeed(
+ feedState = feedState,
+ // Avoid showing a second loading wheel if we already are for the interests
+ // selection
+ showLoadingUIIfLoading =
+ interestsSelectionState !is ForYouInterestsSelectionUiState.Loading,
+ onNewsResourcesCheckedChanged = onNewsResourcesCheckedChanged,
+ loadingContentDescription = R.string.for_you_loading
+ )
- item(span = { GridItemSpan(maxLineSpan) }) {
- Spacer(Modifier.windowInsetsBottomHeight(WindowInsets.safeDrawing))
+ item {
+ Spacer(modifier = Modifier.height(8.dp))
+ }
+
+ item {
+ Spacer(Modifier.windowInsetsBottomHeight(WindowInsets.safeDrawing))
+ }
}
}
}