Skip to content

Commit

Permalink
chore: allow yt download notifications to be dissmissible when paused
Browse files Browse the repository at this point in the history
  • Loading branch information
MSOB7YY committed Jul 25, 2024
1 parent 9f48569 commit 0ad3722
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 28 deletions.
5 changes: 4 additions & 1 deletion lib/controller/notification_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class NotificationService {
required int progress,
required int total,
required DateTime displayTime,
required bool isRunning,
}) {
_createProgressNotification(
id: _youtubeDownloadID,
Expand All @@ -116,6 +117,7 @@ class NotificationService {
isInBytes: true,
tag: notificationID.filename,
displayTime: displayTime,
ongoing: isRunning,
);
}

Expand Down Expand Up @@ -236,6 +238,7 @@ class NotificationService {
String? imagePath,
String? tag,
required DateTime? displayTime,
bool ongoing = true,
}) {
final p = progress / maxProgress;
final sub = isInBytes ? '${progress.fileSizeFormatted} / ${maxProgress.fileSizeFormatted}' : '${progress.formatDecimal()} / ${maxProgress.formatDecimal()}';
Expand All @@ -259,7 +262,7 @@ class NotificationService {
priority: Priority.high,
onlyAlertOnce: true,
showProgress: true,
ongoing: true,
ongoing: ongoing,
visibility: NotificationVisibility.public,
styleInformation: BigTextStyleInformation(subtitle(sub)), // this gets displayed instead of subtitle
largeIcon: pic,
Expand Down
58 changes: 32 additions & 26 deletions lib/youtube/controller/youtube_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -180,43 +180,49 @@ class YoutubeController {
}

void _loopMapAndPostNotification({
required Map<DownloadTaskVideoId, RxMap<DownloadTaskFilename, DownloadProgress>> bigMap,
required Map<DownloadTaskVideoId, RxMap<DownloadTaskFilename, bool>> downloadingMap,
required int Function(DownloadTaskFilename key, int progress) speedInBytes,
required DateTime startTime,
required bool isAudio,
required String? Function(DownloadTaskVideoId videoId) titleCallback,
required File? Function(DownloadTaskVideoId videoId) imageCallback,
}) {
final downloadingText = isAudio ? "Audio" : "Video";
for (final bigEntry in bigMap.entries.toList()) {
for (final bigEntry in downloadingMap.entries.toList()) {
final map = bigEntry.value.value;
final videoId = bigEntry.key;
for (final entry in map.entries.toList()) {
final p = entry.value.progress;
final tp = entry.value.totalProgress;
final filename = entry.key;
final progressInfo = (isAudio ? downloadsAudioProgressMap : downloadsVideoProgressMap).value[videoId]?.value[filename];
if (progressInfo == null) continue;

final p = progressInfo.progress;
final tp = progressInfo.totalProgress;
final percentage = p / tp;
if (percentage.isNaN || percentage.isInfinite || percentage >= 1) {
map.remove(entry.key);
} else {
final filename = entry.key;
final title = titleCallback(videoId) ?? videoId;
final speedB = speedInBytes(filename, entry.value.progress);
if (currentSpeedsInByte.value[videoId] == null) {
currentSpeedsInByte.value[videoId] = <DownloadTaskFilename, int>{}.obs;
currentSpeedsInByte.refresh();
}
if (percentage >= 1 || percentage.isNaN || percentage.isInfinite) continue;

currentSpeedsInByte.value[videoId]![filename] = speedB;
NotificationService.inst.downloadYoutubeNotification(
notificationID: entry.key,
title: "Downloading $downloadingText: $title",
progress: p,
total: tp,
subtitle: (progressText) => "$progressText (${speedB.fileSizeFormatted}/s)",
imagePath: imageCallback(videoId)?.path,
displayTime: startTime,
);
final isRunning = entry.value;
if (isRunning == false) downloadingMap[videoId]?.remove(filename); // to ensure next iteration wont post pause again --^

final title = titleCallback(videoId) ?? videoId;
final speedB = speedInBytes(filename, progressInfo.progress);
if (currentSpeedsInByte.value[videoId] == null) {
currentSpeedsInByte.value[videoId] = <DownloadTaskFilename, int>{}.obs;
currentSpeedsInByte.refresh();
}

currentSpeedsInByte.value[videoId]![filename] = speedB;
var keyword = isRunning ? 'Downloading' : 'Paused';
NotificationService.inst.downloadYoutubeNotification(
notificationID: entry.key,
title: "$keyword $downloadingText: $title",
progress: p,
total: tp,
subtitle: (progressText) => "$progressText (${speedB.fileSizeFormatted}/s)",
imagePath: imageCallback(videoId)?.path,
displayTime: startTime,
isRunning: isRunning,
);
}
}
}
Expand Down Expand Up @@ -273,7 +279,7 @@ class YoutubeController {
_loopMapAndPostNotification(
startTime: startTime,
isAudio: false,
bigMap: downloadsVideoProgressMap.value,
downloadingMap: isDownloading.value,
speedInBytes: (key, newProgress) {
final previousProgress = _notificationData._speedMapVideo[key] ?? 0;
final speed = newProgress - previousProgress;
Expand All @@ -286,7 +292,7 @@ class YoutubeController {
_loopMapAndPostNotification(
startTime: startTime,
isAudio: true,
bigMap: downloadsAudioProgressMap.value,
downloadingMap: isDownloading.value,
speedInBytes: (key, newProgress) {
final previousProgress = _notificationData._speedMapAudio[key] ?? 0;
final speed = newProgress - previousProgress;
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: namida
description: A Beautiful and Feature-rich Music Player, With YouTube & Video Support Built in Flutter
publish_to: "none"
version: 3.6.3-beta+240725225
version: 3.6.4-beta+240725225

environment:
sdk: ">=3.4.0 <4.0.0"
Expand Down

0 comments on commit 0ad3722

Please sign in to comment.