Skip to content

Commit

Permalink
Subscribe once to subscribed podcasts.
Browse files Browse the repository at this point in the history
  • Loading branch information
arriolac committed Apr 26, 2024
1 parent 1a38884 commit fac7832
Showing 1 changed file with 16 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.launch

@OptIn(ExperimentalCoroutinesApi::class)
Expand All @@ -69,42 +70,46 @@ class HomeViewModel @Inject constructor(
// Holds the view state if the UI is refreshing for new data
private val refreshing = MutableStateFlow(false)

private val followedPodcasts = podcastStore.followedPodcastsSortedByLastEpisode(limit = 10)

val state: StateFlow<HomeViewState>
get() = _state

val p = podcastStore.followedPodcastsSortedByLastEpisode(limit = 10).share

init {
viewModelScope.launch {
// Combines the latest value from each of the flows, allowing us to generate a
// view state instance which only contains the latest values.
combine(
homeCategories,
selectedHomeCategory,
followedPodcasts,
refreshing,
_selectedCategory.flatMapLatest { selectedCategory ->
filterableCategoriesUseCase(selectedCategory)
},
_selectedCategory.flatMapLatest {
podcastCategoryFilterUseCase(it)
},
followedPodcasts.flatMapLatest { podcast ->
episodeStore.episodesInPodcasts(
podcastUris = podcast.map { it.podcast.uri },
limit = 20
)
}
podcastStore.followedPodcastsSortedByLastEpisode(limit = 10)
.flatMapLatest { podcasts ->
combine(
flowOf(podcasts),
episodeStore.episodesInPodcasts(
podcastUris = podcasts.map { it.podcast.uri },
limit = 20
),
::Pair
)
}
) { homeCategories,
homeCategory,
podcasts,
refreshing,
filterableCategories,
podcastCategoryFilterResult,
libraryEpisodes ->
podcastLibraryPair ->

_selectedCategory.value = filterableCategories.selectedCategory

val (podcasts, libraryEpisodes) = podcastLibraryPair
// Override selected home category to show 'DISCOVER' if there are no
// featured podcasts
selectedHomeCategory.value =
Expand Down

0 comments on commit fac7832

Please sign in to comment.