mirror of
https://github.com/simon-ding/polaris.git
synced 2026-04-21 19:27:30 +08:00
feat: add bark push notification
This commit is contained in:
@@ -27,12 +27,24 @@ class _NotifierState extends ConsumerState<NotifierSettings> {
|
||||
if (i < v.length) {
|
||||
final client = v[i];
|
||||
return SettingsCard(
|
||||
child: Text("${client.name!} (${client.service})"),
|
||||
onTap: () => showNotifierDetails(client),
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Text(
|
||||
client.name!,
|
||||
style: TextStyle(fontSize: 20, height: 3),
|
||||
),
|
||||
Opacity(
|
||||
opacity: 0.5,
|
||||
child: Text(client.service!),
|
||||
)
|
||||
],
|
||||
),
|
||||
onTap: () => showNotifierAccordingToService(client),
|
||||
);
|
||||
}
|
||||
return SettingsCard(
|
||||
onTap: () => showNotifierDetails(NotifierData()),
|
||||
onTap: () => showSelections(),
|
||||
child: const Icon(Icons.add));
|
||||
}),
|
||||
),
|
||||
@@ -40,14 +52,122 @@ class _NotifierState extends ConsumerState<NotifierSettings> {
|
||||
loading: () => const MyProgressIndicator());
|
||||
}
|
||||
|
||||
Future<void> showNotifierDetails(NotifierData notifier) {
|
||||
Future<void> showSelections() {
|
||||
return showDialog<void>(
|
||||
context: context,
|
||||
barrierDismissible: true,
|
||||
builder: (BuildContext context) {
|
||||
return AlertDialog(
|
||||
content: SizedBox(
|
||||
height: 500,
|
||||
width: 500,
|
||||
child: Wrap(
|
||||
children: [
|
||||
SettingsCard(
|
||||
child: InkWell(
|
||||
child: const Center(
|
||||
child: Text("Pushover"),
|
||||
),
|
||||
onTap: () {
|
||||
Navigator.of(context).pop();
|
||||
showPushoverNotifierDetails(NotifierData());
|
||||
},
|
||||
),
|
||||
),
|
||||
SettingsCard(
|
||||
child: InkWell(
|
||||
child: const Center(
|
||||
child: Text("Bark"),
|
||||
),
|
||||
onTap: () {
|
||||
Navigator.of(context).pop();
|
||||
showBarkNotifierDetails(NotifierData());
|
||||
},
|
||||
),
|
||||
)
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> showNotifierAccordingToService(NotifierData notifier) {
|
||||
switch (notifier.service) {
|
||||
case "bark":
|
||||
return showBarkNotifierDetails(notifier);
|
||||
case "pushover":
|
||||
return showPushoverNotifierDetails(notifier);
|
||||
}
|
||||
return Future<void>.value();
|
||||
}
|
||||
|
||||
Future<void> showBarkNotifierDetails(NotifierData notifier) {
|
||||
final _formKey = GlobalKey<FormBuilderState>();
|
||||
|
||||
var body = FormBuilder(
|
||||
key: _formKey,
|
||||
initialValue: {
|
||||
"name": notifier.name,
|
||||
"enabled": notifier.enabled ?? true,
|
||||
"device_key":
|
||||
notifier.settings != null ? notifier.settings!["device_key"] : "",
|
||||
"url": notifier.settings != null ? notifier.settings!["url"] : "",
|
||||
},
|
||||
child: Column(
|
||||
children: [
|
||||
FormBuilderTextField(
|
||||
name: "name",
|
||||
decoration: Commons.requiredTextFieldStyle(text: "名称"),
|
||||
autovalidateMode: AutovalidateMode.onUserInteraction,
|
||||
validator: FormBuilderValidators.required(),
|
||||
),
|
||||
FormBuilderTextField(
|
||||
name: "url",
|
||||
decoration: const InputDecoration(
|
||||
labelText: "服务器地址", helperText: "留空使用默认地址"),
|
||||
),
|
||||
FormBuilderTextField(
|
||||
name: "device_key",
|
||||
decoration: Commons.requiredTextFieldStyle(text: "Device Key"),
|
||||
autovalidateMode: AutovalidateMode.onUserInteraction,
|
||||
validator: FormBuilderValidators.required(),
|
||||
),
|
||||
FormBuilderSwitch(name: "enabled", title: const Text("启用"))
|
||||
],
|
||||
),
|
||||
);
|
||||
onDelete() async {
|
||||
return ref.read(notifiersDataProvider.notifier).delete(notifier.id!);
|
||||
}
|
||||
|
||||
onSubmit() async {
|
||||
if (_formKey.currentState!.saveAndValidate()) {
|
||||
var values = _formKey.currentState!.value;
|
||||
return ref.read(notifiersDataProvider.notifier).add(NotifierData(
|
||||
name: values["name"],
|
||||
service: "bark",
|
||||
enabled: values["enabled"],
|
||||
settings: {
|
||||
"device_key": values["device_key"],
|
||||
"url": values["url"]
|
||||
}));
|
||||
} else {
|
||||
throw "validation_error";
|
||||
}
|
||||
}
|
||||
|
||||
return showSettingDialog(
|
||||
context, "Bark", notifier.id != null, body, onSubmit, onDelete);
|
||||
}
|
||||
|
||||
Future<void> showPushoverNotifierDetails(NotifierData notifier) {
|
||||
final _formKey = GlobalKey<FormBuilderState>();
|
||||
|
||||
var body = FormBuilder(
|
||||
key: _formKey,
|
||||
initialValue: {
|
||||
"name": notifier.name,
|
||||
"service": notifier.service,
|
||||
"enabled": notifier.enabled ?? true,
|
||||
"app_token":
|
||||
notifier.settings != null ? notifier.settings!["app_token"] : "",
|
||||
@@ -56,13 +176,6 @@ class _NotifierState extends ConsumerState<NotifierSettings> {
|
||||
},
|
||||
child: Column(
|
||||
children: [
|
||||
FormBuilderDropdown(
|
||||
name: "service",
|
||||
decoration: const InputDecoration(labelText: "类型"),
|
||||
items: const [
|
||||
DropdownMenuItem(value: "pushover", child: Text("Pushover")),
|
||||
],
|
||||
),
|
||||
FormBuilderTextField(
|
||||
name: "name",
|
||||
decoration: Commons.requiredTextFieldStyle(text: "名称"),
|
||||
@@ -94,7 +207,7 @@ class _NotifierState extends ConsumerState<NotifierSettings> {
|
||||
var values = _formKey.currentState!.value;
|
||||
return ref.read(notifiersDataProvider.notifier).add(NotifierData(
|
||||
name: values["name"],
|
||||
service: values["service"],
|
||||
service: "pushover",
|
||||
enabled: values["enabled"],
|
||||
settings: {
|
||||
"app_token": values["app_token"],
|
||||
@@ -105,9 +218,7 @@ class _NotifierState extends ConsumerState<NotifierSettings> {
|
||||
}
|
||||
}
|
||||
|
||||
return showSettingDialog(context,
|
||||
"通知客户端", notifier.id != null, body, onSubmit, onDelete);
|
||||
return showSettingDialog(
|
||||
context, "Pushover", notifier.id != null, body, onSubmit, onDelete);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user