Skip to content

Commit 2f2dfb0

Browse files
committed
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.
1 parent a68eede commit 2f2dfb0

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

core/data/src/main/java/com/google/samples/apps/nowinandroid/core/data/repository/OfflineFirstNewsRepository.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,12 @@ class OfflineFirstNewsRepository @Inject constructor(
6363
)
6464
.map { it.map(PopulatedNewsResource::asExternalModel) }
6565

66-
override suspend fun syncWith(synchronizer: Synchronizer) =
67-
synchronizer.changeListSync(
66+
override suspend fun syncWith(synchronizer: Synchronizer): Boolean {
67+
var isFirstSync = false
68+
return synchronizer.changeListSync(
6869
versionReader = ChangeListVersions::newsResourceVersion,
6970
changeListFetcher = { currentVersion ->
71+
isFirstSync = currentVersion <= 0
7072
network.getNewsResourceChangeList(after = currentVersion)
7173
},
7274
versionUpdater = { latestVersion ->
@@ -94,6 +96,12 @@ class OfflineFirstNewsRepository @Inject constructor(
9496
else -> emptySet()
9597
}
9698

99+
if (isFirstSync) {
100+
// When we first retrieve news, mark everything viewed, so that we aren't
101+
// overwhelmed with all historical news.
102+
niaPreferencesDataSource.setNewsResourcesViewed(changedIds, true)
103+
}
104+
97105
// Obtain the news resources which have changed from the network and upsert them locally
98106
changedIds.chunked(SYNC_BATCH_SIZE).forEach { chunkedIds ->
99107
val networkNewsResources = network.getNewsResources(ids = chunkedIds)
@@ -137,4 +145,5 @@ class OfflineFirstNewsRepository @Inject constructor(
137145
}
138146
},
139147
)
148+
}
140149
}

core/data/src/test/java/com/google/samples/apps/nowinandroid/core/data/repository/OfflineFirstNewsRepositoryTest.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,33 @@ class OfflineFirstNewsRepositoryTest {
288288
)
289289
}
290290

291+
@Test
292+
fun offlineFirstNewsRepository_sync_marks_as_read_on_first_run() =
293+
testScope.runTest {
294+
subject.syncWith(synchronizer)
295+
296+
assertEquals(
297+
network.getNewsResources().map { it.id }.toSet(),
298+
niaPreferencesDataSource.userData.first().viewedNewsResources,
299+
)
300+
}
301+
302+
@Test
303+
fun offlineFirstNewsRepository_sync_does_not_mark_as_read_on_subsequent_run() =
304+
testScope.runTest {
305+
// Pretend that we already have up to change list 7
306+
synchronizer.updateChangeListVersions {
307+
copy(newsResourceVersion = 7)
308+
}
309+
310+
subject.syncWith(synchronizer)
311+
312+
assertEquals(
313+
emptySet(),
314+
niaPreferencesDataSource.userData.first().viewedNewsResources,
315+
)
316+
}
317+
291318
@Test
292319
fun offlineFirstNewsRepository_sends_notifications_for_newly_synced_news_that_is_followed() =
293320
testScope.runTest {

core/datastore/src/main/java/com/google/samples/apps/nowinandroid/core/datastore/NiaPreferencesDataSource.kt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,18 @@ class NiaPreferencesDataSource @Inject constructor(
139139
}
140140

141141
suspend fun setNewsResourceViewed(newsResourceId: String, viewed: Boolean) {
142+
setNewsResourcesViewed(listOf(newsResourceId), viewed)
143+
}
144+
145+
suspend fun setNewsResourcesViewed(newsResourceIds: List<String>, viewed: Boolean) {
142146
userPreferences.updateData {
143147
it.copy {
144-
if (viewed) {
145-
viewedNewsResourceIds.put(newsResourceId, true)
146-
} else {
147-
viewedNewsResourceIds.remove(newsResourceId)
148+
newsResourceIds.forEach {
149+
if (viewed) {
150+
viewedNewsResourceIds.put(it, true)
151+
} else {
152+
viewedNewsResourceIds.remove(it)
153+
}
148154
}
149155
}
150156
}

0 commit comments

Comments
 (0)