From 3b69aa86862f2bf7e83e3cf8229f662a057f5244 Mon Sep 17 00:00:00 2001 From: Serhii Date: Fri, 14 Apr 2023 21:55:12 +0300 Subject: [PATCH] CW-273-Don't-add-node-under-Advanced-Privacy-Settings-if-it-already-exists (#876) * don't add existing node * minor fixes * Add back IsExecutingState to connect [skip ci] --------- Co-authored-by: Omar Hatem --- cw_bitcoin/pubspec.lock | 2 +- cw_core/lib/node.dart | 19 +++++++ cw_core/pubspec.lock | 2 +- cw_monero/pubspec.lock | 2 +- .../advanced_privacy_settings_page.dart | 2 +- .../node_create_or_edit_view_model.dart | 52 +++++++++++++------ 6 files changed, 58 insertions(+), 21 deletions(-) diff --git a/cw_bitcoin/pubspec.lock b/cw_bitcoin/pubspec.lock index bfcd9e5a6..4d864059f 100644 --- a/cw_bitcoin/pubspec.lock +++ b/cw_bitcoin/pubspec.lock @@ -746,5 +746,5 @@ packages: source: hosted version: "3.1.1" sdks: - dart: ">=2.19.0 <3.0.0" + dart: ">=2.19.0 <4.0.0" flutter: ">=3.0.0" diff --git a/cw_core/lib/node.dart b/cw_core/lib/node.dart index 1322c5b78..7df25d6a1 100644 --- a/cw_core/lib/node.dart +++ b/cw_core/lib/node.dart @@ -73,6 +73,25 @@ class Node extends HiveObject with Keyable { } } + @override + bool operator ==(other) => + other is Node && + (other.uriRaw == uriRaw && + other.login == login && + other.password == password && + other.typeRaw == typeRaw && + other.useSSL == useSSL && + other.trusted == trusted); + + @override + int get hashCode => + uriRaw.hashCode ^ + login.hashCode ^ + password.hashCode ^ + typeRaw.hashCode ^ + useSSL.hashCode ^ + trusted.hashCode; + @override dynamic get keyIndex { _keyIndex ??= key; diff --git a/cw_core/pubspec.lock b/cw_core/pubspec.lock index 01e19dda4..70652ec35 100644 --- a/cw_core/pubspec.lock +++ b/cw_core/pubspec.lock @@ -665,5 +665,5 @@ packages: source: hosted version: "3.1.1" sdks: - dart: ">=2.19.0 <3.0.0" + dart: ">=2.19.0 <4.0.0" flutter: ">=3.0.0" diff --git a/cw_monero/pubspec.lock b/cw_monero/pubspec.lock index 437184a7d..1e33631d5 100644 --- a/cw_monero/pubspec.lock +++ b/cw_monero/pubspec.lock @@ -672,5 +672,5 @@ packages: source: hosted version: "3.1.1" sdks: - dart: ">=2.19.0 <3.0.0" + dart: ">=2.19.0 <4.0.0" flutter: ">=3.0.0" diff --git a/lib/src/screens/new_wallet/advanced_privacy_settings_page.dart b/lib/src/screens/new_wallet/advanced_privacy_settings_page.dart index 05ff65889..cf0708f21 100644 --- a/lib/src/screens/new_wallet/advanced_privacy_settings_page.dart +++ b/lib/src/screens/new_wallet/advanced_privacy_settings_page.dart @@ -105,7 +105,7 @@ class _AdvancedPrivacySettingsBodyState extends State - address.isNotEmpty && port.isNotEmpty; + bool get isReady => address.isNotEmpty && port.isNotEmpty; - bool get hasAuthCredentials => _walletType == WalletType.monero || - _walletType == WalletType.haven; + bool get hasAuthCredentials => + _walletType == WalletType.monero || _walletType == WalletType.haven; String get uri { var uri = address; @@ -79,22 +76,22 @@ abstract class NodeCreateOrEditViewModelBase with Store { } @action - void setPort (String val) => port = val; + void setPort(String val) => port = val; @action - void setAddress (String val) => address = val; + void setAddress(String val) => address = val; @action - void setLogin (String val) => login = val; + void setLogin(String val) => login = val; @action - void setPassword (String val) => password = val; + void setPassword(String val) => password = val; @action - void setSSL (bool val) => useSSL = val; + void setSSL(bool val) => useSSL = val; @action - void setTrusted (bool val) => trusted = val; + void setTrusted(bool val) => trusted = val; @action Future save({Node? editingNode, bool saveAsCurrent = false}) async { @@ -109,11 +106,14 @@ abstract class NodeCreateOrEditViewModelBase with Store { state = IsExecutingState(); if (editingNode != null) { await _nodeSource.put(editingNode.key, node); + } else if (existingNode(node) != null) { + setAsCurrent(existingNode(node)!); } else { await _nodeSource.add(node); + setAsCurrent(_nodeSource.values.last); } if (saveAsCurrent) { - _settingsStore.nodes[_walletType] = node; + setAsCurrent(node); } state = ExecutedSuccessfullyState(); @@ -124,14 +124,32 @@ abstract class NodeCreateOrEditViewModelBase with Store { @action Future connect() async { + final node = Node( + uri: uri, + type: _walletType, + login: login, + password: password, + useSSL: useSSL, + trusted: trusted); try { connectionState = IsExecutingState(); - final node = - Node(uri: uri, type: _walletType, login: login, password: password); final isAlive = await node.requestNode(); connectionState = ExecutedSuccessfullyState(payload: isAlive); } catch (e) { connectionState = FailureState(e.toString()); } } + + Node? existingNode(Node node) { + final nodes = _nodeSource.values.toList(); + nodes.forEach((item) { + item.login ??= ''; + item.password ??= ''; + item.useSSL ??= false; + }); + return nodes.firstWhereOrNull((item) => item == node); + } + + @action + void setAsCurrent(Node node) => _settingsStore.nodes[_walletType] = node; }