mirror of
https://github.com/venera-app/venera.git
synced 2025-09-28 00:07:24 +00:00
@@ -59,6 +59,16 @@ abstract class DownloadTask with ChangeNotifier {
|
|||||||
return null;
|
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 {
|
class ImagesDownloadTask extends DownloadTask with _TransferSpeedMixin {
|
||||||
@@ -220,7 +230,9 @@ class ImagesDownloadTask extends DownloadTask with _TransferSpeedMixin {
|
|||||||
runRecorder();
|
runRecorder();
|
||||||
|
|
||||||
if (comic == null) {
|
if (comic == null) {
|
||||||
var res = await runWithRetry(() async {
|
_message = "Fetching comic info...";
|
||||||
|
notifyListeners();
|
||||||
|
var res = await _runWithRetry(() async {
|
||||||
var r = await source.loadComicInfo!(comicId);
|
var r = await source.loadComicInfo!(comicId);
|
||||||
if (r.error) {
|
if (r.error) {
|
||||||
throw r.errorMessage!;
|
throw r.errorMessage!;
|
||||||
@@ -260,7 +272,9 @@ class ImagesDownloadTask extends DownloadTask with _TransferSpeedMixin {
|
|||||||
await LocalManager().saveCurrentDownloadingTasks();
|
await LocalManager().saveCurrentDownloadingTasks();
|
||||||
|
|
||||||
if (_cover == null) {
|
if (_cover == null) {
|
||||||
var res = await runWithRetry(() async {
|
_message = "Downloading cover...";
|
||||||
|
notifyListeners();
|
||||||
|
var res = await _runWithRetry(() async {
|
||||||
Uint8List? data;
|
Uint8List? data;
|
||||||
await for (var progress
|
await for (var progress
|
||||||
in ImageDownloader.loadThumbnail(comic!.cover, source.key)) {
|
in ImageDownloader.loadThumbnail(comic!.cover, source.key)) {
|
||||||
@@ -272,8 +286,7 @@ class ImagesDownloadTask extends DownloadTask with _TransferSpeedMixin {
|
|||||||
throw "Failed to download cover";
|
throw "Failed to download cover";
|
||||||
}
|
}
|
||||||
var fileType = detectFileType(data);
|
var fileType = detectFileType(data);
|
||||||
var file =
|
var file = File(FilePath.join(path!, "cover${fileType.ext}"));
|
||||||
File(FilePath.join(path!, "cover${fileType.ext}"));
|
|
||||||
file.writeAsBytesSync(data);
|
file.writeAsBytesSync(data);
|
||||||
return "file://${file.path}";
|
return "file://${file.path}";
|
||||||
});
|
});
|
||||||
@@ -290,7 +303,9 @@ class ImagesDownloadTask extends DownloadTask with _TransferSpeedMixin {
|
|||||||
|
|
||||||
if (_images == null) {
|
if (_images == null) {
|
||||||
if (comic!.chapters == 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);
|
var r = await source.loadComicPages!(comicId, null);
|
||||||
if (r.error) {
|
if (r.error) {
|
||||||
throw r.errorMessage!;
|
throw r.errorMessage!;
|
||||||
@@ -312,6 +327,8 @@ class ImagesDownloadTask extends DownloadTask with _TransferSpeedMixin {
|
|||||||
} else {
|
} else {
|
||||||
_images = {};
|
_images = {};
|
||||||
_totalCount = 0;
|
_totalCount = 0;
|
||||||
|
int cpCount = 0;
|
||||||
|
int totalCpCount = chapters?.length ?? comic!.chapters!.length;
|
||||||
for (var i in comic!.chapters!.keys) {
|
for (var i in comic!.chapters!.keys) {
|
||||||
if (chapters != null && !chapters!.contains(i)) {
|
if (chapters != null && !chapters!.contains(i)) {
|
||||||
continue;
|
continue;
|
||||||
@@ -320,7 +337,9 @@ class ImagesDownloadTask extends DownloadTask with _TransferSpeedMixin {
|
|||||||
_totalCount += _images![i]!.length;
|
_totalCount += _images![i]!.length;
|
||||||
continue;
|
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);
|
var r = await source.loadComicPages!(comicId, i);
|
||||||
if (r.error) {
|
if (r.error) {
|
||||||
throw r.errorMessage!;
|
throw r.errorMessage!;
|
||||||
@@ -458,8 +477,7 @@ class ImagesDownloadTask extends DownloadTask with _TransferSpeedMixin {
|
|||||||
}).toList(),
|
}).toList(),
|
||||||
directory: Directory(path!).name,
|
directory: Directory(path!).name,
|
||||||
chapters: comic!.chapters,
|
chapters: comic!.chapters,
|
||||||
cover:
|
cover: File(_cover!.split("file://").last).name,
|
||||||
File(_cover!.split("file://").last).name,
|
|
||||||
comicType: ComicType(source.key.hashCode),
|
comicType: ComicType(source.key.hashCode),
|
||||||
downloadedChapters: chapters ?? [],
|
downloadedChapters: chapters ?? [],
|
||||||
createdAt: DateTime.now(),
|
createdAt: DateTime.now(),
|
||||||
@@ -478,7 +496,7 @@ class ImagesDownloadTask extends DownloadTask with _TransferSpeedMixin {
|
|||||||
int get hashCode => Object.hash(comicId, source.key);
|
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 {
|
{int retry = 3}) async {
|
||||||
for (var i = 0; i < retry; i++) {
|
for (var i = 0; i < retry; i++) {
|
||||||
try {
|
try {
|
||||||
|
@@ -46,6 +46,7 @@ class _DownloadingPageState extends State<DownloadingPage> {
|
|||||||
i--;
|
i--;
|
||||||
|
|
||||||
return _DownloadTaskTile(
|
return _DownloadTaskTile(
|
||||||
|
key: ValueKey(LocalManager().downloadingTasks[i]),
|
||||||
task: LocalManager().downloadingTasks[i],
|
task: LocalManager().downloadingTasks[i],
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
@@ -120,7 +121,7 @@ class _DownloadingPageState extends State<DownloadingPage> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _DownloadTaskTile extends StatefulWidget {
|
class _DownloadTaskTile extends StatefulWidget {
|
||||||
const _DownloadTaskTile({required this.task});
|
const _DownloadTaskTile({required this.task, super.key});
|
||||||
|
|
||||||
final DownloadTask task;
|
final DownloadTask task;
|
||||||
|
|
||||||
@@ -129,20 +130,33 @@ class _DownloadTaskTile extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _DownloadTaskTileState extends State<_DownloadTaskTile> {
|
class _DownloadTaskTileState extends State<_DownloadTaskTile> {
|
||||||
|
late DownloadTask task;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
widget.task.addListener(update);
|
task = widget.task;
|
||||||
|
task.addListener(update);
|
||||||
super.initState();
|
super.initState();
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
widget.task.removeListener(update);
|
task.removeListener(update);
|
||||||
super.dispose();
|
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() {
|
void update() {
|
||||||
context.findAncestorStateOfType<_DownloadingPageState>()?.update();
|
setState(() {});
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
Reference in New Issue
Block a user