mirror of
https://gitlab.com/famedly/fluffychat.git
synced 2025-01-11 18:22:49 +01:00
refactor: MVC Settings Notifications
This commit is contained in:
parent
a64ada50d4
commit
c291b08d59
@ -24,7 +24,7 @@ import 'package:fluffychat/views/ui/settings_ui.dart';
|
||||
import 'package:fluffychat/views/settings_3pid.dart';
|
||||
import 'package:fluffychat/views/device_settings.dart';
|
||||
import 'package:fluffychat/views/settings_ignore_list.dart';
|
||||
import 'package:fluffychat/views/ui/settings_notifications_ui.dart';
|
||||
import 'package:fluffychat/views/settings_notifications.dart';
|
||||
import 'package:fluffychat/views/settings_style.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
|
112
lib/views/settings_notifications.dart
Normal file
112
lib/views/settings_notifications.dart
Normal file
@ -0,0 +1,112 @@
|
||||
import 'package:famedlysdk/famedlysdk.dart';
|
||||
import 'package:future_loading_dialog/future_loading_dialog.dart';
|
||||
import 'package:fluffychat/config/app_config.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||
import 'package:open_noti_settings/open_noti_settings.dart';
|
||||
|
||||
import 'ui/settings_notifications_ui.dart';
|
||||
import 'widgets/matrix.dart';
|
||||
|
||||
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',
|
||||
(c) => L10n.of(c).directChats,
|
||||
),
|
||||
NotificationSettingsItem(
|
||||
PushRuleKind.override,
|
||||
'.m.rule.contains_display_name',
|
||||
(c) => L10n.of(c).containsDisplayName,
|
||||
),
|
||||
NotificationSettingsItem(
|
||||
PushRuleKind.content,
|
||||
'.m.rule.contains_user_name',
|
||||
(c) => L10n.of(c).containsUserName,
|
||||
),
|
||||
NotificationSettingsItem(
|
||||
PushRuleKind.override,
|
||||
'.m.rule.invite_for_me',
|
||||
(c) => L10n.of(c).inviteForMe,
|
||||
),
|
||||
NotificationSettingsItem(
|
||||
PushRuleKind.override,
|
||||
'.m.rule.member_event',
|
||||
(c) => L10n.of(c).memberChanges,
|
||||
),
|
||||
NotificationSettingsItem(
|
||||
PushRuleKind.override,
|
||||
'.m.rule.suppress_notices',
|
||||
(c) => L10n.of(c).botMessages,
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
class SettingsNotifications extends StatefulWidget {
|
||||
@override
|
||||
SettingsNotificationsController createState() =>
|
||||
SettingsNotificationsController();
|
||||
}
|
||||
|
||||
class SettingsNotificationsController extends State<SettingsNotifications> {
|
||||
void openAndroidNotificationSettingsAction() async {
|
||||
await NotificationSetting.configureChannel(
|
||||
NotificationDetails(
|
||||
android: AndroidNotificationDetails(
|
||||
AppConfig.pushNotificationsChannelId,
|
||||
AppConfig.pushNotificationsChannelName,
|
||||
AppConfig.pushNotificationsChannelDescription,
|
||||
),
|
||||
),
|
||||
);
|
||||
return NotificationSetting.open();
|
||||
}
|
||||
|
||||
bool getNotificationSetting(NotificationSettingsItem item) {
|
||||
final pushRules = Matrix.of(context).client.globalPushRules;
|
||||
switch (item.type) {
|
||||
case PushRuleKind.content:
|
||||
return pushRules.content
|
||||
?.singleWhere((r) => r.ruleId == item.key, orElse: () => null)
|
||||
?.enabled;
|
||||
case PushRuleKind.override:
|
||||
return pushRules.override
|
||||
?.singleWhere((r) => r.ruleId == item.key, orElse: () => null)
|
||||
?.enabled;
|
||||
case PushRuleKind.room:
|
||||
return pushRules.room
|
||||
?.singleWhere((r) => r.ruleId == item.key, orElse: () => null)
|
||||
?.enabled;
|
||||
case PushRuleKind.sender:
|
||||
return pushRules.sender
|
||||
?.singleWhere((r) => r.ruleId == item.key, orElse: () => null)
|
||||
?.enabled;
|
||||
case PushRuleKind.underride:
|
||||
return pushRules.underride
|
||||
?.singleWhere((r) => r.ruleId == item.key, orElse: () => null)
|
||||
?.enabled;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void setNotificationSetting(NotificationSettingsItem item, bool enabled) {
|
||||
showFutureLoadingDialog(
|
||||
context: context,
|
||||
future: () => Matrix.of(context).client.enablePushRule(
|
||||
'global',
|
||||
item.type,
|
||||
item.key,
|
||||
enabled,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => SettingsNotificationsUI(this);
|
||||
}
|
@ -3,109 +3,18 @@ import 'dart:io';
|
||||
import 'package:famedlysdk/famedlysdk.dart';
|
||||
import 'package:fluffychat/views/widgets/max_width_body.dart';
|
||||
import 'package:future_loading_dialog/future_loading_dialog.dart';
|
||||
import 'package:fluffychat/config/app_config.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||
import 'package:open_noti_settings/open_noti_settings.dart';
|
||||
import '../../utils/localized_exception_extension.dart';
|
||||
|
||||
import '../settings_notifications.dart';
|
||||
import '../widgets/matrix.dart';
|
||||
|
||||
class NotificationSettingsItem {
|
||||
final PushRuleKind type;
|
||||
final String key;
|
||||
final String Function(BuildContext) title;
|
||||
NotificationSettingsItem(this.type, this.key, this.title);
|
||||
}
|
||||
class SettingsNotificationsUI extends StatelessWidget {
|
||||
final SettingsNotificationsController controller;
|
||||
|
||||
class SettingsNotifications extends StatelessWidget {
|
||||
static List<NotificationSettingsItem> items = [
|
||||
NotificationSettingsItem(
|
||||
PushRuleKind.underride,
|
||||
'.m.rule.room_one_to_one',
|
||||
(c) => L10n.of(c).directChats,
|
||||
),
|
||||
NotificationSettingsItem(
|
||||
PushRuleKind.override,
|
||||
'.m.rule.contains_display_name',
|
||||
(c) => L10n.of(c).containsDisplayName,
|
||||
),
|
||||
NotificationSettingsItem(
|
||||
PushRuleKind.content,
|
||||
'.m.rule.contains_user_name',
|
||||
(c) => L10n.of(c).containsUserName,
|
||||
),
|
||||
NotificationSettingsItem(
|
||||
PushRuleKind.override,
|
||||
'.m.rule.invite_for_me',
|
||||
(c) => L10n.of(c).inviteForMe,
|
||||
),
|
||||
NotificationSettingsItem(
|
||||
PushRuleKind.override,
|
||||
'.m.rule.member_event',
|
||||
(c) => L10n.of(c).memberChanges,
|
||||
),
|
||||
NotificationSettingsItem(
|
||||
PushRuleKind.override,
|
||||
'.m.rule.suppress_notices',
|
||||
(c) => L10n.of(c).botMessages,
|
||||
),
|
||||
];
|
||||
void _openAndroidNotificationSettingsAction() async {
|
||||
await NotificationSetting.configureChannel(
|
||||
NotificationDetails(
|
||||
android: AndroidNotificationDetails(
|
||||
AppConfig.pushNotificationsChannelId,
|
||||
AppConfig.pushNotificationsChannelName,
|
||||
AppConfig.pushNotificationsChannelDescription,
|
||||
),
|
||||
),
|
||||
);
|
||||
return NotificationSetting.open();
|
||||
}
|
||||
|
||||
bool _getNotificationSetting(
|
||||
BuildContext context, NotificationSettingsItem item) {
|
||||
final pushRules = Matrix.of(context).client.globalPushRules;
|
||||
switch (item.type) {
|
||||
case PushRuleKind.content:
|
||||
return pushRules.content
|
||||
?.singleWhere((r) => r.ruleId == item.key, orElse: () => null)
|
||||
?.enabled;
|
||||
case PushRuleKind.override:
|
||||
return pushRules.override
|
||||
?.singleWhere((r) => r.ruleId == item.key, orElse: () => null)
|
||||
?.enabled;
|
||||
case PushRuleKind.room:
|
||||
return pushRules.room
|
||||
?.singleWhere((r) => r.ruleId == item.key, orElse: () => null)
|
||||
?.enabled;
|
||||
case PushRuleKind.sender:
|
||||
return pushRules.sender
|
||||
?.singleWhere((r) => r.ruleId == item.key, orElse: () => null)
|
||||
?.enabled;
|
||||
case PushRuleKind.underride:
|
||||
return pushRules.underride
|
||||
?.singleWhere((r) => r.ruleId == item.key, orElse: () => null)
|
||||
?.enabled;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void _setNotificationSetting(
|
||||
BuildContext context, NotificationSettingsItem item, bool enabled) {
|
||||
showFutureLoadingDialog(
|
||||
context: context,
|
||||
future: () => Matrix.of(context).client.enablePushRule(
|
||||
'global',
|
||||
item.type,
|
||||
item.key,
|
||||
enabled,
|
||||
),
|
||||
);
|
||||
}
|
||||
const SettingsNotificationsUI(this.controller, {Key key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@ -149,7 +58,7 @@ class SettingsNotifications extends StatelessWidget {
|
||||
foregroundColor: Colors.grey,
|
||||
child: Icon(Icons.edit_outlined),
|
||||
),
|
||||
onTap: () => _openAndroidNotificationSettingsAction(),
|
||||
onTap: controller.openAndroidNotificationSettingsAction,
|
||||
),
|
||||
Divider(thickness: 1),
|
||||
ListTile(
|
||||
@ -161,12 +70,12 @@ class SettingsNotifications extends StatelessWidget {
|
||||
),
|
||||
),
|
||||
),
|
||||
for (var item in items)
|
||||
for (var item in NotificationSettingsItem.items)
|
||||
SwitchListTile(
|
||||
value: _getNotificationSetting(context, item) ?? true,
|
||||
value: controller.getNotificationSetting(item) ?? true,
|
||||
title: Text(item.title(context)),
|
||||
onChanged: (bool enabled) =>
|
||||
_setNotificationSetting(context, item, enabled),
|
||||
controller.setNotificationSetting(item, enabled),
|
||||
),
|
||||
},
|
||||
Divider(thickness: 1),
|
||||
|
Loading…
Reference in New Issue
Block a user