mirror of
https://github.com/venera-app/venera.git
synced 2025-09-27 15:57:25 +00:00
Fallback to local cover if loading fails for favorite comic.
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import 'dart:async' show Future;
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:venera/foundation/comic_type.dart';
|
||||
import 'package:venera/foundation/local.dart';
|
||||
import 'package:venera/network/images.dart';
|
||||
import 'package:venera/utils/io.dart';
|
||||
import 'base_image_provider.dart';
|
||||
@@ -11,7 +13,12 @@ class CachedImageProvider
|
||||
/// Image provider for normal image.
|
||||
///
|
||||
/// [url] is the url of the image. Local file path is also supported.
|
||||
const CachedImageProvider(this.url, {this.headers, this.sourceKey, this.cid});
|
||||
const CachedImageProvider(this.url, {
|
||||
this.headers,
|
||||
this.sourceKey,
|
||||
this.cid,
|
||||
this.fallbackToLocalCover = false,
|
||||
});
|
||||
|
||||
final String url;
|
||||
|
||||
@@ -21,6 +28,9 @@ class CachedImageProvider
|
||||
|
||||
final String? cid;
|
||||
|
||||
// Use local cover if network image fails to load.
|
||||
final bool fallbackToLocalCover;
|
||||
|
||||
static int loadingCount = 0;
|
||||
|
||||
static const _kMaxLoadingCount = 8;
|
||||
@@ -49,6 +59,24 @@ class CachedImageProvider
|
||||
}
|
||||
throw "Error: Empty response body.";
|
||||
}
|
||||
catch(e) {
|
||||
if (fallbackToLocalCover && sourceKey != null && cid != null) {
|
||||
final localComic = LocalManager().find(
|
||||
cid!,
|
||||
ComicType.fromKey(sourceKey!),
|
||||
);
|
||||
if (localComic != null) {
|
||||
var file = localComic.coverFile;
|
||||
if (await file.exists()) {
|
||||
var data = await file.readAsBytes();
|
||||
if (data.isNotEmpty) {
|
||||
return data;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
rethrow;
|
||||
}
|
||||
finally {
|
||||
loadingCount--;
|
||||
}
|
||||
|
Reference in New Issue
Block a user