From 0b9f0b7d35db59982fd06821f9803deef7189e6c Mon Sep 17 00:00:00 2001 From: nyne Date: Fri, 31 Jan 2025 13:08:24 +0800 Subject: [PATCH] Improve downloading message. Close #165 --- lib/network/download.dart | 36 ++++++++++++++++++++++++--------- lib/pages/downloading_page.dart | 22 ++++++++++++++++---- 2 files changed, 45 insertions(+), 13 deletions(-) diff --git a/lib/network/download.dart b/lib/network/download.dart index 5227b0d..058b93e 100644 --- a/lib/network/download.dart +++ b/lib/network/download.dart @@ -59,6 +59,16 @@ abstract class DownloadTask with ChangeNotifier { return null; } } + + @override + bool operator ==(Object other) { + return other is DownloadTask && + other.id == id && + other.comicType == comicType; + } + + @override + int get hashCode => Object.hash(id, comicType); } class ImagesDownloadTask extends DownloadTask with _TransferSpeedMixin { @@ -220,7 +230,9 @@ class ImagesDownloadTask extends DownloadTask with _TransferSpeedMixin { runRecorder(); if (comic == null) { - var res = await runWithRetry(() async { + _message = "Fetching comic info..."; + notifyListeners(); + var res = await _runWithRetry(() async { var r = await source.loadComicInfo!(comicId); if (r.error) { throw r.errorMessage!; @@ -260,7 +272,9 @@ class ImagesDownloadTask extends DownloadTask with _TransferSpeedMixin { await LocalManager().saveCurrentDownloadingTasks(); if (_cover == null) { - var res = await runWithRetry(() async { + _message = "Downloading cover..."; + notifyListeners(); + var res = await _runWithRetry(() async { Uint8List? data; await for (var progress in ImageDownloader.loadThumbnail(comic!.cover, source.key)) { @@ -272,8 +286,7 @@ class ImagesDownloadTask extends DownloadTask with _TransferSpeedMixin { throw "Failed to download cover"; } var fileType = detectFileType(data); - var file = - File(FilePath.join(path!, "cover${fileType.ext}")); + var file = File(FilePath.join(path!, "cover${fileType.ext}")); file.writeAsBytesSync(data); return "file://${file.path}"; }); @@ -290,7 +303,9 @@ class ImagesDownloadTask extends DownloadTask with _TransferSpeedMixin { if (_images == null) { if (comic!.chapters == null) { - var res = await runWithRetry(() async { + _message = "Fetching image list..."; + notifyListeners(); + var res = await _runWithRetry(() async { var r = await source.loadComicPages!(comicId, null); if (r.error) { throw r.errorMessage!; @@ -312,6 +327,8 @@ class ImagesDownloadTask extends DownloadTask with _TransferSpeedMixin { } else { _images = {}; _totalCount = 0; + int cpCount = 0; + int totalCpCount = chapters?.length ?? comic!.chapters!.length; for (var i in comic!.chapters!.keys) { if (chapters != null && !chapters!.contains(i)) { continue; @@ -320,7 +337,9 @@ class ImagesDownloadTask extends DownloadTask with _TransferSpeedMixin { _totalCount += _images![i]!.length; continue; } - var res = await runWithRetry(() async { + _message = "Fetching image list ($cpCount/$totalCpCount)..."; + notifyListeners(); + var res = await _runWithRetry(() async { var r = await source.loadComicPages!(comicId, i); if (r.error) { throw r.errorMessage!; @@ -458,8 +477,7 @@ class ImagesDownloadTask extends DownloadTask with _TransferSpeedMixin { }).toList(), directory: Directory(path!).name, chapters: comic!.chapters, - cover: - File(_cover!.split("file://").last).name, + cover: File(_cover!.split("file://").last).name, comicType: ComicType(source.key.hashCode), downloadedChapters: chapters ?? [], createdAt: DateTime.now(), @@ -478,7 +496,7 @@ class ImagesDownloadTask extends DownloadTask with _TransferSpeedMixin { int get hashCode => Object.hash(comicId, source.key); } -Future> runWithRetry(Future Function() task, +Future> _runWithRetry(Future Function() task, {int retry = 3}) async { for (var i = 0; i < retry; i++) { try { diff --git a/lib/pages/downloading_page.dart b/lib/pages/downloading_page.dart index 790e482..795d6e0 100644 --- a/lib/pages/downloading_page.dart +++ b/lib/pages/downloading_page.dart @@ -46,6 +46,7 @@ class _DownloadingPageState extends State { i--; return _DownloadTaskTile( + key: ValueKey(LocalManager().downloadingTasks[i]), task: LocalManager().downloadingTasks[i], ); }, @@ -120,7 +121,7 @@ class _DownloadingPageState extends State { } class _DownloadTaskTile extends StatefulWidget { - const _DownloadTaskTile({required this.task}); + const _DownloadTaskTile({required this.task, super.key}); final DownloadTask task; @@ -129,20 +130,33 @@ class _DownloadTaskTile extends StatefulWidget { } class _DownloadTaskTileState extends State<_DownloadTaskTile> { + late DownloadTask task; + @override void initState() { - widget.task.addListener(update); + task = widget.task; + task.addListener(update); super.initState(); } @override void dispose() { - widget.task.removeListener(update); + task.removeListener(update); super.dispose(); } + @override + void didUpdateWidget(covariant _DownloadTaskTile oldWidget) { + super.didUpdateWidget(oldWidget); + if (oldWidget.task != widget.task) { + task.removeListener(update); + task = widget.task; + task.addListener(update); + } + } + void update() { - context.findAncestorStateOfType<_DownloadingPageState>()?.update(); + setState(() {}); } @override