Merge pull request #720 from android/jr/mark-read-first-sync

Mark all news resources read on first sync
pull/723/head
Don Turner 2 years ago committed by GitHub
commit 2dd36e3de0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -63,10 +63,12 @@ class OfflineFirstNewsRepository @Inject constructor(
) )
.map { it.map(PopulatedNewsResource::asExternalModel) } .map { it.map(PopulatedNewsResource::asExternalModel) }
override suspend fun syncWith(synchronizer: Synchronizer) = override suspend fun syncWith(synchronizer: Synchronizer): Boolean {
synchronizer.changeListSync( var isFirstSync = false
return synchronizer.changeListSync(
versionReader = ChangeListVersions::newsResourceVersion, versionReader = ChangeListVersions::newsResourceVersion,
changeListFetcher = { currentVersion -> changeListFetcher = { currentVersion ->
isFirstSync = currentVersion <= 0
network.getNewsResourceChangeList(after = currentVersion) network.getNewsResourceChangeList(after = currentVersion)
}, },
versionUpdater = { latestVersion -> versionUpdater = { latestVersion ->
@ -94,6 +96,12 @@ class OfflineFirstNewsRepository @Inject constructor(
else -> emptySet() else -> emptySet()
} }
if (isFirstSync) {
// When we first retrieve news, mark everything viewed, so that we aren't
// overwhelmed with all historical news.
niaPreferencesDataSource.setNewsResourcesViewed(changedIds, true)
}
// Obtain the news resources which have changed from the network and upsert them locally // Obtain the news resources which have changed from the network and upsert them locally
changedIds.chunked(SYNC_BATCH_SIZE).forEach { chunkedIds -> changedIds.chunked(SYNC_BATCH_SIZE).forEach { chunkedIds ->
val networkNewsResources = network.getNewsResources(ids = chunkedIds) val networkNewsResources = network.getNewsResources(ids = chunkedIds)
@ -137,4 +145,5 @@ class OfflineFirstNewsRepository @Inject constructor(
} }
}, },
) )
}
} }

@ -288,6 +288,33 @@ class OfflineFirstNewsRepositoryTest {
) )
} }
@Test
fun offlineFirstNewsRepository_sync_marks_as_read_on_first_run() =
testScope.runTest {
subject.syncWith(synchronizer)
assertEquals(
network.getNewsResources().map { it.id }.toSet(),
niaPreferencesDataSource.userData.first().viewedNewsResources,
)
}
@Test
fun offlineFirstNewsRepository_sync_does_not_mark_as_read_on_subsequent_run() =
testScope.runTest {
// Pretend that we already have up to change list 7
synchronizer.updateChangeListVersions {
copy(newsResourceVersion = 7)
}
subject.syncWith(synchronizer)
assertEquals(
emptySet(),
niaPreferencesDataSource.userData.first().viewedNewsResources,
)
}
@Test @Test
fun offlineFirstNewsRepository_sends_notifications_for_newly_synced_news_that_is_followed() = fun offlineFirstNewsRepository_sends_notifications_for_newly_synced_news_that_is_followed() =
testScope.runTest { testScope.runTest {

@ -139,12 +139,18 @@ class NiaPreferencesDataSource @Inject constructor(
} }
suspend fun setNewsResourceViewed(newsResourceId: String, viewed: Boolean) { suspend fun setNewsResourceViewed(newsResourceId: String, viewed: Boolean) {
setNewsResourcesViewed(listOf(newsResourceId), viewed)
}
suspend fun setNewsResourcesViewed(newsResourceIds: List<String>, viewed: Boolean) {
userPreferences.updateData { userPreferences.updateData {
it.copy { it.copy {
if (viewed) { newsResourceIds.forEach {
viewedNewsResourceIds.put(newsResourceId, true) if (viewed) {
} else { viewedNewsResourceIds.put(it, true)
viewedNewsResourceIds.remove(newsResourceId) } else {
viewedNewsResourceIds.remove(it)
}
} }
} }
} }

Loading…
Cancel
Save