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

Simplify toDownloadItems() method #4097

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
24 changes: 9 additions & 15 deletions app/src/main/java/com/github/libretube/api/obj/Streams.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.github.libretube.api.obj
import com.github.libretube.db.obj.DownloadItem
import com.github.libretube.enums.FileType
import com.github.libretube.helpers.ProxyHelper
import com.github.libretube.parcelable.DownloadData
import java.nio.file.Paths
import kotlinx.datetime.LocalDate
import kotlinx.serialization.Serializable
Expand Down Expand Up @@ -35,40 +36,33 @@ data class Streams(
val uploaderSubscriberCount: Long = 0,
val previewFrames: List<PreviewFrames> = emptyList()
) {
fun toDownloadItems(
videoId: String,
fileName: String,
videoFormat: String?,
videoQuality: String?,
audioFormat: String?,
audioQuality: String?,
subtitleCode: String?
): List<DownloadItem> {
fun toDownloadItems(downloadData: DownloadData): List<DownloadItem> {
val (id, name, videoFormat, videoQuality, audioFormat, audioQuality, subCode) = downloadData
Bnyro marked this conversation as resolved.
Show resolved Hide resolved
val items = mutableListOf<DownloadItem>()

if (!videoQuality.isNullOrEmpty() && !videoFormat.isNullOrEmpty()) {
val stream = videoStreams.find {
it.quality == videoQuality && it.format == videoFormat
}
stream?.toDownloadItem(FileType.VIDEO, videoId, fileName)?.let { items.add(it) }
stream?.toDownloadItem(FileType.VIDEO, id, name)?.let { items.add(it) }
}

if (!audioQuality.isNullOrEmpty() && !audioFormat.isNullOrEmpty()) {
val stream = audioStreams.find {
it.quality == audioQuality && it.format == audioFormat
}
stream?.toDownloadItem(FileType.AUDIO, videoId, fileName)?.let { items.add(it) }
stream?.toDownloadItem(FileType.AUDIO, id, name)?.let { items.add(it) }
}

if (!subtitleCode.isNullOrEmpty()) {
if (!subCode.isNullOrEmpty()) {
items.add(
DownloadItem(
type = FileType.SUBTITLE,
videoId = videoId,
fileName = "${fileName}_$subtitleCode.srt",
videoId = id,
fileName = "${name}_$subCode.srt",
path = Paths.get(""),
url = subtitles.find {
it.code == subtitleCode
it.code == subCode
}?.url?.let { ProxyHelper.unwrapIfEnabled(it) }
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import kotlinx.parcelize.Parcelize
@Parcelize
data class DownloadData(
val videoId: String,
val fileName: String?,
val fileName: String,
val videoFormat: String?,
val videoQuality: String?,
val audioFormat: String?,
Expand Down
18 changes: 5 additions & 13 deletions app/src/main/java/com/github/libretube/services/DownloadService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ class DownloadService : LifecycleService() {
ACTION_DOWNLOAD_PAUSE -> pause(intent.getIntExtra("id", -1))
}

val (videoId, name, videoFormat, videoQuality, audioFormat, audioQuality, subtitleCode) =
intent?.parcelableExtra<DownloadData>(IntentData.downloadData)
?: return START_NOT_STICKY
val fileName = name ?: videoId
val downloadData = intent?.parcelableExtra<DownloadData>(IntentData.downloadData)
?: return START_NOT_STICKY
val (videoId, name) = downloadData
val fileName = name.ifEmpty { videoId }

lifecycleScope.launch(coroutineContext) {
try {
Expand All @@ -120,15 +120,7 @@ class DownloadService : LifecycleService() {
thumbnailTargetPath
)

val downloadItems = streams.toDownloadItems(
videoId,
fileName,
videoFormat,
videoQuality,
audioFormat,
audioQuality,
subtitleCode
)
val downloadItems = streams.toDownloadItems(downloadData.copy(fileName = fileName))
downloadItems.forEach { start(it) }
} catch (e: Exception) {
return@launch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ class DownloadDialog(

val downloadData = DownloadData(
videoId = videoId,
fileName = binding.fileName.text?.toString(),
fileName = binding.fileName.text?.toString().orEmpty(),
videoFormat = videoStream?.format,
videoQuality = videoStream?.quality,
audioFormat = audioStream?.format,
Expand Down