Skip to content

Commit

Permalink
Merge pull request #644 from lamarios/feature/allow-download-cancel
Browse files Browse the repository at this point in the history
fix issues with failed downloads that can't be removed.
  • Loading branch information
lamarios authored Nov 30, 2024
2 parents 5343c31 + 8a650da commit 8ce37df
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 45 deletions.
17 changes: 15 additions & 2 deletions lib/downloads/states/download_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ class DownloadManagerCubit extends Cubit<DownloadManagerState> {
var progresses =
Map<String, DownloadProgress>.from(state.downloadProgresses);
var downloadProgress = progresses[video.videoId];

if (downloadProgress == null ||
downloadProgress.cancelToken.isCancelled) {
return;
}

progresses.remove(video.videoId);
video = video.copyWith(downloadComplete: true);
await db.upsertDownload(video);
Expand Down Expand Up @@ -167,6 +173,10 @@ class DownloadManagerCubit extends Cubit<DownloadManagerState> {
options: Options(headers: server.headersForUrl(thumbUrl)));
}

if (cancelToken.isCancelled) {
return false;
}

// download video
var mediaPath = await downloadedVideo.downloadPath;

Expand All @@ -177,6 +187,9 @@ class DownloadManagerCubit extends Cubit<DownloadManagerState> {
log.info(
"Downloading video ${vid.title}, audioOnly ? $audioOnly, quality: $quality to path: $tempDir");
try {
if (cancelToken.isCancelled) {
return false;
}
await dio
.download(audioUrl, audioPath,
onReceiveProgress: (count, total) => onProgress(
Expand Down Expand Up @@ -263,14 +276,14 @@ class DownloadManagerCubit extends Cubit<DownloadManagerState> {
String path = await vid.effectivePath;
await File(path).delete();
} catch (e) {
log.fine('File might not be available, that\'s ok');
log.fine('Video file might not be available, that\'s ok');
}

try {
String path = await vid.thumbnailPath;
await File(path).delete();
} catch (e) {
log.fine('File might not be available, that\'s ok');
log.fine('Thumbnail might not be available, that\'s ok');
}

await db.deleteDownload(vid);
Expand Down
114 changes: 73 additions & 41 deletions lib/downloads/views/components/downloaded_video.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,45 +25,77 @@ class DownloadedVideoView extends StatelessWidget {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 32.0),
child: Wrap(
alignment: WrapAlignment.center,
runSpacing: 16,
spacing: 16,
children: [
Column(
mainAxisSize: MainAxisSize.min,
children: [
IconButton.filledTonal(
onPressed: () async {
Navigator.of(ctx).pop();
await cubit.copyToDownloadFolder(v);
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content:
Text(locals.fileCopiedToDownloadFolder)));
}
},
icon: const Icon(Icons.copy)),
Text(locals.copyToDownloadFolder)
],
),
Column(
mainAxisSize: MainAxisSize.min,
children: [
IconButton.filledTonal(
onPressed: () async {
Navigator.of(ctx).pop();
await cubit.deleteVideo(v);
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(locals.videoDeleted)));
}
},
icon: const Icon(Icons.delete)),
Text(locals.delete)
],
)
],
),
alignment: WrapAlignment.center,
runSpacing: 16,
spacing: 16,
children: [
if (!v.downloadFailed && !v.downloadComplete)
Column(
mainAxisSize: MainAxisSize.min,
children: [
IconButton.filledTonal(
onPressed: () async {
Navigator.of(ctx).pop();
await cubit.deleteVideo(v);
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(locals.videoDeleted)));
}
},
icon: const Icon(Icons.clear)),
Text(locals.cancel)
],
),
if (v.downloadComplete)
Column(
mainAxisSize: MainAxisSize.min,
children: [
IconButton.filledTonal(
onPressed: () async {
Navigator.of(ctx).pop();
await cubit.copyToDownloadFolder(v);
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
locals.fileCopiedToDownloadFolder)));
}
},
icon: const Icon(Icons.copy)),
Text(locals.copyToDownloadFolder)
],
),
if (v.downloadFailed)
Column(
mainAxisSize: MainAxisSize.min,
children: [
IconButton.filledTonal(
onPressed: () async {
Navigator.of(ctx).pop();
await cubit.retryDownload(v);
},
icon: const Icon(Icons.refresh)),
Text(locals.retry)
],
),
if (v.downloadComplete || v.downloadFailed)
Column(
mainAxisSize: MainAxisSize.min,
children: [
IconButton.filledTonal(
onPressed: () async {
Navigator.of(ctx).pop();
await cubit.deleteVideo(v);
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(locals.videoDeleted)));
}
},
icon: const Icon(Icons.delete)),
Text(locals.delete)
],
)
]),
);
},
);
Expand Down Expand Up @@ -93,8 +125,8 @@ class DownloadedVideoView extends StatelessWidget {
duration: animationDuration,
child: CompactVideo(
offlineVideo: state.video,
onTap: downloadFailed
? cubit.retryDownload
onTap: downloadFailed || !state.video!.downloadComplete
? () => openVideoSheet(context, state.video!)
: cubit.playVideo,
trailing: [
(state.video?.audioOnly ?? false)
Expand Down
3 changes: 2 additions & 1 deletion lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -1382,5 +1382,6 @@
"cancelSleepTimer": "Cancel sleep timer",
"premieresIn": "Premieres in {formattedDuration}",
"screenControls": "Screen controls",
"screenControlsExplanation": "When watching a video in full screen, Vertically dragging from the left or the right will adjust the brightness or volume respectively"
"screenControlsExplanation": "When watching a video in full screen, Vertically dragging from the left or the right will adjust the brightness or volume respectively",
"retry": "Retry"
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: clipious
version: 1.22.3+4066
version: 1.22.4+4067
publish_to: none
description: Client for invidious.
environment:
Expand Down

0 comments on commit 8ce37df

Please sign in to comment.