Change withContext to flowOn.

Change-Id: I679f11aa0bcae1d2de54b0373a284f83d0f0c81b
pull/1238/head
Jaehwa Noh 2 years ago
parent 22130b62cb
commit 4916b9f58c

@ -33,6 +33,7 @@ import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flatMapLatest import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.mapLatest import kotlinx.coroutines.flow.mapLatest
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import javax.inject.Inject import javax.inject.Inject
@ -77,13 +78,11 @@ internal class DefaultSearchContentsRepository @Inject constructor(
.distinctUntilChanged() .distinctUntilChanged()
.flatMapLatest(topicDao::getTopicEntities) .flatMapLatest(topicDao::getTopicEntities)
return combine(newsResourcesFlow, topicsFlow) { newsResources, topics -> return combine(newsResourcesFlow, topicsFlow) { newsResources, topics ->
withContext(defaultDispatcher) {
SearchResult( SearchResult(
topics = topics.map { it.asExternalModel() }, topics = topics.map { it.asExternalModel() },
newsResources = newsResources.map { it.asExternalModel() }, newsResources = newsResources.map { it.asExternalModel() },
) )
} }.flowOn(defaultDispatcher)
}
} }
override fun getSearchContentsCount(): Flow<Int> = override fun getSearchContentsCount(): Flow<Int> =
@ -91,8 +90,6 @@ internal class DefaultSearchContentsRepository @Inject constructor(
newsResourceFtsDao.getCount(), newsResourceFtsDao.getCount(),
topicFtsDao.getCount(), topicFtsDao.getCount(),
) { newsResourceCount, topicsCount -> ) { newsResourceCount, topicsCount ->
withContext(defaultDispatcher) {
newsResourceCount + topicsCount newsResourceCount + topicsCount
} }.flowOn(defaultDispatcher)
}
} }

@ -26,7 +26,7 @@ import com.google.samples.apps.nowinandroid.core.network.NiaDispatchers.Default
import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.withContext import kotlinx.coroutines.flow.flowOn
import javax.inject.Inject import javax.inject.Inject
/** /**
@ -46,7 +46,6 @@ class GetFollowableTopicsUseCase @Inject constructor(
userDataRepository.userData, userDataRepository.userData,
topicsRepository.getTopics(), topicsRepository.getTopics(),
) { userData, topics -> ) { userData, topics ->
withContext(defaultDispatcher) {
val followedTopics = topics val followedTopics = topics
.map { topic -> .map { topic ->
FollowableTopic( FollowableTopic(
@ -58,8 +57,7 @@ class GetFollowableTopicsUseCase @Inject constructor(
NAME -> followedTopics.sortedBy { it.topic.name } NAME -> followedTopics.sortedBy { it.topic.name }
else -> followedTopics else -> followedTopics
} }
} }.flowOn(defaultDispatcher)
}
} }
enum class TopicSortField { enum class TopicSortField {

@ -28,7 +28,7 @@ import com.google.samples.apps.nowinandroid.core.network.NiaDispatchers.Default
import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.withContext import kotlinx.coroutines.flow.flowOn
import javax.inject.Inject import javax.inject.Inject
/** /**
@ -52,7 +52,6 @@ private fun Flow<SearchResult>.mapToUserSearchResult(
defaultDispatcher: CoroutineDispatcher, defaultDispatcher: CoroutineDispatcher,
): Flow<UserSearchResult> = ): Flow<UserSearchResult> =
combine(userDataStream) { searchResult, userData -> combine(userDataStream) { searchResult, userData ->
withContext(defaultDispatcher) {
UserSearchResult( UserSearchResult(
topics = searchResult.topics.map { topic -> topics = searchResult.topics.map { topic ->
FollowableTopic( FollowableTopic(
@ -67,5 +66,4 @@ private fun Flow<SearchResult>.mapToUserSearchResult(
) )
}, },
) )
} }.flowOn(defaultDispatcher)
}

Loading…
Cancel
Save