pikapika/lib/screens/DownloadImportScreen.dart

230 lines
6.1 KiB
Dart
Raw Normal View History

2021-09-29 23:57:09 +00:00
import 'dart:io';
2022-04-30 04:59:03 +00:00
import 'package:file_picker/file_picker.dart';
2022-07-01 05:39:08 +00:00
import 'package:filesystem_picker/filesystem_picker.dart';
2021-09-29 23:57:09 +00:00
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/Method.dart';
import 'package:pikapika/basic/config/ChooserRoot.dart';
2021-09-29 23:57:09 +00:00
2022-07-09 07:11:26 +00:00
import '../basic/Cross.dart';
import '../basic/config/IsPro.dart';
2022-06-30 15:38:42 +00:00
import 'PkzArchiveScreen.dart';
2021-09-29 23:57:09 +00:00
import 'components/ContentLoading.dart';
2022-03-25 14:57:30 +00:00
import 'components/RightClickPop.dart';
2021-09-29 23:57:09 +00:00
// 导入
class DownloadImportScreen extends StatefulWidget {
2022-03-17 03:31:25 +00:00
const DownloadImportScreen({Key? key}) : super(key: key);
2021-09-29 23:57:09 +00:00
@override
State<StatefulWidget> createState() => _DownloadImportScreenState();
}
class _DownloadImportScreenState extends State<DownloadImportScreen> {
bool _importing = false;
String _importMessage = "";
@override
void initState() {
registerEvent(_onMessageChange, "EXPORT");
super.initState();
}
@override
void dispose() {
unregisterEvent(_onMessageChange);
super.dispose();
}
void _onMessageChange(event) {
if (event is String) {
setState(() {
_importMessage = event;
});
}
}
@override
2022-04-30 04:59:03 +00:00
Widget build(BuildContext context) {
2022-03-25 14:57:30 +00:00
return rightClickPop(
child: buildScreen(context),
context: context,
canPop: !_importing,
);
}
Widget buildScreen(BuildContext context) {
2021-09-29 23:57:09 +00:00
if (_importing) {
return Scaffold(
body: ContentLoading(label: _importMessage),
);
}
List<Widget> actions = [];
2022-06-30 15:38:42 +00:00
actions.add(_fileImportButton());
2021-09-29 23:57:09 +00:00
actions.add(_networkImportButton());
2022-07-09 07:11:26 +00:00
actions.add(_importDirFilesZipButton());
2021-09-29 23:57:09 +00:00
return Scaffold(
appBar: AppBar(
2022-03-19 04:12:27 +00:00
title: const Text('导入'),
2021-09-29 23:57:09 +00:00
),
body: ListView(
children: [
Container(
2022-03-19 04:12:27 +00:00
padding: const EdgeInsets.all(10),
2021-09-29 23:57:09 +00:00
child: Text(_importMessage),
),
...actions,
],
),
);
}
Widget _fileImportButton() {
return MaterialButton(
height: 80,
onPressed: () async {
late String chooseRoot;
try {
chooseRoot = await currentChooserRoot();
} catch (e) {
defaultToast(context, "$e");
return;
}
2022-07-01 05:39:08 +00:00
String? path;
if (Platform.isAndroid) {
path = await FilesystemPicker.open(
title: 'Open file',
context: context,
rootDirectory: Directory(chooseRoot),
fsType: FilesystemType.file,
folderIconColor: Colors.teal,
allowedExtensions: ['.pkz', '.zip', '.pki'],
fileTileSelectMode: FileTileSelectMode.wholeTile,
);
2022-07-09 07:11:26 +00:00
} else {
2022-07-01 05:39:08 +00:00
var ls = await FilePicker.platform.pickFiles(
dialogTitle: '选择要导入的文件',
allowMultiple: false,
initialDirectory: chooseRoot,
type: FileType.custom,
allowedExtensions: ['pkz', 'zip', 'pki'],
allowCompression: false,
);
path = ls != null && ls.count > 0 ? ls.paths[0] : null;
}
2021-09-29 23:57:09 +00:00
if (path != null) {
2022-06-30 15:38:42 +00:00
if (path.endsWith(".pkz")) {
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) =>
2022-07-01 05:39:08 +00:00
PkzArchiveScreen(pkzPath: path!),
2022-06-30 15:38:42 +00:00
),
);
2022-07-01 05:39:08 +00:00
} else if (path.endsWith(".zip") || path.endsWith(".pki")) {
2022-06-30 15:38:42 +00:00
try {
setState(() {
_importing = true;
});
2022-07-09 07:11:26 +00:00
if (path.endsWith(".zip")) {
2022-07-01 05:39:08 +00:00
await method.importComicDownload(path);
2022-07-09 07:11:26 +00:00
} else if (path.endsWith(".pki")) {
2022-07-01 05:39:08 +00:00
await method.importComicDownloadPki(path);
}
2022-06-30 15:38:42 +00:00
setState(() {
_importMessage = "导入成功";
});
} catch (e) {
setState(() {
_importMessage = "导入失败 $e";
});
} finally {
setState(() {
_importing = false;
});
}
2021-09-29 23:57:09 +00:00
}
}
},
2022-06-30 15:38:42 +00:00
child: const Text(
2022-07-01 05:39:08 +00:00
'选择zip文件进行导入\n选择pki文件进行导入\n选择pkz文件进行阅读',
2022-06-30 15:38:42 +00:00
style: TextStyle(),
textAlign: TextAlign.center,
),
2021-09-29 23:57:09 +00:00
);
}
Widget _networkImportButton() {
return MaterialButton(
height: 80,
onPressed: () async {
var path =
await inputString(context, '请输入导出设备提供的地址\n例如 "192.168.1.2:50000"');
2021-09-29 23:57:09 +00:00
if (path != null) {
try {
setState(() {
_importing = true;
});
await method.importComicDownloadUsingSocket(path);
setState(() {
_importMessage = "导入成功";
});
} catch (e) {
setState(() {
_importMessage = "导入失败 $e";
});
} finally {
setState(() {
_importing = false;
});
}
}
},
2022-03-19 04:12:27 +00:00
child: const Text('从其他设备导入'),
2021-09-29 23:57:09 +00:00
);
}
2022-07-09 07:11:26 +00:00
Widget _importDirFilesZipButton() {
return MaterialButton(
height: 80,
onPressed: () async {
late String? path;
try {
path = await chooseFolder(context);
} catch (e) {
defaultToast(context, "$e");
return;
}
if (path != null) {
try {
setState(() {
_importing = true;
});
await method.importComicDownloadDir(path);
setState(() {
_importMessage = "导入成功";
});
} catch (e) {
setState(() {
_importMessage = "导入失败 $e";
});
} finally {
setState(() {
_importing = false;
});
}
}
},
child: Text(
'选择文件夹\n(导入里面所有的zip)' + (!isPro ? "\n(发电后使用)" : ""),
style: TextStyle(),
textAlign: TextAlign.center,
),
);
}
2021-09-29 23:57:09 +00:00
}