Skip to content

Commit

Permalink
Merge pull request #594 from project-violet/rf-8
Browse files Browse the repository at this point in the history
Refactor 012: Dedup showArticleInfo
  • Loading branch information
violet-dev authored Dec 27, 2024
2 parents 1fb7fed + 852badf commit 261942d
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 199 deletions.
2 changes: 1 addition & 1 deletion violet/lib/pages/after_loading/afterloading_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class AfterLoadingPageState extends State<AfterLoadingPage>
}

if (int.tryParse(uri.host) != null) {
showArticleInfo(context, int.parse(uri.host));
showArticleInfoById(context, int.parse(uri.host));
}
}

Expand Down
2 changes: 1 addition & 1 deletion violet/lib/pages/article_info/article_info_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion violet/lib/pages/bookmark/crop_bookmark.dart
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ class _CropBookmarkPageState extends State<CropBookmarkPage> {
color: Colors.transparent,
child: InkWell(
onTap: () async {
showArticleInfo(context, articleId);
showArticleInfoById(context, articleId);
},
onDoubleTap: () async {
_showViewer(articleId, page);
Expand Down
123 changes: 77 additions & 46 deletions violet/lib/pages/common/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<ArticleInfo>? cache;
showModalBottomSheet(
context: context,
isScrollControlled: true,
builder: (_) {
return DraggableScrollableSheet(
initialChildSize: 400 / height,
minChildSize: 400 / height,
maxChildSize: 0.9,
expand: false,
builder: (_, controller) {
cache ??= Provider<ArticleInfo>.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<QueryResult>? 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<ArticleInfo>? cache;
showModalBottomSheet(
context: context,
isScrollControlled: true,
builder: (_) {
return DraggableScrollableSheet(
initialChildSize: 400 / height,
minChildSize: 400 / height,
maxChildSize: 0.9,
expand: false,
builder: (_, controller) {
cache ??= Provider<ArticleInfo>.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!;
},
);
},
);
}
2 changes: 1 addition & 1 deletion violet/lib/pages/download/download_item_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ class DownloadItemWidgetState extends State<DownloadItemWidget>
scale = 1.0;
});
if (int.tryParse(widget.item.url()) != null) {
showArticleInfo(context, int.parse(widget.item.url()));
showArticleInfoById(context, int.parse(widget.item.url()));
}
},
);
Expand Down
2 changes: 1 addition & 1 deletion violet/lib/pages/lab/lab/search_comment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class _LabSearchCommentsState extends State<LabSearchComments> {
return InkWell(
onTap: () async {
FocusScope.of(context).unfocus();
showArticleInfo(context, e.$1);
showArticleInfoById(context, e.$1);
},
onLongPress: () async {
FocusScope.of(context).unfocus();
Expand Down
2 changes: 1 addition & 1 deletion violet/lib/pages/lab/lab/search_comment_author.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class _LabSearchCommentsAuthorState extends State<LabSearchCommentsAuthor> {
return InkWell(
onTap: () async {
FocusScope.of(context).unfocus();
showArticleInfo(context, e.$1);
showArticleInfoById(context, e.$1);
},
splashColor: Colors.white,
child: ListTile(
Expand Down
2 changes: 1 addition & 1 deletion violet/lib/pages/lab/lab/search_message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class _LabSearchMessageState extends State<LabSearchMessage> {
return InkWell(
onTap: () async {
FocusScope.of(context).unfocus();
showArticleInfo(context, e.$2);
showArticleInfoById(context, e.$2);
},
splashColor: Colors.white,
child: Column(
Expand Down
46 changes: 5 additions & 41 deletions violet/lib/pages/viewer/overlay/viewer_overlay.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ 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';
import 'package:violet/pages/viewer/overlay/viewer_record_panel.dart';
Expand Down Expand Up @@ -303,8 +302,6 @@ class _ViewerOverlayState extends State<ViewerOverlay> {
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;

Expand All @@ -316,47 +313,14 @@ class _ViewerOverlayState extends State<ViewerOverlay> {
});
}

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<ArticleInfo>? 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<ArticleInfo>.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();
Expand Down
45 changes: 5 additions & 40 deletions violet/lib/pages/viewer/overlay/viewer_tab_panel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ 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';
import 'package:violet/server/violet.dart';
Expand Down Expand Up @@ -409,44 +407,11 @@ class __ArtistsArticleTabListState extends State<_ArtistsArticleTabList>
);
}

Future<void> _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<ArticleInfo>? cache;
showModalBottomSheet(
Future<void> _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<ArticleInfo>.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,
);
}

Expand Down
Loading

0 comments on commit 261942d

Please sign in to comment.