Slightly more precise flow transformations in the ForYouViewModel

Change-Id: Ib79ce98ad715f8d5f805f285c678dfcc6370ca13
tj/for-you-flows
Adetunji Dahunsi 2 years ago
parent 912049c3dc
commit 1f12a9e6ea

@ -106,40 +106,33 @@ class ForYouViewModel @Inject constructor(
} }
val feedState: StateFlow<NewsFeedUiState> = val feedState: StateFlow<NewsFeedUiState> =
combine( followedInterestsUiState.flatMapLatest { interestsState ->
followedInterestsUiState, when (interestsState) {
snapshotFlow { inProgressTopicSelection },
snapshotFlow { inProgressAuthorSelection }
) { followedInterestsUserState, inProgressTopicSelection, inProgressAuthorSelection ->
when (followedInterestsUserState) {
// If we don't know the current selection state, emit loading.
Unknown -> flowOf<NewsFeedUiState>(NewsFeedUiState.Loading)
// If the user has followed topics, use those followed topics to populate the feed // If the user has followed topics, use those followed topics to populate the feed
is FollowedInterests -> { is FollowedInterests -> newsRepository.getNewsResourcesStream(
filterTopicIds = interestsState.topicIds,
newsRepository.getNewsResourcesStream( filterAuthorIds = interestsState.authorIds
filterTopicIds = followedInterestsUserState.topicIds, ).mapToFeedState(savedNewsResourcesState)
filterAuthorIds = followedInterestsUserState.authorIds
).mapToFeedState(savedNewsResourcesState)
}
// If the user hasn't followed interests yet, show a realtime populated feed based // If the user hasn't followed interests yet, show a realtime populated feed based
// on the in-progress interests selections, if there are any. // on the in-progress interests selections, if there are any.
None -> { None -> combine(
if (inProgressTopicSelection.isEmpty() && inProgressAuthorSelection.isEmpty()) { snapshotFlow { inProgressTopicSelection },
flowOf<NewsFeedUiState>(NewsFeedUiState.Success(emptyList())) snapshotFlow { inProgressAuthorSelection },
} else { ::Pair
newsRepository.getNewsResourcesStream( ).flatMapLatest { (inProgressTopicSelection, inProgressAuthorSelection) ->
when {
inProgressTopicSelection.isEmpty() && inProgressAuthorSelection.isEmpty() ->
flowOf<NewsFeedUiState>(NewsFeedUiState.Success(emptyList()))
else -> newsRepository.getNewsResourcesStream(
filterTopicIds = inProgressTopicSelection, filterTopicIds = inProgressTopicSelection,
filterAuthorIds = inProgressAuthorSelection filterAuthorIds = inProgressAuthorSelection
).mapToFeedState(savedNewsResourcesState) ).mapToFeedState(savedNewsResourcesState)
} }
} }
// If we don't know the current selection state, emit loading.
Unknown -> flowOf<NewsFeedUiState>(NewsFeedUiState.Loading)
} }
} }
// Flatten the feed flows.
// As the selected topics and topic state changes, this will cancel the old feed
// monitoring and start the new one.
.flatMapLatest { it }
.stateIn( .stateIn(
scope = viewModelScope, scope = viewModelScope,
started = SharingStarted.WhileSubscribed(5_000), started = SharingStarted.WhileSubscribed(5_000),
@ -147,19 +140,19 @@ class ForYouViewModel @Inject constructor(
) )
val interestsSelectionState: StateFlow<ForYouInterestsSelectionUiState> = val interestsSelectionState: StateFlow<ForYouInterestsSelectionUiState> =
combine( followedInterestsUiState.flatMapLatest { followedInterestsState ->
followedInterestsUiState, when (followedInterestsState) {
topicsRepository.getTopicsStream(), Unknown ->
authorsRepository.getAuthorsStream(), flowOf(ForYouInterestsSelectionUiState.Loading)
snapshotFlow { inProgressTopicSelection }, is FollowedInterests ->
snapshotFlow { inProgressAuthorSelection }, flowOf(ForYouInterestsSelectionUiState.NoInterestsSelection)
) { followedInterestsUserState, availableTopics, availableAuthors, inProgressTopicSelection, None -> combine(
inProgressAuthorSelection -> topicsRepository.getTopicsStream(),
authorsRepository.getAuthorsStream(),
when (followedInterestsUserState) { snapshotFlow { inProgressTopicSelection },
Unknown -> ForYouInterestsSelectionUiState.Loading snapshotFlow { inProgressAuthorSelection },
is FollowedInterests -> ForYouInterestsSelectionUiState.NoInterestsSelection ) { availableTopics, availableAuthors, inProgressTopicSelection,
None -> { inProgressAuthorSelection ->
val topics = availableTopics.map { topic -> val topics = availableTopics.map { topic ->
FollowableTopic( FollowableTopic(
topic = topic, topic = topic,

Loading…
Cancel
Save