Skip to content

Commit d560ac3

Browse files
dev: refactor downloadFormatDialog
1 parent 3221263 commit d560ac3

1 file changed

Lines changed: 32 additions & 44 deletions

File tree

android/src/main/kotlin/project/pipepipe/app/ui/component/DownloadFormatDialog.kt

Lines changed: 32 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ fun DownloadFormatDialog(
7676

7777
// State for duplicate download confirmation
7878
var showDuplicateDialog by remember { mutableStateOf(false) }
79-
var duplicateDownloadAction by remember { mutableStateOf<(() -> Unit)?>(null) }
8079

8180
// Permission launcher for Android 8-9 (API 26-28)
8281
val permissionLauncher = rememberLauncherForActivityResult(
@@ -176,6 +175,27 @@ fun DownloadFormatDialog(
176175
}
177176
}
178177

178+
// Perform download with selected format
179+
val performDownload: (Format, DownloadType) -> Unit = { format, type ->
180+
val finalFormatId = if (type == DownloadType.VIDEO && format.isVideoOnly) {
181+
"${format.id}+bestaudio"
182+
} else {
183+
format.id
184+
}
185+
GlobalScope.launch(Dispatchers.IO) {
186+
DownloadManagerHolder.instance.addDownload(
187+
url = streamInfo.url,
188+
title = streamInfo.name ?: "Unknown",
189+
imageUrl = streamInfo.thumbnailUrl,
190+
duration = streamInfo.duration?.toInt() ?: 0,
191+
downloadType = type,
192+
quality = format.displayLabel,
193+
codec = FormatHelper.parseCodecName(format.codec),
194+
formatId = finalFormatId
195+
)
196+
}
197+
}
198+
179199
AlertDialog(
180200
onDismissRequest = onDismiss,
181201
title = { Text(stringResource(MR.strings.download)) },
@@ -379,13 +399,6 @@ fun DownloadFormatDialog(
379399
// Define the download action
380400
val downloadAction: () -> Unit = {
381401
GlobalScope.launch(Dispatchers.IO) {
382-
// For video-only formats, append +bestaudio to ensure audio is merged
383-
val finalFormatId = if (selectedType == DownloadType.VIDEO && format.isVideoOnly) {
384-
"${format.id}+bestaudio"
385-
} else {
386-
format.id
387-
}
388-
389402
// Check for duplicate downloads (same url and download type)
390403
val existingDownload = DatabaseOperations.findDownloadByUrlAndType(
391404
url = streamInfo.url,
@@ -395,34 +408,11 @@ fun DownloadFormatDialog(
395408
if (existingDownload != null) {
396409
// Show duplicate confirmation dialog on main thread
397410
withContext(Dispatchers.Main) {
398-
duplicateDownloadAction = {
399-
GlobalScope.launch(Dispatchers.IO) {
400-
DownloadManagerHolder.instance.addDownload(
401-
url = streamInfo.url,
402-
title = streamInfo.name ?: "Unknown",
403-
imageUrl = streamInfo.thumbnailUrl,
404-
duration = streamInfo.duration?.toInt() ?: 0,
405-
downloadType = selectedType,
406-
quality = format.displayLabel,
407-
codec = FormatHelper.parseCodecName(format.codec),
408-
formatId = finalFormatId
409-
)
410-
}
411-
}
412411
showDuplicateDialog = true
413412
}
414413
} else {
415414
// No duplicate, proceed with download
416-
DownloadManagerHolder.instance.addDownload(
417-
url = streamInfo.url,
418-
title = streamInfo.name ?: "Unknown",
419-
imageUrl = streamInfo.thumbnailUrl,
420-
duration = streamInfo.duration?.toInt() ?: 0,
421-
downloadType = selectedType,
422-
quality = format.displayLabel,
423-
codec = FormatHelper.parseCodecName(format.codec),
424-
formatId = finalFormatId
425-
)
415+
performDownload(format, selectedType)
426416
// Close dialog on main thread
427417
withContext(Dispatchers.Main) {
428418
onDismiss()
@@ -484,34 +474,32 @@ fun DownloadFormatDialog(
484474

485475
// Duplicate download confirmation dialog
486476
if (showDuplicateDialog) {
477+
val selectedFormat = when (selectedType) {
478+
DownloadType.VIDEO -> selectedVideoFormat
479+
DownloadType.AUDIO -> selectedAudioFormat
480+
}
481+
487482
AlertDialog(
488-
onDismissRequest = {
489-
showDuplicateDialog = false
490-
duplicateDownloadAction = null
491-
},
483+
onDismissRequest = { showDuplicateDialog = false },
492484
title = { Text(stringResource(MR.strings.download_duplicate_title)) },
493485
text = {
494486
Text(stringResource(MR.strings.download_duplicate_message))
495487
},
496488
confirmButton = {
497489
TextButton(
498490
onClick = {
499-
duplicateDownloadAction?.invoke()
491+
selectedFormat?.let { format ->
492+
performDownload(format, selectedType)
493+
}
500494
showDuplicateDialog = false
501-
duplicateDownloadAction = null
502495
onDismiss()
503496
}
504497
) {
505498
Text(stringResource(MR.strings.download_duplicate_replace))
506499
}
507500
},
508501
dismissButton = {
509-
TextButton(
510-
onClick = {
511-
showDuplicateDialog = false
512-
duplicateDownloadAction = null
513-
}
514-
) {
502+
TextButton(onClick = { showDuplicateDialog = false }) {
515503
Text(stringResource(MR.strings.cancel))
516504
}
517505
}

0 commit comments

Comments
 (0)