Skip to content

Commit

Permalink
[Jetcaster]: Display latest episodes for all subscribed podcasts in l…
Browse files Browse the repository at this point in the history
…ibrary.
  • Loading branch information
arriolac committed Apr 25, 2024
1 parent ae41864 commit dc4fcef
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 51 deletions.
Expand Up @@ -119,12 +119,12 @@ import com.example.jetcaster.util.fullWidthItem
import com.example.jetcaster.util.isCompact
import com.example.jetcaster.util.quantityStringResource
import com.example.jetcaster.util.radialGradientScrim
import java.time.Duration
import java.time.LocalDateTime
import java.time.OffsetDateTime
import kotlinx.collections.immutable.PersistentList
import kotlinx.collections.immutable.toPersistentList
import kotlinx.coroutines.launch
import java.time.Duration
import java.time.LocalDateTime
import java.time.OffsetDateTime

data class HomeState(
val windowSizeClass: WindowSizeClass,
Expand Down Expand Up @@ -877,7 +877,7 @@ private fun PreviewHomeContent() {
),
podcastCategoryFilterResult = PodcastCategoryFilterResult(
topPodcasts = PreviewPodcasts,
episodes = PreviewPodcastCategoryEpisodes
episodes = PreviewPodcastEpisodes
),
library = LibraryInfo(),
onCategorySelected = {},
Expand Down Expand Up @@ -914,7 +914,7 @@ private fun PreviewHomeContentExpanded() {
),
podcastCategoryFilterResult = PodcastCategoryFilterResult(
topPodcasts = PreviewPodcasts,
episodes = PreviewPodcastCategoryEpisodes
episodes = PreviewPodcastEpisodes
),
library = LibraryInfo(),
onCategorySelected = {},
Expand Down
Expand Up @@ -20,6 +20,7 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.jetcaster.core.data.database.model.EpisodeToPodcast
import com.example.jetcaster.core.data.database.model.asExternalModel
import com.example.jetcaster.core.data.database.model.asPodcastToEpisodeInfo
import com.example.jetcaster.core.data.domain.FilterableCategoriesUseCase
import com.example.jetcaster.core.data.domain.PodcastCategoryFilterUseCase
import com.example.jetcaster.core.data.repository.EpisodeStore
Expand All @@ -34,7 +35,6 @@ 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
Expand All @@ -44,6 +44,7 @@ import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.launch
import javax.inject.Inject

@OptIn(ExperimentalCoroutinesApi::class)
@HiltViewModel
Expand All @@ -68,6 +69,8 @@ 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

Expand All @@ -78,17 +81,17 @@ class HomeViewModel @Inject constructor(
combine(
homeCategories,
selectedHomeCategory,
podcastStore.followedPodcastsSortedByLastEpisode(limit = 10),
followedPodcasts,
refreshing,
_selectedCategory.flatMapLatest { selectedCategory ->
filterableCategoriesUseCase(selectedCategory)
},
_selectedCategory.flatMapLatest {
podcastCategoryFilterUseCase(it)
},
selectedLibraryPodcast.flatMapLatest {
episodeStore.episodesInPodcast(
podcastUri = it?.uri ?: "",
followedPodcasts.flatMapLatest { podcast ->
episodeStore.episodesInPodcasts(
podcastUris = podcast.map { it.podcast.uri },
limit = 20
)
}
Expand Down Expand Up @@ -171,8 +174,7 @@ class HomeViewModel @Inject constructor(

private fun List<EpisodeToPodcast>.asLibrary(): LibraryInfo =
LibraryInfo(
podcast = this.firstOrNull()?.podcast?.asExternalModel(),
episodes = this.map { it.episode.asExternalModel() }
episodes = this.map { it.asPodcastToEpisodeInfo() }
)

enum class HomeCategory {
Expand Down
Expand Up @@ -18,8 +18,8 @@ package com.example.jetcaster.ui.home

import com.example.jetcaster.core.model.CategoryInfo
import com.example.jetcaster.core.model.EpisodeInfo
import com.example.jetcaster.core.model.PodcastCategoryEpisode
import com.example.jetcaster.core.model.PodcastInfo
import com.example.jetcaster.core.model.PodcastToEpisodeInfo
import java.time.OffsetDateTime
import java.time.ZoneOffset

Expand Down Expand Up @@ -58,8 +58,8 @@ val PreviewEpisodes = listOf(
)
)

val PreviewPodcastCategoryEpisodes = listOf(
PodcastCategoryEpisode(
val PreviewPodcastEpisodes = listOf(
PodcastToEpisodeInfo(
podcast = PreviewPodcasts[0],
episode = PreviewEpisodes[0],
)
Expand Down
Expand Up @@ -40,12 +40,6 @@ fun LazyListScope.libraryItems(
navigateToPlayer: (EpisodeInfo) -> Unit,
onQueueEpisode: (PlayerEpisode) -> Unit
) {
val podcast = library.podcast
if (podcast == null || library.episodes.isEmpty()) {
// TODO: Empty state
return
}

item {
Text(
text = stringResource(id = R.string.latest_episodes),
Expand All @@ -58,12 +52,12 @@ fun LazyListScope.libraryItems(
}

items(
library.episodes,
key = { it.uri }
library,
key = { it.episode.uri }
) { item ->
EpisodeListItem(
episode = item,
podcast = podcast,
episode = item.episode,
podcast = item.podcast,
onClick = navigateToPlayer,
onQueueEpisode = onQueueEpisode,
modifier = Modifier.fillParentMaxWidth(),
Expand All @@ -76,12 +70,6 @@ fun LazyGridScope.libraryItems(
navigateToPlayer: (EpisodeInfo) -> Unit,
onQueueEpisode: (PlayerEpisode) -> Unit
) {
val podcast = library.podcast
if (podcast == null || library.episodes.isEmpty()) {
// TODO: Empty state
return
}

fullWidthItem {
Text(
text = stringResource(id = R.string.latest_episodes),
Expand All @@ -94,12 +82,12 @@ fun LazyGridScope.libraryItems(
}

items(
library.episodes,
key = { it.uri }
library,
key = { it.episode.uri }
) { item ->
EpisodeListItem(
episode = item,
podcast = podcast,
episode = item.episode,
podcast = item.podcast,
onClick = navigateToPlayer,
onQueueEpisode = onQueueEpisode,
modifier = Modifier.fillMaxWidth()
Expand Down
Expand Up @@ -17,6 +17,5 @@
package com.example.jetcaster.core.model

data class LibraryInfo(
val podcast: PodcastInfo? = null,
val episodes: List<EpisodeInfo> = emptyList()
)
val episodes: List<PodcastToEpisodeInfo> = emptyList()
) : List<PodcastToEpisodeInfo> by episodes
Expand Up @@ -21,10 +21,5 @@ package com.example.jetcaster.core.model
*/
data class PodcastCategoryFilterResult(
val topPodcasts: List<PodcastInfo> = emptyList(),
val episodes: List<PodcastCategoryEpisode> = emptyList()
)

data class PodcastCategoryEpisode(
val episode: EpisodeInfo,
val podcast: PodcastInfo,
val episodes: List<PodcastToEpisodeInfo> = emptyList()
)
@@ -0,0 +1,6 @@
package com.example.jetcaster.core.model

data class PodcastToEpisodeInfo(
val episode: EpisodeInfo,
val podcast: PodcastInfo,
)
Expand Up @@ -20,7 +20,7 @@ import androidx.room.Embedded
import androidx.room.Ignore
import androidx.room.Relation
import com.example.jetcaster.core.model.PlayerEpisode
import com.example.jetcaster.core.model.PodcastCategoryEpisode
import com.example.jetcaster.core.model.PodcastToEpisodeInfo
import java.util.Objects

class EpisodeToPodcast {
Expand Down Expand Up @@ -62,8 +62,8 @@ fun EpisodeToPodcast.toPlayerEpisode(): PlayerEpisode =
podcastImageUrl = podcast.imageUrl ?: "",
)

fun EpisodeToPodcast.asPodcastCategoryEpisode(): PodcastCategoryEpisode =
PodcastCategoryEpisode(
fun EpisodeToPodcast.asPodcastToEpisodeInfo(): PodcastToEpisodeInfo =
PodcastToEpisodeInfo(
episode = episode.asExternalModel(),
podcast = podcast.asExternalModel(),
)
Expand Up @@ -18,7 +18,7 @@ package com.example.jetcaster.core.data.domain

import com.example.jetcaster.core.data.database.model.Category
import com.example.jetcaster.core.data.database.model.asExternalModel
import com.example.jetcaster.core.data.database.model.asPodcastCategoryEpisode
import com.example.jetcaster.core.data.database.model.asPodcastToEpisodeInfo
import com.example.jetcaster.core.data.repository.CategoryStore
import com.example.jetcaster.core.model.CategoryInfo
import com.example.jetcaster.core.model.PodcastCategoryFilterResult
Expand Down Expand Up @@ -52,7 +52,7 @@ class PodcastCategoryFilterUseCase @Inject constructor(
return combine(recentPodcastsFlow, episodesFlow) { topPodcasts, episodes ->
PodcastCategoryFilterResult(
topPodcasts = topPodcasts.map { it.asExternalModel() },
episodes = episodes.map { it.asPodcastCategoryEpisode() }
episodes = episodes.map { it.asPodcastToEpisodeInfo() }
)
}
}
Expand Down
Expand Up @@ -22,7 +22,7 @@ import com.example.jetcaster.core.data.database.model.EpisodeToPodcast
import com.example.jetcaster.core.data.database.model.Podcast
import com.example.jetcaster.core.data.database.model.PodcastWithExtraInfo
import com.example.jetcaster.core.data.database.model.asExternalModel
import com.example.jetcaster.core.data.database.model.asPodcastCategoryEpisode
import com.example.jetcaster.core.data.database.model.asPodcastToEpisodeInfo
import com.example.jetcaster.core.data.repository.TestCategoryStore
import java.time.OffsetDateTime
import kotlinx.coroutines.flow.first
Expand Down Expand Up @@ -109,7 +109,7 @@ class PodcastCategoryFilterUseCaseTest {
result.topPodcasts
)
assertEquals(
testEpisodeToPodcast.map { it.asPodcastCategoryEpisode() },
testEpisodeToPodcast.map { it.asPodcastToEpisodeInfo() },
result.episodes
)
}
Expand Down

0 comments on commit dc4fcef

Please sign in to comment.