diff --git a/app/src/main/java/com/google/samples/apps/nowinandroid/ui/NiaAppState.kt b/app/src/main/java/com/google/samples/apps/nowinandroid/ui/NiaAppState.kt index e472ee2af..df6fe1da2 100644 --- a/app/src/main/java/com/google/samples/apps/nowinandroid/ui/NiaAppState.kt +++ b/app/src/main/java/com/google/samples/apps/nowinandroid/ui/NiaAppState.kt @@ -126,8 +126,8 @@ class NiaAppState( * The top level destinations that have unread news resources. */ val topLevelDestinationsWithUnreadResources: StateFlow> = - userNewsResourceRepository.getUserNewsResourcesForFollowedTopics() - .combine(userNewsResourceRepository.getBookmarkedUserNewsResources()) { forYouNewsResources, bookmarkedNewsResources -> + userNewsResourceRepository.observeAllForFollowedTopics() + .combine(userNewsResourceRepository.observeAllBookmarked()) { forYouNewsResources, bookmarkedNewsResources -> setOfNotNull( FOR_YOU.takeIf { forYouNewsResources.any { !it.hasBeenViewed } }, BOOKMARKS.takeIf { bookmarkedNewsResources.any { !it.hasBeenViewed } }, diff --git a/core/data/src/main/java/com/google/samples/apps/nowinandroid/core/data/repository/CompositeUserNewsResourceRepository.kt b/core/data/src/main/java/com/google/samples/apps/nowinandroid/core/data/repository/CompositeUserNewsResourceRepository.kt index dc9ad299f..64e02e7d9 100644 --- a/core/data/src/main/java/com/google/samples/apps/nowinandroid/core/data/repository/CompositeUserNewsResourceRepository.kt +++ b/core/data/src/main/java/com/google/samples/apps/nowinandroid/core/data/repository/CompositeUserNewsResourceRepository.kt @@ -38,7 +38,7 @@ class CompositeUserNewsResourceRepository @Inject constructor( /** * Returns available news resources (joined with user data) matching the given query. */ - override fun getUserNewsResources( + override fun observeAll( query: NewsResourceQuery, ): Flow> = newsRepository.getNewsResources(query) @@ -49,21 +49,21 @@ class CompositeUserNewsResourceRepository @Inject constructor( /** * Returns available news resources (joined with user data) for the followed topics. */ - override fun getUserNewsResourcesForFollowedTopics(): Flow> = + override fun observeAllForFollowedTopics(): Flow> = userDataRepository.userData.map { it.followedTopics }.distinctUntilChanged() .flatMapLatest { followedTopics -> when { followedTopics.isEmpty() -> flowOf(emptyList()) - else -> getUserNewsResources(NewsResourceQuery(filterTopicIds = followedTopics)) + else -> observeAll(NewsResourceQuery(filterTopicIds = followedTopics)) } } - override fun getBookmarkedUserNewsResources(): Flow> = + override fun observeAllBookmarked(): Flow> = userDataRepository.userData.map { it.bookmarkedNewsResources }.distinctUntilChanged() .flatMapLatest { bookmarkedNewsResources -> when { bookmarkedNewsResources.isEmpty() -> flowOf(emptyList()) - else -> getUserNewsResources(NewsResourceQuery(filterNewsIds = bookmarkedNewsResources)) + else -> observeAll(NewsResourceQuery(filterNewsIds = bookmarkedNewsResources)) } } } diff --git a/core/data/src/main/java/com/google/samples/apps/nowinandroid/core/data/repository/UserNewsResourceRepository.kt b/core/data/src/main/java/com/google/samples/apps/nowinandroid/core/data/repository/UserNewsResourceRepository.kt index 9f7540da2..4e3e214bc 100644 --- a/core/data/src/main/java/com/google/samples/apps/nowinandroid/core/data/repository/UserNewsResourceRepository.kt +++ b/core/data/src/main/java/com/google/samples/apps/nowinandroid/core/data/repository/UserNewsResourceRepository.kt @@ -26,7 +26,7 @@ interface UserNewsResourceRepository { /** * Returns available news resources as a stream. */ - fun getUserNewsResources( + fun observeAll( query: NewsResourceQuery = NewsResourceQuery( filterTopicIds = null, filterNewsIds = null, @@ -36,10 +36,10 @@ interface UserNewsResourceRepository { /** * Returns available news resources for the user's followed topics as a stream. */ - fun getUserNewsResourcesForFollowedTopics(): Flow> + fun observeAllForFollowedTopics(): Flow> /** * */ - fun getBookmarkedUserNewsResources(): Flow> + fun observeAllBookmarked(): Flow> } diff --git a/core/data/src/test/java/com/google/samples/apps/nowinandroid/core/data/CompositeUserNewsResourceRepositoryTest.kt b/core/data/src/test/java/com/google/samples/apps/nowinandroid/core/data/CompositeUserNewsResourceRepositoryTest.kt index 78271b809..eb4241295 100644 --- a/core/data/src/test/java/com/google/samples/apps/nowinandroid/core/data/CompositeUserNewsResourceRepositoryTest.kt +++ b/core/data/src/test/java/com/google/samples/apps/nowinandroid/core/data/CompositeUserNewsResourceRepositoryTest.kt @@ -44,7 +44,7 @@ class CompositeUserNewsResourceRepositoryTest { @Test fun whenNoFilters_allNewsResourcesAreReturned() = runTest { // Obtain the user news resources flow. - val userNewsResources = userNewsResourceRepository.getUserNewsResources() + val userNewsResources = userNewsResourceRepository.observeAll() // Send some news resources and user data into the data repositories. newsRepository.sendNewsResources(sampleNewsResources) @@ -68,7 +68,7 @@ class CompositeUserNewsResourceRepositoryTest { fun whenFilteredByTopicId_matchingNewsResourcesAreReturned() = runTest { // Obtain a stream of user news resources for the given topic id. val userNewsResources = - userNewsResourceRepository.getUserNewsResources( + userNewsResourceRepository.observeAll( NewsResourceQuery( filterTopicIds = setOf( sampleTopic1.id, @@ -93,7 +93,7 @@ class CompositeUserNewsResourceRepositoryTest { fun whenFilteredByFollowedTopics_matchingNewsResourcesAreReturned() = runTest { // Obtain a stream of user news resources for the given topic id. val userNewsResources = - userNewsResourceRepository.getUserNewsResourcesForFollowedTopics() + userNewsResourceRepository.observeAllForFollowedTopics() // Send test data into the repositories. val userData = emptyUserData.copy( @@ -114,7 +114,7 @@ class CompositeUserNewsResourceRepositoryTest { @Test fun whenFilteredByBookmarkedResources_matchingNewsResourcesAreReturned() = runTest { // Obtain the bookmarked user news resources flow. - val userNewsResources = userNewsResourceRepository.getBookmarkedUserNewsResources() + val userNewsResources = userNewsResourceRepository.observeAllBookmarked() // Send some news resources and user data into the data repositories. newsRepository.sendNewsResources(sampleNewsResources) diff --git a/feature/bookmarks/src/main/java/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksViewModel.kt b/feature/bookmarks/src/main/java/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksViewModel.kt index 82d2c0e19..8a1869322 100644 --- a/feature/bookmarks/src/main/java/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksViewModel.kt +++ b/feature/bookmarks/src/main/java/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksViewModel.kt @@ -39,7 +39,7 @@ class BookmarksViewModel @Inject constructor( ) : ViewModel() { val feedUiState: StateFlow = - userNewsResourceRepository.getBookmarkedUserNewsResources() + userNewsResourceRepository.observeAllBookmarked() .map, NewsFeedUiState>(NewsFeedUiState::Success) .onStart { emit(Loading) } .stateIn( diff --git a/feature/topic/src/main/java/com/google/samples/apps/nowinandroid/feature/topic/TopicViewModel.kt b/feature/topic/src/main/java/com/google/samples/apps/nowinandroid/feature/topic/TopicViewModel.kt index 425d66c73..2b2565f9e 100644 --- a/feature/topic/src/main/java/com/google/samples/apps/nowinandroid/feature/topic/TopicViewModel.kt +++ b/feature/topic/src/main/java/com/google/samples/apps/nowinandroid/feature/topic/TopicViewModel.kt @@ -145,7 +145,7 @@ private fun newsUiState( userDataRepository: UserDataRepository, ): Flow { // Observe news - val newsStream: Flow> = userNewsResourceRepository.getUserNewsResources( + val newsStream: Flow> = userNewsResourceRepository.observeAll( NewsResourceQuery(filterTopicIds = setOf(element = topicId)), )