2021-09-29 23:57:09 +00:00
|
|
|
import 'dart:async';
|
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
import 'package:flutter/material.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';
|
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> {
|
|
|
|
DownloadComic? _downloading;
|
|
|
|
late bool _downloadRunning = false;
|
|
|
|
late Future<List<DownloadComic>> _f = method.allDownloads();
|
|
|
|
|
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(
|
2021-09-29 23:57:09 +00:00
|
|
|
appBar: AppBar(
|
2022-03-19 04:12:27 +00:00
|
|
|
title: const Text('下载列表'),
|
2021-09-29 23:57:09 +00:00
|
|
|
actions: [
|
2021-10-20 02:54:23 +00:00
|
|
|
importButton(),
|
|
|
|
pauseButton(),
|
|
|
|
resetFailedButton(),
|
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(() {
|
|
|
|
_f = method.allDownloads();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
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,
|
|
|
|
MaterialPageRoute(
|
|
|
|
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,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-10-20 02:54:23 +00:00
|
|
|
Widget importButton() {
|
|
|
|
return MaterialButton(
|
|
|
|
minWidth: 0,
|
|
|
|
onPressed: () async {
|
|
|
|
await Navigator.push(
|
|
|
|
context,
|
|
|
|
MaterialPageRoute(
|
2022-03-25 14:57:30 +00:00
|
|
|
builder: (context) => const DownloadImportScreen(),
|
2021-10-20 02:54:23 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
setState(() {
|
|
|
|
_f = method.allDownloads();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
child: Column(
|
|
|
|
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() {
|
|
|
|
return MaterialButton(
|
|
|
|
minWidth: 0,
|
|
|
|
onPressed: () async {
|
|
|
|
await method.resetFailed();
|
|
|
|
setState(() {
|
|
|
|
_f = method.allDownloads();
|
|
|
|
});
|
|
|
|
defaultToast(context, "所有失败的下载已经恢复");
|
|
|
|
},
|
|
|
|
child: Column(
|
|
|
|
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
|
|
|
}
|