Skip to content

Commit 6a8736d

Browse files
authored
Improvement in the Search screen (#717)
Combine the search result of topics and news resources in one LazyVerticalGrid so that topics don't get in the way. Before this change, the result of topics created a LazyColumn and the result of news resources created the LazyVerticalGrid separately that made the situation where there were two separete vertical scrolling lists.
1 parent 3b1b1ea commit 6a8736d

File tree

1 file changed

+65
-42
lines changed
  • feature/search/src/main/java/com/google/samples/apps/nowinandroid/feature/search

1 file changed

+65
-42
lines changed

feature/search/src/main/java/com/google/samples/apps/nowinandroid/feature/search/SearchScreen.kt

Lines changed: 65 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import androidx.compose.foundation.layout.windowInsetsBottomHeight
3131
import androidx.compose.foundation.layout.windowInsetsTopHeight
3232
import androidx.compose.foundation.lazy.LazyColumn
3333
import androidx.compose.foundation.lazy.grid.GridCells.Adaptive
34+
import androidx.compose.foundation.lazy.grid.GridItemSpan
3435
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
3536
import androidx.compose.foundation.lazy.grid.rememberLazyGridState
3637
import androidx.compose.foundation.lazy.items
@@ -83,12 +84,11 @@ import com.google.samples.apps.nowinandroid.core.ui.DevicePreviews
8384
import com.google.samples.apps.nowinandroid.core.ui.NewsFeedUiState
8485
import com.google.samples.apps.nowinandroid.core.ui.R.string
8586
import com.google.samples.apps.nowinandroid.core.ui.TrackScreenViewEvent
86-
import com.google.samples.apps.nowinandroid.core.ui.TrackScrollJank
8787
import com.google.samples.apps.nowinandroid.core.ui.newsFeed
8888
import com.google.samples.apps.nowinandroid.feature.bookmarks.BookmarksViewModel
8989
import com.google.samples.apps.nowinandroid.feature.foryou.ForYouViewModel
90+
import com.google.samples.apps.nowinandroid.feature.interests.InterestsItem
9091
import com.google.samples.apps.nowinandroid.feature.interests.InterestsViewModel
91-
import com.google.samples.apps.nowinandroid.feature.interests.TopicsTabContent
9292
import com.google.samples.apps.nowinandroid.feature.search.R as searchR
9393

9494
@Composable
@@ -289,49 +289,72 @@ private fun SearchResultBody(
289289
onTopicClick: (String) -> Unit,
290290
searchQuery: String = "",
291291
) {
292-
if (topics.isNotEmpty()) {
293-
Text(
294-
text = buildAnnotatedString {
295-
withStyle(style = SpanStyle(fontWeight = FontWeight.Bold)) {
296-
append(stringResource(id = searchR.string.topics))
292+
val state = rememberLazyGridState()
293+
LazyVerticalGrid(
294+
columns = Adaptive(300.dp),
295+
contentPadding = PaddingValues(16.dp),
296+
horizontalArrangement = Arrangement.spacedBy(16.dp),
297+
verticalArrangement = Arrangement.spacedBy(24.dp),
298+
modifier = Modifier
299+
.fillMaxSize()
300+
.testTag("search:newsResources"),
301+
state = state,
302+
) {
303+
if (topics.isNotEmpty()) {
304+
item(
305+
span = {
306+
GridItemSpan(maxLineSpan)
307+
},
308+
) {
309+
Text(
310+
text = buildAnnotatedString {
311+
withStyle(style = SpanStyle(fontWeight = FontWeight.Bold)) {
312+
append(stringResource(id = searchR.string.topics))
313+
}
314+
},
315+
modifier = Modifier.padding(horizontal = 16.dp, vertical = 8.dp),
316+
)
317+
}
318+
topics.forEach { followableTopic ->
319+
val topicId = followableTopic.topic.id
320+
item(
321+
key = "topic-$topicId", // Append a prefix to distinguish a key for news resources
322+
span = {
323+
GridItemSpan(maxLineSpan)
324+
},
325+
) {
326+
InterestsItem(
327+
name = followableTopic.topic.name,
328+
following = followableTopic.isFollowed,
329+
description = followableTopic.topic.shortDescription,
330+
topicImageUrl = followableTopic.topic.imageUrl,
331+
onClick = {
332+
// Pass the current search query to ViewModel to save it as recent searches
333+
onSearchTriggered(searchQuery)
334+
onTopicClick(topicId)
335+
},
336+
onFollowButtonClick = { onFollowButtonClick(topicId, it) },
337+
)
297338
}
298-
},
299-
modifier = Modifier.padding(horizontal = 16.dp, vertical = 8.dp),
300-
)
301-
TopicsTabContent(
302-
topics = topics,
303-
onTopicClick = {
304-
// Pass the current search query to ViewModel to save it as recent searches
305-
onSearchTriggered(searchQuery)
306-
onTopicClick(it)
307-
},
308-
onFollowButtonClick = onFollowButtonClick,
309-
withBottomSpacer = false,
310-
)
311-
}
339+
}
340+
}
312341

313-
if (newsResources.isNotEmpty()) {
314-
Text(
315-
text = buildAnnotatedString {
316-
withStyle(style = SpanStyle(fontWeight = FontWeight.Bold)) {
317-
append(stringResource(id = searchR.string.updates))
318-
}
319-
},
320-
modifier = Modifier.padding(horizontal = 16.dp, vertical = 8.dp),
321-
)
342+
if (newsResources.isNotEmpty()) {
343+
item(
344+
span = {
345+
GridItemSpan(maxLineSpan)
346+
},
347+
) {
348+
Text(
349+
text = buildAnnotatedString {
350+
withStyle(style = SpanStyle(fontWeight = FontWeight.Bold)) {
351+
append(stringResource(id = searchR.string.updates))
352+
}
353+
},
354+
modifier = Modifier.padding(horizontal = 16.dp, vertical = 8.dp),
355+
)
356+
}
322357

323-
val state = rememberLazyGridState()
324-
TrackScrollJank(scrollableState = state, stateName = "search:newsResource")
325-
LazyVerticalGrid(
326-
columns = Adaptive(300.dp),
327-
contentPadding = PaddingValues(16.dp),
328-
horizontalArrangement = Arrangement.spacedBy(16.dp),
329-
verticalArrangement = Arrangement.spacedBy(24.dp),
330-
modifier = Modifier
331-
.fillMaxSize()
332-
.testTag("search:newsResources"),
333-
state = state,
334-
) {
335358
newsFeed(
336359
feedState = NewsFeedUiState.Success(feed = newsResources),
337360
onNewsResourcesCheckedChanged = onNewsResourcesCheckedChanged,

0 commit comments

Comments
 (0)