pikapika/lib/screens/PkzComicInfoScreen.dart

266 lines
7.7 KiB
Dart
Raw Normal View History

2022-06-30 15:38:42 +00:00
import 'dart:async';
import 'dart:io';
2022-06-29 19:02:01 +00:00
import 'package:flutter/material.dart';
import 'package:path/path.dart' as p;
import 'package:pikapika/basic/Entities.dart';
import 'package:pikapika/basic/Method.dart';
import 'package:pikapika/screens/PkzReaderScreen.dart';
2022-06-30 16:31:15 +00:00
import 'package:uni_links/uni_links.dart';
2022-06-30 15:38:42 +00:00
import 'package:uri_to_file/uri_to_file.dart';
2022-06-29 19:02:01 +00:00
2022-07-01 05:39:08 +00:00
import '../basic/Common.dart';
2022-06-29 19:02:01 +00:00
import '../basic/Navigator.dart';
2023-01-31 10:50:51 +00:00
import '../basic/config/IconLoading.dart';
2022-06-30 15:38:42 +00:00
import 'PkzArchiveScreen.dart';
2022-06-29 19:02:01 +00:00
import 'components/PkzComicInfoCard.dart';
class PkzComicInfoScreen extends StatefulWidget {
2022-06-30 15:38:42 +00:00
final bool holdPkz;
2022-06-29 19:02:01 +00:00
final String pkzPath;
final PkzComic pkzComic;
2022-06-30 15:38:42 +00:00
const PkzComicInfoScreen({
Key? key,
required this.pkzPath,
required this.pkzComic,
this.holdPkz = false,
}) : super(key: key);
2022-06-29 19:02:01 +00:00
@override
State<StatefulWidget> createState() => _PkzComicInfoScreenState();
}
class _PkzComicInfoScreenState extends State<PkzComicInfoScreen>
with RouteAware {
PkzComicViewLog? _log;
2022-06-30 16:31:15 +00:00
StreamSubscription<String?>? _linkSubscription;
2022-06-29 19:02:01 +00:00
@override
void initState() {
2022-06-30 15:38:42 +00:00
if (widget.holdPkz) {
2022-07-01 05:39:08 +00:00
_linkSubscription = linkSubscript(context);
2022-06-30 15:38:42 +00:00
}
2022-06-29 19:02:01 +00:00
_load();
super.initState();
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
routeObserver.subscribe(this, ModalRoute.of(context)!);
}
@override
void dispose() {
2022-06-30 15:38:42 +00:00
_linkSubscription?.cancel();
2022-06-29 19:02:01 +00:00
routeObserver.unsubscribe(this);
super.dispose();
}
@override
void didPopNext() {
() async {
_log = await method.pkzComicViewLogByPkzNameAndId(
p.basename(widget.pkzPath),
widget.pkzComic.id,
);
setState(() {});
}();
}
_load() async {
await method.viewPkzComic(
p.basename(widget.pkzPath),
widget.pkzPath,
widget.pkzComic.id,
widget.pkzComic.title,
);
_log = await method.pkzComicViewLogByPkzNameAndId(
p.basename(widget.pkzPath),
widget.pkzComic.id,
);
setState(() {});
}
@override
Widget build(BuildContext context) {
List<Widget> chapterButtons = [];
for (var volume in widget.pkzComic.volumes) {
for (var chapter in volume.chapters) {
chapterButtons.add(MaterialButton(
onPressed: () {
2023-01-31 10:50:51 +00:00
Navigator.of(context).push(mixRoute(
2022-06-29 19:02:01 +00:00
builder: (BuildContext context) {
return PkzReaderScreen(
comicInfo: widget.pkzComic,
currentEpId: chapter.id,
pkzPath: widget.pkzPath,
);
},
));
},
color: Colors.white,
child: Text(
chapter.title,
style: const TextStyle(color: Colors.black),
),
));
}
}
final theme = Theme.of(context);
return Scaffold(
appBar: AppBar(
title: Text(
widget.pkzComic.title,
),
),
body: ListView(children: [
PkzComicInfoCard(info: widget.pkzComic, pkzPath: widget.pkzPath),
Container(
padding: const EdgeInsets.only(top: 5, bottom: 5),
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(
color: theme.dividerColor,
),
),
),
child: Wrap(
children: widget.pkzComic.tags.map((e) {
return Container(
padding: const EdgeInsets.only(
left: 10,
right: 10,
top: 3,
bottom: 3,
),
margin: const EdgeInsets.only(
left: 5,
right: 5,
top: 3,
bottom: 3,
),
decoration: BoxDecoration(
color: Colors.pink.shade100,
border: Border.all(
style: BorderStyle.solid,
color: Colors.pink.shade400,
),
borderRadius: const BorderRadius.all(Radius.circular(30)),
),
child: Text(
e,
style: TextStyle(
color: Colors.pink.shade500,
height: 1.4,
),
strutStyle: const StrutStyle(
height: 1.4,
),
),
);
}).toList(),
),
),
Container(
padding: const EdgeInsets.only(
top: 5,
bottom: 5,
left: 10,
right: 10,
),
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(
color: theme.dividerColor,
),
),
),
child: SelectableText(
widget.pkzComic.description,
style: const TextStyle(
fontSize: 13,
color: Colors.grey,
),
),
),
LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
PkzChapter? first;
Map<String, PkzChapter> chapters = {};
for (var vol in widget.pkzComic.volumes) {
for (var c in vol.chapters) {
first ??= c;
chapters[c.id] = (c);
}
}
if (chapters.isEmpty) {
return Container();
}
final width = constraints.maxWidth;
return Container(
padding: const EdgeInsets.only(left: 10, right: 10),
2022-06-30 11:16:10 +00:00
margin: const EdgeInsets.only(top: 10, bottom: 10),
2022-06-29 19:02:01 +00:00
width: width,
child: MaterialButton(
onPressed: () {
if (chapters.containsKey(_log?.lastViewEpId)) {
2023-01-31 10:50:51 +00:00
Navigator.of(context).push(mixRoute(
2022-06-29 19:02:01 +00:00
builder: (BuildContext context) {
return PkzReaderScreen(
comicInfo: widget.pkzComic,
currentEpId: _log!.lastViewEpId,
pkzPath: widget.pkzPath,
initPicturePosition: _log!.lastViewPictureRank,
);
},
));
return;
}
2023-01-31 10:50:51 +00:00
Navigator.of(context).push(mixRoute(
2022-06-29 19:02:01 +00:00
builder: (BuildContext context) {
return PkzReaderScreen(
comicInfo: widget.pkzComic,
currentEpId: first!.id,
pkzPath: widget.pkzPath,
);
},
));
},
child: Row(
children: [
Expanded(
child: Container(
color: Theme.of(context)
.textTheme
.bodyText1!
.color!
.withOpacity(.05),
padding: const EdgeInsets.all(10),
child: Text(
chapters.containsKey(_log?.lastViewEpId)
? "继续阅读 ${chapters[_log?.lastViewEpId]!.title}"
: "开始阅读",
textAlign: TextAlign.center,
),
),
)
],
),
),
);
},
),
Wrap(
spacing: 10,
runSpacing: 10,
alignment: WrapAlignment.spaceAround,
children: chapterButtons,
),
]),
);
}
}