From 805914dadeb928ecbb090acd6a6f43ddf6267441 Mon Sep 17 00:00:00 2001 From: blackbracken Date: Thu, 13 Jul 2023 18:52:53 +0900 Subject: [PATCH 1/3] Call getNewsResourceIds instead of getNewsResources --- .../repository/OfflineFirstNewsRepository.kt | 5 +--- .../data/testdoubles/TestNewsResourceDao.kt | 27 +++++++++++++++++++ .../core/database/dao/NewsResourceDao.kt | 27 +++++++++++++++++++ 3 files changed, 55 insertions(+), 4 deletions(-) diff --git a/core/data/src/main/java/com/google/samples/apps/nowinandroid/core/data/repository/OfflineFirstNewsRepository.kt b/core/data/src/main/java/com/google/samples/apps/nowinandroid/core/data/repository/OfflineFirstNewsRepository.kt index b18bb9044..ce395ad1c 100644 --- a/core/data/src/main/java/com/google/samples/apps/nowinandroid/core/data/repository/OfflineFirstNewsRepository.kt +++ b/core/data/src/main/java/com/google/samples/apps/nowinandroid/core/data/repository/OfflineFirstNewsRepository.kt @@ -80,17 +80,14 @@ class OfflineFirstNewsRepository @Inject constructor( val hasOnboarded = userData.shouldHideOnboarding 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 { - hasOnboarded -> newsResourceDao.getNewsResources( + hasOnboarded -> newsResourceDao.getNewsResourceIds( useFilterTopicIds = true, filterTopicIds = followedTopicIds, useFilterNewsIds = true, filterNewsIds = changedIds.toSet(), ) .first() - .map { it.entity.id } .toSet() // No need to retrieve anything if notifications won't be sent else -> emptySet() diff --git a/core/data/src/test/java/com/google/samples/apps/nowinandroid/core/data/testdoubles/TestNewsResourceDao.kt b/core/data/src/test/java/com/google/samples/apps/nowinandroid/core/data/testdoubles/TestNewsResourceDao.kt index d5d8932e7..6e5c45305 100644 --- a/core/data/src/test/java/com/google/samples/apps/nowinandroid/core/data/testdoubles/TestNewsResourceDao.kt +++ b/core/data/src/test/java/com/google/samples/apps/nowinandroid/core/data/testdoubles/TestNewsResourceDao.kt @@ -67,6 +67,33 @@ class TestNewsResourceDao : NewsResourceDao { result } + override fun getNewsResourceIds( + useFilterTopicIds: Boolean, + filterTopicIds: Set, + useFilterNewsIds: Boolean, + filterNewsIds: Set, + ): Flow> = + 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( entities: List, ): List { diff --git a/core/database/src/main/java/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceDao.kt b/core/database/src/main/java/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceDao.kt index a05507a8b..a3e1e158b 100644 --- a/core/database/src/main/java/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceDao.kt +++ b/core/database/src/main/java/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceDao.kt @@ -65,6 +65,33 @@ interface NewsResourceDao { filterNewsIds: Set = emptySet(), ): Flow> + @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 = emptySet(), + useFilterNewsIds: Boolean = false, + filterNewsIds: Set = emptySet(), + ): Flow> + /** * Inserts [entities] into the db if they don't exist, and ignores those that do */ From e9a16e437def5feab1c94e46fadeed671e065604 Mon Sep 17 00:00:00 2001 From: blackbracken Date: Wed, 19 Jul 2023 02:45:29 +0900 Subject: [PATCH 2/3] add kdoc for getNewsResourceIds --- .../apps/nowinandroid/core/database/dao/NewsResourceDao.kt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/core/database/src/main/java/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceDao.kt b/core/database/src/main/java/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceDao.kt index a3e1e158b..4134f569b 100644 --- a/core/database/src/main/java/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceDao.kt +++ b/core/database/src/main/java/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceDao.kt @@ -65,6 +65,9 @@ interface NewsResourceDao { filterNewsIds: Set = emptySet(), ): Flow> + /** + * Fetches ids of news resources that match the query parameters + */ @Transaction @Query( value = """ From b570be2abbefc9176b726baec289be7618ebb7a6 Mon Sep 17 00:00:00 2001 From: blackbracken Date: Wed, 19 Jul 2023 02:53:29 +0900 Subject: [PATCH 3/3] reformat by spotless --- .../apps/nowinandroid/core/database/dao/NewsResourceDao.kt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/core/database/src/main/java/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceDao.kt b/core/database/src/main/java/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceDao.kt index 4134f569b..0ad1e4f7d 100644 --- a/core/database/src/main/java/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceDao.kt +++ b/core/database/src/main/java/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceDao.kt @@ -70,7 +70,7 @@ interface NewsResourceDao { */ @Transaction @Query( - value = """ + value = """ SELECT id FROM news_resources WHERE CASE WHEN :useFilterNewsIds @@ -87,7 +87,8 @@ interface NewsResourceDao { ELSE 1 END ORDER BY publish_date DESC - """) + """, + ) fun getNewsResourceIds( useFilterTopicIds: Boolean = false, filterTopicIds: Set = emptySet(),