fluffychat/lib/pages/settings_notifications/settings_notifications.dart

136 lines
3.9 KiB
Dart
Raw Normal View History

2021-04-24 08:14:53 +02:00
import 'package:flutter/material.dart';
2021-10-26 18:50:34 +02:00
import 'package:adaptive_dialog/adaptive_dialog.dart';
2022-01-29 12:35:03 +01:00
import 'package:collection/collection.dart' show IterableExtension;
2021-04-24 08:14:53 +02:00
import 'package:flutter_gen/gen_l10n/l10n.dart';
2021-10-26 18:50:34 +02:00
import 'package:future_loading_dialog/future_loading_dialog.dart';
import 'package:matrix/matrix.dart';
2021-04-24 08:14:53 +02:00
2021-11-09 21:32:16 +01:00
import '../../widgets/matrix.dart';
import 'settings_notifications_view.dart';
2021-04-24 08:14:53 +02:00
class NotificationSettingsItem {
final PushRuleKind type;
final String key;
final String Function(BuildContext) title;
const NotificationSettingsItem(this.type, this.key, this.title);
static List<NotificationSettingsItem> items = [
NotificationSettingsItem(
PushRuleKind.underride,
'.m.rule.room_one_to_one',
2022-01-29 12:35:03 +01:00
(c) => L10n.of(c)!.directChats,
2021-04-24 08:14:53 +02:00
),
NotificationSettingsItem(
PushRuleKind.override,
'.m.rule.contains_display_name',
2022-01-29 12:35:03 +01:00
(c) => L10n.of(c)!.containsDisplayName,
2021-04-24 08:14:53 +02:00
),
NotificationSettingsItem(
PushRuleKind.content,
'.m.rule.contains_user_name',
2022-01-29 12:35:03 +01:00
(c) => L10n.of(c)!.containsUserName,
2021-04-24 08:14:53 +02:00
),
NotificationSettingsItem(
PushRuleKind.override,
'.m.rule.invite_for_me',
2022-01-29 12:35:03 +01:00
(c) => L10n.of(c)!.inviteForMe,
2021-04-24 08:14:53 +02:00
),
NotificationSettingsItem(
PushRuleKind.override,
'.m.rule.member_event',
2022-01-29 12:35:03 +01:00
(c) => L10n.of(c)!.memberChanges,
2021-04-24 08:14:53 +02:00
),
NotificationSettingsItem(
PushRuleKind.override,
'.m.rule.suppress_notices',
2022-01-29 12:35:03 +01:00
(c) => L10n.of(c)!.botMessages,
2021-04-24 08:14:53 +02:00
),
];
}
class SettingsNotifications extends StatefulWidget {
2022-01-29 12:35:03 +01:00
const SettingsNotifications({Key? key}) : super(key: key);
2021-10-14 18:09:30 +02:00
2021-04-24 08:14:53 +02:00
@override
SettingsNotificationsController createState() =>
SettingsNotificationsController();
}
class SettingsNotificationsController extends State<SettingsNotifications> {
2022-01-29 12:35:03 +01:00
bool? getNotificationSetting(NotificationSettingsItem item) {
2023-02-02 09:47:41 +01:00
final pushRules = Matrix.of(context).client.globalPushRules;
if (pushRules == null) return null;
2021-04-24 08:14:53 +02:00
switch (item.type) {
case PushRuleKind.content:
2022-09-06 20:21:11 +02:00
return pushRules.content
2022-01-29 12:35:03 +01:00
?.singleWhereOrNull((r) => r.ruleId == item.key)
2021-04-24 08:14:53 +02:00
?.enabled;
case PushRuleKind.override:
2022-09-06 20:21:11 +02:00
return pushRules.override
2022-01-29 12:35:03 +01:00
?.singleWhereOrNull((r) => r.ruleId == item.key)
2021-04-24 08:14:53 +02:00
?.enabled;
case PushRuleKind.room:
2022-09-06 20:21:11 +02:00
return pushRules.room
2022-01-29 12:35:03 +01:00
?.singleWhereOrNull((r) => r.ruleId == item.key)
2021-04-24 08:14:53 +02:00
?.enabled;
case PushRuleKind.sender:
2022-09-06 20:21:11 +02:00
return pushRules.sender
2022-01-29 12:35:03 +01:00
?.singleWhereOrNull((r) => r.ruleId == item.key)
2021-04-24 08:14:53 +02:00
?.enabled;
case PushRuleKind.underride:
2022-09-06 20:21:11 +02:00
return pushRules.underride
2022-01-29 12:35:03 +01:00
?.singleWhereOrNull((r) => r.ruleId == item.key)
2021-04-24 08:14:53 +02:00
?.enabled;
}
}
void setNotificationSetting(NotificationSettingsItem item, bool enabled) {
showFutureLoadingDialog(
context: context,
2021-05-20 13:59:55 +02:00
future: () => Matrix.of(context).client.setPushRuleEnabled(
2021-04-24 08:14:53 +02:00
'global',
item.type,
item.key,
enabled,
),
);
}
void onPusherTap(Pusher pusher) async {
final delete = await showModalActionSheet<bool>(
context: context,
title: pusher.deviceDisplayName,
message: '${pusher.appDisplayName} (${pusher.appId})',
actions: [
SheetAction(
label: L10n.of(context)!.delete,
isDestructiveAction: true,
key: true,
)
],
);
if (delete != true) return;
final success = await showFutureLoadingDialog(
context: context,
future: () => Matrix.of(context).client.deletePusher(
PusherId(
appId: pusher.appId,
pushkey: pusher.pushkey,
),
),
);
if (success.error != null) return;
setState(() {
pusherFuture = null;
});
}
Future<List<Pusher>?>? pusherFuture;
2021-04-24 08:14:53 +02:00
@override
2021-05-22 09:13:47 +02:00
Widget build(BuildContext context) => SettingsNotificationsView(this);
2021-04-24 08:14:53 +02:00
}