feat: setting refactor

This commit is contained in:
Simon Ding
2024-07-18 14:45:47 +08:00
parent e26b21d03f
commit af7a7ba469

View File

@@ -258,15 +258,20 @@ class _SystemSettingsPageState extends ConsumerState<SystemSettingsPage> {
var nameController = TextEditingController(text: indexer.name); var nameController = TextEditingController(text: indexer.name);
var urlController = TextEditingController(text: indexer.url); var urlController = TextEditingController(text: indexer.url);
var apiKeyController = TextEditingController(text: indexer.apiKey); var apiKeyController = TextEditingController(text: indexer.apiKey);
return showDialog<void>( var selectImpl = "torznab";
context: context, final children = <Widget>[
barrierDismissible: true, // user must tap button! DropdownMenu(
builder: (BuildContext context) { label: const Text("类型"),
return AlertDialog( onSelected: (value) {
title: const Text('索引器'), setState(() {
content: SingleChildScrollView( selectImpl = value!;
child: ListBody( });
children: <Widget>[ },
initialSelection: selectImpl,
dropdownMenuEntries: const [
DropdownMenuEntry(value: "torznab", label: "Torznab"),
],
),
TextField( TextField(
decoration: Commons.requiredTextFieldStyle(text: "名称"), decoration: Commons.requiredTextFieldStyle(text: "名称"),
controller: nameController, controller: nameController,
@@ -279,65 +284,40 @@ class _SystemSettingsPageState extends ConsumerState<SystemSettingsPage> {
decoration: Commons.requiredTextFieldStyle(text: "API Key"), decoration: Commons.requiredTextFieldStyle(text: "API Key"),
controller: apiKeyController, controller: apiKeyController,
), ),
], ];
), onDelete() async {
), return ref.read(indexersProvider.notifier).deleteIndexer(indexer.id!);
actions: <Widget>[ }
indexer.id == null
? const Text("") onSubmit() async {
: TextButton( return ref.read(indexersProvider.notifier).addIndexer(Indexer(
onPressed: () {
var f = ref
.read(indexersProvider.notifier)
.deleteIndexer(indexer.id!);
f.whenComplete(() {
Utils.showSnakeBar("操作成功");
Navigator.of(context).pop();
}).onError((e, s) {
Utils.showSnakeBar("操作失败:$e");
});
},
child: const Text(
'删除',
style: TextStyle(color: Colors.red),
)),
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text('取消')),
TextButton(
child: const Text('确定'),
onPressed: () {
var f = ref.read(indexersProvider.notifier).addIndexer(
Indexer(
name: nameController.text, name: nameController.text,
url: urlController.text, url: urlController.text,
apiKey: apiKeyController.text)); apiKey: apiKeyController.text));
f.whenComplete(() { }
Utils.showSnakeBar("操作成功");
Navigator.of(context).pop(); return showSettingDialog(
}).onError((e, s) { "索引器", indexer.id != null, children, onSubmit, onDelete);
Utils.showSnakeBar("操作失败:$e");
});
},
),
],
);
});
} }
Future<void> showDownloadClientDetails(DownloadClient client) { Future<void> showDownloadClientDetails(DownloadClient client) {
var nameController = TextEditingController(text: client.name); var nameController = TextEditingController(text: client.name);
var urlController = TextEditingController(text: client.url); var urlController = TextEditingController(text: client.url);
return showDialog<void>( String selectImpl = "transmission";
context: context, var body = <Widget>[
barrierDismissible: true, // user must tap button! DropdownMenu(
builder: (BuildContext context) { label: const Text("类型"),
return AlertDialog( onSelected: (value) {
title: const Text('下载客户端'), setState(() {
content: SingleChildScrollView( selectImpl = value!;
child: ListBody( });
children: <Widget>[ },
initialSelection: selectImpl,
dropdownMenuEntries: const [
DropdownMenuEntry(value: "transmission", label: "Transmission"),
],
),
TextField( TextField(
decoration: Commons.requiredTextFieldStyle(text: "名称"), decoration: Commons.requiredTextFieldStyle(text: "名称"),
controller: nameController, controller: nameController,
@@ -346,56 +326,25 @@ class _SystemSettingsPageState extends ConsumerState<SystemSettingsPage> {
decoration: Commons.requiredTextFieldStyle(text: "地址"), decoration: Commons.requiredTextFieldStyle(text: "地址"),
controller: urlController, controller: urlController,
), ),
],
), ];
), onDelete() async {
actions: <Widget>[ return ref
client.id == null
? const Text("")
: TextButton(
onPressed: () {
var f = ref
.read(dwonloadClientsProvider.notifier) .read(dwonloadClientsProvider.notifier)
.deleteDownloadClients(client.id!); .deleteDownloadClients(client.id!);
f.whenComplete(() { }
Utils.showSnakeBar("操作成功");
Navigator.of(context).pop(); onSubmit() async {
}).onError((e, s) { return ref
Utils.showSnakeBar("操作失败:$e");
});
},
child: const Text(
'删除',
style: TextStyle(color: Colors.red),
)),
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text('取消')),
TextButton(
child: const Text('确定'),
onPressed: () {
var f = ref
.read(dwonloadClientsProvider.notifier) .read(dwonloadClientsProvider.notifier)
.addDownloadClients( .addDownloadClients(nameController.text, urlController.text);
nameController.text, urlController.text); }
f.whenComplete(() {
Utils.showSnakeBar("操作成功"); return showSettingDialog(
Navigator.of(context).pop(); "下载客户端", client.id != null, body, onSubmit, onDelete);
}).onError((e, s) {
Utils.showSnakeBar("操作失败:$e");
});
},
),
],
);
});
} }
Future<void> showStorageDetails(Storage s) { Future<void> showStorageDetails(Storage s) {
return showDialog<void>(
context: context,
barrierDismissible: true, // user must tap button!
builder: (BuildContext context) {
var nameController = TextEditingController(text: s.name); var nameController = TextEditingController(text: s.name);
var tvPathController = TextEditingController(); var tvPathController = TextEditingController();
var moviePathController = TextEditingController(); var moviePathController = TextEditingController();
@@ -410,16 +359,10 @@ class _SystemSettingsPageState extends ConsumerState<SystemSettingsPage> {
passController.text = s.settings!["password"] ?? ""; passController.text = s.settings!["password"] ?? "";
} }
String selectImpl = String selectImpl = s.implementation == null ? "local" : s.implementation!;
s.implementation == null ? "local" : s.implementation!; final widgets = <Widget>[
return StatefulBuilder(builder: (context, setState) {
return AlertDialog(
title: const Text('存储'),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
DropdownMenu( DropdownMenu(
label: const Text("实现"), label: const Text("类型"),
onSelected: (value) { onSelected: (value) {
setState(() { setState(() {
selectImpl = value!; selectImpl = value!;
@@ -439,18 +382,15 @@ class _SystemSettingsPageState extends ConsumerState<SystemSettingsPage> {
? Column( ? Column(
children: [ children: [
TextField( TextField(
decoration: const InputDecoration( decoration: const InputDecoration(labelText: "Webdav地址"),
labelText: "Webdav地址"),
controller: urlController, controller: urlController,
), ),
TextField( TextField(
decoration: decoration: const InputDecoration(labelText: "用户"),
const InputDecoration(labelText: "用户"),
controller: userController, controller: userController,
), ),
TextField( TextField(
decoration: decoration: const InputDecoration(labelText: "密码"),
const InputDecoration(labelText: "密码"),
controller: passController, controller: passController,
), ),
], ],
@@ -464,34 +404,9 @@ class _SystemSettingsPageState extends ConsumerState<SystemSettingsPage> {
decoration: const InputDecoration(labelText: "电影路径"), decoration: const InputDecoration(labelText: "电影路径"),
controller: moviePathController, controller: moviePathController,
) )
], ];
), onSubmit() async {
), return ref.read(storageSettingProvider.notifier).addStorage(Storage(
actions: <Widget>[
s.id == null
? const Text("")
: TextButton(
onPressed: () {
var f = ref
.read(storageSettingProvider.notifier)
.deleteStorage(s.id!);
f.whenComplete(() {
Utils.showSnakeBar("操作成功");
Navigator.of(context).pop();
}).onError((e, s) {
Utils.showSnakeBar("操作失败:$e");
});
},
child: const Text('删除')),
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text('取消')),
TextButton(
child: const Text('确定'),
onPressed: () async {
final f = ref
.read(storageSettingProvider.notifier)
.addStorage(Storage(
name: nameController.text, name: nameController.text,
implementation: selectImpl, implementation: selectImpl,
settings: { settings: {
@@ -502,6 +417,59 @@ class _SystemSettingsPageState extends ConsumerState<SystemSettingsPage> {
"password": passController.text "password": passController.text
}, },
)); ));
}
onDelete() async {
return ref.read(storageSettingProvider.notifier).deleteStorage(s.id!);
}
return showSettingDialog('存储', s.id != null, widgets, onSubmit, onDelete);
}
Future<void> showSettingDialog(
String title,
bool showDelete,
List<Widget> children,
Future Function() onSubmit,
Future Function() onDelete) {
return showDialog<void>(
context: context,
barrierDismissible: true,
builder: (BuildContext context) {
return AlertDialog(
title: Text(title),
content: SingleChildScrollView(
child: Container(
constraints: const BoxConstraints(maxWidth: 200),
child: ListBody(
children: children,
),
),
),
actions: <Widget>[
showDelete
? TextButton(
onPressed: () {
final f = onDelete();
f.whenComplete(() {
Utils.showSnakeBar("删除成功");
Navigator.of(context).pop();
}).onError((e, s) {
Utils.showSnakeBar("删除失败:$e");
});
},
child: const Text(
'删除',
style: TextStyle(color: Colors.red),
))
: const Text(""),
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text('取消')),
TextButton(
child: const Text('确定'),
onPressed: () {
final f = onSubmit();
f.whenComplete(() { f.whenComplete(() {
Utils.showSnakeBar("操作成功"); Utils.showSnakeBar("操作成功");
Navigator.of(context).pop(); Navigator.of(context).pop();
@@ -513,16 +481,5 @@ class _SystemSettingsPageState extends ConsumerState<SystemSettingsPage> {
], ],
); );
}); });
});
}
bool showError(AsyncSnapshot<void> snapshot) {
final isErrored = snapshot.hasError &&
snapshot.connectionState != ConnectionState.waiting;
if (isErrored) {
Utils.showSnakeBar("当前操作出错: ${snapshot.error}");
return true;
}
return false;
} }
} }