Rename getUserNewsResources to observeAll

pull/595/head
James Rose 2 years ago
parent 57c13d84bd
commit b49767a88c

@ -126,8 +126,8 @@ class NiaAppState(
* The top level destinations that have unread news resources.
*/
val topLevelDestinationsWithUnreadResources: StateFlow<Set<TopLevelDestination>> =
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 } },

@ -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<List<UserNewsResource>> =
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<List<UserNewsResource>> =
override fun observeAllForFollowedTopics(): Flow<List<UserNewsResource>> =
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<List<UserNewsResource>> =
override fun observeAllBookmarked(): Flow<List<UserNewsResource>> =
userDataRepository.userData.map { it.bookmarkedNewsResources }.distinctUntilChanged()
.flatMapLatest { bookmarkedNewsResources ->
when {
bookmarkedNewsResources.isEmpty() -> flowOf(emptyList())
else -> getUserNewsResources(NewsResourceQuery(filterNewsIds = bookmarkedNewsResources))
else -> observeAll(NewsResourceQuery(filterNewsIds = bookmarkedNewsResources))
}
}
}

@ -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<List<UserNewsResource>>
fun observeAllForFollowedTopics(): Flow<List<UserNewsResource>>
/**
*
*/
fun getBookmarkedUserNewsResources(): Flow<List<UserNewsResource>>
fun observeAllBookmarked(): Flow<List<UserNewsResource>>
}

@ -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)

@ -39,7 +39,7 @@ class BookmarksViewModel @Inject constructor(
) : ViewModel() {
val feedUiState: StateFlow<NewsFeedUiState> =
userNewsResourceRepository.getBookmarkedUserNewsResources()
userNewsResourceRepository.observeAllBookmarked()
.map<List<UserNewsResource>, NewsFeedUiState>(NewsFeedUiState::Success)
.onStart { emit(Loading) }
.stateIn(

@ -145,7 +145,7 @@ private fun newsUiState(
userDataRepository: UserDataRepository,
): Flow<NewsUiState> {
// Observe news
val newsStream: Flow<List<UserNewsResource>> = userNewsResourceRepository.getUserNewsResources(
val newsStream: Flow<List<UserNewsResource>> = userNewsResourceRepository.observeAll(
NewsResourceQuery(filterTopicIds = setOf(element = topicId)),
)

Loading…
Cancel
Save