diff --git a/core/data/src/main/kotlin/com/google/samples/apps/nowinandroid/core/data/repository/DefaultSearchContentsRepository.kt b/core/data/src/main/kotlin/com/google/samples/apps/nowinandroid/core/data/repository/DefaultSearchContentsRepository.kt index 38c0ebe86..8893665fc 100644 --- a/core/data/src/main/kotlin/com/google/samples/apps/nowinandroid/core/data/repository/DefaultSearchContentsRepository.kt +++ b/core/data/src/main/kotlin/com/google/samples/apps/nowinandroid/core/data/repository/DefaultSearchContentsRepository.kt @@ -33,6 +33,7 @@ import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.flatMapLatest +import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.flow.mapLatest import kotlinx.coroutines.withContext import javax.inject.Inject @@ -77,13 +78,11 @@ internal class DefaultSearchContentsRepository @Inject constructor( .distinctUntilChanged() .flatMapLatest(topicDao::getTopicEntities) return combine(newsResourcesFlow, topicsFlow) { newsResources, topics -> - withContext(defaultDispatcher) { - SearchResult( - topics = topics.map { it.asExternalModel() }, - newsResources = newsResources.map { it.asExternalModel() }, - ) - } - } + SearchResult( + topics = topics.map { it.asExternalModel() }, + newsResources = newsResources.map { it.asExternalModel() }, + ) + }.flowOn(defaultDispatcher) } override fun getSearchContentsCount(): Flow = @@ -91,8 +90,6 @@ internal class DefaultSearchContentsRepository @Inject constructor( newsResourceFtsDao.getCount(), topicFtsDao.getCount(), ) { newsResourceCount, topicsCount -> - withContext(defaultDispatcher) { - newsResourceCount + topicsCount - } - } + newsResourceCount + topicsCount + }.flowOn(defaultDispatcher) } diff --git a/core/domain/src/main/kotlin/com/google/samples/apps/nowinandroid/core/domain/GetFollowableTopicsUseCase.kt b/core/domain/src/main/kotlin/com/google/samples/apps/nowinandroid/core/domain/GetFollowableTopicsUseCase.kt index f80a722ed..680b317c6 100644 --- a/core/domain/src/main/kotlin/com/google/samples/apps/nowinandroid/core/domain/GetFollowableTopicsUseCase.kt +++ b/core/domain/src/main/kotlin/com/google/samples/apps/nowinandroid/core/domain/GetFollowableTopicsUseCase.kt @@ -26,7 +26,7 @@ import com.google.samples.apps.nowinandroid.core.network.NiaDispatchers.Default import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.combine -import kotlinx.coroutines.withContext +import kotlinx.coroutines.flow.flowOn import javax.inject.Inject /** @@ -46,20 +46,18 @@ class GetFollowableTopicsUseCase @Inject constructor( userDataRepository.userData, topicsRepository.getTopics(), ) { userData, topics -> - withContext(defaultDispatcher) { - val followedTopics = topics - .map { topic -> - FollowableTopic( - topic = topic, - isFollowed = topic.id in userData.followedTopics, - ) - } - when (sortBy) { - NAME -> followedTopics.sortedBy { it.topic.name } - else -> followedTopics + val followedTopics = topics + .map { topic -> + FollowableTopic( + topic = topic, + isFollowed = topic.id in userData.followedTopics, + ) } + when (sortBy) { + NAME -> followedTopics.sortedBy { it.topic.name } + else -> followedTopics } - } + }.flowOn(defaultDispatcher) } enum class TopicSortField { diff --git a/core/domain/src/main/kotlin/com/google/samples/apps/nowinandroid/core/domain/GetSearchContentsUseCase.kt b/core/domain/src/main/kotlin/com/google/samples/apps/nowinandroid/core/domain/GetSearchContentsUseCase.kt index edb12641d..2e8db5f0e 100644 --- a/core/domain/src/main/kotlin/com/google/samples/apps/nowinandroid/core/domain/GetSearchContentsUseCase.kt +++ b/core/domain/src/main/kotlin/com/google/samples/apps/nowinandroid/core/domain/GetSearchContentsUseCase.kt @@ -28,7 +28,7 @@ import com.google.samples.apps.nowinandroid.core.network.NiaDispatchers.Default import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.combine -import kotlinx.coroutines.withContext +import kotlinx.coroutines.flow.flowOn import javax.inject.Inject /** @@ -52,20 +52,18 @@ private fun Flow.mapToUserSearchResult( defaultDispatcher: CoroutineDispatcher, ): Flow = combine(userDataStream) { searchResult, userData -> - withContext(defaultDispatcher) { - UserSearchResult( - topics = searchResult.topics.map { topic -> - FollowableTopic( - topic = topic, - isFollowed = topic.id in userData.followedTopics, - ) - }, - newsResources = searchResult.newsResources.map { news -> - UserNewsResource( - newsResource = news, - userData = userData, - ) - }, - ) - } - } + UserSearchResult( + topics = searchResult.topics.map { topic -> + FollowableTopic( + topic = topic, + isFollowed = topic.id in userData.followedTopics, + ) + }, + newsResources = searchResult.newsResources.map { news -> + UserNewsResource( + newsResource = news, + userData = userData, + ) + }, + ) + }.flowOn(defaultDispatcher)