diff --git a/app/src/main/java/com/github/libretube/api/obj/Streams.kt b/app/src/main/java/com/github/libretube/api/obj/Streams.kt index 20a292a70..f701cf0ef 100644 --- a/app/src/main/java/com/github/libretube/api/obj/Streams.kt +++ b/app/src/main/java/com/github/libretube/api/obj/Streams.kt @@ -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 @@ -35,40 +36,33 @@ data class Streams( val uploaderSubscriberCount: Long = 0, val previewFrames: List = emptyList() ) { - fun toDownloadItems( - videoId: String, - fileName: String, - videoFormat: String?, - videoQuality: String?, - audioFormat: String?, - audioQuality: String?, - subtitleCode: String? - ): List { + fun toDownloadItems(downloadData: DownloadData): List { + val (id, name, videoFormat, videoQuality, audioFormat, audioQuality, subCode) = downloadData val items = mutableListOf() 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) } ) ) diff --git a/app/src/main/java/com/github/libretube/parcelable/DownloadData.kt b/app/src/main/java/com/github/libretube/parcelable/DownloadData.kt index 298157cb4..004564d75 100644 --- a/app/src/main/java/com/github/libretube/parcelable/DownloadData.kt +++ b/app/src/main/java/com/github/libretube/parcelable/DownloadData.kt @@ -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?, diff --git a/app/src/main/java/com/github/libretube/services/DownloadService.kt b/app/src/main/java/com/github/libretube/services/DownloadService.kt index b64928b78..968d6c6e1 100644 --- a/app/src/main/java/com/github/libretube/services/DownloadService.kt +++ b/app/src/main/java/com/github/libretube/services/DownloadService.kt @@ -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(IntentData.downloadData) - ?: return START_NOT_STICKY - val fileName = name ?: videoId + val downloadData = intent?.parcelableExtra(IntentData.downloadData) + ?: return START_NOT_STICKY + val (videoId, name) = downloadData + val fileName = name.ifEmpty { videoId } lifecycleScope.launch(coroutineContext) { try { @@ -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 diff --git a/app/src/main/java/com/github/libretube/ui/dialogs/DownloadDialog.kt b/app/src/main/java/com/github/libretube/ui/dialogs/DownloadDialog.kt index 81d2e50d4..0b8405f85 100644 --- a/app/src/main/java/com/github/libretube/ui/dialogs/DownloadDialog.kt +++ b/app/src/main/java/com/github/libretube/ui/dialogs/DownloadDialog.kt @@ -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,