replace object with data object in search feature, simple format code for the reader

pull/913/head
hoangchungk53qx1 1 year ago
parent 014d388e92
commit cd30ecf359

@ -19,7 +19,7 @@ package com.google.samples.apps.nowinandroid.feature.search
import com.google.samples.apps.nowinandroid.core.data.model.RecentSearchQuery
sealed interface RecentSearchQueriesUiState {
object Loading : RecentSearchQueriesUiState
data object Loading : RecentSearchQueriesUiState
data class Success(
val recentQueries: List<RecentSearchQuery> = emptyList(),

@ -20,14 +20,14 @@ import com.google.samples.apps.nowinandroid.core.model.data.FollowableTopic
import com.google.samples.apps.nowinandroid.core.model.data.UserNewsResource
sealed interface SearchResultUiState {
object Loading : SearchResultUiState
data object Loading : SearchResultUiState
/**
* The state query is empty or too short. To distinguish the state between the
* (initial state or when the search query is cleared) vs the state where no search
* result is returned, explicitly define the empty query state.
*/
object EmptyQuery : SearchResultUiState
data object EmptyQuery : SearchResultUiState
object LoadFailed : SearchResultUiState
@ -42,5 +42,5 @@ sealed interface SearchResultUiState {
* A state where the search contents are not ready. This happens when the *Fts tables are not
* populated yet.
*/
object SearchNotReady : SearchResultUiState
data object SearchNotReady : SearchResultUiState
}

@ -312,11 +312,7 @@ private fun SearchResultBody(
state = state,
) {
if (topics.isNotEmpty()) {
item(
span = {
GridItemSpan(maxLineSpan)
},
) {
item(span = { GridItemSpan(maxLineSpan) }) {
Text(
text = buildAnnotatedString {
withStyle(style = SpanStyle(fontWeight = FontWeight.Bold)) {
@ -330,9 +326,7 @@ private fun SearchResultBody(
val topicId = followableTopic.topic.id
item(
key = "topic-$topicId", // Append a prefix to distinguish a key for news resources
span = {
GridItemSpan(maxLineSpan)
},
span = { GridItemSpan(maxLineSpan) },
) {
InterestsItem(
name = followableTopic.topic.name,
@ -351,11 +345,7 @@ private fun SearchResultBody(
}
if (newsResources.isNotEmpty()) {
item(
span = {
GridItemSpan(maxLineSpan)
},
) {
item(span = { GridItemSpan(maxLineSpan) }) {
Text(
text = buildAnnotatedString {
withStyle(style = SpanStyle(fontWeight = FontWeight.Bold)) {
@ -440,9 +430,7 @@ private fun RecentSearchesBody(
style = MaterialTheme.typography.headlineSmall,
modifier = Modifier
.padding(vertical = 16.dp)
.clickable {
onRecentSearchClicked(recentSearch)
}
.clickable { onRecentSearchClicked(recentSearch) }
.fillMaxWidth(),
)
}

@ -51,43 +51,41 @@ class SearchViewModel @Inject constructor(
val searchQuery = savedStateHandle.getStateFlow(SEARCH_QUERY, "")
val searchResultUiState: StateFlow<SearchResultUiState> =
getSearchContentsCountUseCase().flatMapLatest { totalCount ->
if (totalCount < SEARCH_MIN_FTS_ENTITY_COUNT) {
flowOf(SearchResultUiState.SearchNotReady)
} else {
searchQuery.flatMapLatest { query ->
if (query.length < SEARCH_QUERY_MIN_LENGTH) {
flowOf(SearchResultUiState.EmptyQuery)
} else {
getSearchContentsUseCase(query).asResult().map {
when (it) {
is Result.Success -> {
SearchResultUiState.Success(
topics = it.data.topics,
newsResources = it.data.newsResources,
)
}
is Result.Loading -> {
SearchResultUiState.Loading
}
getSearchContentsCountUseCase()
.flatMapLatest { totalCount ->
if (totalCount < SEARCH_MIN_FTS_ENTITY_COUNT) {
flowOf(SearchResultUiState.SearchNotReady)
} else {
searchQuery.flatMapLatest { query ->
if (query.length < SEARCH_QUERY_MIN_LENGTH) {
flowOf(SearchResultUiState.EmptyQuery)
} else {
getSearchContentsUseCase(query)
.asResult()
.map { result ->
when (result) {
is Result.Success -> SearchResultUiState.Success(
topics = result.data.topics,
newsResources = result.data.newsResources,
)
is Result.Error -> {
SearchResultUiState.LoadFailed
is Result.Loading -> SearchResultUiState.Loading
is Result.Error -> SearchResultUiState.LoadFailed
}
}
}
}
}
}
}
}.stateIn(
scope = viewModelScope,
started = SharingStarted.WhileSubscribed(5_000),
initialValue = SearchResultUiState.Loading,
)
.stateIn(
scope = viewModelScope,
started = SharingStarted.WhileSubscribed(5_000),
initialValue = SearchResultUiState.Loading,
)
val recentSearchQueriesUiState: StateFlow<RecentSearchQueriesUiState> =
recentSearchQueriesUseCase().map(RecentSearchQueriesUiState::Success)
recentSearchQueriesUseCase()
.map(RecentSearchQueriesUiState::Success)
.stateIn(
scope = viewModelScope,
started = SharingStarted.WhileSubscribed(5_000),
@ -109,14 +107,9 @@ class SearchViewModel @Inject constructor(
viewModelScope.launch {
recentSearchRepository.insertOrReplaceRecentSearch(query)
}
analyticsHelper.logEvent(
AnalyticsEvent(
type = SEARCH_QUERY,
extras = listOf(
Param(SEARCH_QUERY, query),
),
),
)
val eventExtras = listOf(element = Param(key = SEARCH_QUERY, value = query))
val analyticsEvent = AnalyticsEvent(type = SEARCH_QUERY, extras = eventExtras)
analyticsHelper.logEvent(event = analyticsEvent)
}
fun clearRecentSearches() {

Loading…
Cancel
Save