From 337c940d66b18823807679bb40bba01316ab5d09 Mon Sep 17 00:00:00 2001 From: Adetunji Dahunsi Date: Tue, 25 Apr 2023 05:41:30 -0400 Subject: [PATCH] Add comments explaining backend sync and made variable names easier to grok Change-Id: I90cd7444de95efa20bf243a922a772f7849a23ec --- .../core/data/repository/OfflineFirstNewsRepository.kt | 9 +++++---- 1 file changed, 5 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 5467deb73..3e22103b9 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,8 +80,8 @@ class OfflineFirstNewsRepository @Inject constructor( // TODO: Make this more efficient, there is no need to retrieve populated // news resources when all that's needed are the ids - val existingFollowedChangedNewsResourceIds = when { - hasOnBoarded -> newsResourceDao.getNewsResources( + val existingNewsResourceIdsThatHaveChanged = when { + hasOnboarded -> newsResourceDao.getNewsResources( useFilterTopicIds = true, filterTopicIds = followedTopicIds, useFilterNewsIds = true, @@ -94,6 +94,7 @@ class OfflineFirstNewsRepository @Inject constructor( else -> emptySet() } + // Obtain the news resources which have changed from the network and upsert them locally changedIds.chunked(SYNC_BATCH_SIZE).forEach { chunkedIds -> val networkNewsResources = network.getNewsResources(ids = chunkedIds) @@ -118,12 +119,12 @@ class OfflineFirstNewsRepository @Inject constructor( ) } - if (hasOnBoarded) { + if (hasOnboarded) { val addedNewsResources = newsResourceDao.getNewsResources( useFilterTopicIds = true, filterTopicIds = followedTopicIds, useFilterNewsIds = true, - filterNewsIds = changedIds.toSet() - existingFollowedChangedNewsResourceIds, + filterNewsIds = changedIds.toSet() - existingNewsResourceIdsThatHaveChanged, ) .first() .map(PopulatedNewsResource::asExternalModel)