From a7c1983f35914fcc7f026b4b9891f6d5f3287aab Mon Sep 17 00:00:00 2001 From: nyne Date: Sun, 14 Sep 2025 17:19:23 +0800 Subject: [PATCH] Fallback to local cover if loading fails for favorite comic. --- lib/components/comic.dart | 1 + .../image_provider/cached_image.dart | 30 ++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/lib/components/comic.dart b/lib/components/comic.dart index 3ad1f2b..6b8a723 100644 --- a/lib/components/comic.dart +++ b/lib/components/comic.dart @@ -17,6 +17,7 @@ ImageProvider? _findImageProvider(Comic comic) { comic.cover, sourceKey: comic.sourceKey, cid: comic.id, + fallbackToLocalCover: comic is FavoriteItem, ); } return image; diff --git a/lib/foundation/image_provider/cached_image.dart b/lib/foundation/image_provider/cached_image.dart index 6a4fd54..9410502 100644 --- a/lib/foundation/image_provider/cached_image.dart +++ b/lib/foundation/image_provider/cached_image.dart @@ -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--; }