pikapika/lib/screens/PkzArchiveScreen.dart

139 lines
3.8 KiB
Dart
Raw Permalink Normal View History

2022-06-30 15:38:42 +00:00
import 'dart:async';
2022-06-29 19:02:01 +00:00
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:path/path.dart' as p;
import 'package:permission_handler/permission_handler.dart';
import 'package:pikapika/basic/Entities.dart';
import 'package:pikapika/basic/Method.dart';
import 'package:pikapika/screens/components/ContentBuilder.dart';
import 'package:pikapika/screens/components/PkzComicInfoCard.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';
import 'PkzComicInfoScreen.dart';
class PkzArchiveScreen extends StatefulWidget {
2022-06-30 15:38:42 +00:00
final bool holdPkz;
2022-06-29 19:02:01 +00:00
final String pkzPath;
2022-06-30 15:38:42 +00:00
const PkzArchiveScreen({
Key? key,
required this.pkzPath,
this.holdPkz = false,
}) : super(key: key);
2022-06-29 19:02:01 +00:00
@override
State<StatefulWidget> createState() => _PkzArchiveScreenState();
}
class _PkzArchiveScreenState extends State<PkzArchiveScreen> with RouteAware {
Map<String, PkzComicViewLog> _logMap = {};
late String _fileName;
late Future _future;
late PkzArchive _info;
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
_fileName = p.basename(widget.pkzPath);
_future = _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 {
var a = await method.pkzComicViewLogs(_fileName, widget.pkzPath);
for (var value in a) {
_logMap[value.lastViewComicId] = value;
}
setState(() {});
}();
}
Future _load() async {
await method.viewPkz(_fileName, widget.pkzPath);
var p = await Permission.storage.request();
if (!p.isGranted) {
throw 'error permission';
}
_info = await method.pkzInfo(widget.pkzPath);
if (_info.comics.length == 1) {
Navigator.of(context).pushReplacement(MaterialPageRoute(
builder: (BuildContext context) => PkzComicInfoScreen(
pkzPath: widget.pkzPath,
pkzComic: _info.comics.first,
2022-06-30 16:31:15 +00:00
holdPkz: widget.holdPkz,
2022-06-29 19:02:01 +00:00
),
));
}
var a = await method.pkzComicViewLogs(_fileName, widget.pkzPath);
for (var value in a) {
_logMap[value.lastViewComicId] = value;
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(_fileName),
),
body: ContentBuilder(
future: _future,
onRefresh: () async {
setState(() {
_future = _load();
});
},
successBuilder: (
BuildContext context,
AsyncSnapshot snapshot,
) {
return ListView(children: [
..._info.comics
.map((e) => GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (BuildContext context) {
return PkzComicInfoScreen(
pkzComic: e,
pkzPath: widget.pkzPath,
);
},
));
},
child: PkzComicInfoCard(
info: e,
pkzPath: widget.pkzPath,
displayViewLog: _logMap[e.id],
),
))
.toList(),
]);
},
),
);
}
}