Skip to content

Commit

Permalink
Add auto-receive option on the onboard node-management screen (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
KingGorrin authored Feb 29, 2024
1 parent b6ac68a commit 368afd9
Showing 1 changed file with 75 additions and 6 deletions.
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

0 comments on commit 368afd9

Please sign in to comment.