Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,39 @@ import 'package:matrix/matrix.dart';

import 'settings_notifications_view.dart';

enum PushRule {
roomOneToOne(ruleKey: '.m.rule.room_one_to_one'),
roomEncryptedOneToOne(ruleKey: '.m.rule.encrypted_room_one_to_one'),
containsDisplayName(ruleKey: '.m.rule.contains_display_name'),
containsUserName(ruleKey: '.m.rule.contains_user_name'),
inviteForMe(ruleKey: '.m.rule.invite_for_me'),
memberEvent(ruleKey: '.m.rule.member_event'),
suppressNotices(ruleKey: '.m.rule.suppress_notices'),
;

const PushRule({required this.ruleKey});

final String ruleKey;

String getRuleTitle(L10n l10n) {
switch (this) {
case PushRule.roomOneToOne:
case PushRule.roomEncryptedOneToOne:
return l10n.directChats;
case PushRule.containsDisplayName:
return l10n.containsDisplayName;
case PushRule.containsUserName:
return l10n.containsUserName;
case PushRule.inviteForMe:
return l10n.inviteForMe;
case PushRule.memberEvent:
return l10n.memberChanges;
case PushRule.suppressNotices:
return l10n.botMessages;
}
}
}

class NotificationSettingsItem {
final PushRuleKind type;
final String key;
Expand All @@ -18,33 +51,33 @@ class NotificationSettingsItem {
static List<NotificationSettingsItem> items = [
NotificationSettingsItem(
PushRuleKind.underride,
'.m.rule.room_one_to_one',
(c) => L10n.of(c)!.directChats,
PushRule.roomOneToOne.ruleKey,
(c) => PushRule.roomOneToOne.getRuleTitle(L10n.of(c)!),
),
NotificationSettingsItem(
PushRuleKind.override,
'.m.rule.contains_display_name',
(c) => L10n.of(c)!.containsDisplayName,
PushRule.containsDisplayName.ruleKey,
(c) => PushRule.containsDisplayName.getRuleTitle(L10n.of(c)!),
),
NotificationSettingsItem(
PushRuleKind.content,
'.m.rule.contains_user_name',
(c) => L10n.of(c)!.containsUserName,
PushRule.containsUserName.ruleKey,
(c) => PushRule.containsUserName.getRuleTitle(L10n.of(c)!),
),
NotificationSettingsItem(
PushRuleKind.override,
'.m.rule.invite_for_me',
(c) => L10n.of(c)!.inviteForMe,
PushRule.inviteForMe.ruleKey,
(c) => PushRule.inviteForMe.getRuleTitle(L10n.of(c)!),
),
NotificationSettingsItem(
PushRuleKind.override,
'.m.rule.member_event',
(c) => L10n.of(c)!.memberChanges,
PushRule.memberEvent.ruleKey,
(c) => PushRule.memberEvent.getRuleTitle(L10n.of(c)!),
),
NotificationSettingsItem(
PushRuleKind.override,
'.m.rule.suppress_notices',
(c) => L10n.of(c)!.botMessages,
PushRule.suppressNotices.ruleKey,
(c) => PushRule.suppressNotices.getRuleTitle(L10n.of(c)!),
),
];
}
Expand All @@ -61,6 +94,13 @@ class SettingsNotificationsController extends State<SettingsNotifications> {
bool? getNotificationSetting(NotificationSettingsItem item) {
final pushRules = Matrix.of(context).client.globalPushRules;
if (pushRules == null) return null;

if (item.key == PushRule.roomOneToOne.ruleKey) {
return pushRules.underride
?.where((r) => r.ruleId == item.key)
.any((r) => r.actions.isNotEmpty);
}

switch (item.type) {
case PushRuleKind.content:
return pushRules.content
Expand All @@ -85,14 +125,58 @@ class SettingsNotificationsController extends State<SettingsNotifications> {
}
}

static const _oneToOneEnabledActions = [
'notify',
{
"set_tweak": "sound",
"value": "default",
},
{
"set_tweak": "highlight",
"value": false,
}
];

void setNotificationSetting(NotificationSettingsItem item, bool enabled) {
TwakeDialog.showFutureLoadingDialogFullScreen(
future: () => Matrix.of(context).client.setPushRuleEnabled(
'global',
item.type,
item.key,
enabled,
),
future: () {
final client = Matrix.of(context).client;
if (item.key == PushRule.roomOneToOne.ruleKey) {
return Future.wait([
client.setPushRuleEnabled(
'global',
item.type,
item.key,
true,
),
client.setPushRuleEnabled(
'global',
item.type,
PushRule.roomEncryptedOneToOne.ruleKey,
true,
),
client.setPushRuleActions(
'global',
item.type,
item.key,
enabled ? _oneToOneEnabledActions : [],
),
client.setPushRuleActions(
'global',
item.type,
PushRule.roomEncryptedOneToOne.ruleKey,
enabled ? _oneToOneEnabledActions : [],
),
]);
}

return client.setPushRuleEnabled(
'global',
item.type,
item.key,
enabled,
);
},
);
}

Expand Down
Loading