feat: Implement rich notification settings
parent
d5fe9a257a
commit
ef8638eede
@ -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),
|
||||
),
|
||||
}
|
||||
],
|
||||
);
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
@ -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),
|
||||
);
|
||||
}),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue