From d8a2bc2d3fb0a504f58ae12ce599966e73fb4474 Mon Sep 17 00:00:00 2001 From: violet-dev Date: Fri, 27 Dec 2024 14:30:58 +0900 Subject: [PATCH 1/2] Dedup showArticleInfo related codes --- .../after_loading/afterloading_page.dart | 2 +- .../pages/article_info/article_info_page.dart | 2 +- violet/lib/pages/bookmark/crop_bookmark.dart | 2 +- violet/lib/pages/common/utils.dart | 123 +++++++++++------- .../pages/download/download_item_widget.dart | 2 +- violet/lib/pages/lab/lab/search_comment.dart | 2 +- .../pages/lab/lab/search_comment_author.dart | 2 +- violet/lib/pages/lab/lab/search_message.dart | 2 +- .../pages/viewer/overlay/viewer_overlay.dart | 44 +------ .../viewer/overlay/viewer_tab_panel.dart | 42 +----- .../article_list_item_widget.dart | 66 +--------- 11 files changed, 98 insertions(+), 191 deletions(-) diff --git a/violet/lib/pages/after_loading/afterloading_page.dart b/violet/lib/pages/after_loading/afterloading_page.dart index c361b6c67..56d67e350 100644 --- a/violet/lib/pages/after_loading/afterloading_page.dart +++ b/violet/lib/pages/after_loading/afterloading_page.dart @@ -96,7 +96,7 @@ class AfterLoadingPageState extends State } if (int.tryParse(uri.host) != null) { - showArticleInfo(context, int.parse(uri.host)); + showArticleInfoById(context, int.parse(uri.host)); } } diff --git a/violet/lib/pages/article_info/article_info_page.dart b/violet/lib/pages/article_info/article_info_page.dart index 1aa0645be..f68380053 100644 --- a/violet/lib/pages/article_info/article_info_page.dart +++ b/violet/lib/pages/article_info/article_info_page.dart @@ -781,7 +781,7 @@ class __InfoAreaWidgetState extends State<_InfoAreaWidget> { if (ehPattern.stringMatch(url) == url) { var match = ehPattern.allMatches(url); var id = match.first.namedGroup('id')!.trim(); - showArticleInfo(context, int.parse(id)); + showArticleInfoById(context, int.parse(id)); } else if (await canLaunchUrlString(url)) { await launchUrlString(url); } diff --git a/violet/lib/pages/bookmark/crop_bookmark.dart b/violet/lib/pages/bookmark/crop_bookmark.dart index 9ea069c62..a5146d3a8 100644 --- a/violet/lib/pages/bookmark/crop_bookmark.dart +++ b/violet/lib/pages/bookmark/crop_bookmark.dart @@ -209,7 +209,7 @@ class _CropBookmarkPageState extends State { color: Colors.transparent, child: InkWell( onTap: () async { - showArticleInfo(context, articleId); + showArticleInfoById(context, articleId); }, onDoubleTap: () async { _showViewer(articleId, page); diff --git a/violet/lib/pages/common/utils.dart b/violet/lib/pages/common/utils.dart index a2ba3c28b..ab5cc2ba9 100644 --- a/violet/lib/pages/common/utils.dart +++ b/violet/lib/pages/common/utils.dart @@ -4,57 +4,88 @@ import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'package:violet/component/hentai.dart'; +import 'package:violet/component/image_provider.dart'; +import 'package:violet/database/query.dart'; import 'package:violet/database/user/bookmark.dart'; import 'package:violet/model/article_info.dart'; import 'package:violet/pages/article_info/article_info_page.dart'; import 'package:violet/widgets/article_item/image_provider_manager.dart'; // TODO: expand using optional arguments -Future showArticleInfo(BuildContext context, int id) async { - final height = MediaQuery.of(context).size.height; - +Future showArticleInfoById(BuildContext context, int id) async { final search = await HentaiManager.idSearch(id.toString()); - if (search.results.length != 1) return; - - final qr = search.results.first; - - HentaiManager.getImageProvider(qr).then((value) async { - final thumbnail = await value.getThumbnailUrl(); - final headers = await value.getHeader(0); - ProviderManager.insert(qr.id(), value); - - final isBookmarked = - await (await Bookmark.getInstance()).isBookmark(qr.id()); - - if (!context.mounted) return; - Provider? cache; - showModalBottomSheet( - context: context, - isScrollControlled: true, - builder: (_) { - return DraggableScrollableSheet( - initialChildSize: 400 / height, - minChildSize: 400 / height, - maxChildSize: 0.9, - expand: false, - builder: (_, controller) { - cache ??= Provider.value( - value: ArticleInfo.fromArticleInfo( - queryResult: qr, - thumbnail: thumbnail, - headers: headers, - heroKey: 'zxcvzxcvzxcv', - isBookmarked: isBookmarked, - controller: controller, - ), - child: const ArticleInfoPage( - key: ObjectKey('asdfasdf'), - ), - ); - return cache!; - }, - ); - }, - ); - }); + if (search.results.isEmpty) { + return; + } + + if (!context.mounted) return; + showArticleInfoRaw( + context: context, + queryResult: search.results.first, + ); +} + +Future showArticleInfoRaw({ + required BuildContext context, + required QueryResult queryResult, + List? usableTabList, + bool lockRead = false, +}) async { + final id = queryResult.id(); + final hasNoValidQuery = queryResult.result.keys.length == 1 && + queryResult.result.keys.lastOrNull == 'Id'; + + if (hasNoValidQuery) { + queryResult = await HentaiManager.idQueryWeb('$id'); + } + + late final VioletImageProvider provider; + if (ProviderManager.isExists(id)) { + provider = await ProviderManager.get(id); + } else { + provider = await HentaiManager.getImageProvider(queryResult); + await provider.init(); + ProviderManager.insert(id, provider); + } + + final thumbnail = await provider.getThumbnailUrl(); + final headers = await provider.getHeader(0); + + final isBookmarked = + await (await Bookmark.getInstance()).isBookmark(queryResult.id()); + + if (!context.mounted) return; + final height = MediaQuery.of(context).size.height; + // https://github.com/flutter/flutter/issues/67219 + Provider? cache; + showModalBottomSheet( + context: context, + isScrollControlled: true, + builder: (_) { + return DraggableScrollableSheet( + initialChildSize: 400 / height, + minChildSize: 400 / height, + maxChildSize: 0.9, + expand: false, + builder: (_, controller) { + cache ??= Provider.value( + value: ArticleInfo.fromArticleInfo( + queryResult: queryResult, + thumbnail: thumbnail, + headers: headers, + heroKey: 'zxcvzxcvzxcv', + isBookmarked: isBookmarked, + controller: controller, + usableTabList: usableTabList, + lockRead: lockRead, + ), + child: const ArticleInfoPage( + key: ObjectKey('asdfasdf'), + ), + ); + return cache!; + }, + ); + }, + ); } diff --git a/violet/lib/pages/download/download_item_widget.dart b/violet/lib/pages/download/download_item_widget.dart index d9ef620b4..b41fd75d8 100644 --- a/violet/lib/pages/download/download_item_widget.dart +++ b/violet/lib/pages/download/download_item_widget.dart @@ -363,7 +363,7 @@ class DownloadItemWidgetState extends State scale = 1.0; }); if (int.tryParse(widget.item.url()) != null) { - showArticleInfo(context, int.parse(widget.item.url())); + showArticleInfoById(context, int.parse(widget.item.url())); } }, ); diff --git a/violet/lib/pages/lab/lab/search_comment.dart b/violet/lib/pages/lab/lab/search_comment.dart index e5f72dd23..20d4990c2 100644 --- a/violet/lib/pages/lab/lab/search_comment.dart +++ b/violet/lib/pages/lab/lab/search_comment.dart @@ -50,7 +50,7 @@ class _LabSearchCommentsState extends State { return InkWell( onTap: () async { FocusScope.of(context).unfocus(); - showArticleInfo(context, e.$1); + showArticleInfoById(context, e.$1); }, onLongPress: () async { FocusScope.of(context).unfocus(); diff --git a/violet/lib/pages/lab/lab/search_comment_author.dart b/violet/lib/pages/lab/lab/search_comment_author.dart index 29f2c2a64..28cde4e7a 100644 --- a/violet/lib/pages/lab/lab/search_comment_author.dart +++ b/violet/lib/pages/lab/lab/search_comment_author.dart @@ -47,7 +47,7 @@ class _LabSearchCommentsAuthorState extends State { return InkWell( onTap: () async { FocusScope.of(context).unfocus(); - showArticleInfo(context, e.$1); + showArticleInfoById(context, e.$1); }, splashColor: Colors.white, child: ListTile( diff --git a/violet/lib/pages/lab/lab/search_message.dart b/violet/lib/pages/lab/lab/search_message.dart index ae555e689..df78748cf 100644 --- a/violet/lib/pages/lab/lab/search_message.dart +++ b/violet/lib/pages/lab/lab/search_message.dart @@ -169,7 +169,7 @@ class _LabSearchMessageState extends State { return InkWell( onTap: () async { FocusScope.of(context).unfocus(); - showArticleInfo(context, e.$2); + showArticleInfoById(context, e.$2); }, splashColor: Colors.white, child: Column( diff --git a/violet/lib/pages/viewer/overlay/viewer_overlay.dart b/violet/lib/pages/viewer/overlay/viewer_overlay.dart index 6cd4a1e2f..dcb267aec 100644 --- a/violet/lib/pages/viewer/overlay/viewer_overlay.dart +++ b/violet/lib/pages/viewer/overlay/viewer_overlay.dart @@ -22,6 +22,7 @@ import 'package:violet/locale/locale.dart' as locale; import 'package:violet/model/article_info.dart'; import 'package:violet/other/dialogs.dart'; import 'package:violet/pages/article_info/article_info_page.dart'; +import 'package:violet/pages/common/utils.dart'; import 'package:violet/pages/viewer/others/preload_page_view.dart'; import 'package:violet/pages/viewer/overlay/page_label.dart'; import 'package:violet/pages/viewer/overlay/viewer_record_panel.dart'; @@ -303,8 +304,6 @@ class _ViewerOverlayState extends State { icon: const Icon(MdiIcons.information), color: Colors.white, onPressed: () async { - final height = MediaQuery.of(context).size.height; - final search = await HentaiManager.idSearch(c.articleId.toString()); if (search.results.length != 1) return; @@ -316,47 +315,14 @@ class _ViewerOverlayState extends State { }); } - var prov = await ProviderManager.get(c.articleId); - var thumbnail = await prov.getThumbnailUrl(); - var headers = await prov.getHeader(0); - ProviderManager.insert(qr.id(), prov); - - var isBookmarked = - await (await Bookmark.getInstance()).isBookmark(qr.id()); - c.isStaring = false; c.stopTimer(); - if (!mounted) return; - Provider? cache; - showModalBottomSheet( + if (!context.mounted) return; + return showArticleInfoRaw( context: context, - isScrollControlled: true, - builder: (_) { - return DraggableScrollableSheet( - initialChildSize: 400 / height, - minChildSize: 400 / height, - maxChildSize: 0.9, - expand: false, - builder: (_, controller) { - cache ??= Provider.value( - value: ArticleInfo.fromArticleInfo( - queryResult: qr, - thumbnail: thumbnail, - headers: headers, - heroKey: 'zxcvzxcvzxcv', - isBookmarked: isBookmarked, - controller: controller, - lockRead: true, - ), - child: const ArticleInfoPage( - key: ObjectKey('asdfasdf'), - ), - ); - return cache!; - }, - ); - }, + queryResult: qr, + lockRead: true, ).then((value) { c.isStaring = true; c.startTimer(); diff --git a/violet/lib/pages/viewer/overlay/viewer_tab_panel.dart b/violet/lib/pages/viewer/overlay/viewer_tab_panel.dart index 9a1871de3..8af062271 100644 --- a/violet/lib/pages/viewer/overlay/viewer_tab_panel.dart +++ b/violet/lib/pages/viewer/overlay/viewer_tab_panel.dart @@ -16,6 +16,7 @@ import 'package:violet/database/user/record.dart'; import 'package:violet/model/article_info.dart'; import 'package:violet/model/article_list_item.dart'; import 'package:violet/pages/article_info/article_info_page.dart'; +import 'package:violet/pages/common/utils.dart'; import 'package:violet/pages/viewer/viewer_page.dart'; import 'package:violet/pages/viewer/viewer_page_provider.dart'; import 'package:violet/server/violet.dart'; @@ -409,44 +410,11 @@ class __ArtistsArticleTabListState extends State<_ArtistsArticleTabList> ); } - Future _showArticleInfo(QueryResult e) async { - var prov = await ProviderManager.get(e.id()); - var thumbnail = await prov.getThumbnailUrl(); - var headers = await prov.getHeader(0); - ProviderManager.insert(e.id(), prov); - - var isBookmarked = await (await Bookmark.getInstance()).isBookmark(e.id()); - - if (!mounted) return; - Provider? cache; - showModalBottomSheet( + Future _showArticleInfo(QueryResult queryResult) async { + return showArticleInfoRaw( context: context, - isScrollControlled: true, - builder: (_) { - return DraggableScrollableSheet( - initialChildSize: 400 / widget.height, - minChildSize: 400 / widget.height, - maxChildSize: 0.9, - expand: false, - builder: (_, controller) { - cache ??= Provider.value( - value: ArticleInfo.fromArticleInfo( - queryResult: e, - thumbnail: thumbnail, - headers: headers, - heroKey: 'zxcvzxcvzxcv', - isBookmarked: isBookmarked, - controller: controller, - usableTabList: articleList, - ), - child: const ArticleInfoPage( - key: ObjectKey('asdfasdf'), - ), - ); - return cache!; - }, - ); - }, + queryResult: queryResult, + usableTabList: articleList, ); } diff --git a/violet/lib/widgets/article_item/article_list_item_widget.dart b/violet/lib/widgets/article_item/article_list_item_widget.dart index d50dbb9d4..39e27136b 100644 --- a/violet/lib/widgets/article_item/article_list_item_widget.dart +++ b/violet/lib/widgets/article_item/article_list_item_widget.dart @@ -24,6 +24,7 @@ import 'package:violet/model/article_info.dart'; import 'package:violet/model/article_list_item.dart'; import 'package:violet/other/dialogs.dart'; import 'package:violet/pages/article_info/article_info_page.dart'; +import 'package:violet/pages/common/utils.dart'; import 'package:violet/pages/search/search_page.dart'; import 'package:violet/pages/viewer/viewer_page.dart'; import 'package:violet/pages/viewer/viewer_page_provider.dart'; @@ -241,69 +242,10 @@ class _ArticleListItemWidgetState extends State _animateScale(1.0); - _showArticleInfo(); - } - - Future _showArticleInfo() async { - /** - * It shows up details with [Download button, Read button] - */ - final height = MediaQuery.of(context).size.height; - - // https://github.com/flutter/flutter/issues/67219 - FutureBuilder? cache; - showModalBottomSheet( + showArticleInfoRaw( context: context, - isScrollControlled: true, - builder: (_) { - return DraggableScrollableSheet( - initialChildSize: 400 / height, - minChildSize: 400 / height, - maxChildSize: 0.9, - expand: false, - builder: (_, controller) { - cache ??= FutureBuilder( - /* - * This checks QueryResult then - * do idQueryWeb when QueryResult keys was only exists 'Id' - */ - future: (() { - if (data.queryResult.result.keys.length == 1 && - data.queryResult.result.keys.lastOrNull == 'Id') { - return HentaiManager.idQueryWeb('${data.queryResult.id()}'); - } else { - return Future.value(data.queryResult); - } - })(), - builder: (context, snapshot) { - createBody(queryResult) { - return Provider.value( - value: ArticleInfo.fromArticleInfo( - queryResult: queryResult, - thumbnail: c.thumbnail.value, - headers: c.headers, - heroKey: data.thumbnailTag, - isBookmarked: c.isBookmarked.value, - controller: controller, - usableTabList: data.usableTabList, - ), - child: const ArticleInfoPage( - key: ObjectKey('asdfasdf'), - ), - ); - } - - if (snapshot.hasData) { - return createBody(snapshot.data); - } else { - return createBody(data.queryResult); - } - }, - ); - return cache!; - }, - ); - }, + queryResult: data.queryResult, + usableTabList: data.usableTabList, ); } From 852badfeab76b3e554a7de0973ad9212057c5e1b Mon Sep 17 00:00:00 2001 From: violet-dev Date: Fri, 27 Dec 2024 14:37:24 +0900 Subject: [PATCH 2/2] Fix clippy --- violet/lib/pages/viewer/overlay/viewer_overlay.dart | 2 -- violet/lib/pages/viewer/overlay/viewer_tab_panel.dart | 3 --- violet/lib/widgets/article_item/article_list_item_widget.dart | 3 --- 3 files changed, 8 deletions(-) diff --git a/violet/lib/pages/viewer/overlay/viewer_overlay.dart b/violet/lib/pages/viewer/overlay/viewer_overlay.dart index dcb267aec..6ed0d8916 100644 --- a/violet/lib/pages/viewer/overlay/viewer_overlay.dart +++ b/violet/lib/pages/viewer/overlay/viewer_overlay.dart @@ -19,9 +19,7 @@ import 'package:violet/component/hitomi/tag_translate.dart'; import 'package:violet/database/query.dart'; import 'package:violet/database/user/bookmark.dart'; import 'package:violet/locale/locale.dart' as locale; -import 'package:violet/model/article_info.dart'; import 'package:violet/other/dialogs.dart'; -import 'package:violet/pages/article_info/article_info_page.dart'; import 'package:violet/pages/common/utils.dart'; import 'package:violet/pages/viewer/others/preload_page_view.dart'; import 'package:violet/pages/viewer/overlay/page_label.dart'; diff --git a/violet/lib/pages/viewer/overlay/viewer_tab_panel.dart b/violet/lib/pages/viewer/overlay/viewer_tab_panel.dart index 8af062271..6e05122ce 100644 --- a/violet/lib/pages/viewer/overlay/viewer_tab_panel.dart +++ b/violet/lib/pages/viewer/overlay/viewer_tab_panel.dart @@ -11,11 +11,8 @@ import 'package:violet/component/hentai.dart'; import 'package:violet/component/hitomi/hitomi.dart'; import 'package:violet/database/database.dart'; import 'package:violet/database/query.dart'; -import 'package:violet/database/user/bookmark.dart'; import 'package:violet/database/user/record.dart'; -import 'package:violet/model/article_info.dart'; import 'package:violet/model/article_list_item.dart'; -import 'package:violet/pages/article_info/article_info_page.dart'; import 'package:violet/pages/common/utils.dart'; import 'package:violet/pages/viewer/viewer_page.dart'; import 'package:violet/pages/viewer/viewer_page_provider.dart'; diff --git a/violet/lib/widgets/article_item/article_list_item_widget.dart b/violet/lib/widgets/article_item/article_list_item_widget.dart index 39e27136b..065d130bd 100644 --- a/violet/lib/widgets/article_item/article_list_item_widget.dart +++ b/violet/lib/widgets/article_item/article_list_item_widget.dart @@ -16,14 +16,11 @@ import 'package:uuid/uuid.dart'; import 'package:violet/component/hentai.dart'; import 'package:violet/component/hitomi/tag_translate.dart'; import 'package:violet/context/modal_bottom_sheet_context.dart'; -import 'package:violet/database/query.dart'; import 'package:violet/database/user/bookmark.dart'; import 'package:violet/database/user/record.dart'; import 'package:violet/locale/locale.dart' as locale; -import 'package:violet/model/article_info.dart'; import 'package:violet/model/article_list_item.dart'; import 'package:violet/other/dialogs.dart'; -import 'package:violet/pages/article_info/article_info_page.dart'; import 'package:violet/pages/common/utils.dart'; import 'package:violet/pages/search/search_page.dart'; import 'package:violet/pages/viewer/viewer_page.dart';