Mark all news resources read on first sync

Otherwise, there will be unread articles until the user clicks through
every article that has ever been published.
pull/720/head
James Rose 2 years ago
parent a68eedebfa
commit 2f2dfb00a4

@ -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)
@ -138,3 +146,4 @@ 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 {
newsResourceIds.forEach {
if (viewed) { if (viewed) {
viewedNewsResourceIds.put(newsResourceId, true) viewedNewsResourceIds.put(it, true)
} else { } else {
viewedNewsResourceIds.remove(newsResourceId) viewedNewsResourceIds.remove(it)
}
} }
} }
} }

Loading…
Cancel
Save