pikapika/lib/screens/DownloadListScreen.dart

306 lines
8.2 KiB
Dart
Raw Normal View History

2021-09-29 23:57:09 +00:00
import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
2022-10-27 07:21:46 +00:00
import 'package:flutter_search_bar/flutter_search_bar.dart';
2021-11-11 03:00:38 +00:00
import 'package:pikapika/basic/Channels.dart';
import 'package:pikapika/basic/Common.dart';
import 'package:pikapika/basic/Entities.dart';
import 'package:pikapika/basic/Method.dart';
2022-06-30 11:16:10 +00:00
import 'package:pikapika/screens/DownloadExportGroupScreen.dart';
2023-01-31 10:50:51 +00:00
import '../basic/config/IconLoading.dart';
2021-09-29 23:57:09 +00:00
import 'DownloadImportScreen.dart';
import 'DownloadInfoScreen.dart';
import 'components/ContentLoading.dart';
import 'components/DownloadInfoCard.dart';
2022-03-19 04:12:27 +00:00
import 'components/RightClickPop.dart';
2021-09-29 23:57:09 +00:00
// 下载列表
class DownloadListScreen extends StatefulWidget {
2022-03-17 03:31:25 +00:00
const DownloadListScreen({Key? key}) : super(key: key);
2021-09-29 23:57:09 +00:00
@override
State<StatefulWidget> createState() => _DownloadListScreenState();
}
class _DownloadListScreenState extends State<DownloadListScreen> {
2022-10-27 07:21:46 +00:00
String _search = "";
late final SearchBar _searchBar = SearchBar(
hintText: '搜索下载',
inBar: false,
setState: setState,
onSubmitted: (value) {
if (value.isNotEmpty) {
_search = value;
_f = method.allDownloads(_search);
_searchBar.controller.text = value;
}
},
buildDefaultAppBar: (BuildContext context) {
return AppBar(
title: Text(_search == "" ? "下载列表" : ('搜索下载 - $_search')),
actions: [
_searchBar.getSearchAction(context),
exportButton(),
importButton(),
resetFailedButton(),
pauseButton(),
],
);
},
);
2021-09-29 23:57:09 +00:00
DownloadComic? _downloading;
late bool _downloadRunning = false;
2022-10-27 07:21:46 +00:00
late Future<List<DownloadComic>> _f = method.allDownloads(_search);
2021-09-29 23:57:09 +00:00
2021-10-20 02:54:23 +00:00
void _onMessageChange(String event) {
2021-09-29 23:57:09 +00:00
print("EVENT");
print(event);
2022-03-17 03:31:25 +00:00
try {
setState(() {
_downloading = DownloadComic.fromJson(json.decode(event));
});
} catch (e, s) {
print(e);
print(s);
2021-09-29 23:57:09 +00:00
}
}
@override
void initState() {
registerEvent(_onMessageChange, "DOWNLOAD");
method
.downloadRunning()
.then((val) => setState(() => _downloadRunning = val));
super.initState();
}
@override
void dispose() {
unregisterEvent(_onMessageChange);
super.dispose();
}
@override
Widget build(BuildContext context) {
2022-03-19 04:12:27 +00:00
final screen = Scaffold(
2022-10-27 07:21:46 +00:00
appBar: _searchBar.build(context),
2021-09-29 23:57:09 +00:00
body: FutureBuilder(
future: _f,
builder: (BuildContext context,
AsyncSnapshot<List<DownloadComic>> snapshot) {
2021-10-24 06:37:46 +00:00
if (snapshot.connectionState != ConnectionState.done) {
2022-03-19 04:12:27 +00:00
return const ContentLoading(label: '加载中');
2021-10-24 06:37:46 +00:00
}
2021-09-29 23:57:09 +00:00
if (snapshot.hasError) {
print("${snapshot.error}");
print("${snapshot.stackTrace}");
2022-03-19 04:12:27 +00:00
return const Center(child: Text('加载失败'));
2021-09-29 23:57:09 +00:00
}
var data = snapshot.data!;
if (_downloading != null) {
try {
for (var i = 0; i < data.length; i++) {
if (_downloading!.id == data[i].id) {
data[i].copy(_downloading!);
}
}
} catch (e, s) {
print(e);
print(s);
}
}
2021-10-24 06:37:46 +00:00
return RefreshIndicator(
onRefresh: () async {
setState(() {
2022-10-27 07:21:46 +00:00
_f = method.allDownloads(_search);
2021-10-24 06:37:46 +00:00
});
},
child: ListView(
children: [
...data.map(downloadWidget),
],
),
2021-09-29 23:57:09 +00:00
);
},
),
);
2022-03-25 14:57:30 +00:00
return rightClickPop(
child: screen,
context: context,
canPop: true,
);
2021-09-29 23:57:09 +00:00
}
2021-10-20 02:54:23 +00:00
2021-10-24 06:37:46 +00:00
Widget downloadWidget(DownloadComic e) {
return InkWell(
onTap: () {
if (e.deleting) {
return;
}
Navigator.push(
context,
2023-01-31 10:50:51 +00:00
mixRoute(
2021-10-24 06:37:46 +00:00
builder: (context) => DownloadInfoScreen(
comicId: e.id,
comicTitle: e.title,
),
),
);
},
onLongPress: () async {
String? action = await chooseListDialog(context, e.title, ['删除']);
if (action == '删除') {
await method.deleteDownloadComic(e.id);
setState(() => e.deleting = true);
}
},
child: DownloadInfoCard(
task: e,
downloading: _downloading != null && _downloading!.id == e.id,
),
);
}
2022-06-30 11:16:10 +00:00
Widget exportButton() {
2022-06-30 15:38:42 +00:00
return IconButton(
2022-06-30 11:16:10 +00:00
onPressed: () async {
await Navigator.push(
context,
2023-01-31 10:50:51 +00:00
mixRoute(
2022-06-30 11:16:10 +00:00
builder: (context) => const DownloadExportGroupScreen(),
),
);
},
2022-06-30 15:38:42 +00:00
icon: Column(
2022-06-30 11:16:10 +00:00
children: [
Expanded(child: Container()),
const Icon(
Icons.send_to_mobile,
size: 18,
color: Colors.white,
),
const Text(
'导出',
style: TextStyle(fontSize: 14, color: Colors.white),
),
Expanded(child: Container()),
],
));
}
2021-10-20 02:54:23 +00:00
Widget importButton() {
2022-06-30 15:38:42 +00:00
return IconButton(
2021-10-20 02:54:23 +00:00
onPressed: () async {
await Navigator.push(
context,
2023-01-31 10:50:51 +00:00
mixRoute(
2022-03-25 14:57:30 +00:00
builder: (context) => const DownloadImportScreen(),
2021-10-20 02:54:23 +00:00
),
);
setState(() {
2022-10-27 07:21:46 +00:00
_f = method.allDownloads(_search);
2021-10-20 02:54:23 +00:00
});
},
2022-06-30 15:38:42 +00:00
icon: Column(
2021-10-20 02:54:23 +00:00
children: [
Expanded(child: Container()),
2022-03-19 04:12:27 +00:00
const Icon(
2021-10-20 02:54:23 +00:00
Icons.label_important,
size: 18,
color: Colors.white,
),
2022-03-19 04:12:27 +00:00
const Text(
2021-10-20 02:54:23 +00:00
'导入',
style: TextStyle(fontSize: 14, color: Colors.white),
),
Expanded(child: Container()),
],
));
}
Widget pauseButton() {
return MaterialButton(
minWidth: 0,
onPressed: () {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
2022-03-25 14:57:30 +00:00
title: const Text('下载任务'),
2021-10-20 02:54:23 +00:00
content: Text(
_downloadRunning ? "暂停下载吗?" : "启动下载吗?",
),
actions: [
MaterialButton(
onPressed: () async {
Navigator.pop(context);
},
2022-03-25 14:57:30 +00:00
child: const Text('取消'),
2021-10-20 02:54:23 +00:00
),
MaterialButton(
onPressed: () async {
Navigator.pop(context);
var to = !_downloadRunning;
await method.setDownloadRunning(to);
setState(() {
_downloadRunning = to;
});
},
2022-03-19 04:12:27 +00:00
child: const Text('确认'),
2021-10-20 02:54:23 +00:00
),
],
);
},
);
},
child: Column(
children: [
Expanded(child: Container()),
Icon(
_downloadRunning
? Icons.compare_arrows_sharp
: Icons.schedule_send,
size: 18,
color: Colors.white,
),
Text(
_downloadRunning ? '下载中' : '暂停中',
2022-03-25 14:57:30 +00:00
style: const TextStyle(fontSize: 14, color: Colors.white),
2021-10-20 02:54:23 +00:00
),
Expanded(child: Container()),
],
));
}
Widget resetFailedButton() {
2022-06-30 15:38:42 +00:00
return IconButton(
2021-10-20 02:54:23 +00:00
onPressed: () async {
await method.resetFailed();
setState(() {
2022-10-27 07:21:46 +00:00
_f = method.allDownloads(_search);
2021-10-20 02:54:23 +00:00
});
defaultToast(context, "所有失败的下载已经恢复");
},
2022-06-30 15:38:42 +00:00
icon: Column(
2021-10-20 02:54:23 +00:00
children: [
Expanded(child: Container()),
2022-03-25 14:57:30 +00:00
const Icon(
2021-10-20 02:54:23 +00:00
Icons.sync_problem,
size: 18,
color: Colors.white,
),
2022-03-25 14:57:30 +00:00
const Text(
2021-10-20 02:54:23 +00:00
'恢复',
style: TextStyle(fontSize: 14, color: Colors.white),
),
Expanded(child: Container()),
],
));
}
2021-09-29 23:57:09 +00:00
}