From 15ba251440ef427987cc0c34340d58a468c220f8 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sun, 11 Apr 2021 14:31:38 +0200 Subject: [PATCH] Can update conversation settings from mobile application --- lib/helpers/settings_helper.dart | 25 +++++++ lib/models/notifications_settings.dart | 16 +++++ .../settings/account_settings_route.dart | 9 +++ .../settings/notifications_settings.dart | 67 +++++++++++++++++++ 4 files changed, 117 insertions(+) create mode 100644 lib/models/notifications_settings.dart create mode 100644 lib/ui/routes/settings/notifications_settings.dart diff --git a/lib/helpers/settings_helper.dart b/lib/helpers/settings_helper.dart index 16dfa1f..bc66d12 100644 --- a/lib/helpers/settings_helper.dart +++ b/lib/helpers/settings_helper.dart @@ -4,6 +4,7 @@ import 'package:comunic/models/api_request.dart'; import 'package:comunic/models/data_conservation_policy_settings.dart'; import 'package:comunic/models/general_settings.dart'; import 'package:comunic/models/new_emoji.dart'; +import 'package:comunic/models/notifications_settings.dart'; import 'package:comunic/models/security_settings.dart'; import '../models/api_request.dart'; @@ -232,4 +233,28 @@ class SettingsHelper { .addInt("likes_lifetime", newSettings.likesLifetime ?? 0) .execWithThrow(); } + + /// Get notifications settings + /// + /// Throws in case of failure + static Future getNotificationsSettings() async { + final response = await APIRequest.withLogin("settings/get_notifications") + .execWithThrowGetObject(); + + return NotificationsSettings( + allowConversations: response["allow_conversations"], + allowNotificationsSound: response["allow_notifications_sound"], + ); + } + + /// Apply new notifications settings + /// + /// Throws in case of failure + static Future setNotificationsSettings( + NotificationsSettings settings) async => + await APIRequest.withLogin("settings/set_notifications") + .addBool( + "allow_notifications_sound", settings.allowNotificationsSound) + .addBool("allow_conversations", settings.allowConversations) + .execWithThrow(); } diff --git a/lib/models/notifications_settings.dart b/lib/models/notifications_settings.dart new file mode 100644 index 0000000..e0f5d9d --- /dev/null +++ b/lib/models/notifications_settings.dart @@ -0,0 +1,16 @@ +import 'package:flutter/material.dart'; + +/// Notifications settings +/// +/// @author Pierre Hubert + +class NotificationsSettings { + bool allowConversations; + bool allowNotificationsSound; + + NotificationsSettings({ + @required this.allowConversations, + @required this.allowNotificationsSound, + }) : assert(allowConversations != null), + assert(allowNotificationsSound != null); +} diff --git a/lib/ui/routes/settings/account_settings_route.dart b/lib/ui/routes/settings/account_settings_route.dart index 2d2307d..f8bb765 100644 --- a/lib/ui/routes/settings/account_settings_route.dart +++ b/lib/ui/routes/settings/account_settings_route.dart @@ -4,6 +4,7 @@ import 'package:comunic/ui/routes/settings/account_security_settings.dart'; import 'package:comunic/ui/routes/settings/application_settings.dart'; import 'package:comunic/ui/routes/settings/custom_emojies_account_settings.dart'; import 'package:comunic/ui/routes/settings/general_account_settings.dart'; +import 'package:comunic/ui/routes/settings/notifications_settings.dart'; import 'package:comunic/ui/widgets/settings/header_spacer_section.dart'; import 'package:comunic/utils/intl_utils.dart'; import 'package:comunic/utils/ui_utils.dart'; @@ -84,6 +85,14 @@ class __AccountSettingsBodyState extends State<_AccountSettingsBody> { onBuild: () => AccountImageSettingsScreen(), ), + // Notifications settings + _SettingsSection( + title: tr("Notifications"), + subtitle: tr("Customize the notifications you receive on you phone"), + icon: Icons.notifications, + onBuild: () => NotificationsSettingsScreen(), + ), + // Security settings _SettingsSection( title: tr("Security"), diff --git a/lib/ui/routes/settings/notifications_settings.dart b/lib/ui/routes/settings/notifications_settings.dart new file mode 100644 index 0000000..b52f162 --- /dev/null +++ b/lib/ui/routes/settings/notifications_settings.dart @@ -0,0 +1,67 @@ +import 'package:comunic/helpers/settings_helper.dart'; +import 'package:comunic/models/notifications_settings.dart'; +import 'package:comunic/ui/widgets/async_screen_widget.dart'; +import 'package:comunic/ui/widgets/settings/header_spacer_section.dart'; +import 'package:comunic/utils/intl_utils.dart'; +import 'package:comunic/utils/log_utils.dart'; +import 'package:comunic/utils/ui_utils.dart'; +import 'package:flutter/material.dart'; +import 'package:settings_ui/settings_ui.dart'; + +/// Notifications settings +/// +/// @author Pierre Hubert + +class NotificationsSettingsScreen extends StatefulWidget { + @override + _NotificationsSettingsScreenState createState() => + _NotificationsSettingsScreenState(); +} + +class _NotificationsSettingsScreenState + extends State { + final key = GlobalKey(); + + NotificationsSettings _settings; + + Future _loadSettings() async { + _settings = await SettingsHelper.getNotificationsSettings(); + } + + @override + Widget build(BuildContext context) => AsyncScreenWidget( + key: key, + onReload: _loadSettings, + onBuild: _buildScreen, + errorMessage: tr("Failed to load notifications settings!"), + ); + + Widget _buildScreen() => SettingsList(sections: [ + HeadSpacerSection(), + SettingsSection( + title: tr("Push notifications"), + tiles: [ + SettingsTile.switchTile( + title: tr("Allow conversations notification"), + onToggle: (v) { + _settings.allowConversations = v; + _updatedSettings(); + }, + switchValue: _settings.allowConversations, + subtitle: + tr("Receive notifications for the conversations you follow."), + ) + ], + ) + ]); + + void _updatedSettings() async { + try { + await SettingsHelper.setNotificationsSettings(_settings); + setState(() {}); + } catch (e, s) { + logError(e, s); + snack(context, tr("Failed to update settings!")); + } + } +}