1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-12-26 12:58:51 +00:00

Can update conversation settings from mobile application

This commit is contained in:
Pierre HUBERT 2021-04-11 14:31:38 +02:00
parent f546cdb060
commit 15ba251440
4 changed files with 117 additions and 0 deletions

View File

@ -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<NotificationsSettings> 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<void> setNotificationsSettings(
NotificationsSettings settings) async =>
await APIRequest.withLogin("settings/set_notifications")
.addBool(
"allow_notifications_sound", settings.allowNotificationsSound)
.addBool("allow_conversations", settings.allowConversations)
.execWithThrow();
}

View File

@ -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);
}

View File

@ -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"),

View File

@ -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<NotificationsSettingsScreen> {
final key = GlobalKey<AsyncScreenWidgetState>();
NotificationsSettings _settings;
Future<void> _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!"));
}
}
}