Skip to content

Commit

Permalink
chore: assign downloads progresses on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
MSOB7YY committed Dec 2, 2023
1 parent dbff88d commit ea5d4c2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
24 changes: 22 additions & 2 deletions lib/youtube/controller/youtube_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -604,9 +604,29 @@ class YoutubeController {
}
for (final v in res.entries) {
final ytitem = YoutubeItemDownloadConfig.fromJson(v.value as Map<String, dynamic>);
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] ??= <String, DownloadProgress>{}.obs;
downloadsAudioProgressMap[ytitem.id]![ytitem.filename] = DownloadProgress(
progress: aFile.fileSizeSync() ?? 0,
totalProgress: 0,
);
}
if (vFile.existsSync()) {
downloadsVideoProgressMap[ytitem.id] ??= <String, DownloadProgress>{}.obs;
downloadsVideoProgressMap[ytitem.id]![ytitem.filename] = DownloadProgress(
progress: vFile.fileSizeSync() ?? 0,
totalProgress: 0,
);
}
}
}
}
}
Expand Down
18 changes: 15 additions & 3 deletions lib/youtube/widgets/yt_download_task_item_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down Expand Up @@ -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,
Expand All @@ -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),
),
],
Expand Down

0 comments on commit ea5d4c2

Please sign in to comment.