From 05bdc8c52fe53d1ff3d07dc1f7899480fa20a4d2 Mon Sep 17 00:00:00 2001 From: julian Date: Fri, 25 Nov 2022 13:50:13 -0600 Subject: [PATCH] fix node loading on initial start for desktop, only add default node back if there are no nodes exist for a certain coin --- .../create_password/create_password_view.dart | 6 +++++- lib/services/node_service.dart | 15 ++++++++++----- lib/utilities/default_nodes.dart | 3 ++- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/lib/pages_desktop_specific/create_password/create_password_view.dart b/lib/pages_desktop_specific/create_password/create_password_view.dart index 8e752f508..a8c9e7758 100644 --- a/lib/pages_desktop_specific/create_password/create_password_view.dart +++ b/lib/pages_desktop_specific/create_password/create_password_view.dart @@ -7,9 +7,9 @@ import 'package:stackwallet/notifications/show_flush_bar.dart'; import 'package:stackwallet/pages_desktop_specific/home/desktop_home_view.dart'; import 'package:stackwallet/providers/desktop/storage_crypto_handler_provider.dart'; import 'package:stackwallet/providers/global/secure_store_provider.dart'; +import 'package:stackwallet/providers/providers.dart'; import 'package:stackwallet/utilities/assets.dart'; import 'package:stackwallet/utilities/constants.dart'; -import 'package:stackwallet/utilities/enums/flush_bar_type.dart'; import 'package:stackwallet/utilities/flutter_secure_storage_interface.dart'; import 'package:stackwallet/utilities/text_styles.dart'; import 'package:stackwallet/utilities/theme/stack_colors.dart'; @@ -81,6 +81,10 @@ class _CreatePasswordViewState extends ConsumerState { await ref.read(storageCryptoHandlerProvider).initFromNew(passphrase); await (ref.read(secureStoreProvider).store as DesktopSecureStore).init(); + + // load default nodes now as node service requires storage handler to exist + + await ref.read(nodeServiceChangeNotifierProvider).updateDefaults(); } catch (e) { unawaited(showFloatingFlushBar( type: FlushBarType.warning, diff --git a/lib/services/node_service.dart b/lib/services/node_service.dart index aa8d5a6d9..ca1aae082 100644 --- a/lib/services/node_service.dart +++ b/lib/services/node_service.dart @@ -24,11 +24,14 @@ class NodeService extends ChangeNotifier { final savedNode = DB.instance .get(boxName: DB.boxNameNodeModels, key: defaultNode.id); if (savedNode == null) { - // save the default node to hive - await DB.instance.put( + // save the default node to hive only if no other nodes for the specific coin exist + if (getNodesFor(coinFromPrettyName(defaultNode.coinName)).isEmpty) { + await DB.instance.put( boxName: DB.boxNameNodeModels, key: defaultNode.id, - value: defaultNode); + value: defaultNode, + ); + } } else { // update all fields but copy over previously set enabled state await DB.instance.put( @@ -81,14 +84,16 @@ class NodeService extends ChangeNotifier { final list = DB.instance .values(boxName: DB.boxNameNodeModels) .where((e) => - e.coinName == coin.name && e.name != DefaultNodes.defaultName) + e.coinName == coin.name && + !e.id.startsWith(DefaultNodes.defaultNodeIdPrefix)) .toList(); // add default to end of list list.addAll(DB.instance .values(boxName: DB.boxNameNodeModels) .where((e) => - e.coinName == coin.name && e.name == DefaultNodes.defaultName) + e.coinName == coin.name && + e.id.startsWith(DefaultNodes.defaultNodeIdPrefix)) .toList()); // return reversed list so default node appears at beginning diff --git a/lib/utilities/default_nodes.dart b/lib/utilities/default_nodes.dart index abe702b78..c9e96fbac 100644 --- a/lib/utilities/default_nodes.dart +++ b/lib/utilities/default_nodes.dart @@ -4,7 +4,8 @@ import 'package:stackwallet/models/node_model.dart'; import 'package:stackwallet/utilities/enums/coin_enum.dart'; abstract class DefaultNodes { - static String _nodeId(Coin coin) => "default_${coin.name}"; + static const String defaultNodeIdPrefix = "default_"; + static String _nodeId(Coin coin) => "$defaultNodeIdPrefix${coin.name}"; static const String defaultName = "Stack Default"; static List get all => [