Skip to content

Commit

Permalink
Extract showArticleInfo method
Browse files Browse the repository at this point in the history
  • Loading branch information
violet-dev committed Dec 31, 2023
1 parent 9e07395 commit 3e9ad95
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 245 deletions.
51 changes: 2 additions & 49 deletions lib/pages/article_info/article_info_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import 'package:violet/other/dialogs.dart';
import 'package:violet/pages/article_info/simple_info.dart';
import 'package:violet/pages/artist_info/article_list_page.dart';
import 'package:violet/pages/artist_info/artist_info_page.dart';
import 'package:violet/pages/common/utils.dart';
import 'package:violet/pages/download/download_page.dart';
import 'package:violet/pages/main/info/lab/search_comment_author.dart';
import 'package:violet/pages/segment/platform_navigator.dart';
Expand Down Expand Up @@ -854,60 +855,12 @@ class __InfoAreaWidgetState extends State<_InfoAreaWidget> {
if (ehPattern.stringMatch(url) == url) {
var match = ehPattern.allMatches(url);
var id = match.first.namedGroup('id')!.trim();
_showArticleInfo(int.parse(id));
showArticleInfo(context, int.parse(id));
} else if (await canLaunchUrlString(url)) {
await launchUrlString(url);
}
}

void _showArticleInfo(int id) async {
final height = MediaQuery.of(context).size.height;

final search = await HentaiManager.idSearch(id.toString());
if (search.results.length != 1) return;

final qr = search.results.first;

HentaiManager.getImageProvider(qr).then((value) async {
var thumbnail = await value.getThumbnailUrl();
var headers = await value.getHeader(0);
ProviderManager.insert(qr.id(), value);

var isBookmarked =
await (await Bookmark.getInstance()).isBookmark(qr.id());

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!;
},
);
},
);
});
}

List<InlineSpan> linkify(String text) {
final List<InlineSpan> list = <InlineSpan>[];
final RegExpMatch? match =
Expand Down
59 changes: 59 additions & 0 deletions lib/pages/common/utils.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// This source code is a part of Project Violet.
// Copyright (C) 2020-2023. violet-team. Licensed under the Apache-2.0 License.

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:violet/component/hentai.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;

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());

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!;
},
);
},
);
});
}
51 changes: 2 additions & 49 deletions lib/pages/download/download_item_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import 'package:violet/database/user/record.dart';
import 'package:violet/locale/locale.dart';
import 'package:violet/model/article_info.dart';
import 'package:violet/pages/article_info/article_info_page.dart';
import 'package:violet/pages/common/utils.dart';
import 'package:violet/pages/download/download_item_menu.dart';
import 'package:violet/pages/download/download_routine.dart';
import 'package:violet/pages/viewer/viewer_page.dart';
Expand Down Expand Up @@ -385,60 +386,12 @@ class DownloadItemWidgetState extends State<DownloadItemWidget>
scale = 1.0;
});
if (int.tryParse(widget.item.url()) != null) {
_showArticleInfo(int.parse(widget.item.url()));
showArticleInfo(context, int.parse(widget.item.url()));
}
},
);
}

void _showArticleInfo(int id) async {
final height = MediaQuery.of(context).size.height;

final search = await HentaiManager.idSearch(id.toString());
if (search.results.length != 1) return;

final qr = search.results.first;

HentaiManager.getImageProvider(qr).then((value) async {
var thumbnail = await value.getThumbnailUrl();
var headers = await value.getHeader(0);
ProviderManager.insert(qr.id(), value);

var isBookmarked =
await (await Bookmark.getInstance()).isBookmark(qr.id());

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!;
},
);
},
);
});
}

_retry() {
// Retry
var copy = Map<String, dynamic>.from(widget.item.result);
Expand Down
51 changes: 2 additions & 49 deletions lib/pages/main/info/lab/search_comment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:violet/component/hentai.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/pages/common/utils.dart';
import 'package:violet/pages/main/info/lab/search_comment_author.dart';
import 'package:violet/pages/segment/card_panel.dart';
import 'package:violet/pages/segment/platform_navigator.dart';
Expand Down Expand Up @@ -57,7 +58,7 @@ class _LabSearchCommentsState extends State<LabSearchComments> {
return InkWell(
onTap: () async {
FocusScope.of(context).unfocus();
_showArticleInfo(e.item1);
showArticleInfo(context, e.item1);
},
onLongPress: () async {
FocusScope.of(context).unfocus();
Expand Down Expand Up @@ -107,52 +108,4 @@ class _LabSearchCommentsState extends State<LabSearchComments> {
_navigate(Widget page) {
PlatformNavigator.navigateSlide(context, page);
}

void _showArticleInfo(int id) async {
final height = MediaQuery.of(context).size.height;

final search = await HentaiManager.idSearch(id.toString());
if (search.results.length != 1) return;

final qr = search.results.first;

HentaiManager.getImageProvider(qr).then((value) async {
var thumbnail = await value.getThumbnailUrl();
var headers = await value.getHeader(0);
ProviderManager.insert(qr.id(), value);

var isBookmarked =
await (await Bookmark.getInstance()).isBookmark(qr.id());

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!;
},
);
},
);
});
}
}
51 changes: 2 additions & 49 deletions lib/pages/main/info/lab/search_comment_author.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:violet/component/hentai.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/pages/common/utils.dart';
import 'package:violet/pages/segment/card_panel.dart';
import 'package:violet/server/violet.dart';
import 'package:violet/widgets/article_item/image_provider_manager.dart';
Expand Down Expand Up @@ -54,7 +55,7 @@ class _LabSearchCommentsAuthorState extends State<LabSearchCommentsAuthor> {
return InkWell(
onTap: () async {
FocusScope.of(context).unfocus();
_showArticleInfo(e.item1);
showArticleInfo(context, e.item1);
},
splashColor: Colors.white,
child: ListTile(
Expand Down Expand Up @@ -105,52 +106,4 @@ class _LabSearchCommentsAuthorState extends State<LabSearchCommentsAuthor> {
// Navigator.of(context).push(CupertinoPageRoute(builder: (_) => page));
// }
// }

void _showArticleInfo(int id) async {
final height = MediaQuery.of(context).size.height;

final search = await HentaiManager.idSearch(id.toString());
if (search.results.length != 1) return;

final qr = search.results.first;

HentaiManager.getImageProvider(qr).then((value) async {
var thumbnail = await value.getThumbnailUrl();
var headers = await value.getHeader(0);
ProviderManager.insert(qr.id(), value);

var isBookmarked =
await (await Bookmark.getInstance()).isBookmark(qr.id());

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!;
},
);
},
);
});
}
}
Loading

0 comments on commit 3e9ad95

Please sign in to comment.