pikapika/lib/screens/DownloadConfirmScreen.dart

232 lines
6.7 KiB
Dart
Raw Permalink Normal View History

2021-09-29 23:57:09 +00:00
import 'dart:convert';
import 'package:flutter/material.dart';
2021-11-11 03:00:38 +00:00
import 'package:pikapika/basic/Common.dart';
import 'package:pikapika/basic/Entities.dart';
import 'package:pikapika/screens/components/ContentLoading.dart';
import 'package:pikapika/basic/Method.dart';
2021-09-29 23:57:09 +00:00
import 'components/ComicInfoCard.dart';
2022-03-19 04:12:27 +00:00
import 'components/RightClickPop.dart';
2021-09-29 23:57:09 +00:00
// 确认下载
class DownloadConfirmScreen extends StatefulWidget {
final ComicInfo comicInfo;
final List<Ep> epList;
const DownloadConfirmScreen({
Key? key,
required this.comicInfo,
required this.epList,
}) : super(key: key);
@override
State<StatefulWidget> createState() => _DownloadConfirmScreenState();
}
class _DownloadConfirmScreenState extends State<DownloadConfirmScreen> {
DownloadComic? _task; // 之前的下载任务
2022-03-17 03:31:25 +00:00
final List<int> _taskedEps = []; // 已经下载的EP
final List<int> _selectedEps = []; // 选中的EP
2021-09-29 23:57:09 +00:00
late Future f = _load();
Future<dynamic> _load() async {
_taskedEps.clear();
_task = await method.loadDownloadComic(widget.comicInfo.id);
if (_task != null) {
var epList = await method.downloadEpList(widget.comicInfo.id);
_taskedEps.addAll(epList.map((e) => e.epOrder));
}
}
void _selectAll() {
setState(() {
_selectedEps.clear();
2022-03-17 03:31:25 +00:00
for (var element in widget.epList) {
2021-09-29 23:57:09 +00:00
if (!_taskedEps.contains(element.order)) {
_selectedEps.add(element.order);
}
2022-03-17 03:31:25 +00:00
}
2021-09-29 23:57:09 +00:00
});
}
Future<dynamic> _download() async {
// 必须选中才能下载
if (_selectedEps.isEmpty) {
defaultToast(context, "请选择下载的EP");
return;
}
// 下载对象
Map<String, dynamic> create = {
"id": widget.comicInfo.id,
"createdAt": widget.comicInfo.createdAt,
"updatedAt": widget.comicInfo.updatedAt,
"title": widget.comicInfo.title,
"author": widget.comicInfo.author,
"pagesCount": widget.comicInfo.pagesCount,
"epsCount": widget.comicInfo.epsCount,
"finished": widget.comicInfo.finished,
"categories": json.encode(widget.comicInfo.categories),
"thumbOriginalName": widget.comicInfo.thumb.originalName,
"thumbFileServer": widget.comicInfo.thumb.fileServer,
"thumbPath": widget.comicInfo.thumb.path,
"description": widget.comicInfo.description,
"chineseTeam": widget.comicInfo.chineseTeam,
"tags": json.encode(widget.comicInfo.tags),
};
// 下载EP列表
List<Map<String, dynamic>> list = [];
2022-03-17 03:31:25 +00:00
for (var element in widget.epList) {
2021-09-29 23:57:09 +00:00
if (_selectedEps.contains(element.order)) {
list.add({
"comicId": widget.comicInfo.id,
"id": element.id,
"updatedAt": element.updatedAt,
"epOrder": element.order,
"title": element.title,
});
}
2022-03-17 03:31:25 +00:00
}
2021-09-29 23:57:09 +00:00
// 如果之前下载过就将EP加入下载
// 如果之前没有下载过就创建下载
if (_task != null) {
await method.addDownload(create, list);
} else {
await method.createDownload(create, list);
}
// 退出
defaultToast(context, "已经加入下载列表");
Navigator.pop(context);
}
@override
2022-03-19 04:12:27 +00:00
Widget build(BuildContext context){
return RightClickPop(buildScreen(context));
}
Widget buildScreen(BuildContext context) {
2021-09-29 23:57:09 +00:00
return Scaffold(
appBar: AppBar(
title: Text("下载 - ${widget.comicInfo.title}"),
),
body: FutureBuilder(
future: f,
builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
if (snapshot.hasError) {
print(snapshot.error);
print(snapshot.stackTrace);
2022-03-19 04:12:27 +00:00
return const Text('error');
2021-09-29 23:57:09 +00:00
}
if (snapshot.connectionState != ConnectionState.done) {
2022-03-19 04:12:27 +00:00
return const ContentLoading(label: '加载中');
2021-09-29 23:57:09 +00:00
}
return ListView(
children: [
ComicInfoCard(widget.comicInfo),
_buildButtons(),
Wrap(
alignment: WrapAlignment.spaceAround,
runSpacing: 10,
spacing: 10,
children: [
...widget.epList.map((e) {
return Container(
2022-03-19 04:12:27 +00:00
padding: const EdgeInsets.all(5),
2021-09-29 23:57:09 +00:00
child: MaterialButton(
onPressed: () {
_clickOfEp(e);
},
color: _colorOfEp(e),
child: Row(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: [
_iconOfEp(e),
Container(
width: 10,
),
Text(e.title,
2022-03-19 04:12:27 +00:00
style: const TextStyle(color: Colors.black)),
2021-09-29 23:57:09 +00:00
],
),
),
);
}),
],
),
],
);
},
),
);
}
Widget _buildButtons() {
var theme = Theme.of(context);
return Container(
2022-03-19 04:12:27 +00:00
padding: const EdgeInsets.all(5),
2021-09-29 23:57:09 +00:00
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(
color: Colors.grey.shade200,
),
),
),
child: Wrap(
spacing: 10,
runSpacing: 10,
alignment: WrapAlignment.spaceAround,
children: [
MaterialButton(
color: theme.colorScheme.secondary,
textColor: Colors.white,
onPressed: _selectAll,
2022-03-19 04:12:27 +00:00
child: const Text('全选'),
2021-09-29 23:57:09 +00:00
),
MaterialButton(
color: theme.colorScheme.secondary,
textColor: Colors.white,
onPressed: _download,
2022-03-19 04:12:27 +00:00
child: const Text('确定下载'),
2021-09-29 23:57:09 +00:00
),
],
),
);
}
Color _colorOfEp(Ep e) {
if (_taskedEps.contains(e.order)) {
return Colors.grey.shade300;
}
if (_selectedEps.contains(e.order)) {
return Colors.blueGrey.shade300;
}
return Colors.grey.shade200;
}
Icon _iconOfEp(Ep e) {
if (_taskedEps.contains(e.order)) {
2022-03-19 04:12:27 +00:00
return const Icon(Icons.download_rounded, color: Colors.black);
2021-09-29 23:57:09 +00:00
}
if (_selectedEps.contains(e.order)) {
2022-03-19 04:12:27 +00:00
return const Icon(Icons.check_box, color: Colors.black);
2021-09-29 23:57:09 +00:00
}
2022-03-19 04:12:27 +00:00
return const Icon(Icons.check_box_outline_blank, color: Colors.black);
2021-09-29 23:57:09 +00:00
}
void _clickOfEp(Ep e) {
if (_taskedEps.contains(e.order)) {
return;
}
if (_selectedEps.contains(e.order)) {
setState(() {
_selectedEps.remove(e.order);
});
} else {
setState(() {
_selectedEps.add(e.order);
});
}
}
}