feat: Implement rich notification settings

This commit is contained in:
Christian Pauly 2020-11-25 20:39:54 +01:00
commit ef8638eede
7 changed files with 409 additions and 124 deletions

View file

@ -64,20 +64,18 @@ class ThemesSettingsState extends State<ThemesSettings> {
});
},
),
ListTile(
SwitchListTile(
title: Text(
L10n.of(context).useAmoledTheme,
),
trailing: Switch(
value: _amoledEnabled,
activeColor: Theme.of(context).primaryColor,
onChanged: (bool value) {
setState(() {
_amoledEnabled = value;
themeEngine.switchTheme(matrix, _selectedTheme, value);
});
},
),
value: _amoledEnabled,
activeColor: Theme.of(context).primaryColor,
onChanged: (bool value) {
setState(() {
_amoledEnabled = value;
themeEngine.switchTheme(matrix, _selectedTheme, value);
});
},
),
],
);

View file

@ -559,6 +559,51 @@
"type": "text",
"placeholders": {}
},
"directChats": "Direct Chats",
"@directChats": {
"type": "text",
"placeholders": {}
},
"containsDisplayName": "Contains display name",
"@containsDisplayName": {
"type": "text",
"placeholders": {}
},
"containsUserName": "Contains user name",
"@containsUserName": {
"type": "text",
"placeholders": {}
},
"inviteForMe": "Invite for me",
"@inviteForMe": {
"type": "text",
"placeholders": {}
},
"memberChanges": "Member changes",
"@memberChanges": {
"type": "text",
"placeholders": {}
},
"botMessages": "Bot messages",
"@botMessages": {
"type": "text",
"placeholders": {}
},
"pushRules": "Push rules",
"@pushRules": {
"type": "text",
"placeholders": {}
},
"notifications": "Notifications",
"@notifications": {
"type": "text",
"placeholders": {}
},
"notificationsEnabledForThisAccount": "Notifications enabled for this account",
"@notificationsEnabledForThisAccount": {
"type": "text",
"placeholders": {}
},
"edit": "Edit",
"@edit": {
"type": "text",

View file

@ -1,12 +1,11 @@
import 'dart:io';
import 'package:adaptive_dialog/adaptive_dialog.dart';
import 'package:fluffychat/views/settings_3pid.dart';
import 'package:fluffychat/views/settings_notifications.dart';
import 'package:fluffychat/views/settings_style.dart';
import 'package:flushbar/flushbar_helper.dart';
import 'package:famedlysdk/famedlysdk.dart';
import 'package:file_picker_cross/file_picker_cross.dart';
import 'package:fluffychat/components/settings_themes.dart';
import 'package:fluffychat/config/app_config.dart';
import 'package:fluffychat/utils/platform_infos.dart';
import 'package:fluffychat/utils/sentry_controller.dart';
@ -202,22 +201,6 @@ class _SettingsState extends State<Settings> {
}
}
void setWallpaperAction(BuildContext context) async {
final wallpaper = await ImagePicker().getImage(source: ImageSource.gallery);
if (wallpaper == null) return;
Matrix.of(context).wallpaper = File(wallpaper.path);
await Matrix.of(context)
.store
.setItem(SettingKeys.wallpaper, wallpaper.path);
setState(() => null);
}
void deleteWallpaperAction(BuildContext context) async {
Matrix.of(context).wallpaper = null;
await Matrix.of(context).store.deleteItem(SettingKeys.wallpaper);
setState(() => null);
}
Future<void> requestSSSSCache(BuildContext context) async {
final handle = Matrix.of(context).client.encryption.ssss.open();
final input = await showTextInputDialog(
@ -323,48 +306,23 @@ class _SettingsState extends State<Settings> {
children: <Widget>[
ListTile(
title: Text(
L10n.of(context).changeTheme,
L10n.of(context).notifications,
style: TextStyle(
color: Theme.of(context).primaryColor,
fontWeight: FontWeight.bold,
),
),
),
ThemesSettings(),
if (!kIsWeb && Matrix.of(context).store != null)
Divider(thickness: 1),
if (!kIsWeb && Matrix.of(context).store != null)
ListTile(
title: Text(
L10n.of(context).wallpaper,
style: TextStyle(
color: Theme.of(context).primaryColor,
fontWeight: FontWeight.bold,
),
ListTile(
trailing: Icon(Icons.notifications),
title: Text(L10n.of(context).notifications),
onTap: () async => await Navigator.of(context).push(
AppRoute.defaultRoute(
context,
SettingsNotificationsView(),
),
),
if (Matrix.of(context).wallpaper != null)
ListTile(
title: Image.file(
Matrix.of(context).wallpaper,
height: 38,
fit: BoxFit.cover,
),
trailing: Icon(
Icons.delete_forever,
color: Colors.red,
),
onTap: () => deleteWallpaperAction(context),
),
if (!kIsWeb && Matrix.of(context).store != null)
Builder(builder: (context) {
return ListTile(
title: Text(L10n.of(context).changeWallpaper),
trailing: Icon(Icons.wallpaper),
onTap: () => setWallpaperAction(context),
);
}),
Divider(thickness: 1),
),
ListTile(
title: Text(
L10n.of(context).chat,
@ -375,44 +333,45 @@ class _SettingsState extends State<Settings> {
),
),
ListTile(
title: Text(L10n.of(context).changeTheme),
onTap: () async => await Navigator.of(context).push(
AppRoute.defaultRoute(
context,
SettingsStyleView(),
),
),
trailing: Icon(Icons.wallpaper),
),
SwitchListTile(
title: Text(L10n.of(context).renderRichContent),
trailing: Switch(
value: AppConfig.renderHtml,
activeColor: Theme.of(context).primaryColor,
onChanged: (bool newValue) async {
AppConfig.renderHtml = newValue;
await Matrix.of(context)
.store
.setItem(SettingKeys.renderHtml, newValue.toString());
setState(() => null);
},
),
value: AppConfig.renderHtml,
onChanged: (bool newValue) async {
AppConfig.renderHtml = newValue;
await Matrix.of(context)
.store
.setItem(SettingKeys.renderHtml, newValue.toString());
setState(() => null);
},
),
ListTile(
SwitchListTile(
title: Text(L10n.of(context).hideRedactedEvents),
trailing: Switch(
value: AppConfig.hideRedactedEvents,
activeColor: Theme.of(context).primaryColor,
onChanged: (bool newValue) async {
AppConfig.hideRedactedEvents = newValue;
await Matrix.of(context).store.setItem(
SettingKeys.hideRedactedEvents, newValue.toString());
setState(() => null);
},
),
value: AppConfig.hideRedactedEvents,
onChanged: (bool newValue) async {
AppConfig.hideRedactedEvents = newValue;
await Matrix.of(context).store.setItem(
SettingKeys.hideRedactedEvents, newValue.toString());
setState(() => null);
},
),
ListTile(
SwitchListTile(
title: Text(L10n.of(context).hideUnknownEvents),
trailing: Switch(
value: AppConfig.hideUnknownEvents,
activeColor: Theme.of(context).primaryColor,
onChanged: (bool newValue) async {
AppConfig.hideUnknownEvents = newValue;
await Matrix.of(context).store.setItem(
SettingKeys.hideUnknownEvents, newValue.toString());
setState(() => null);
},
),
value: AppConfig.hideUnknownEvents,
onChanged: (bool newValue) async {
AppConfig.hideUnknownEvents = newValue;
await Matrix.of(context).store.setItem(
SettingKeys.hideUnknownEvents, newValue.toString());
setState(() => null);
},
),
ListTile(
title: Text(L10n.of(context).emoteSettings),

View file

@ -0,0 +1,181 @@
import 'dart:io';
import 'package:famedlysdk/famedlysdk.dart';
import 'package:fluffychat/components/adaptive_page_layout.dart';
import 'package:fluffychat/components/dialogs/simple_dialogs.dart';
import 'package:fluffychat/utils/firebase_controller.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 '../components/matrix.dart';
import 'chat_list.dart';
class NotificationSettingsItem {
final PushRuleKind type;
final String key;
final String Function(BuildContext) title;
NotificationSettingsItem(this.type, this.key, this.title);
}
class SettingsNotificationsView extends StatelessWidget {
@override
Widget build(BuildContext context) {
return AdaptivePageLayout(
primaryPage: FocusPage.SECOND,
firstScaffold: ChatList(),
secondScaffold: SettingsNotifications(),
);
}
}
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(
FirebaseController.CHANNEL_ID,
FirebaseController.CHANNEL_NAME,
FirebaseController.CHANNEL_DESCRIPTION,
),
),
);
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) {
SimpleDialogs(context).tryRequestWithLoadingDialog(
Matrix.of(context).client.enablePushRule(
'global',
item.type,
item.key,
enabled,
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(L10n.of(context).notifications),
),
body: StreamBuilder(
stream: Matrix.of(context)
.client
.onAccountData
.stream
.where((event) => event.type == 'm.push_rules'),
builder: (BuildContext context, _) {
return ListView(
children: [
SwitchListTile(
value: !Matrix.of(context).client.allPushNotificationsMuted,
title:
Text(L10n.of(context).notificationsEnabledForThisAccount),
onChanged: (_) =>
SimpleDialogs(context).tryRequestWithLoadingDialog(
Matrix.of(context).client.setMuteAllPushNotifications(
!Matrix.of(context).client.allPushNotificationsMuted,
),
),
),
if (!Matrix.of(context).client.allPushNotificationsMuted) ...{
if (!kIsWeb && Platform.isAndroid)
ListTile(
title: Text('Ton, Vibration, LED-Farbe'),
trailing: CircleAvatar(
backgroundColor:
Theme.of(context).scaffoldBackgroundColor,
foregroundColor: Colors.grey,
child: Icon(Icons.edit_outlined),
),
onTap: () => _openAndroidNotificationSettingsAction(),
),
Divider(thickness: 1),
ListTile(
title: Text(
L10n.of(context).pushRules,
style: TextStyle(
color: Theme.of(context).primaryColor,
fontWeight: FontWeight.bold,
),
),
),
for (var item in items)
SwitchListTile(
value: _getNotificationSetting(context, item) ?? true,
title: Text(item.title(context)),
onChanged: (bool enabled) =>
_setNotificationSetting(context, item, enabled),
),
}
],
);
}),
);
}
}

View file

@ -0,0 +1,89 @@
import 'dart:io';
import 'package:fluffychat/components/adaptive_page_layout.dart';
import 'package:fluffychat/components/settings_themes.dart';
import 'package:fluffychat/config/setting_keys.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:image_picker/image_picker.dart';
import '../components/matrix.dart';
import 'chat_list.dart';
class SettingsStyleView extends StatelessWidget {
@override
Widget build(BuildContext context) {
return AdaptivePageLayout(
primaryPage: FocusPage.SECOND,
firstScaffold: ChatList(),
secondScaffold: SettingsStyle(),
);
}
}
class SettingsStyle extends StatefulWidget {
@override
_SettingsStyleState createState() => _SettingsStyleState();
}
class _SettingsStyleState extends State<SettingsStyle> {
void setWallpaperAction(BuildContext context) async {
final wallpaper = await ImagePicker().getImage(source: ImageSource.gallery);
if (wallpaper == null) return;
Matrix.of(context).wallpaper = File(wallpaper.path);
await Matrix.of(context)
.store
.setItem(SettingKeys.wallpaper, wallpaper.path);
setState(() => null);
}
void deleteWallpaperAction(BuildContext context) async {
Matrix.of(context).wallpaper = null;
await Matrix.of(context).store.deleteItem(SettingKeys.wallpaper);
setState(() => null);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(L10n.of(context).changeTheme),
),
body: ListView(
children: [
ThemesSettings(),
Divider(thickness: 1),
ListTile(
title: Text(
L10n.of(context).wallpaper,
style: TextStyle(
color: Theme.of(context).primaryColor,
fontWeight: FontWeight.bold,
),
),
),
if (Matrix.of(context).wallpaper != null)
ListTile(
title: Image.file(
Matrix.of(context).wallpaper,
height: 38,
fit: BoxFit.cover,
),
trailing: Icon(
Icons.delete_forever,
color: Colors.red,
),
onTap: () => deleteWallpaperAction(context),
),
Builder(builder: (context) {
return ListTile(
title: Text(L10n.of(context).changeWallpaper),
trailing: Icon(Icons.wallpaper),
onTap: () => setWallpaperAction(context),
);
}),
],
),
);
}
}