Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement downloaded section on Feed #616

Merged
merged 7 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ internal class ContactChatNavigatorImpl @Inject constructor(
}

override suspend fun toPodcastPlayer(chatId: ChatId, feedId: FeedId, feedUrl: FeedUrl) {
detailDriver.submitNavigationRequest(ToPodcastPlayerScreen(chatId, feedId, feedUrl, true))
detailDriver.submitNavigationRequest(ToPodcastPlayerScreen(chatId, feedId, feedUrl, fromFeed = false, fromDownloaded = false))
}

override suspend fun toChatContact(chatId: ChatId?, contactId: ContactId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,13 @@ internal class DashboardNavigatorImpl @Inject constructor(
)
}

override suspend fun toPodcastPlayerScreen(chatId: ChatId, feedId: FeedId, feedUrl: FeedUrl) {
detailDriver.submitNavigationRequest(ToPodcastPlayerScreen(chatId, feedId, feedUrl, true))
override suspend fun toPodcastPlayerScreen(
chatId: ChatId,
feedId: FeedId,
feedUrl: FeedUrl,
fromDownloadedSection: Boolean
) {
detailDriver.submitNavigationRequest(ToPodcastPlayerScreen(chatId, feedId, feedUrl, true, fromDownloadedSection))
}

override suspend fun toCommonPlayerScreen(podcastId: FeedId, episodeId: FeedId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,6 @@ internal class GroupChatNavigatorImpl @Inject constructor(
}

override suspend fun toPodcastPlayer(chatId: ChatId, feedId: FeedId, feedUrl: FeedUrl) {
detailDriver.submitNavigationRequest(ToPodcastPlayerScreen(chatId, feedId, feedUrl, true))
detailDriver.submitNavigationRequest(ToPodcastPlayerScreen(chatId, feedId, feedUrl, fromFeed = false, fromDownloaded = false))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,13 @@ internal class TribeChatNavigatorImpl @Inject constructor(
detailDriver.submitNavigationRequest(ToPaymentReceiveDetail(contactId, chatId))
}

override suspend fun toPodcastPlayerScreen(chatId: ChatId, feedId: FeedId, feedUrl: FeedUrl) {
detailDriver.submitNavigationRequest(ToPodcastPlayerScreen(chatId, feedId, feedUrl, false))
override suspend fun toPodcastPlayerScreen(
chatId: ChatId,
feedId: FeedId,
feedUrl: FeedUrl,
fromDownloadedSection: Boolean
) {
detailDriver.submitNavigationRequest(ToPodcastPlayerScreen(chatId, feedId, feedUrl, fromFeed = false, fromDownloaded = false))
}

override suspend fun toTribeDetailScreen(chatId: ChatId) {
Expand Down Expand Up @@ -139,6 +144,6 @@ internal class TribeChatNavigatorImpl @Inject constructor(
}

override suspend fun toPodcastPlayer(chatId: ChatId, feedId: FeedId, feedUrl: FeedUrl) {
detailDriver.submitNavigationRequest(ToPodcastPlayerScreen(chatId, feedId, feedUrl, true))
detailDriver.submitNavigationRequest(ToPodcastPlayerScreen(chatId, feedId, feedUrl, fromFeed = false, fromDownloaded = false))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,22 @@ data class Podcast(
return episodesList
}

fun getDownloadedEpisodesListCopy(): ArrayList<PodcastEpisode> {
var episodesList = ArrayList<PodcastEpisode>()

for (episode in this.episodes) {
if (episode.downloaded) {
val episodeCopy = episode.copy()
episodeCopy.playing = episode.playing
episodeCopy.contentEpisodeStatus = episode.contentEpisodeStatus
episodeCopy.played = episode.played

episodesList.add(episodeCopy)
}
}
return episodesList
}

fun setCurrentEpisodeWith(episodeId: String) {
this.playingEpisode?.playing = false

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4131,9 +4131,10 @@ abstract class SphinxRepository(
.asFlow()
.mapToList(io)
.map { listFeedItemDbo ->
listFeedItemDbo.map {
feedItemDboPresenterMapper.mapFrom(it)
}
mapFeedItemDboList(
listFeedItemDbo,
coreDB.getSphinxDatabaseQueries()
)
}
.distinctUntilChanged()
)
Expand Down Expand Up @@ -4503,6 +4504,36 @@ abstract class SphinxRepository(
return feedItem
}

private suspend fun mapFeedItemDboList(
listFeedItemDbo: List<FeedItemDbo>,
queries: SphinxDatabaseQueries
): List<FeedItem> {

val feedsMap: MutableMap<FeedId, Feed> = mutableMapOf()
val feedIds = listFeedItemDbo.map { it.feed_id }.distinct()

queries.feedGetByIds(feedIds)
.executeAsList()
.let { response ->
response.forEach { dbo ->
val feed = feedDboPresenterMapper.mapFrom(dbo)
feedsMap[dbo.id] = feed
}
}

val feedItems = listFeedItemDbo.map {
feedItemDboPresenterMapper.mapFrom(it).apply {
it.feed_id
}
}

feedItems.forEach { item ->
item.feed = feedsMap[item.feedId]
}

return feedItems
}

private val podcastDboPresenterMapper: FeedDboPodcastPresenterMapper by lazy {
FeedDboPodcastPresenterMapper(dispatchers)
}
Expand Down Expand Up @@ -4729,36 +4760,6 @@ abstract class SphinxRepository(
}
}

private suspend fun mapFeedItemDboList(
listFeedItemDbo: List<FeedItemDbo>,
queries: SphinxDatabaseQueries
): List<FeedItem> {
val feedsMap: MutableMap<FeedId, ArrayList<Feed>> =
LinkedHashMap(listFeedItemDbo.size)

for (dbo in listFeedItemDbo) {
feedsMap[dbo.feed_id] = ArrayList(0)
}

feedsMap.keys.chunked(500).forEach { chunkedFeedIds ->
queries.feedGetAllByIds(chunkedFeedIds)
.executeAsList()
.let { response ->
response.forEach { dbo ->
feedsMap[dbo.id]?.add(
feedDboPresenterMapper.mapFrom(dbo)
)
}
}
}

return listFeedItemDbo.map {
feedItemDboPresenterMapper.mapFrom(it).apply {
it.feed_id
}
}
}

override val networkRefreshFeedContent: Flow<LoadResponse<RestoreProgress, ResponseError>> by lazy {
flow {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ abstract class PodcastPlayerNavigator(
chatId: ChatId,
feedId: FeedId,
feedUrl: FeedUrl,
fromDownloadedSection: Boolean = false
) {
navigationDriver.submitNavigationRequest(ToPodcastPlayerScreen(chatId, feedId, feedUrl, false))
navigationDriver.submitNavigationRequest(ToPodcastPlayerScreen(chatId, feedId, feedUrl, false, fromDownloadedSection))
}

@JvmSynthetic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class ToPodcastPlayerScreen(
private val feedId: FeedId,
private val feedUrl: FeedUrl,
private val fromFeed: Boolean,
private val fromDownloaded: Boolean,
private val options: NavOptions = DetailNavOptions.defaultBuilt
) : NavigationRequest<NavController>() {

Expand All @@ -25,7 +26,8 @@ class ToPodcastPlayerScreen(
chatId.value,
feedId.value,
feedUrl.value,
fromFeed
fromFeed,
fromDownloaded
).build().toBundle(),
options
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ internal inline val PodcastPlayerFragmentArgs.feedId: FeedId
internal inline val PodcastPlayerFragmentArgs.fromFeed: Boolean
get() = argFromFeed

internal inline val PodcastPlayerFragmentArgs.fromDownloaded: Boolean
get() = argFromDownloaded

@HiltViewModel
internal class PodcastPlayerViewModel @Inject constructor(
dispatchers: CoroutineDispatchers,
Expand Down Expand Up @@ -205,6 +208,7 @@ internal class PodcastPlayerViewModel @Inject constructor(
viewStateContainer.updateViewState(
PodcastPlayerViewState.MediaStateUpdate(
podcast,
args.fromDownloaded,
serviceState
)
)
Expand All @@ -215,6 +219,7 @@ internal class PodcastPlayerViewModel @Inject constructor(
viewStateContainer.updateViewState(
PodcastPlayerViewState.MediaStateUpdate(
podcast,
args.fromDownloaded,
serviceState
)
)
Expand All @@ -227,6 +232,7 @@ internal class PodcastPlayerViewModel @Inject constructor(
viewStateContainer.updateViewState(
PodcastPlayerViewState.MediaStateUpdate(
podcast,
args.fromDownloaded,
serviceState
)
)
Expand Down Expand Up @@ -357,7 +363,10 @@ internal class PodcastPlayerViewModel @Inject constructor(
podcast.applyPlayingContentState(playingContent)

viewStateContainer.updateViewState(
PodcastPlayerViewState.PodcastLoaded(podcast)
PodcastPlayerViewState.PodcastLoaded(
podcast,
args.fromDownloaded
)
)

val contentFeedStatus = podcast.getUpdatedContentFeedStatus()
Expand Down Expand Up @@ -453,7 +462,8 @@ internal class PodcastPlayerViewModel @Inject constructor(

viewStateContainer.updateViewState(
PodcastPlayerViewState.EpisodePlayed(
podcast
podcast,
args.fromDownloaded
)
)

Expand Down Expand Up @@ -861,23 +871,33 @@ internal class PodcastPlayerViewModel @Inject constructor(
viewState.podcast.forceUpdate = !viewState.podcast.forceUpdate

viewStateContainer.updateViewState(
PodcastPlayerViewState.PodcastLoaded(viewState.podcast)
PodcastPlayerViewState.PodcastLoaded(
viewState.podcast,
args.fromDownloaded
)
)
Log.d("isAudioComingOut", "UpdateState! PodcastLoaded")
}
is PodcastPlayerViewState.EpisodePlayed -> {
viewState.podcast.forceUpdate = !viewState.podcast.forceUpdate

viewStateContainer.updateViewState(
PodcastPlayerViewState.EpisodePlayed(viewState.podcast)
PodcastPlayerViewState.EpisodePlayed(
viewState.podcast,
args.fromDownloaded
)
)
Log.d("isAudioComingOut", "UpdateState! EpisodePlayed ")
}
is PodcastPlayerViewState.MediaStateUpdate -> {
viewState.podcast.forceUpdate = !viewState.podcast.forceUpdate

viewStateContainer.updateViewState(
PodcastPlayerViewState.MediaStateUpdate(viewState.podcast, viewState.state)
PodcastPlayerViewState.MediaStateUpdate(
viewState.podcast,
args.fromDownloaded,
viewState.state
)
)
Log.d("isAudioComingOut", "UpdateState! MediaStateUpdate")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,19 @@ internal class PodcastEpisodesListAdapter(
var episodes = ArrayList<PodcastEpisode>()

if (viewState is PodcastPlayerViewState.PodcastLoaded) {
episodes = viewState.podcast.getEpisodesListCopy()
episodes = if (viewState.downloadedOnly) {
viewState.podcast.getDownloadedEpisodesListCopy()
} else {
viewState.podcast.getEpisodesListCopy()
}
}

if (viewState is PodcastPlayerViewState.EpisodePlayed) {
episodes = viewState.podcast.getEpisodesListCopy()
episodes = if (viewState.downloadedOnly) {
viewState.podcast.getDownloadedEpisodesListCopy()
} else {
viewState.podcast.getEpisodesListCopy()
}
}

if (viewState is PodcastPlayerViewState.MediaStateUpdate) {
Expand All @@ -125,7 +133,11 @@ internal class PodcastEpisodesListAdapter(
viewState.state is MediaPlayerServiceState.ServiceActive.MediaState.Ended ||
viewState.state is MediaPlayerServiceState.ServiceActive.MediaState.Playing
) {
episodes = viewState.podcast.getEpisodesListCopy()
episodes = if (viewState.downloadedOnly) {
viewState.podcast.getDownloadedEpisodesListCopy()
} else {
viewState.podcast.getEpisodesListCopy()
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,21 @@ internal sealed class PodcastPlayerViewState: ViewState<PodcastPlayerViewState>(

class PodcastLoaded(
val podcast: Podcast,
val downloadedOnly: Boolean
): PodcastPlayerViewState()

class LoadingEpisode(
val episode: PodcastEpisode
val episode: PodcastEpisode,
): PodcastPlayerViewState()

class EpisodePlayed(
val podcast: Podcast
val podcast: Podcast,
val downloadedOnly: Boolean
): PodcastPlayerViewState()

class MediaStateUpdate(
val podcast: Podcast,
val downloadedOnly: Boolean,
val state: MediaPlayerServiceState.ServiceActive.MediaState
): PodcastPlayerViewState()
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/body">
android:background="@color/headerBG">

<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/layout_constraint_episode_list_footer"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
<argument
android:name="argFromFeed"
app:argType="boolean" />
<argument
android:name="argFromDownloaded"
app:argType="boolean" />
</fragment>

</navigation>
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ abstract class TribeChatNavigator(
navigationDriver: BaseNavigationDriver<NavController>
): ChatNavigator(navigationDriver)
{
abstract suspend fun toPodcastPlayerScreen(chatId: ChatId, feedId: FeedId, feedUrl: FeedUrl)
abstract suspend fun toPodcastPlayerScreen(
chatId: ChatId,
feedId: FeedId,
feedUrl: FeedUrl,
fromDownloadedSection: Boolean = false
)

abstract suspend fun toTribeDetailScreen(chatId: ChatId)

abstract suspend fun toShareTribeScreen(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ abstract class DashboardNavigator(

abstract suspend fun toNewsletterDetail(chatId: ChatId, feedUrl: FeedUrl)

abstract suspend fun toPodcastPlayerScreen(chatId: ChatId, feedId: FeedId, feedUrl: FeedUrl)
abstract suspend fun toPodcastPlayerScreen(
chatId: ChatId,
feedId: FeedId,
feedUrl: FeedUrl,
fromDownloadedSection: Boolean = false
)

abstract suspend fun toCommonPlayerScreen(
podcastId: FeedId,
Expand Down
Loading