|
|
@ -22,8 +22,13 @@ import com.google.samples.apps.nowinandroid.core.database.dao.TopicDao
|
|
|
|
import com.google.samples.apps.nowinandroid.core.database.dao.TopicFtsDao
|
|
|
|
import com.google.samples.apps.nowinandroid.core.database.dao.TopicFtsDao
|
|
|
|
import com.google.samples.apps.nowinandroid.core.database.model.asExternalModel
|
|
|
|
import com.google.samples.apps.nowinandroid.core.database.model.asExternalModel
|
|
|
|
import com.google.samples.apps.nowinandroid.core.database.model.asFtsEntity
|
|
|
|
import com.google.samples.apps.nowinandroid.core.database.model.asFtsEntity
|
|
|
|
|
|
|
|
import com.google.samples.apps.nowinandroid.core.network.Dispatcher
|
|
|
|
|
|
|
|
import com.google.samples.apps.nowinandroid.core.network.NiaDispatchers.IO
|
|
|
|
|
|
|
|
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.flow.flattenConcat
|
|
|
|
|
|
|
|
import kotlinx.coroutines.withContext
|
|
|
|
import javax.inject.Inject
|
|
|
|
import javax.inject.Inject
|
|
|
|
|
|
|
|
|
|
|
|
class DefaultSearchContentsRepository @Inject constructor(
|
|
|
|
class DefaultSearchContentsRepository @Inject constructor(
|
|
|
@ -31,11 +36,16 @@ class DefaultSearchContentsRepository @Inject constructor(
|
|
|
|
private val newsResourceFtsDao: NewsResourceFtsDao,
|
|
|
|
private val newsResourceFtsDao: NewsResourceFtsDao,
|
|
|
|
private val topicDao: TopicDao,
|
|
|
|
private val topicDao: TopicDao,
|
|
|
|
private val topicFtsDao: TopicFtsDao,
|
|
|
|
private val topicFtsDao: TopicFtsDao,
|
|
|
|
|
|
|
|
@Dispatcher(IO) private val ioDispatcher: CoroutineDispatcher,
|
|
|
|
) : SearchContentsRepository {
|
|
|
|
) : SearchContentsRepository {
|
|
|
|
|
|
|
|
|
|
|
|
override fun populateFtsData() {
|
|
|
|
override suspend fun populateFtsData() {
|
|
|
|
newsResourceFtsDao.insertAll(newsResourceDao.getOneOffNewsResources().map { it.asFtsEntity() })
|
|
|
|
withContext(ioDispatcher) {
|
|
|
|
topicFtsDao.insertAll(topicDao.getOneOffTopicEntities().map { it.asFtsEntity() })
|
|
|
|
newsResourceFtsDao.insertAll(
|
|
|
|
|
|
|
|
newsResourceDao.getOneOffNewsResources().map { it.asFtsEntity() },
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
topicFtsDao.insertAll(topicDao.getOneOffTopicEntities().map { it.asFtsEntity() })
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
override fun searchContents(searchQuery: String): Flow<SearchResult> {
|
|
|
|
override fun searchContents(searchQuery: String): Flow<SearchResult> {
|
|
|
@ -44,14 +54,16 @@ class DefaultSearchContentsRepository @Inject constructor(
|
|
|
|
val newsResourceIds = newsResourceFtsDao.searchAllNewsResources("*$searchQuery*")
|
|
|
|
val newsResourceIds = newsResourceFtsDao.searchAllNewsResources("*$searchQuery*")
|
|
|
|
val topicIds = topicFtsDao.searchAllTopics("*$searchQuery*")
|
|
|
|
val topicIds = topicFtsDao.searchAllTopics("*$searchQuery*")
|
|
|
|
|
|
|
|
|
|
|
|
return combine(
|
|
|
|
return combine(newsResourceIds, topicIds) { news, topics ->
|
|
|
|
newsResourceDao.getNewsResources(filterNewsIds = newsResourceIds.toSet()),
|
|
|
|
combine(
|
|
|
|
topicDao.getTopicEntities(topicIds.toSet()),
|
|
|
|
newsResourceDao.getNewsResources(filterNewsIds = news.toSet()),
|
|
|
|
) { newsResources, topics ->
|
|
|
|
topicDao.getTopicEntities(topics.toSet()),
|
|
|
|
SearchResult(
|
|
|
|
) { newsResources, topics ->
|
|
|
|
topics = topics.map { it.asExternalModel() },
|
|
|
|
SearchResult(
|
|
|
|
newsResources = newsResources.map { it.asExternalModel() },
|
|
|
|
topics = topics.map { it.asExternalModel() },
|
|
|
|
)
|
|
|
|
newsResources = newsResources.map { it.asExternalModel() },
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}.flattenConcat()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|