mirror of
https://github.com/simon-ding/polaris.git
synced 2026-04-21 19:27:30 +08:00
separate api calls
This commit is contained in:
@@ -1,9 +1,6 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:ui/APIs.dart';
|
||||
import 'package:ui/providers/welcome_data.dart';
|
||||
import 'package:ui/server_response.dart';
|
||||
import 'package:ui/providers/settings.dart';
|
||||
import 'package:ui/utils.dart';
|
||||
|
||||
class SystemSettingsPage extends ConsumerStatefulWidget {
|
||||
@@ -18,139 +15,160 @@ class SystemSettingsPage extends ConsumerStatefulWidget {
|
||||
|
||||
class _SystemSettingsPageState extends ConsumerState<SystemSettingsPage> {
|
||||
final GlobalKey _formKey = GlobalKey<FormState>();
|
||||
|
||||
List<dynamic> indexers = List.empty();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
}
|
||||
|
||||
Future<void>? _pendingTmdb;
|
||||
Future<void>? _pendingIndexer;
|
||||
Future<void>? _pendingDownloadClient;
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
var key = ref.watch(tmdbApiSettingProvider);
|
||||
|
||||
var tmdbSetting = key.when(
|
||||
data: (data) => Container(
|
||||
padding: const EdgeInsets.fromLTRB(40, 10, 40, 0),
|
||||
child: Form(
|
||||
key: _formKey, //设置globalKey,用于后面获取FormState
|
||||
autovalidateMode: AutovalidateMode.onUserInteraction,
|
||||
child: Column(
|
||||
children: [
|
||||
TextFormField(
|
||||
autofocus: true,
|
||||
initialValue: data,
|
||||
decoration: const InputDecoration(
|
||||
labelText: "TMDB Api Key",
|
||||
icon: Icon(Icons.key),
|
||||
),
|
||||
//
|
||||
validator: (v) {
|
||||
return v!.trim().isNotEmpty ? null : "ApiKey 不能为空";
|
||||
},
|
||||
onSaved: (newValue) {
|
||||
_submitSettings(context, newValue!);
|
||||
},
|
||||
),
|
||||
Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(top: 28.0),
|
||||
child: ElevatedButton(
|
||||
child: const Padding(
|
||||
padding: EdgeInsets.all(16.0),
|
||||
child: Text("保存"),
|
||||
var tmdbSetting = FutureBuilder(
|
||||
// We listen to the pending operation, to update the UI accordingly.
|
||||
future: _pendingTmdb,
|
||||
builder: (context, snapshot) {
|
||||
return key.when(
|
||||
data: (value) => Container(
|
||||
padding: const EdgeInsets.fromLTRB(40, 10, 40, 0),
|
||||
child: Form(
|
||||
key: _formKey, //设置globalKey,用于后面获取FormState
|
||||
autovalidateMode: AutovalidateMode.onUserInteraction,
|
||||
child: Column(
|
||||
children: [
|
||||
TextFormField(
|
||||
autofocus: true,
|
||||
initialValue: value,
|
||||
decoration: const InputDecoration(
|
||||
labelText: "TMDB Api Key",
|
||||
icon: Icon(Icons.key),
|
||||
),
|
||||
//
|
||||
validator: (v) {
|
||||
return v!.trim().isNotEmpty
|
||||
? null
|
||||
: "ApiKey 不能为空";
|
||||
},
|
||||
onSaved: (newValue) {
|
||||
var furture = ref
|
||||
.read(tmdbApiSettingProvider.notifier)
|
||||
.submitSettings(newValue!);
|
||||
setState(() {
|
||||
_pendingTmdb = furture;
|
||||
});
|
||||
if (!showError(snapshot)) {
|
||||
Navigator.of(context).pop();
|
||||
}
|
||||
},
|
||||
),
|
||||
onPressed: () {
|
||||
// 通过_formKey.currentState 获取FormState后,
|
||||
// 调用validate()方法校验用户名密码是否合法,校验
|
||||
// 通过后再提交数据。
|
||||
if ((_formKey.currentState as FormState)
|
||||
.validate()) {
|
||||
(_formKey.currentState as FormState).save();
|
||||
}
|
||||
},
|
||||
),
|
||||
Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(top: 28.0),
|
||||
child: ElevatedButton(
|
||||
child: const Padding(
|
||||
padding: EdgeInsets.all(16.0),
|
||||
child: Text("保存"),
|
||||
),
|
||||
onPressed: () {
|
||||
// 通过_formKey.currentState 获取FormState后,
|
||||
// 调用validate()方法校验用户名密码是否合法,校验
|
||||
// 通过后再提交数据。
|
||||
if ((_formKey.currentState as FormState)
|
||||
.validate()) {
|
||||
(_formKey.currentState as FormState).save();
|
||||
}
|
||||
},
|
||||
),
|
||||
),
|
||||
)
|
||||
],
|
||||
),
|
||||
)
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
error: (err, trace) => Text("$err"),
|
||||
loading: () => const CircularProgressIndicator());
|
||||
),
|
||||
),
|
||||
error: (err, trace) => Text("$err"),
|
||||
loading: () => const CircularProgressIndicator());
|
||||
});
|
||||
|
||||
var indexers = ref.watch(indexersProvider);
|
||||
var indexerSetting = indexers.when(
|
||||
data: (value) => GridView.builder(
|
||||
itemCount: value.length + 1,
|
||||
scrollDirection: Axis.vertical,
|
||||
shrinkWrap: true,
|
||||
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
|
||||
crossAxisCount: 6),
|
||||
itemBuilder: (context, i) {
|
||||
if (i < value.length) {
|
||||
var indexer = value[i];
|
||||
return Card(
|
||||
margin: const EdgeInsets.all(4),
|
||||
clipBehavior: Clip.hardEdge,
|
||||
child: InkWell(
|
||||
//splashColor: Colors.blue.withAlpha(30),
|
||||
onTap: () {
|
||||
showIndexerDetails(context, indexer);
|
||||
},
|
||||
child: Center(child: Text(indexer.name!))));
|
||||
}
|
||||
return Card(
|
||||
margin: const EdgeInsets.all(4),
|
||||
clipBehavior: Clip.hardEdge,
|
||||
child: InkWell(
|
||||
//splashColor: Colors.blue.withAlpha(30),
|
||||
onTap: () {
|
||||
showIndexerDetails(context, Indexer());
|
||||
},
|
||||
child: const Center(
|
||||
child: Icon(Icons.add),
|
||||
)));
|
||||
}),
|
||||
error: (err, trace) => Text("$err"),
|
||||
loading: () => const CircularProgressIndicator());
|
||||
var indexerSetting = FutureBuilder(
|
||||
// We listen to the pending operation, to update the UI accordingly.
|
||||
future: _pendingIndexer,
|
||||
builder: (context, snapshot) {
|
||||
return indexers.when(
|
||||
data: (value) => GridView.builder(
|
||||
itemCount: value.length + 1,
|
||||
scrollDirection: Axis.vertical,
|
||||
shrinkWrap: true,
|
||||
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
|
||||
crossAxisCount: 6),
|
||||
itemBuilder: (context, i) {
|
||||
if (i < value.length) {
|
||||
var indexer = value[i];
|
||||
return Card(
|
||||
margin: const EdgeInsets.all(4),
|
||||
clipBehavior: Clip.hardEdge,
|
||||
child: InkWell(
|
||||
//splashColor: Colors.blue.withAlpha(30),
|
||||
onTap: () {
|
||||
showIndexerDetails(snapshot, context, indexer);
|
||||
},
|
||||
child: Center(child: Text(indexer.name!))));
|
||||
}
|
||||
return Card(
|
||||
margin: const EdgeInsets.all(4),
|
||||
clipBehavior: Clip.hardEdge,
|
||||
child: InkWell(
|
||||
//splashColor: Colors.blue.withAlpha(30),
|
||||
onTap: () {
|
||||
showIndexerDetails(snapshot, context, Indexer());
|
||||
},
|
||||
child: const Center(
|
||||
child: Icon(Icons.add),
|
||||
)));
|
||||
}),
|
||||
error: (err, trace) => Text("$err"),
|
||||
loading: () => const CircularProgressIndicator());
|
||||
});
|
||||
|
||||
var downloadClients = ref.watch(dwonloadClientsProvider);
|
||||
var downloadSetting = downloadClients.when(
|
||||
data: (value) => GridView.builder(
|
||||
itemCount: value.length + 1,
|
||||
scrollDirection: Axis.vertical,
|
||||
shrinkWrap: true,
|
||||
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
|
||||
crossAxisCount: 6),
|
||||
itemBuilder: (context, i) {
|
||||
if (i < value.length) {
|
||||
var client = value[i];
|
||||
return Card(
|
||||
margin: const EdgeInsets.all(4),
|
||||
clipBehavior: Clip.hardEdge,
|
||||
child: InkWell(
|
||||
//splashColor: Colors.blue.withAlpha(30),
|
||||
onTap: () {
|
||||
showDownloadClientDetails(context, client);
|
||||
},
|
||||
child: Center(child: Text(client.name!))));
|
||||
}
|
||||
return Card(
|
||||
margin: const EdgeInsets.all(4),
|
||||
clipBehavior: Clip.hardEdge,
|
||||
child: InkWell(
|
||||
//splashColor: Colors.blue.withAlpha(30),
|
||||
onTap: () {
|
||||
showDownloadClientDetails(context, DownloadClient());
|
||||
},
|
||||
child: const Center(
|
||||
child: Icon(Icons.add),
|
||||
)));
|
||||
}),
|
||||
error: (err, trace) => Text("$err"),
|
||||
loading: () => const CircularProgressIndicator());
|
||||
var downloadSetting = FutureBuilder(
|
||||
// We listen to the pending operation, to update the UI accordingly.
|
||||
future: _pendingDownloadClient,
|
||||
builder: (context, snapshot) {
|
||||
return downloadClients.when(
|
||||
data: (value) => GridView.builder(
|
||||
itemCount: value.length + 1,
|
||||
scrollDirection: Axis.vertical,
|
||||
shrinkWrap: true,
|
||||
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
|
||||
crossAxisCount: 6),
|
||||
itemBuilder: (context, i) {
|
||||
if (i < value.length) {
|
||||
var client = value[i];
|
||||
return Card(
|
||||
margin: const EdgeInsets.all(4),
|
||||
clipBehavior: Clip.hardEdge,
|
||||
child: InkWell(
|
||||
//splashColor: Colors.blue.withAlpha(30),
|
||||
onTap: () {
|
||||
showDownloadClientDetails(
|
||||
snapshot, context, client);
|
||||
},
|
||||
child: Center(child: Text(client.name!))));
|
||||
}
|
||||
return Card(
|
||||
margin: const EdgeInsets.all(4),
|
||||
clipBehavior: Clip.hardEdge,
|
||||
child: InkWell(
|
||||
//splashColor: Colors.blue.withAlpha(30),
|
||||
onTap: () {
|
||||
showDownloadClientDetails(
|
||||
snapshot, context, DownloadClient());
|
||||
},
|
||||
child: const Center(
|
||||
child: Icon(Icons.add),
|
||||
)));
|
||||
}),
|
||||
error: (err, trace) => Text("$err"),
|
||||
loading: () => const CircularProgressIndicator());
|
||||
});
|
||||
|
||||
return ListView(
|
||||
children: [
|
||||
@@ -179,17 +197,8 @@ class _SystemSettingsPageState extends ConsumerState<SystemSettingsPage> {
|
||||
);
|
||||
}
|
||||
|
||||
void _submitSettings(BuildContext context, String v) async {
|
||||
var resp = await Dio().post(APIs.settingsUrl, data: {APIs.tmdbApiKey: v});
|
||||
var sp = ServerResponse.fromJson(resp.data as Map<String, dynamic>);
|
||||
if (sp.code != 0) {
|
||||
if (context.mounted) {
|
||||
Utils.showAlertDialog(context, sp.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> showIndexerDetails(BuildContext context, Indexer indexer) {
|
||||
Future<void> showIndexerDetails(
|
||||
AsyncSnapshot<void> snapshot, BuildContext context, Indexer indexer) {
|
||||
var nameController = TextEditingController(text: indexer.name);
|
||||
var urlController = TextEditingController(text: indexer.url);
|
||||
var apiKeyController = TextEditingController(text: indexer.apiKey);
|
||||
@@ -219,9 +228,19 @@ class _SystemSettingsPageState extends ConsumerState<SystemSettingsPage> {
|
||||
),
|
||||
actions: <Widget>[
|
||||
indexer.id == null
|
||||
? Text("")
|
||||
? const Text("")
|
||||
: TextButton(
|
||||
onPressed: () => {deleteIndexer(context, indexer.id!)},
|
||||
onPressed: () {
|
||||
var f = ref
|
||||
.read(indexersProvider.notifier)
|
||||
.deleteIndexer(indexer.id!);
|
||||
setState(() {
|
||||
_pendingIndexer = f;
|
||||
});
|
||||
if (!showError(snapshot)) {
|
||||
Navigator.of(context).pop();
|
||||
}
|
||||
},
|
||||
child: const Text('删除')),
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
@@ -229,8 +248,18 @@ class _SystemSettingsPageState extends ConsumerState<SystemSettingsPage> {
|
||||
TextButton(
|
||||
child: const Text('确定'),
|
||||
onPressed: () {
|
||||
addIndexer(context, nameController.text, urlController.text,
|
||||
apiKeyController.text);
|
||||
var f = ref.read(indexersProvider.notifier).addIndexer(
|
||||
Indexer(
|
||||
name: nameController.text,
|
||||
url: urlController.text,
|
||||
apiKey: apiKeyController.text));
|
||||
setState(() {
|
||||
_pendingIndexer = f;
|
||||
});
|
||||
|
||||
if (!showError(snapshot)) {
|
||||
Navigator.of(context).pop();
|
||||
}
|
||||
},
|
||||
),
|
||||
],
|
||||
@@ -238,36 +267,7 @@ class _SystemSettingsPageState extends ConsumerState<SystemSettingsPage> {
|
||||
});
|
||||
}
|
||||
|
||||
void addIndexer(
|
||||
BuildContext context, String name, String url, String apiKey) async {
|
||||
if (name.isEmpty || url.isEmpty || apiKey.isEmpty) {
|
||||
return;
|
||||
}
|
||||
var dio = Dio();
|
||||
var resp = await dio.post(APIs.addIndexerUrl,
|
||||
data: Indexer(name: name, url: url, apiKey: apiKey).toJson());
|
||||
var sp = ServerResponse.fromJson(resp.data);
|
||||
if (sp.code != 0 && context.mounted) {
|
||||
Utils.showAlertDialog(context, sp.message);
|
||||
return;
|
||||
}
|
||||
Navigator.of(context).pop();
|
||||
ref.refresh(indexersProvider);
|
||||
}
|
||||
|
||||
void deleteIndexer(BuildContext context, int id) async {
|
||||
var dio = Dio();
|
||||
var resp = await dio.delete("${APIs.delIndexerUrl}$id");
|
||||
var sp = ServerResponse.fromJson(resp.data);
|
||||
if (sp.code != 0 && context.mounted) {
|
||||
Utils.showAlertDialog(context, sp.message);
|
||||
return;
|
||||
}
|
||||
Navigator.of(context).pop();
|
||||
ref.refresh(indexersProvider);
|
||||
}
|
||||
|
||||
Future<void> showDownloadClientDetails(
|
||||
Future<void> showDownloadClientDetails(AsyncSnapshot<void> snapshot,
|
||||
BuildContext context, DownloadClient client) {
|
||||
var nameController = TextEditingController(text: client.name);
|
||||
var urlController = TextEditingController(text: client.url);
|
||||
@@ -294,10 +294,19 @@ class _SystemSettingsPageState extends ConsumerState<SystemSettingsPage> {
|
||||
),
|
||||
actions: <Widget>[
|
||||
client.id == null
|
||||
? Text("")
|
||||
? const Text("")
|
||||
: TextButton(
|
||||
onPressed: () =>
|
||||
{deleteDownloadClients(context, client.id!)},
|
||||
onPressed: () {
|
||||
var f = ref
|
||||
.read(dwonloadClientsProvider.notifier)
|
||||
.deleteDownloadClients(client.id!);
|
||||
setState(() {
|
||||
_pendingDownloadClient = f;
|
||||
});
|
||||
if (!showError(snapshot)) {
|
||||
Navigator.of(context).pop();
|
||||
}
|
||||
},
|
||||
child: const Text('删除')),
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
@@ -305,8 +314,16 @@ class _SystemSettingsPageState extends ConsumerState<SystemSettingsPage> {
|
||||
TextButton(
|
||||
child: const Text('确定'),
|
||||
onPressed: () {
|
||||
addDownloadClients(
|
||||
context, nameController.text, urlController.text);
|
||||
var f = ref
|
||||
.read(dwonloadClientsProvider.notifier)
|
||||
.addDownloadClients(
|
||||
nameController.text, urlController.text);
|
||||
setState(() {
|
||||
_pendingDownloadClient = f;
|
||||
});
|
||||
if (!showError(snapshot)) {
|
||||
Navigator.of(context).pop();
|
||||
}
|
||||
},
|
||||
),
|
||||
],
|
||||
@@ -314,33 +331,13 @@ class _SystemSettingsPageState extends ConsumerState<SystemSettingsPage> {
|
||||
});
|
||||
}
|
||||
|
||||
void addDownloadClients(BuildContext context, String name, String url) async {
|
||||
if (name.isEmpty || url.isEmpty) {
|
||||
return;
|
||||
bool showError(AsyncSnapshot<void> snapshot) {
|
||||
final isErrored = snapshot.hasError &&
|
||||
snapshot.connectionState != ConnectionState.waiting;
|
||||
if (isErrored) {
|
||||
Utils.showSnakeBar(context, "当前操作出错: ${snapshot.error}");
|
||||
return true;
|
||||
}
|
||||
var dio = Dio();
|
||||
var resp = await dio.post(APIs.addDownloadClientUrl, data: {
|
||||
"name": name,
|
||||
"url": url,
|
||||
});
|
||||
var sp = ServerResponse.fromJson(resp.data);
|
||||
if (sp.code != 0 && context.mounted) {
|
||||
Utils.showAlertDialog(context, sp.message);
|
||||
return;
|
||||
}
|
||||
Navigator.of(context).pop();
|
||||
ref.refresh(dwonloadClientsProvider);
|
||||
}
|
||||
|
||||
void deleteDownloadClients(BuildContext context, int id) async {
|
||||
var dio = Dio();
|
||||
var resp = await dio.delete("${APIs.delDownloadClientUrl}$id");
|
||||
var sp = ServerResponse.fromJson(resp.data);
|
||||
if (sp.code != 0 && context.mounted) {
|
||||
Utils.showAlertDialog(context, sp.message);
|
||||
return;
|
||||
}
|
||||
Navigator.of(context).pop();
|
||||
ref.refresh(dwonloadClientsProvider);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user