Skip to content

Commit

Permalink
feat: 兜底一些可能报错场景
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyray-fan committed Jan 8, 2025
1 parent cdb090b commit 9b5f1aa
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 100 deletions.
2 changes: 2 additions & 0 deletions assets/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@
"Support sliding to collect images": "支持滑动收藏图片",
"On the image browsing page, you can quickly collect images by sliding horizontally or vertically according to your reading mode": "在图片浏览页面, 你可以根据你的阅读模式横滑或者竖滑快速收藏图片",
"Calculate your favorite from @a comics and @b images, After the parentheses are the number of pictures or the number of pictures compared to the number of comic pages": "从 @a 本漫画和 @b 张图片中, 计算你最喜欢的, 括号后是图片数量或图片数比漫画页数",
"The chapter order of the comic may have changed, temporarily not supported for collection": "漫画的章节顺序可能发生了变化, 暂不支持收藏此章节",
"Author: ": "作者: ",
"Tags: ": "标签: ",
"Comics(number): ": "漫画(数量): ",
Expand Down Expand Up @@ -564,6 +565,7 @@
"Support sliding to collect images": "支持滑動收藏圖片",
"On the image browsing page, you can quickly collect images by sliding horizontally or vertically according to your reading mode": "在圖片瀏覽頁面, 你可以根據你的閱讀模式橫向或者縱向滑動快速收藏圖片",
"Calculate your favorite from @a comics and @b images, After the parentheses are the number of pictures or the number of pictures compared to the number of comic pages": "從 @a 本漫畫和 @b 張圖片中, 計算你最喜歡的, 括號後是圖片數量或圖片數比漫畫頁數",
"The chapter order of the comic may have changed, temporarily not supported for collection": "漫畫的章節順序可能發生了變化, 暫不支持收藏此章節",
"Author: ": "作者: ",
"Tags: ": "標籤: ",
"Comics(number): ": "漫畫(數量): ",
Expand Down
4 changes: 2 additions & 2 deletions lib/foundation/image_favorites.dart
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ class ImageFavoriteManager with ChangeNotifier {
_debouncer.run(() {
imageFavoritesComicList = getAll(null);
notifyListeners();
}, Duration(seconds: 5));
}, Duration(seconds: 4));
}

