Skip to content

Commit

Permalink
Use .shareIn for subscribed podcasts.
Browse files Browse the repository at this point in the history
  • Loading branch information
arriolac committed Apr 26, 2024
1 parent fac7832 commit 737e634
Showing 1 changed file with 14 additions and 16 deletions.
Expand Up @@ -35,17 +35,18 @@ import com.example.jetcaster.core.model.PodcastInfo
import com.example.jetcaster.core.player.EpisodePlayer
import com.example.jetcaster.core.util.combine
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject
import kotlinx.collections.immutable.PersistentList
import kotlinx.collections.immutable.persistentListOf
import kotlinx.collections.immutable.toPersistentList
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.shareIn
import kotlinx.coroutines.launch
import javax.inject.Inject

@OptIn(ExperimentalCoroutinesApi::class)
@HiltViewModel
Expand Down Expand Up @@ -73,7 +74,8 @@ class HomeViewModel @Inject constructor(
val state: StateFlow<HomeViewState>
get() = _state

val p = podcastStore.followedPodcastsSortedByLastEpisode(limit = 10).share
val subscribedPodcasts = podcastStore.followedPodcastsSortedByLastEpisode(limit = 10)
.shareIn(viewModelScope, SharingStarted.WhileSubscribed())

init {
viewModelScope.launch {
Expand All @@ -82,34 +84,30 @@ class HomeViewModel @Inject constructor(
combine(
homeCategories,
selectedHomeCategory,
subscribedPodcasts,
refreshing,
_selectedCategory.flatMapLatest { selectedCategory ->
filterableCategoriesUseCase(selectedCategory)
},
_selectedCategory.flatMapLatest {
podcastCategoryFilterUseCase(it)
},
podcastStore.followedPodcastsSortedByLastEpisode(limit = 10)
.flatMapLatest { podcasts ->
combine(
flowOf(podcasts),
episodeStore.episodesInPodcasts(
podcastUris = podcasts.map { it.podcast.uri },
limit = 20
),
::Pair
)
}
subscribedPodcasts.flatMapLatest { podcasts ->
episodeStore.episodesInPodcasts(
podcastUris = podcasts.map { it.podcast.uri },
limit = 20
)
}
) { homeCategories,
homeCategory,
podcasts,
refreshing,
filterableCategories,
podcastCategoryFilterResult,
podcastLibraryPair ->
libraryEpisodes ->

_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 737e634

Please sign in to comment.