Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add auto-receive option on the onboard node-management screen #65

Merged
merged 1 commit into from
Feb 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 75 additions & 6 deletions lib/screens/node_management_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:isolate';

import 'package:flutter/material.dart';
import 'package:hive/hive.dart';
import 'package:logging/logging.dart';
import 'package:wakelock_plus/wakelock_plus.dart';
import 'package:zenon_syrius_wallet_flutter/blocs/blocs.dart';
import 'package:zenon_syrius_wallet_flutter/embedded_node/embedded_node.dart';
Expand All @@ -26,6 +27,7 @@ class NodeManagementScreen extends StatefulWidget {

class _NodeManagementScreenState extends State<NodeManagementScreen> {
String? _selectedNode;
bool? _autoReceive;

final GlobalKey<LoadingButtonState> _confirmNodeButtonKey = GlobalKey();
final GlobalKey<LoadingButtonState> _addNodeButtonKey = GlobalKey();
Expand All @@ -39,6 +41,10 @@ class _NodeManagementScreenState extends State<NodeManagementScreen> {
void initState() {
super.initState();
kDefaultCommunityNodes.shuffle();
_autoReceive = sharedPrefsService!.get(
kAutoReceiveKey,
defaultValue: kAutoReceiveDefaultValue,
);
}

@override
Expand Down Expand Up @@ -88,7 +94,14 @@ class _NodeManagementScreenState extends State<NodeManagementScreen> {
'Node selection',
style: Theme.of(context).textTheme.bodyLarge,
),
_getNodeSelectionColumn(),
_getNodeTiles(),
kVerticalSpacing,
Text(
'Wallet options',
style: Theme.of(context).textTheme.bodyLarge,
),
_getAutoReceiveCheckboxContainer(),
_getConfirmNodeSelectionButton(),
kVerticalSpacing,
Text(
'Add node',
Expand All @@ -110,15 +123,71 @@ class _NodeManagementScreenState extends State<NodeManagementScreen> {
);
}

Widget _getNodeSelectionColumn() {
return Column(
children: [
_getNodeTiles(),
_getConfirmNodeSelectionButton(),
Widget _getAutoReceiveCheckboxContainer() {
return Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Checkbox(
value: _autoReceive,
checkColor: Theme.of(context).colorScheme.primary,
activeColor: AppColors.znnColor,
onChanged: (bool? value) async {
if (value == true) {
NodeUtils.getUnreceivedTransactions().then((value) {
sl<AutoReceiveTxWorker>().autoReceive();
}).onError((error, stackTrace) {
Logger('MainAppContainer').log(Level.WARNING,
'_getAutoReceiveCheckboxContainer', error, stackTrace);
});
} else if (value == false &&
sl<AutoReceiveTxWorker>().pool.isNotEmpty) {
sl<AutoReceiveTxWorker>().pool.clear();
}
setState(() {
_autoReceive = value;
_changeAutoReceiveStatus(value ?? false);
});
},
),
Text(
'Automatically receive transactions',
style: Theme.of(context).textTheme.headlineSmall,
)
],
);
}

Future<void> _changeAutoReceiveStatus(bool enabled) async {
try {
await _saveAutoReceiveValueToCache(enabled);
_sendAutoReceiveNotification(enabled);
} on Exception catch (e) {
NotificationUtils.sendNotificationError(
e,
'Something went wrong while setting automatic receive preference',
);
}
}

void _sendAutoReceiveNotification(bool enabled) {
sl.get<NotificationsBloc>().addNotification(
WalletNotification(
title: 'Auto-receiver ${enabled ? 'enabled' : 'disabled'}',
details:
'Auto-receiver preference was ${enabled ? 'enabled' : 'disabled'}',
timestamp: DateTime.now().millisecondsSinceEpoch,
type: NotificationType.paymentSent,
),
);
}

Future<void> _saveAutoReceiveValueToCache(bool enabled) async {
await sharedPrefsService!.put(
kAutoReceiveKey,
enabled,
);
}

_getConfirmNodeSelectionButton() {
return Row(
mainAxisAlignment: MainAxisAlignment.center,
Expand Down
Loading