pikapika/lib/screens/DownloadImportScreen.dart

170 lines
4.2 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';
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-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());
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-04-30 04:59:03 +00:00
var ls = await FilePicker.platform.pickFiles(
dialogTitle: '选择要导入的文件',
allowMultiple: false,
initialDirectory: chooseRoot,
type: FileType.custom,
2022-06-30 15:38:42 +00:00
allowedExtensions: ['pkz', 'zip'],
2022-04-30 04:59:03 +00:00
allowCompression: false,
2021-09-29 23:57:09 +00:00
);
2022-04-30 04:59:03 +00:00
String? 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) =>
PkzArchiveScreen(pkzPath: path),
),
);
} else if (path.endsWith(".zip")) {
try {
setState(() {
_importing = true;
});
await method.importComicDownload(path);
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(
'选择zip文件进行导入\n选择pkz文件进行阅读',
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
);
}
}