Skip to content

Commit

Permalink
chore: display if video is dowloaded in miniplayer
Browse files Browse the repository at this point in the history
  • Loading branch information
MSOB7YY committed Nov 16, 2023
1 parent 9967af0 commit 2afc16e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
36 changes: 33 additions & 3 deletions lib/youtube/controller/youtube_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ class YoutubeController {
/// {groupName: {filename: YoutubeItemDownloadConfig}}
final youtubeDownloadTasksMap = <String, Map<String, YoutubeItemDownloadConfig>>{}.obs;

/// Used to keep track of existing downloaded files, more performant than real-time checking.
///
/// {groupName: {filename: File}}
final downloadedFilesMap = <String, Map<String, File?>>{}.obs;

/// Temporarely saves StreamInfoItem info for flawless experience while waiting for real info.
final _tempVideoInfosFromStreams = <String, StreamInfoItem>{}; // {id: StreamInfoItem()}

Expand Down Expand Up @@ -568,12 +573,33 @@ class YoutubeController {
final res = await f.readAsJson() as Map<String, dynamic>?;
if (res != null) {
youtubeDownloadTasksMap[groupName] ??= {};
downloadedFilesMap[groupName] ??= {};
for (final v in res.entries) {
youtubeDownloadTasksMap[groupName]![v.key] = YoutubeItemDownloadConfig.fromJson(v.value as Map<String, dynamic>);
final ytitem = YoutubeItemDownloadConfig.fromJson(v.value as Map<String, dynamic>);
final file = File("${AppDirs.YOUTUBE_DOWNLOADS}$groupName/${ytitem.filename}");
youtubeDownloadTasksMap[groupName]![v.key] = ytitem;
downloadedFilesMap[groupName]![v.key] = file.existsSync() ? file : null;
}
}
}
}
youtubeDownloadTasksMap.refresh();
downloadedFilesMap.refresh();
}

File? doesIDHasFileDownloaded(String id) {
for (final e in youtubeDownloadTasksMap.entries) {
for (final config in e.value.values) {
final groupName = e.key;
if (config.id == id) {
final file = downloadedFilesMap[groupName]?[config.filename];
if (file != null) {
return file;
}
}
}
}
return null;
}

void _matchIDsForItemConfig({
Expand Down Expand Up @@ -646,12 +672,13 @@ class YoutubeController {
}) async {
youtubeDownloadTasksMap[groupName] ??= {};
if (remove) {
final directory = groupName == '' ? Directory(AppDirs.YOUTUBE_DOWNLOADS) : Directory("${AppDirs.YOUTUBE_DOWNLOADS}$groupName");
final directory = Directory("${AppDirs.YOUTUBE_DOWNLOADS}$groupName");
await itemsConfig.loopFuture((c, _) async {
_downloadClientsMap[groupName]?[c.filename]?.close(force: true);
_downloadClientsMap[groupName]?.remove(c.filename);
youtubeDownloadTasksMap[groupName]?.remove(c.filename);
await File("$directory/${c.filename}").deleteIfExists();
downloadedFilesMap[groupName]?[c.filename] = null;
});
} else {
itemsConfig.loop((c, _) {
Expand All @@ -660,6 +687,7 @@ class YoutubeController {
}

youtubeDownloadTasksMap.refresh();
downloadedFilesMap.refresh();

await File("${AppDirs.YT_DOWNLOAD_TASKS}$groupName.json").writeAsJson(youtubeDownloadTasksMap[groupName]);
}
Expand All @@ -680,7 +708,7 @@ class YoutubeController {
}) async {
_updateDownloadTask(groupName: groupName, itemsConfig: itemsConfig);

final directory = groupName == '' ? Directory(AppDirs.YOUTUBE_DOWNLOADS) : Directory("${AppDirs.YOUTUBE_DOWNLOADS}$groupName");
final directory = Directory("${AppDirs.YOUTUBE_DOWNLOADS}$groupName");
await directory.create(recursive: true);
for (final config in itemsConfig) {
final videoID = config.id;
Expand Down Expand Up @@ -790,6 +818,8 @@ class YoutubeController {
downloadedFile?.path.removeTrackThenExtract();
}

downloadedFilesMap[groupName]?[config.filename] = downloadedFile;
downloadedFilesMap.refresh();
await onFileDownloaded?.call(downloadedFile);
}
}
Expand Down
8 changes: 5 additions & 3 deletions lib/youtube/youtube_miniplayer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import 'package:namida/core/extensions.dart';
import 'package:namida/core/icon_fonts/broken_icons.dart';
import 'package:namida/core/namida_converter_ext.dart';
import 'package:namida/core/translations/language.dart';
import 'package:namida/packages/dots_triangle.dart';
import 'package:namida/packages/mp.dart';
import 'package:namida/packages/three_arched_circle.dart';
import 'package:namida/ui/widgets/custom_widgets.dart';
Expand Down Expand Up @@ -113,6 +112,9 @@ class YoutubeMiniPlayer extends StatelessWidget {
},
);

YoutubeController.inst.downloadedFilesMap; // for refreshing.
final downloadedFileExists = YoutubeController.inst.doesIDHasFileDownloaded(currentId) != null;

return NamidaYTMiniplayer(
key: MiniPlayerController.inst.ytMiniplayerKey,
duration: const Duration(milliseconds: 1000),
Expand Down Expand Up @@ -541,9 +543,9 @@ class YoutubeMiniPlayer extends StatelessWidget {
? Broken.play_circle
: wasDownloading
? Broken.pause_circle
: false
: downloadedFileExists
? Broken.tick_circle
: Broken.import; // TODO: check if video already downloaded
: Broken.import;
return SmallYTActionButton(
titleWidget: videoPerc == null && audioPerc == null && isDownloading ? const LoadingIndicator() : null,
title: videoPerc ?? audioPerc ?? lang.DOWNLOAD,
Expand Down

0 comments on commit 2afc16e

Please sign in to comment.