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. * The top level destinations that have unread news resources.
*/ */
val topLevelDestinationsWithUnreadResources: StateFlow<Set<TopLevelDestination>> = val topLevelDestinationsWithUnreadResources: StateFlow<Set<TopLevelDestination>> =
userNewsResourceRepository.getUserNewsResourcesForFollowedTopics() userNewsResourceRepository.observeAllForFollowedTopics()
.combine(userNewsResourceRepository.getBookmarkedUserNewsResources()) { forYouNewsResources, bookmarkedNewsResources -> .combine(userNewsResourceRepository.observeAllBookmarked()) { forYouNewsResources, bookmarkedNewsResources ->
setOfNotNull( setOfNotNull(
FOR_YOU.takeIf { forYouNewsResources.any { !it.hasBeenViewed } }, FOR_YOU.takeIf { forYouNewsResources.any { !it.hasBeenViewed } },
BOOKMARKS.takeIf { bookmarkedNewsResources.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. * Returns available news resources (joined with user data) matching the given query.
*/ */
override fun getUserNewsResources( override fun observeAll(
query: NewsResourceQuery, query: NewsResourceQuery,
): Flow<List<UserNewsResource>> = ): Flow<List<UserNewsResource>> =
newsRepository.getNewsResources(query) newsRepository.getNewsResources(query)
@ -49,21 +49,21 @@ class CompositeUserNewsResourceRepository @Inject constructor(
/** /**
* Returns available news resources (joined with user data) for the followed topics. * 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() userDataRepository.userData.map { it.followedTopics }.distinctUntilChanged()
.flatMapLatest { followedTopics -> .flatMapLatest { followedTopics ->
when { when {
followedTopics.isEmpty() -> flowOf(emptyList()) 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() userDataRepository.userData.map { it.bookmarkedNewsResources }.distinctUntilChanged()
.flatMapLatest { bookmarkedNewsResources -> .flatMapLatest { bookmarkedNewsResources ->
when { when {
bookmarkedNewsResources.isEmpty() -> flowOf(emptyList()) 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. * Returns available news resources as a stream.
*/ */
fun getUserNewsResources( fun observeAll(
query: NewsResourceQuery = NewsResourceQuery( query: NewsResourceQuery = NewsResourceQuery(
filterTopicIds = null, filterTopicIds = null,
filterNewsIds = null, filterNewsIds = null,
@ -36,10 +36,10 @@ interface UserNewsResourceRepository {
/** /**
* Returns available news resources for the user's followed topics as a stream. * 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 @Test
fun whenNoFilters_allNewsResourcesAreReturned() = runTest { fun whenNoFilters_allNewsResourcesAreReturned() = runTest {
// Obtain the user news resources flow. // 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. // Send some news resources and user data into the data repositories.
newsRepository.sendNewsResources(sampleNewsResources) newsRepository.sendNewsResources(sampleNewsResources)
@ -68,7 +68,7 @@ class CompositeUserNewsResourceRepositoryTest {
fun whenFilteredByTopicId_matchingNewsResourcesAreReturned() = runTest { fun whenFilteredByTopicId_matchingNewsResourcesAreReturned() = runTest {
// Obtain a stream of user news resources for the given topic id. // Obtain a stream of user news resources for the given topic id.
val userNewsResources = val userNewsResources =
userNewsResourceRepository.getUserNewsResources( userNewsResourceRepository.observeAll(
NewsResourceQuery( NewsResourceQuery(
filterTopicIds = setOf( filterTopicIds = setOf(
sampleTopic1.id, sampleTopic1.id,
@ -93,7 +93,7 @@ class CompositeUserNewsResourceRepositoryTest {
fun whenFilteredByFollowedTopics_matchingNewsResourcesAreReturned() = runTest { fun whenFilteredByFollowedTopics_matchingNewsResourcesAreReturned() = runTest {
// Obtain a stream of user news resources for the given topic id. // Obtain a stream of user news resources for the given topic id.
val userNewsResources = val userNewsResources =
userNewsResourceRepository.getUserNewsResourcesForFollowedTopics() userNewsResourceRepository.observeAllForFollowedTopics()
// Send test data into the repositories. // Send test data into the repositories.
val userData = emptyUserData.copy( val userData = emptyUserData.copy(
@ -114,7 +114,7 @@ class CompositeUserNewsResourceRepositoryTest {
@Test @Test
fun whenFilteredByBookmarkedResources_matchingNewsResourcesAreReturned() = runTest { fun whenFilteredByBookmarkedResources_matchingNewsResourcesAreReturned() = runTest {
// Obtain the bookmarked user news resources flow. // 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. // Send some news resources and user data into the data repositories.
newsRepository.sendNewsResources(sampleNewsResources) newsRepository.sendNewsResources(sampleNewsResources)

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

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

Loading…
Cancel
Save