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,20 +46,18 @@ 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( topic = topic,
topic = topic, isFollowed = topic.id in userData.followedTopics,
isFollowed = topic.id in userData.followedTopics, )
)
}
when (sortBy) {
NAME -> followedTopics.sortedBy { it.topic.name }
else -> followedTopics
} }
when (sortBy) {
NAME -> followedTopics.sortedBy { it.topic.name }
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,20 +52,18 @@ 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( topic = topic,
topic = topic, isFollowed = topic.id in userData.followedTopics,
isFollowed = topic.id in userData.followedTopics, )
) },
}, newsResources = searchResult.newsResources.map { news ->
newsResources = searchResult.newsResources.map { news -> UserNewsResource(
UserNewsResource( newsResource = news,
newsResource = news, userData = userData,
userData = userData, )
) },
}, )
) }.flowOn(defaultDispatcher)
}
}

Loading…
Cancel
Save