Skip to content

Commit

Permalink
Fix: Save native notifications preference in cache
Browse files Browse the repository at this point in the history
  • Loading branch information
mik3mast3rs committed Jan 23, 2023
1 parent ad99253 commit 3dcd546
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
8 changes: 7 additions & 1 deletion lib/blocs/notifications_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -17,7 +18,7 @@ class NotificationsBloc extends BaseBloc<WalletNotification?> {
}
}
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',
Expand Down Expand Up @@ -52,4 +53,9 @@ class NotificationsBloc extends BaseBloc<WalletNotification?> {
),
);
}

bool _areDesktopNotificationsEnabled() => sharedPrefsService!.get(
kEnableDesktopNotificationsKey,
defaultValue: kEnableDesktopNotificationsDefaultValue,
);
}
2 changes: 2 additions & 0 deletions lib/utils/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
56 changes: 54 additions & 2 deletions lib/widgets/modular_widgets/settings_widgets/wallet_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class WalletOptions extends StatefulWidget {

class _WalletOptionsState extends State<WalletOptions> {
bool? _launchAtStartup;
bool? _enableDesktopNotifications;

@override
void initState() {
Expand All @@ -31,6 +32,10 @@ class _WalletOptionsState extends State<WalletOptions> {
kLaunchAtStartupKey,
defaultValue: kLaunchAtStartupDefaultValue,
);
_enableDesktopNotifications = sharedPrefsService!.get(
kEnableDesktopNotificationsKey,
defaultValue: kEnableDesktopNotificationsDefaultValue,
);
}

@override
Expand Down Expand Up @@ -106,6 +111,7 @@ class _WalletOptionsState extends State<WalletOptions> {
return Column(
children: [
_getLaunchAtStartupWidget(),
_getEnableDesktopNotifications(),
],
);
}
Expand Down Expand Up @@ -148,7 +154,7 @@ class _WalletOptionsState extends State<WalletOptions> {
await launchAtStartup.disable();
}
await _saveLaunchAtStartupValueToCache(enabled);
_sendNotification(enabled);
_sendLaunchAtStartupStatusNotification(enabled);
} on Exception catch (e) {
NotificationUtils.sendNotificationError(
e,
Expand All @@ -164,7 +170,7 @@ class _WalletOptionsState extends State<WalletOptions> {
);
}

void _sendNotification(bool enabled) {
void _sendLaunchAtStartupStatusNotification(bool enabled) {
sl.get<NotificationsBloc>().addNotification(
WalletNotification(
title: 'Launch at startup was ${enabled ? 'enabled' : 'disabled'}',
Expand All @@ -175,4 +181,50 @@ class _WalletOptionsState extends State<WalletOptions> {
),
);
}

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<void> _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<NotificationsBloc>().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,
),
);
}
}

0 comments on commit 3dcd546

Please sign in to comment.