Skip to content

Commit cd30ecf

Browse files
replace object with data object in search feature, simple format code for the reader
1 parent 014d388 commit cd30ecf

File tree

4 files changed

+38
-57
lines changed

4 files changed

+38
-57
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package com.google.samples.apps.nowinandroid.feature.search
1919
import com.google.samples.apps.nowinandroid.core.data.model.RecentSearchQuery
2020

2121
sealed interface RecentSearchQueriesUiState {
22-
object Loading : RecentSearchQueriesUiState
22+
data object Loading : RecentSearchQueriesUiState
2323

2424
data class Success(
2525
val recentQueries: List<RecentSearchQuery> = emptyList(),

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ import com.google.samples.apps.nowinandroid.core.model.data.FollowableTopic
2020
import com.google.samples.apps.nowinandroid.core.model.data.UserNewsResource
2121

2222
sealed interface SearchResultUiState {
23-
object Loading : SearchResultUiState
23+
data object Loading : SearchResultUiState
2424

2525
/**
2626
* The state query is empty or too short. To distinguish the state between the
2727
* (initial state or when the search query is cleared) vs the state where no search
2828
* result is returned, explicitly define the empty query state.
2929
*/
30-
object EmptyQuery : SearchResultUiState
30+
data object EmptyQuery : SearchResultUiState
3131

3232
object LoadFailed : SearchResultUiState
3333

@@ -42,5 +42,5 @@ sealed interface SearchResultUiState {
4242
* A state where the search contents are not ready. This happens when the *Fts tables are not
4343
* populated yet.
4444
*/
45-
object SearchNotReady : SearchResultUiState
45+
data object SearchNotReady : SearchResultUiState
4646
}

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

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -312,11 +312,7 @@ private fun SearchResultBody(
312312
state = state,
313313
) {
314314
if (topics.isNotEmpty()) {
315-
item(
316-
span = {
317-
GridItemSpan(maxLineSpan)
318-
},
319-
) {
315+
item(span = { GridItemSpan(maxLineSpan) }) {
320316
Text(
321317
text = buildAnnotatedString {
322318
withStyle(style = SpanStyle(fontWeight = FontWeight.Bold)) {
@@ -330,9 +326,7 @@ private fun SearchResultBody(
330326
val topicId = followableTopic.topic.id
331327
item(
332328
key = "topic-$topicId", // Append a prefix to distinguish a key for news resources
333-
span = {
334-
GridItemSpan(maxLineSpan)
335-
},
329+
span = { GridItemSpan(maxLineSpan) },
336330
) {
337331
InterestsItem(
338332
name = followableTopic.topic.name,
@@ -351,11 +345,7 @@ private fun SearchResultBody(
351345
}
352346

353347
if (newsResources.isNotEmpty()) {
354-
item(
355-
span = {
356-
GridItemSpan(maxLineSpan)
357-
},
358-
) {
348+
item(span = { GridItemSpan(maxLineSpan) }) {
359349
Text(
360350
text = buildAnnotatedString {
361351
withStyle(style = SpanStyle(fontWeight = FontWeight.Bold)) {
@@ -440,9 +430,7 @@ private fun RecentSearchesBody(
440430
style = MaterialTheme.typography.headlineSmall,
441431
modifier = Modifier
442432
.padding(vertical = 16.dp)
443-
.clickable {
444-
onRecentSearchClicked(recentSearch)
445-
}
433+
.clickable { onRecentSearchClicked(recentSearch) }
446434
.fillMaxWidth(),
447435
)
448436
}

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

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -51,43 +51,41 @@ class SearchViewModel @Inject constructor(
5151
val searchQuery = savedStateHandle.getStateFlow(SEARCH_QUERY, "")
5252

5353
val searchResultUiState: StateFlow<SearchResultUiState> =
54-
getSearchContentsCountUseCase().flatMapLatest { totalCount ->
55-
if (totalCount < SEARCH_MIN_FTS_ENTITY_COUNT) {
56-
flowOf(SearchResultUiState.SearchNotReady)
57-
} else {
58-
searchQuery.flatMapLatest { query ->
59-
if (query.length < SEARCH_QUERY_MIN_LENGTH) {
60-
flowOf(SearchResultUiState.EmptyQuery)
61-
} else {
62-
getSearchContentsUseCase(query).asResult().map {
63-
when (it) {
64-
is Result.Success -> {
65-
SearchResultUiState.Success(
66-
topics = it.data.topics,
67-
newsResources = it.data.newsResources,
68-
)
69-
}
70-
71-
is Result.Loading -> {
72-
SearchResultUiState.Loading
73-
}
54+
getSearchContentsCountUseCase()
55+
.flatMapLatest { totalCount ->
56+
if (totalCount < SEARCH_MIN_FTS_ENTITY_COUNT) {
57+
flowOf(SearchResultUiState.SearchNotReady)
58+
} else {
59+
searchQuery.flatMapLatest { query ->
60+
if (query.length < SEARCH_QUERY_MIN_LENGTH) {
61+
flowOf(SearchResultUiState.EmptyQuery)
62+
} else {
63+
getSearchContentsUseCase(query)
64+
.asResult()
65+
.map { result ->
66+
when (result) {
67+
is Result.Success -> SearchResultUiState.Success(
68+
topics = result.data.topics,
69+
newsResources = result.data.newsResources,
70+
)
7471

75-
is Result.Error -> {
76-
SearchResultUiState.LoadFailed
72+
is Result.Loading -> SearchResultUiState.Loading
73+
is Result.Error -> SearchResultUiState.LoadFailed
74+
}
7775
}
78-
}
7976
}
8077
}
8178
}
8279
}
83-
}.stateIn(
84-
scope = viewModelScope,
85-
started = SharingStarted.WhileSubscribed(5_000),
86-
initialValue = SearchResultUiState.Loading,
87-
)
80+
.stateIn(
81+
scope = viewModelScope,
82+
started = SharingStarted.WhileSubscribed(5_000),
83+
initialValue = SearchResultUiState.Loading,
84+
)
8885

8986
val recentSearchQueriesUiState: StateFlow<RecentSearchQueriesUiState> =
90-
recentSearchQueriesUseCase().map(RecentSearchQueriesUiState::Success)
87+
recentSearchQueriesUseCase()
88+
.map(RecentSearchQueriesUiState::Success)
9189
.stateIn(
9290
scope = viewModelScope,
9391
started = SharingStarted.WhileSubscribed(5_000),
@@ -109,14 +107,9 @@ class SearchViewModel @Inject constructor(
109107
viewModelScope.launch {
110108
recentSearchRepository.insertOrReplaceRecentSearch(query)
111109
}
112-
analyticsHelper.logEvent(
113-
AnalyticsEvent(
114-
type = SEARCH_QUERY,
115-
extras = listOf(
116-
Param(SEARCH_QUERY, query),
117-
),
118-
),
119-
)
110+
val eventExtras = listOf(element = Param(key = SEARCH_QUERY, value = query))
111+
val analyticsEvent = AnalyticsEvent(type = SEARCH_QUERY, extras = eventExtras)
112+
analyticsHelper.logEvent(event = analyticsEvent)
120113
}
121114

122115
fun clearRecentSearches() {

0 commit comments

Comments
 (0)