diff --git a/lib/controller/notification_controller.dart b/lib/controller/notification_controller.dart index 66cf49f4..57afe557 100644 --- a/lib/controller/notification_controller.dart +++ b/lib/controller/notification_controller.dart @@ -102,6 +102,7 @@ class NotificationService { required int progress, required int total, required DateTime displayTime, + required bool isRunning, }) { _createProgressNotification( id: _youtubeDownloadID, @@ -116,6 +117,7 @@ class NotificationService { isInBytes: true, tag: notificationID.filename, displayTime: displayTime, + ongoing: isRunning, ); } @@ -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()}'; @@ -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, diff --git a/lib/youtube/controller/youtube_controller.dart b/lib/youtube/controller/youtube_controller.dart index c3b6cf45..e6183a37 100644 --- a/lib/youtube/controller/youtube_controller.dart +++ b/lib/youtube/controller/youtube_controller.dart @@ -180,7 +180,7 @@ class YoutubeController { } void _loopMapAndPostNotification({ - required Map> bigMap, + required Map> downloadingMap, required int Function(DownloadTaskFilename key, int progress) speedInBytes, required DateTime startTime, required bool isAudio, @@ -188,35 +188,41 @@ class YoutubeController { 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] = {}.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] = {}.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, + ); } } } @@ -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; @@ -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; diff --git a/pubspec.yaml b/pubspec.yaml index 183364e9..0f7f9f32 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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"