From ea5d4c25104dd614352426807bd1cbd474a6747f Mon Sep 17 00:00:00 2001 From: MSOB7YY Date: Sat, 2 Dec 2023 04:18:43 +0200 Subject: [PATCH] chore: assign downloads progresses on startup --- .../controller/youtube_controller.dart | 24 +++++++++++++++++-- .../widgets/yt_download_task_item_card.dart | 18 +++++++++++--- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/lib/youtube/controller/youtube_controller.dart b/lib/youtube/controller/youtube_controller.dart index 933fcae3..4c6899b1 100644 --- a/lib/youtube/controller/youtube_controller.dart +++ b/lib/youtube/controller/youtube_controller.dart @@ -604,9 +604,29 @@ class YoutubeController { } for (final v in res.entries) { final ytitem = YoutubeItemDownloadConfig.fromJson(v.value as Map); - final file = File("${AppDirs.YOUTUBE_DOWNLOADS}$groupName/${ytitem.filename}"); + final saveDirPath = "${AppDirs.YOUTUBE_DOWNLOADS}$groupName"; + final file = File("$saveDirPath/${ytitem.filename}"); + final fileExists = file.existsSync(); youtubeDownloadTasksMap[groupName]![v.key] = ytitem; - downloadedFilesMap[groupName]![v.key] = file.existsSync() ? file : null; + downloadedFilesMap[groupName]![v.key] = fileExists ? file : null; + if (!fileExists) { + final aFile = File("$saveDirPath/.tempa_${ytitem.filename}"); + final vFile = File("$saveDirPath/.tempv_${ytitem.filename}"); + if (aFile.existsSync()) { + downloadsAudioProgressMap[ytitem.id] ??= {}.obs; + downloadsAudioProgressMap[ytitem.id]![ytitem.filename] = DownloadProgress( + progress: aFile.fileSizeSync() ?? 0, + totalProgress: 0, + ); + } + if (vFile.existsSync()) { + downloadsVideoProgressMap[ytitem.id] ??= {}.obs; + downloadsVideoProgressMap[ytitem.id]![ytitem.filename] = DownloadProgress( + progress: vFile.fileSizeSync() ?? 0, + totalProgress: 0, + ); + } + } } } } diff --git a/lib/youtube/widgets/yt_download_task_item_card.dart b/lib/youtube/widgets/yt_download_task_item_card.dart index dd5aaedc..634078ed 100644 --- a/lib/youtube/widgets/yt_download_task_item_card.dart +++ b/lib/youtube/widgets/yt_download_task_item_card.dart @@ -500,10 +500,22 @@ class YTDownloadTaskItemCard extends StatelessWidget { final cp = videoP?.progress ?? audioP?.progress ?? 0; final ctp = videoP?.totalProgress ?? audioP?.totalProgress ?? 0; final speedText = speedB == null ? '' : ' (${speedB.fileSizeFormatted}/s)'; - final downloadInfoText = " • ${cp.fileSizeFormatted}/${ctp.fileSizeFormatted}$speedText"; + final downloadInfoText = "${cp.fileSizeFormatted}/${ctp.fileSizeFormatted}$speedText"; final canDisplayPercentage = audioPerc != null || videoPerc != null; final fileExists = YoutubeController.inst.downloadedFilesMap[groupName]?[item.filename] != null; + + double finalPercentage = 0.0; + if (fileExists) { + finalPercentage = 1.0; + } else { + if (audioPerc != null && !audioPerc.isNaN) { + finalPercentage = audioPerc; + } else if (videoPerc != null && !videoPerc.isNaN) { + finalPercentage = videoPerc; + } + } + final percentageText = finalPercentage.isInfinite ? '' : "${(finalPercentage * 100).toStringAsFixed(0)}%"; return Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ @@ -540,7 +552,7 @@ class YTDownloadTaskItemCard extends StatelessWidget { color: CurrentColor.inst.color.withOpacity(0.6), borderRadius: BorderRadius.circular(6.0.multipliedRadius), ), - width: (fileExists ? 1.0 : (audioPerc ?? videoPerc ?? 0.0)) * constraints.maxWidth, + width: finalPercentage * constraints.maxWidth, ), Container( alignment: Alignment.centerLeft, @@ -561,7 +573,7 @@ class YTDownloadTaskItemCard extends StatelessWidget { if (canDisplayPercentage) ...[ const SizedBox(height: 4.0), Text( - "${audioPerc != null ? "${(audioPerc * 100).toStringAsFixed(0)}%" : videoPerc != null ? "${(videoPerc * 100).toStringAsFixed(0)}%" : ''}$downloadInfoText", + [percentageText, downloadInfoText].joinText(), style: context.textTheme.displaySmall?.copyWith(fontSize: 11.0.multipliedFontScale), ), ],