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) }
override suspend fun syncWith(synchronizer: Synchronizer) =
synchronizer.changeListSync(
override suspend fun syncWith(synchronizer: Synchronizer): Boolean {
var isFirstSync = false
return synchronizer.changeListSync(
versionReader = ChangeListVersions::newsResourceVersion,
changeListFetcher = { currentVersion ->
isFirstSync = currentVersion <= 0
network.getNewsResourceChangeList(after = currentVersion)
},
versionUpdater = { latestVersion ->
@ -94,6 +96,12 @@ class OfflineFirstNewsRepository @Inject constructor(
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
changedIds.chunked(SYNC_BATCH_SIZE).forEach { 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
fun offlineFirstNewsRepository_sends_notifications_for_newly_synced_news_that_is_followed() =
testScope.runTest {

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

Loading…
Cancel
Save