Skip to content

Commit

Permalink
add goGalleryByGid
Browse files Browse the repository at this point in the history
  • Loading branch information
honjow committed Oct 20, 2022
1 parent 4ffad4a commit 22bb9d2
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 62 deletions.
14 changes: 14 additions & 0 deletions lib/common/parser/parse_gallery_detail.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ Future<Gallery> parseGalleryDetail(String html) async {
document.querySelectorAll(selectorThumb);
logger.v('galleryThumbsElm ${galleryThumbsElm.length}');

String? mediaId;

final List<GalleryImage> galleryImagePages = [];
// for galleryThumbsElm
for (final elm in galleryThumbsElm) {
Expand All @@ -66,6 +68,10 @@ Future<Gallery> parseGalleryDetail(String html) async {
final imgHeight = elm.querySelector('img')?.attributes['height'];
final imgWidth = elm.querySelector('img')?.attributes['width'];

mediaId ??=
RegExp(r'/galleries/(\d+)/').firstMatch(thumbUrl ?? '')?.group(1);
// logger.d('mediaId: $mediaId');

// 扩展名
final ext = RegExp(r'\.(\w+)$').firstMatch(thumbUrl)?.group(1) ?? '';
final type = ext.substring(0, 1);
Expand All @@ -79,6 +85,12 @@ Future<Gallery> parseGalleryDetail(String html) async {
));
}

final thumbImage = GalleryImage(
type: galleryImagePages.first.type,
imgHeight: galleryImagePages.first.imgHeight,
imgWidth: galleryImagePages.first.imgWidth,
);

final tuple = parseGalleryTags(document);
final tags = tuple.item1;
final uploadedDateTime = tuple.item2;
Expand All @@ -105,7 +117,9 @@ Future<Gallery> parseGalleryDetail(String html) async {
),
images: GalleryImages(
pages: galleryImagePages,
thumbnail: thumbImage,
),
mediaId: mediaId,
isFavorited: isFav,
numFavorites: int.tryParse(favNum) ?? 0,
moreLikeGallerys: moreLikeGalleryList,
Expand Down
10 changes: 6 additions & 4 deletions lib/component/models/gallery.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ class Gallery with _$Gallery {
DateTime.tryParse(uploadedDateTime ?? '')?.toLocal();

@JsonKey(ignore: true)
String get thumbUrl =>
'https://t.nhentai.net/galleries/$mediaId/thumb.${NHConst.extMap[images.thumbnail.type]}';
String? get thumbUrl => mediaId != null
? 'https://t.nhentai.net/galleries/$mediaId/thumb.${NHConst.extMap[images.thumbnail.type]}'
: null;
@JsonKey(ignore: true)
String get coverUrl =>
'https://t.nhentai.net/galleries/$mediaId/cover.${NHConst.extMap[images.cover.type]}';
String? get coverUrl => mediaId != null
? 'https://t.nhentai.net/galleries/$mediaId/cover.${NHConst.extMap[images.cover.type]}'
: null;
@JsonKey(ignore: true)
String get url => 'https://nhentai.net/g/$gid/';
}
1 change: 1 addition & 0 deletions lib/network/request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ Future<List<Comment>> getGalleryComments({
DioHttpClient dioHttpClient = DioHttpClient(dioConfig: globalDioConfig);

final url = '/api/gallery/$gid/comments';
logger.d('url $url');

DioHttpResponse httpResponse = await dioHttpClient.get(
url,
Expand Down
24 changes: 17 additions & 7 deletions lib/pages/gallery/gallery_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,36 +35,45 @@ class GalleryNotifier extends StateNotifier<Gallery> {

loadData();
500.milliseconds.delay(() {
ref.read(historyProvider.notifier).addHistory(gallery);
ref.read(historyProvider.notifier).addHistory(state);
});
}

void initFromGid(int gid) {
logger.d('init $gid');

state = state.copyWith(
gid: gid,
);

loadData()
.then((value) => ref.read(historyProvider.notifier).addHistory(state));
}

void setInitialPage(int page) {
state = state.copyWith(currentPageIndex: page);
}

/// 加载数据
Future<void> loadData({bool refresh = false}) async {
logger.v('loadData refresh $refresh url: ${state.url}');
logger.d('loadData refresh $refresh url: ${state.url}');
if (state.images.pages.isEmpty) {
ref
.read(pageStateProvider(state.gid).notifier)
.update((state) => state.copyWith(pageStatus: PageStatus.loading));
}

logger.d('url ${state.url}');
// 获取画廊数据
try {
final gallery = await getGalleryDetail(
url: state.url,
refresh: refresh,
);
state = gallery.copyWith(
images: gallery.images.copyWith(
thumbnail: state.images.thumbnail,
),
gid: state.gid,
mediaId: state.mediaId,
mediaId: gallery.mediaId ?? state.mediaId,
currentPageIndex: state.currentPageIndex,
gid: state.gid,
);
} on HttpException catch (e) {
rethrow;
Expand All @@ -75,6 +84,7 @@ class GalleryNotifier extends StateNotifier<Gallery> {
}

// 获取评论数据
logger.d('url ${state.url}');
try {
final comments = await getGalleryComments(
gid: state.gid,
Expand Down
101 changes: 59 additions & 42 deletions lib/pages/gallery/gallery_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ class _GalleryPageState extends ConsumerState<GalleryPage> {
final thumbUrl =
ref.watch(galleryProvider(widget.gid).select((g) => g.thumbUrl));
if (useGalleryTint) {
final paletteGenerator = ref.watch(paletteGeneratorProvider(thumbUrl));
final paletteGenerator =
ref.watch(paletteGeneratorProvider(thumbUrl ?? ''));
paletteGenerator.whenData((palette) {
// logger.d(palette);
final seedColor = getSeedColors(palette);
Expand All @@ -87,7 +88,7 @@ class _GalleryPageState extends ConsumerState<GalleryPage> {
}
final hslColor = HSLColor.fromColor(seedColor);
final hsvColor = HSVColor.fromColor(seedColor);
logger.d('$seedColor, $hslColor, $hsvColor');
logger.v('$seedColor, $hslColor, $hsvColor');
if (hsvColor.hue < 0.1) {
return;
}
Expand Down Expand Up @@ -132,8 +133,14 @@ class GalleryPageBody extends HookConsumerWidget {

@override
Widget build(BuildContext context, WidgetRef ref) {
final gallery = ref.read(galleryProvider(gid));
logger.v('build gallery $gid ${gallery.title}');
// final gallery = ref.read(galleryProvider(gid));
// logger.v('build gallery $gid ${gallery.title}');

final thumbUrl = ref.watch(galleryProvider(gid).select((g) => g.thumbUrl));
final title = ref.watch(galleryProvider(gid).select((g) => g.title));
final url = ref.watch(galleryProvider(gid).select((g) => g.url));
final images = ref.watch(galleryProvider(gid).select((g) => g.images));
final mediaId = ref.watch(galleryProvider(gid).select((g) => g.mediaId));

final ScrollController scrollController = useScrollController();

Expand All @@ -156,14 +163,15 @@ class GalleryPageBody extends HookConsumerWidget {
blendMode: BlendMode.dstOut,
child: ClipRect(
child: BlurImage(
// sigma: context.isTablet ? 4 : 4,
sigma: 4,
color: Theme.of(context).canvasColor.withOpacity(0.5),
child: ErosCachedNetworkImage(
imageUrl: gallery.thumbUrl,
filterQuality: FilterQuality.medium,
fit: BoxFit.cover,
),
child: thumbUrl != null
? ErosCachedNetworkImage(
imageUrl: thumbUrl,
filterQuality: FilterQuality.medium,
fit: BoxFit.cover,
)
: const SizedBox(),
),
),
);
Expand All @@ -187,7 +195,7 @@ class GalleryPageBody extends HookConsumerWidget {
icon: const Icon(Icons.share),
onPressed: () {
final shareText =
'${gallery.title} ${NHConst.baseUrl}${gallery.url}';
'${title.englishTitle} ${NHConst.baseUrl}${url}';
logger.d(shareText);
Share.share(shareText);
},
Expand Down Expand Up @@ -239,7 +247,7 @@ class GalleryPageBody extends HookConsumerWidget {
mainAxisSize: MainAxisSize.min,
children: [
SelectableText(
gallery.title.englishTitle ?? '',
title.englishTitle ?? '',
style: Theme.of(context).textTheme.titleLarge,
maxLines: context.isTablet ? 2 : 3,
minLines: 1,
Expand All @@ -256,27 +264,28 @@ class GalleryPageBody extends HookConsumerWidget {
margin: const EdgeInsets.only(right: 12),
alignment: Alignment.center,
child: Hero(
tag: '${heroTag ?? ''}_${gallery.thumbUrl}',
tag: '${heroTag ?? ''}_$thumbUrl',
child: Card(
margin: const EdgeInsets.all(0),
clipBehavior: Clip.antiAlias,
child: AspectRatio(
aspectRatio: (gallery.images.thumbnail
.imgWidth ??
300) /
(gallery.images.thumbnail
.imgHeight ??
400),
child: ErosCachedNetworkImage(
imageUrl: gallery.thumbUrl,
fit: BoxFit.cover,
),
aspectRatio:
(images.thumbnail.imgWidth ?? 300) /
(images.thumbnail.imgHeight ??
400),
child: thumbUrl == null
? nil
: ErosCachedNetworkImage(
imageUrl: thumbUrl,
fit: BoxFit.cover,
),
),
),
),
),
Expanded(
child: buildGalleryInfo(gallery, context),
child:
HeadInfoView(gid: gid, context: context),
),
],
),
Expand All @@ -296,8 +305,20 @@ class GalleryPageBody extends HookConsumerWidget {
),
);
}
}

Widget buildGalleryInfo(Gallery gallery, BuildContext context) {
class HeadInfoView extends StatelessWidget {
const HeadInfoView({
super.key,
required this.gid,
required this.context,
});

final int gid;
final BuildContext context;

@override
Widget build(BuildContext context) {
return Container(
padding: const EdgeInsets.only(right: 12),
child: Column(
Expand All @@ -320,14 +341,11 @@ class GalleryPageBody extends HookConsumerWidget {
),
const SizedBox(height: 8),
Wrap(
// mainAxisSize: MainAxisSize.min,
// mainAxisAlignment: MainAxisAlignment.start,
// crossAxisAlignment: CrossAxisAlignment.center,
spacing: 12,
runSpacing: 2,
children: [
Text(
'#${gallery.gid}',
'#$gid',
style: Theme.of(context).textTheme.bodySmall,
textAlign: TextAlign.start,
),
Expand Down Expand Up @@ -409,7 +427,8 @@ class GalleryPageBody extends HookConsumerWidget {
final artistTagsWidgetsWithSeparator = artistTagsWidgets
.expand((element) => [
element,
Text('/', style: Theme.of(context).textTheme.caption)
Text('/',
style: Theme.of(context).textTheme.bodySmall)
])
.toList();

Expand Down Expand Up @@ -484,7 +503,8 @@ class PaletteGeneratorWidget extends HookConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
final thumbUrl = ref.watch(galleryProvider(gid).select((g) => g.thumbUrl));
final paletteGenerator = ref.watch(paletteGeneratorProvider(thumbUrl));
final paletteGenerator =
ref.watch(paletteGeneratorProvider(thumbUrl ?? ''));
return MultiSliver(children: [
ListTile(
title: const Text('PaletteGenerator (debug)'),
Expand Down Expand Up @@ -791,11 +811,6 @@ class ThumbListView extends HookConsumerWidget {
gid: gid,
colorScheme: Theme.of(context).colorScheme,
));
// Navigator.of(context).push(
// MaterialPageRoute(
// builder: (context) => ThumbPage(gid: gid),
// ),
// );
},
),
SizedBox(
Expand All @@ -819,11 +834,13 @@ class ThumbListView extends HookConsumerWidget {
clipBehavior: Clip.antiAlias,
child: Hero(
tag: '${gid}_$index',
child: ErosCachedNetworkImage(
imageUrl:
'https://t.nhentai.net/galleries/$mediaId/${index + 1}t.${NHConst.extMap[image.type]}',
fit: BoxFit.cover,
),
child: mediaId == null
? nil
: ErosCachedNetworkImage(
imageUrl:
'https://t.nhentai.net/galleries/$mediaId/${index + 1}t.${NHConst.extMap[image.type]}',
fit: BoxFit.cover,
),
),
),
),
Expand Down Expand Up @@ -907,7 +924,7 @@ class MoreLikeListView extends HookConsumerWidget {
),
),
child: ErosCachedNetworkImage(
imageUrl: likeGallery.thumbUrl,
imageUrl: likeGallery.thumbUrl ?? '',
fit: BoxFit.cover,
),
),
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/list_view/item/item_grid_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class ItemGridCard extends HookConsumerWidget {
child: Hero(
tag: '${tabTag ?? ''}_${gallery.thumbUrl}',
child: ErosCachedNetworkImage(
imageUrl: gallery.thumbUrl,
imageUrl: gallery.thumbUrl ?? '',
height: gallery.images.thumbnail.imgHeight?.toDouble(),
width: gallery.images.thumbnail.imgWidth?.toDouble(),
fit: isBoxFitCover ? BoxFit.cover : BoxFit.contain,
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/list_view/item/item_list_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class ItemListCard extends HookConsumerWidget {
child: Hero(
tag: '${tabTag ?? ''}_${gallery.thumbUrl}',
child: ErosCachedNetworkImage(
imageUrl: gallery.thumbUrl,
imageUrl: gallery.thumbUrl ?? '',
height: gallery.images.thumbnail.imgHeight?.toDouble(),
width: gallery.images.thumbnail.imgWidth?.toDouble(),
fit: isBoxFitCover ? BoxFit.cover : BoxFit.contain,
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/list_view/item/item_waterfall_flow_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class ItemWaterfallFlowCard extends HookConsumerWidget {
),
),
child: CoverImg(
imgUrl: gallery.thumbUrl,
imgUrl: gallery.thumbUrl ?? '',
fit: BoxFit.contain,
),
);
Expand Down
13 changes: 7 additions & 6 deletions lib/pages/nav/front/front_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ class PopularListView extends ConsumerWidget {
},
blendMode: BlendMode.darken,
child: CoverImg(
imgUrl: gallery.thumbUrl,
imgUrl: gallery.thumbUrl ?? '',
fit: BoxFit.cover,
),
),
Expand Down Expand Up @@ -262,11 +262,12 @@ class PopularListView extends ConsumerWidget {
return GestureDetector(
onTap: () {
// popular
RouteUtil.goGallery(
ref,
gallery,
heroTag: '${NHRoutes.front}_popular',
);
// RouteUtil.goGallery(
// ref,
// gallery,
// heroTag: '${NHRoutes.front}_popular',
// );
RouteUtil.goGalleryByGid(ref, gallery.gid);
},
child: card,
);
Expand Down
Loading

0 comments on commit 22bb9d2

Please sign in to comment.