Use flow.catch instead of try catch

pull/668/head
Takeshi Hagikura 1 year ago
parent 55483e82df
commit d0c729a56f

@ -24,6 +24,7 @@ import com.google.samples.apps.nowinandroid.feature.search.SearchResultUiState.L
import dagger.hilt.android.lifecycle.HiltViewModel import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.flatMapLatest import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
@ -44,14 +45,12 @@ class SearchViewModel @Inject constructor(
if (query.length < 2) { if (query.length < 2) {
flowOf(SearchResultUiState.EmptyQuery) flowOf(SearchResultUiState.EmptyQuery)
} else { } else {
try { getSearchContentsUseCase(query).map {
getSearchContentsUseCase(query).map { SearchResultUiState.Success(
SearchResultUiState.Success( topics = it.topics,
topics = it.topics, newsResources = it.newsResources,
newsResources = it.newsResources, )
) }.catch {
}
} catch (exception: Exception) {
flowOf(LoadFailed) flowOf(LoadFailed)
} }
} }

Loading…
Cancel
Save