Improve downloading message.

Close #165
This commit is contained in:
2025-01-31 13:08:24 +08:00
parent 9ea749a84a
commit 0b9f0b7d35
2 changed files with 45 additions and 13 deletions

View File

@@ -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<Res<T>> runWithRetry<T>(Future<T> Function() task,
Future<Res<T>> _runWithRetry<T>(Future<T> Function() task,
{int retry = 3}) async {
for (var i = 0; i < retry; i++) {
try {

View File

@@ -46,6 +46,7 @@ class _DownloadingPageState extends State<DownloadingPage> {
i--;
return _DownloadTaskTile(
key: ValueKey(LocalManager().downloadingTasks[i]),
task: LocalManager().downloadingTasks[i],
);
},
@@ -120,7 +121,7 @@ class _DownloadingPageState extends State<DownloadingPage> {
}
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