/// 检查表image_favorites是否存在, 不存在则创建
Expand Down Expand Up @@ -315,7 +315,7 @@ class ImageFavoriteManager with ChangeNotifier {
for (ImageFavoritePro j in e.imageFavorites) {
int index =
finalImageFavorites.indexWhere((i) => i["page"] == j.page);
if (index == -1) {
if (index == -1 && j.page > 0) {
// isAutoFavorite 为 null 不写入数据库, 同时只保留需要的属性, 避免增加太多重复字段在数据库里
if (j.isAutoFavorite != null) {
finalImageFavorites.add({
Expand Down
1 change: 1 addition & 0 deletions lib/pages/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'package:venera/foundation/history.dart';
import 'package:venera/foundation/image_provider/history_image_provider.dart';
import 'package:venera/foundation/image_provider/local_comic_image.dart';
import 'package:venera/foundation/local.dart';
import 'package:venera/foundation/log.dart';
import 'package:venera/pages/accounts_page.dart';
import 'package:venera/pages/comic_page.dart';
import 'package:venera/pages/comic_source_page.dart';
Expand Down
24 changes: 14 additions & 10 deletions lib/pages/home_page/image_favorites.dart
Original file line number Diff line number Diff line change
Expand Up @@ -160,17 +160,21 @@ class ImageFavoritesState extends State<ImageFavorites> {
}

void refreshImageFavorites() async {
if (mounted) {
imageFavoritesCompute = null;
allImageFavoritePros = [];
for (var comic in ImageFavoriteManager.imageFavoritesComicList) {
allImageFavoritePros.addAll(comic.sortedImageFavoritePros);
try {
if (mounted) {
imageFavoritesCompute = null;
allImageFavoritePros = [];
for (var comic in ImageFavoriteManager.imageFavoritesComicList) {
allImageFavoritePros.addAll(comic.sortedImageFavoritePros);
}
setState(() {});
// 避免性能开销, 开一个线程计算
imageFavoritesCompute = await compute(computeImageFavorites,
jsonEncode(ImageFavoriteManager.imageFavoritesComicList));
setState(() {});
}
setState(() {});
// 避免性能开销, 开一个线程计算
imageFavoritesCompute = await compute(computeImageFavorites,
jsonEncode(ImageFavoriteManager.imageFavoritesComicList));
setState(() {});
} catch (e, stackTrace) {
Log.error("Unhandled Exception", e.toString(), stackTrace);
}
}

Expand Down
154 changes: 79 additions & 75 deletions lib/pages/image_favorites_page/image_favorites_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,91 +30,95 @@ class ImageFavoritesItemState extends State<ImageFavoritesItem> {

// 如果刚从pica导入(没有imageKey) 或者 imageKey 失效了, 刷新一下
void refreshImageKey(ImageFavoritesEp imageFavoritesEp) async {
if (isImageKeyLoading ||
hasRefreshImageKeyOnErr ||
loadingImageFavoritesComicRes.isLoaded) {
return;
}
loadingImageFavoritesComicRes.isLoaded = true;
widget.setRefreshComicList(loadingImageFavoritesComicRes);
isImageKeyLoading = true;
ComicSource? comicSource =
ComicSource.find(widget.imageFavoritesComic.sourceKey);
// 拿一下漫画信息和对应章节的图片
var resArr = await Future.wait([
comicSource!.loadComicPages!(
widget.imageFavoritesComic.id,
imageFavoritesEp.eid,
),
comicSource.loadComicInfo!(
widget.imageFavoritesComic.id,
)
]);
Res<List<String>> comicPagesRes = resArr[0] as Res<List<String>>;
Res<ComicDetails> comicInfoRes = resArr[1] as Res<ComicDetails>;
if (comicInfoRes.errorMessage?.contains("404") ?? false) {
loadingImageFavoritesComicRes.isInvalid = true;
widget.setRefreshComicList(loadingImageFavoritesComicRes);
if (mounted) {
setState(() {});
}
return;
}
if (!comicInfoRes.error) {
ImageFavoritesSomething something =
ImageFavoritesComic.getSomethingFromComicDetails(
comicInfoRes.data, imageFavoritesEp.ep);
// 刷新一下值, 保存最新的
widget.imageFavoritesComic.author = something.author;
widget.imageFavoritesComic.subTitle = something.subTitle;
widget.imageFavoritesComic.tags = something.tags;
widget.imageFavoritesComic.translatedTags = something.translatedTags;
imageFavoritesEp.epName = something.epName;
} else {
return;
}
if (comicPagesRes.error) {
// 能加载漫画信息, 说明只是章节对不太上, 刷新一下章节
var chapters = comicInfoRes.data.chapters;
// 兜底一下, 如果不是从pica导入的空字符串, 说明是调用接口更新过章节的, 比如jm, 避免丢失最初正确的eid
// 拷贝等多章节可能会更新章节顺序, 后续如果碰到这种情况多的话, 就在右上角出一个功能批量刷新一下
if (imageFavoritesEp.eid != "") {
try {
if (isImageKeyLoading ||
hasRefreshImageKeyOnErr ||
loadingImageFavoritesComicRes.isLoaded) {
return;
}
var finalEid = chapters?.keys.elementAt(imageFavoritesEp.ep - 1) ?? '0';
loadingImageFavoritesComicRes.isLoaded = true;
widget.setRefreshComicList(loadingImageFavoritesComicRes);
isImageKeyLoading = true;
ComicSource? comicSource =
ComicSource.find(widget.imageFavoritesComic.sourceKey);
// 拿一下漫画信息和对应章节的图片
var resArr = await Future.wait([
comicSource.loadComicPages!(
comicSource!.loadComicPages!(
widget.imageFavoritesComic.id,
imageFavoritesEp.eid,
),
comicSource.loadComicInfo!(
widget.imageFavoritesComic.id,
finalEid,
)
]);
comicPagesRes = resArr[0];
if (comicPagesRes.error) {
Res<List<String>> comicPagesRes = resArr[0] as Res<List<String>>;
Res<ComicDetails> comicInfoRes = resArr[1] as Res<ComicDetails>;
if (comicInfoRes.errorMessage?.contains("404") ?? false) {
loadingImageFavoritesComicRes.isInvalid = true;
widget.setRefreshComicList(loadingImageFavoritesComicRes);
if (mounted) {
setState(() {});
}
return;
}
if (!comicInfoRes.error) {
ImageFavoritesSomething something =
ImageFavoritesComic.getSomethingFromComicDetails(
comicInfoRes.data, imageFavoritesEp.ep);
// 刷新一下值, 保存最新的
widget.imageFavoritesComic.author = something.author;
widget.imageFavoritesComic.subTitle = something.subTitle;
widget.imageFavoritesComic.tags = something.tags;
widget.imageFavoritesComic.translatedTags = something.translatedTags;
imageFavoritesEp.epName = something.epName;
} else {
imageFavoritesEp.eid = finalEid;
return;
}
if (comicPagesRes.error) {
// 能加载漫画信息, 说明只是章节对不太上, 刷新一下章节
var chapters = comicInfoRes.data.chapters;
// 兜底一下, 如果不是从pica导入的空字符串, 说明是调用接口更新过章节的, 比如jm, 避免丢失最初正确的eid
// 拷贝等多章节可能会更新章节顺序, 后续如果碰到这种情况多的话, 就在右上角出一个功能批量刷新一下
if (imageFavoritesEp.eid != "") {
return;
}
var finalEid = chapters?.keys.elementAt(imageFavoritesEp.ep - 1) ?? '0';
var resArr = await Future.wait([
comicSource.loadComicPages!(
widget.imageFavoritesComic.id,
finalEid,
)
]);
comicPagesRes = resArr[0];
if (comicPagesRes.error) {
return;
} else {
imageFavoritesEp.eid = finalEid;
}
}
List<String> images = comicPagesRes.data;
widget.imageFavoritesComic.maxPage = images.length;
imageFavoritesEp.maxPage = images.length;
// 塞一个封面进去
if (!imageFavoritesEp.isHasFirstPage) {
ImageFavoritePro copy =
ImageFavoritePro.copy(imageFavoritesEp.imageFavorites[0]);
copy.page = ImageFavoritesEp.firstPage;
copy.isAutoFavorite = true;
imageFavoritesEp.imageFavorites.insert(0, copy);
}
// 统一刷一下最新的imageKey
for (var ele in imageFavoritesEp.imageFavorites) {
ele.imageKey = images[ele.page - 1];
}
ImageFavoriteManager.addOrUpdateOrDelete(widget.imageFavoritesComic);
if (mounted) {
setState(() {});
}
isImageKeyLoading = false;
} catch (e, stackTrace) {
Log.error("Unhandled Exception", e.toString(), stackTrace);
}
List<String> images = comicPagesRes.data;
widget.imageFavoritesComic.maxPage = images.length;
imageFavoritesEp.maxPage = images.length;
// 塞一个封面进去
if (!imageFavoritesEp.isHasFirstPage) {
ImageFavoritePro copy =
ImageFavoritePro.copy(imageFavoritesEp.imageFavorites[0]);
copy.page = ImageFavoritesEp.firstPage;
copy.isAutoFavorite = true;
imageFavoritesEp.imageFavorites.insert(0, copy);
}
// 统一刷一下最新的imageKey
for (var ele in imageFavoritesEp.imageFavorites) {
ele.imageKey = images[ele.page - 1];
}
ImageFavoriteManager.addOrUpdateOrDelete(widget.imageFavoritesComic);
if (mounted) {
setState(() {});
}
isImageKeyLoading = false;
}

void goComicInfo(ImageFavoritesComic comic) {
Expand Down
1 change: 1 addition & 0 deletions lib/pages/image_favorites_page/image_favorites_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import 'package:venera/foundation/comic_source/comic_source.dart';
import 'package:venera/foundation/consts.dart';
import 'package:venera/foundation/history.dart';
import 'package:venera/foundation/image_provider/image_favorites_provider.dart';
import 'package:venera/foundation/log.dart';
import 'package:venera/foundation/res.dart';
import 'package:venera/pages/comic_page.dart';
import 'package:venera/pages/image_favorites_page/type.dart';
Expand Down
37 changes: 24 additions & 13 deletions lib/pages/reader/scaffold.dart
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,17 @@ class _ReaderScaffoldState extends State<_ReaderScaffold> {
}

bool isLiked() {
String cid = context.reader.cid;
String eid = context.reader.eid;
int ep = context.reader.chapter;
int page = context.reader.page;
String sourceKey = context.reader.type.sourceKey;
return ImageFavoriteManager.isHas(cid, sourceKey, eid, page, ep);
try {
String cid = context.reader.cid;
String eid = context.reader.eid;
int ep = context.reader.chapter;
int page = context.reader.page;
String sourceKey = context.reader.type.sourceKey;
return ImageFavoriteManager.isHas(cid, sourceKey, eid, page, ep);
} catch (e, stackTrace) {
Log.error("Unhandled Exception", e.toString(), stackTrace);
return false;
}
}

void imageFavoritesAction() {
Expand Down Expand Up @@ -328,8 +333,12 @@ class _ReaderScaffoldState extends State<_ReaderScaffold> {
if (imageFavoritesEp.eid == "") {
imageFavoritesEp.eid == eid;
} else {
// 避免多章节漫画源的章节顺序发生变化, 如果情况比较多, 做一个以eid为准更新ep的功能
showToast(
message: "漫画的章节顺序可能发生了变化, 暂不支持收藏此章节".tl, context: context);
message:
"The chapter order of the comic may have changed, temporarily not supported for collection"
.tl,
context: context);
return;
}
}
Expand Down Expand Up @@ -508,12 +517,14 @@ class _ReaderScaffoldState extends State<_ReaderScaffold> {
child: Container(
decoration: BoxDecoration(
color: context.colorScheme.surface.toOpacity(0.82),
border: Border(
top: BorderSide(
color: Colors.grey.toOpacity(0.5),
width: 0.5,
),
),
border: isOpen
? Border(
top: BorderSide(
color: Colors.grey.toOpacity(0.5),
width: 0.5,
),
)
: null,
),
padding: EdgeInsets.only(bottom: context.padding.bottom),
child: child,
Expand Down

0 comments on commit 9b5f1aa

Please sign in to comment.