pikapika/lib/screens/DownloadExportingGroupScree...

218 lines
4.8 KiB
Dart
Raw Normal View History

2022-10-14 17:54:50 +00:00
import 'dart:io';
2022-06-30 11:16:10 +00:00
import 'package:flutter/material.dart';
import 'package:pikapika/basic/Common.dart';
import '../basic/Channels.dart';
import '../basic/Cross.dart';
import '../basic/Method.dart';
import '../basic/config/ExportRename.dart';
2022-07-09 07:11:26 +00:00
import '../basic/config/IsPro.dart';
2022-06-30 11:16:10 +00:00
import 'components/ContentLoading.dart';
class DownloadExportingGroupScreen extends StatefulWidget {
final List<String> idList;
const DownloadExportingGroupScreen({Key? key, required this.idList})
: super(key: key);
@override
State<StatefulWidget> createState() => _DownloadExportingGroupScreenState();
}
class _DownloadExportingGroupScreenState
extends State<DownloadExportingGroupScreen> {
bool exporting = false;
bool exported = false;
bool exportFail = false;
dynamic e;
String exportMessage = "正在导出";
@override
void initState() {
registerEvent(_onMessageChange, "EXPORT");
super.initState();
}
@override
void dispose() {
unregisterEvent(_onMessageChange);
super.dispose();
}
void _onMessageChange(event) {
setState(() {
exportMessage = event;
});
}
Widget _body() {
if (exporting) {
return ContentLoading(label: exportMessage);
}
if (exportFail) {
return Center(child: Text("导出失败\n$e"));
}
if (exported) {
2022-07-09 07:11:26 +00:00
return const Center(child: Text("导出成功"));
2022-06-30 11:16:10 +00:00
}
2022-07-09 07:11:26 +00:00
return ListView(
children: [
Container(height: 20),
MaterialButton(
onPressed: _exportPkz,
child: const Text("导出PKZ"),
),
Container(height: 20),
MaterialButton(
onPressed: _exportPkis,
child: Text("分别导出PKI" + (!isPro ? "\n(发电后使用)" : "")),
),
Container(height: 20),
MaterialButton(
onPressed: _exportZips,
child: Text("分别导出ZIP" + (!isPro ? "\n(发电后使用)" : "")),
),
Container(height: 20),
],
2022-06-30 11:16:10 +00:00
);
}
2022-07-09 07:11:26 +00:00
_exportPkz() async {
2022-06-30 11:16:10 +00:00
late String? path;
try {
2022-10-14 17:54:50 +00:00
path = Platform.isIOS
? await method.iosGetDocumentDir()
: await chooseFolder(context);
2022-06-30 11:16:10 +00:00
} catch (e) {
defaultToast(context, "$e");
return;
}
var name = "";
if (currentExportRename()) {
var rename = await inputString(
context,
"请输入保存后的名称",
2022-07-01 05:39:08 +00:00
defaultValue: "${DateTime.now().millisecondsSinceEpoch}",
2022-06-30 11:16:10 +00:00
);
if (rename != null && rename.isNotEmpty) {
name = rename;
} else {
return;
}
}
print("path $path");
if (path != null) {
try {
setState(() {
exporting = true;
});
await method.exportComicDownloadToPkz(
widget.idList,
path,
name,
);
exported = true;
} catch (err) {
e = err;
exportFail = true;
} finally {
setState(() {
exporting = false;
});
}
}
}
2022-07-09 07:11:26 +00:00
_exportPkis() async {
if (!isPro) {
defaultToast(context, "请先发电鸭");
return;
}
late String? path;
try {
2022-10-14 17:54:50 +00:00
path = Platform.isIOS
? await method.iosGetDocumentDir()
: await chooseFolder(context);
2022-07-09 07:11:26 +00:00
} catch (e) {
defaultToast(context, "$e");
return;
}
print("path $path");
if (path != null) {
try {
setState(() {
exporting = true;
});
await method.exportAnyComicDownloadsToPki(
widget.idList,
path,
);
exported = true;
} catch (err) {
e = err;
exportFail = true;
} finally {
setState(() {
exporting = false;
});
}
}
}
_exportZips() async {
if (!isPro) {
defaultToast(context, "请先发电鸭");
return;
}
late String? path;
try {
2022-10-14 17:54:50 +00:00
path = Platform.isIOS
? await method.iosGetDocumentDir()
: await chooseFolder(context);
2022-07-09 07:11:26 +00:00
} catch (e) {
defaultToast(context, "$e");
return;
}
print("path $path");
if (path != null) {
try {
setState(() {
exporting = true;
});
await method.exportAnyComicDownloadsToZip(
widget.idList,
path,
);
exported = true;
} catch (err) {
e = err;
exportFail = true;
} finally {
setState(() {
exporting = false;
});
}
}
}
2022-06-30 11:16:10 +00:00
@override
Widget build(BuildContext context) {
return WillPopScope(
child: Scaffold(
appBar: AppBar(
title: const Text("批量导出"),
),
body: _body(),
),
onWillPop: () async {
if (exporting) {
defaultToast(context, "导出中, 请稍后");
return false;
}
return true;
},
);
}
}