Skip to content

Commit

Permalink
Add support for Next Episode in downloads (#1228)
Browse files Browse the repository at this point in the history
  • Loading branch information
Luna712 authored Aug 5, 2024
1 parent 4d9a080 commit f6a65f3
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.lagradost.cloudstream3.ui.download

import android.content.DialogInterface
import android.net.Uri
import androidx.appcompat.app.AlertDialog
import com.google.android.material.snackbar.Snackbar
import com.lagradost.cloudstream3.AcraApplication.Companion.getKey
import com.lagradost.cloudstream3.AcraApplication.Companion.getKeys
import com.lagradost.cloudstream3.CommonActivity.activity
import com.lagradost.cloudstream3.R
import com.lagradost.cloudstream3.mvvm.logError
Expand All @@ -12,6 +14,7 @@ import com.lagradost.cloudstream3.ui.player.ExtractorUri
import com.lagradost.cloudstream3.ui.player.GeneratorPlayer
import com.lagradost.cloudstream3.utils.AppContextUtils.getNameFull
import com.lagradost.cloudstream3.utils.AppContextUtils.setDefaultFocus
import com.lagradost.cloudstream3.utils.DOWNLOAD_EPISODE_CACHE
import com.lagradost.cloudstream3.utils.DOWNLOAD_HEADER_CACHE
import com.lagradost.cloudstream3.utils.SnackbarHelper.showSnackbar
import com.lagradost.cloudstream3.utils.UIHelper.navigate
Expand Down Expand Up @@ -109,56 +112,69 @@ object DownloadButtonSetup {

DOWNLOAD_ACTION_PLAY_FILE -> {
activity?.let { act ->
val info =
VideoDownloadManager.getDownloadFileInfoAndUpdateSettings(
act,
click.data.id
) ?: return
val keyInfo = getKey<VideoDownloadManager.DownloadedFileInfo>(
VideoDownloadManager.KEY_DOWNLOAD_INFO,
click.data.id.toString()
) ?: return
val parent = getKey<VideoDownloadHelper.DownloadHeaderCached>(
DOWNLOAD_HEADER_CACHE,
click.data.parentId.toString()
) ?: return

val episodes = getKeys(DOWNLOAD_EPISODE_CACHE)
?.mapNotNull {
getKey<VideoDownloadHelper.DownloadEpisodeCached>(it)
}
?.filter { it.parentId == click.data.parentId }

val currentSeason = click.data.season ?: 0
val currentEpisode = click.data.episode

val items = mutableListOf<ExtractorUri>()

// Make sure we only get this episode and episodes after it,
// and that we can go to the next season if we need to.
val allRelevantEpisodes = episodes
?.sortedWith(
compareByDescending<VideoDownloadHelper.DownloadEpisodeCached> { it.id == click.data.id }
.thenBy { it.season ?: 0 }
.thenBy { it.episode }
)
?.filter {
if (it.season == null) return@filter true
val isCurrentOrLaterInSeason = it.season == currentSeason && (it.episode >= currentEpisode || it.id == click.data.id)
val isInFutureSeasons = it.season > currentSeason

isCurrentOrLaterInSeason || isInFutureSeasons
}

allRelevantEpisodes?.forEach {
val keyInfo = getKey<VideoDownloadManager.DownloadedFileInfo>(
VideoDownloadManager.KEY_DOWNLOAD_INFO,
it.id.toString()
) ?: return@forEach

items.add(
ExtractorUri(
// We just use a temporary placeholder for the URI,
// it will be updated in generateLinks().
// We just do this for performance since getting
// all paths at once can be quite expensive.
uri = Uri.EMPTY,
id = it.id,
parentId = it.parentId,
name = act.getString(R.string.downloaded_file),
season = it.season,
episode = it.episode,
headerName = parent.name,
tvType = parent.type,
basePath = keyInfo.basePath,
displayName = keyInfo.displayName,
relativePath = keyInfo.relativePath,
)
)
}

act.navigate(
R.id.global_to_navigation_player, GeneratorPlayer.newInstance(
DownloadFileGenerator(
listOf(
ExtractorUri(
uri = info.path,

id = click.data.id,
parentId = click.data.parentId,
name = act.getString(R.string.downloaded_file), // click.data.name ?: keyInfo.displayName
season = click.data.season,
episode = click.data.episode,
headerName = parent.name,
tvType = parent.type,

basePath = keyInfo.basePath,
displayName = keyInfo.displayName,
relativePath = keyInfo.relativePath,
)
)
)
DownloadFileGenerator(items)
)
// R.id.global_to_navigation_player, PlayerFragment.newInstance(
// UriData(
// info.path.toString(),
// keyInfo.basePath,
// keyInfo.relativePath,
// keyInfo.displayName,
// click.data.parentId,
// click.data.id,
// headerName ?: "null",
// if (click.data.episode <= 0) null else click.data.episode,
// click.data.season
// ),
// getViewPos(click.data.id)?.position ?: 0
// )
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.lagradost.cloudstream3.ui.player

import android.net.Uri
import com.lagradost.cloudstream3.AcraApplication.Companion.context
import com.lagradost.cloudstream3.CommonActivity.activity
import com.lagradost.cloudstream3.R
import com.lagradost.cloudstream3.ui.player.PlayerSubtitleHelper.Companion.toSubtitleMimeType
import com.lagradost.cloudstream3.utils.ExtractorLink
import com.lagradost.cloudstream3.utils.SubtitleUtils.cleanDisplayName
import com.lagradost.cloudstream3.utils.SubtitleUtils.isMatchingSubtitle
import com.lagradost.cloudstream3.utils.VideoDownloadManager.getDownloadFileInfoAndUpdateSettings
import com.lagradost.cloudstream3.utils.VideoDownloadManager.getFolder
import kotlin.math.max
import kotlin.math.min
Expand All @@ -15,6 +18,7 @@ class DownloadFileGenerator(
private var currentIndex: Int = 0
) : IGenerator {
override val hasCache = false
override val canSkipLoading = false

override fun hasNext(): Boolean {
return currentIndex < episodes.size - 1
Expand Down Expand Up @@ -59,7 +63,21 @@ class DownloadFileGenerator(
offset: Int
): Boolean {
val meta = episodes[currentIndex + offset]
callback(null to meta)

if (meta.uri == Uri.EMPTY) {
// We do this here so that we only load it when
// we actually need it as it can be more expensive.
val info = meta.id?.let { id ->
activity?.let { act ->
getDownloadFileInfoAndUpdateSettings(act, id)
}
}

if (info != null) {
val newMeta = meta.copy(uri = info.path)
callback(null to newMeta)
} else callback(null to meta)
} else callback(null to meta)

val ctx = context ?: return true
val relative = meta.relativePath ?: return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class ExtractorLinkGenerator(
private val subtitles: List<SubtitleData>,
) : IGenerator {
override val hasCache = false
override val canSkipLoading = true

override fun getCurrentId(): Int? {
return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1462,7 +1462,7 @@ class GeneratorPlayer : FullScreenPlayer() {

observe(viewModel.currentLinks) {
currentLinks = it
val turnVisible = it.isNotEmpty()
val turnVisible = it.isNotEmpty() && lastUsedGenerator?.canSkipLoading == true
val wasGone = binding?.overlayLoadingSkipButton?.isGone == true
binding?.overlayLoadingSkipButton?.isVisible = turnVisible

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ fun LoadType.toSet() : Set<ExtractorLinkType> {

interface IGenerator {
val hasCache: Boolean
val canSkipLoading: Boolean

fun hasNext(): Boolean
fun hasPrev(): Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class LinkGenerator(
private val isM3u8: Boolean? = null
) : IGenerator {
override val hasCache = false
override val canSkipLoading = true

override fun getCurrentId(): Int? {
return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class RepoLinkGenerator(
}

override val hasCache = true
override val canSkipLoading = true

override fun hasNext(): Boolean {
return currentIndex < episodes.size - 1
Expand Down

0 comments on commit f6a65f3

Please sign in to comment.