Merge pull request #835 from blackbracken/avoid_retrieving_unnecessary_entity

Call getNewsResourceIds instead of getNewsResources
pull/856/head
Adetunji Dahunsi 1 year ago committed by GitHub
commit 48041fcaa4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -80,17 +80,14 @@ class OfflineFirstNewsRepository @Inject constructor(
val hasOnboarded = userData.shouldHideOnboarding val hasOnboarded = userData.shouldHideOnboarding
val followedTopicIds = userData.followedTopics val followedTopicIds = userData.followedTopics
// TODO: Make this more efficient, there is no need to retrieve populated
// news resources when all that's needed are the ids
val existingNewsResourceIdsThatHaveChanged = when { val existingNewsResourceIdsThatHaveChanged = when {
hasOnboarded -> newsResourceDao.getNewsResources( hasOnboarded -> newsResourceDao.getNewsResourceIds(
useFilterTopicIds = true, useFilterTopicIds = true,
filterTopicIds = followedTopicIds, filterTopicIds = followedTopicIds,
useFilterNewsIds = true, useFilterNewsIds = true,
filterNewsIds = changedIds.toSet(), filterNewsIds = changedIds.toSet(),
) )
.first() .first()
.map { it.entity.id }
.toSet() .toSet()
// No need to retrieve anything if notifications won't be sent // No need to retrieve anything if notifications won't be sent
else -> emptySet() else -> emptySet()

@ -67,6 +67,33 @@ class TestNewsResourceDao : NewsResourceDao {
result result
} }
override fun getNewsResourceIds(
useFilterTopicIds: Boolean,
filterTopicIds: Set<String>,
useFilterNewsIds: Boolean,
filterNewsIds: Set<String>,
): Flow<List<String>> =
entitiesStateFlow
.map { newsResourceEntities ->
newsResourceEntities.map { entity ->
entity.asPopulatedNewsResource(topicCrossReferences)
}
}
.map { resources ->
var result = resources
if (useFilterTopicIds) {
result = result.filter { resource ->
resource.topics.any { it.id in filterTopicIds }
}
}
if (useFilterNewsIds) {
result = result.filter { resource ->
resource.entity.id in filterNewsIds
}
}
result.map { it.entity.id }
}
override suspend fun insertOrIgnoreNewsResources( override suspend fun insertOrIgnoreNewsResources(
entities: List<NewsResourceEntity>, entities: List<NewsResourceEntity>,
): List<Long> { ): List<Long> {

@ -65,6 +65,37 @@ interface NewsResourceDao {
filterNewsIds: Set<String> = emptySet(), filterNewsIds: Set<String> = emptySet(),
): Flow<List<PopulatedNewsResource>> ): Flow<List<PopulatedNewsResource>>
/**
* Fetches ids of news resources that match the query parameters
*/
@Transaction
@Query(
value = """
SELECT id FROM news_resources
WHERE
CASE WHEN :useFilterNewsIds
THEN id IN (:filterNewsIds)
ELSE 1
END
AND
CASE WHEN :useFilterTopicIds
THEN id IN
(
SELECT news_resource_id FROM news_resources_topics
WHERE topic_id IN (:filterTopicIds)
)
ELSE 1
END
ORDER BY publish_date DESC
""",
)
fun getNewsResourceIds(
useFilterTopicIds: Boolean = false,
filterTopicIds: Set<String> = emptySet(),
useFilterNewsIds: Boolean = false,
filterNewsIds: Set<String> = emptySet(),
): Flow<List<String>>
/** /**
* Inserts [entities] into the db if they don't exist, and ignores those that do * Inserts [entities] into the db if they don't exist, and ignores those that do
*/ */

Loading…
Cancel
Save