diff --git a/lib/blocs/notifications_bloc.dart b/lib/blocs/notifications_bloc.dart index 981a9e0b..4a51ceb9 100644 --- a/lib/blocs/notifications_bloc.dart +++ b/lib/blocs/notifications_bloc.dart @@ -3,6 +3,7 @@ import 'dart:async'; import 'package:hive/hive.dart'; import 'package:local_notifier/local_notifier.dart'; import 'package:zenon_syrius_wallet_flutter/blocs/blocs.dart'; +import 'package:zenon_syrius_wallet_flutter/main.dart'; import 'package:zenon_syrius_wallet_flutter/model/model.dart'; import 'package:zenon_syrius_wallet_flutter/utils/constants.dart'; @@ -17,7 +18,7 @@ class NotificationsBloc extends BaseBloc { } } await notificationsBox.add(notification); - if (notification != null) { + if (notification != null && _areDesktopNotificationsEnabled()) { LocalNotification localNotification = LocalNotification( title: notification.title ?? 'Empty title', body: notification.details ?? 'No details available', @@ -52,4 +53,9 @@ class NotificationsBloc extends BaseBloc { ), ); } + + bool _areDesktopNotificationsEnabled() => sharedPrefsService!.get( + kEnableDesktopNotificationsKey, + defaultValue: kEnableDesktopNotificationsDefaultValue, + ); } diff --git a/lib/utils/constants.dart b/lib/utils/constants.dart index 84c1e817..f16a2236 100644 --- a/lib/utils/constants.dart +++ b/lib/utils/constants.dart @@ -169,6 +169,8 @@ const String kAutoEraseNumAttemptsKey = 'auto_erase_num_attempts'; // Wallet preferences const String kLaunchAtStartupKey = 'launch_at_startup'; const bool kLaunchAtStartupDefaultValue = false; +const String kEnableDesktopNotificationsKey = 'enable_desktop_notifications'; +const bool kEnableDesktopNotificationsDefaultValue = false; // Display constants const String kThemeModeKey = 'theme_mode_key'; diff --git a/lib/widgets/modular_widgets/settings_widgets/wallet_options.dart b/lib/widgets/modular_widgets/settings_widgets/wallet_options.dart index e9ca4ade..cf4d9866 100644 --- a/lib/widgets/modular_widgets/settings_widgets/wallet_options.dart +++ b/lib/widgets/modular_widgets/settings_widgets/wallet_options.dart @@ -23,6 +23,7 @@ class WalletOptions extends StatefulWidget { class _WalletOptionsState extends State { bool? _launchAtStartup; + bool? _enableDesktopNotifications; @override void initState() { @@ -31,6 +32,10 @@ class _WalletOptionsState extends State { kLaunchAtStartupKey, defaultValue: kLaunchAtStartupDefaultValue, ); + _enableDesktopNotifications = sharedPrefsService!.get( + kEnableDesktopNotificationsKey, + defaultValue: kEnableDesktopNotificationsDefaultValue, + ); } @override @@ -106,6 +111,7 @@ class _WalletOptionsState extends State { return Column( children: [ _getLaunchAtStartupWidget(), + _getEnableDesktopNotifications(), ], ); } @@ -148,7 +154,7 @@ class _WalletOptionsState extends State { await launchAtStartup.disable(); } await _saveLaunchAtStartupValueToCache(enabled); - _sendNotification(enabled); + _sendLaunchAtStartupStatusNotification(enabled); } on Exception catch (e) { NotificationUtils.sendNotificationError( e, @@ -164,7 +170,7 @@ class _WalletOptionsState extends State { ); } - void _sendNotification(bool enabled) { + void _sendLaunchAtStartupStatusNotification(bool enabled) { sl.get().addNotification( WalletNotification( title: 'Launch at startup was ${enabled ? 'enabled' : 'disabled'}', @@ -175,4 +181,50 @@ class _WalletOptionsState extends State { ), ); } + + Widget _getEnableDesktopNotifications() { + return Row( + children: [ + Text( + 'Enable desktop notifications: ', + style: Theme.of(context).textTheme.subtitle1, + ), + SyriusCheckbox( + onChanged: (value) { + setState(() { + _enableDesktopNotifications = value; + _changeEnableDesktopNotificationsStatus(value ?? false); + }); + }, + value: _enableDesktopNotifications, + context: context, + ), + ], + ); + } + + Future _changeEnableDesktopNotificationsStatus(bool enabled) async { + try { + await sharedPrefsService!.put(kEnableDesktopNotificationsKey, enabled); + _sendEnabledDesktopNotificationsStatusNotification(enabled); + } on Exception catch (e) { + NotificationUtils.sendNotificationError( + e, + 'Something went wrong while changing the preferences of desktop notifications', + ); + } + } + + void _sendEnabledDesktopNotificationsStatusNotification(bool enabled) { + sl.get().addNotification( + WalletNotification( + title: + 'Desktop notifications have been ${enabled ? 'enabled' : 'disabled'}', + details: + 'You changed the desktop notifications preference to ${enabled ? 'enabled' : 'disabled'}', + timestamp: DateTime.now().millisecondsSinceEpoch, + type: NotificationType.paymentSent, + ), + ); + } }