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 <omarh.ismail1@gmail.com>

Serhii committed Apr 14, 2023 at 21:55 UTC 3b69aa86862f2bf7e83e3cf8229f662a057f5244
6 files changed +58 -21
cw_bitcoin/pubspec.lock
+1 -1
@@ -746,5 +746,5 @@ packages:
746 source: hosted
747 version: "3.1.1"
748 sdks:
749 - dart: ">=2.19.0 <3.0.0"
749 + dart: ">=2.19.0 <4.0.0"
750 flutter: ">=3.0.0"
cw_core/lib/node.dart
+19
@@ -73,6 +73,25 @@ class Node extends HiveObject with Keyable {
73 }
74 }
75
76 + @override
77 + bool operator ==(other) =>
78 + other is Node &&
79 + (other.uriRaw == uriRaw &&
80 + other.login == login &&
81 + other.password == password &&
82 + other.typeRaw == typeRaw &&
83 + other.useSSL == useSSL &&
84 + other.trusted == trusted);
85 +
86 + @override
87 + int get hashCode =>
88 + uriRaw.hashCode ^
89 + login.hashCode ^
90 + password.hashCode ^
91 + typeRaw.hashCode ^
92 + useSSL.hashCode ^
93 + trusted.hashCode;
94 +
95 @override
96 dynamic get keyIndex {
97 _keyIndex ??= key;
cw_core/pubspec.lock
+1 -1
@@ -665,5 +665,5 @@ packages:
665 source: hosted
666 version: "3.1.1"
667 sdks:
668 - dart: ">=2.19.0 <3.0.0"
668 + dart: ">=2.19.0 <4.0.0"
669 flutter: ">=3.0.0"
cw_monero/pubspec.lock
+1 -1
@@ -672,5 +672,5 @@ packages:
672 source: hosted
673 version: "3.1.1"
674 sdks:
675 - dart: ">=2.19.0 <3.0.0"
675 + dart: ">=2.19.0 <4.0.0"
676 flutter: ">=3.0.0"
lib/src/screens/new_wallet/advanced_privacy_settings_page.dart
+1 -1
@@ -105,7 +105,7 @@ class _AdvancedPrivacySettingsBodyState extends State<AdvancedPrivacySettingsBod
105 return;
106 }
107
108 - widget.nodeViewModel.save(saveAsCurrent: true);
108 + widget.nodeViewModel.save();
109 }
110
111 Navigator.pop(context);
lib/view_model/node_list/node_create_or_edit_view_model.dart
+35 -17
@@ -4,13 +4,11 @@ import 'package:hive/hive.dart';
4 import 'package:mobx/mobx.dart';
5 import 'package:cw_core/node.dart';
6 import 'package:cw_core/wallet_type.dart';
7 -
8 -import 'node_list_view_model.dart';
7 +import 'package:collection/collection.dart';
8
9 part 'node_create_or_edit_view_model.g.dart';
10
12 -class NodeCreateOrEditViewModel = NodeCreateOrEditViewModelBase
13 - with _$NodeCreateOrEditViewModel;
11 +class NodeCreateOrEditViewModel = NodeCreateOrEditViewModelBase with _$NodeCreateOrEditViewModel;
12
13 abstract class NodeCreateOrEditViewModelBase with Store {
14 NodeCreateOrEditViewModelBase(this._nodeSource, this._walletType, this._settingsStore)
@@ -48,11 +46,10 @@ abstract class NodeCreateOrEditViewModelBase with Store {
46 bool trusted;
47
48 @computed
51 - bool get isReady =>
52 - address.isNotEmpty && port.isNotEmpty;
49 + bool get isReady => address.isNotEmpty && port.isNotEmpty;
50
54 - bool get hasAuthCredentials => _walletType == WalletType.monero ||
55 - _walletType == WalletType.haven;
51 + bool get hasAuthCredentials =>
52 + _walletType == WalletType.monero || _walletType == WalletType.haven;
53
54 String get uri {
55 var uri = address;
@@ -79,22 +76,22 @@ abstract class NodeCreateOrEditViewModelBase with Store {
76 }
77
78 @action
82 - void setPort (String val) => port = val;
79 + void setPort(String val) => port = val;
80
81 @action
85 - void setAddress (String val) => address = val;
82 + void setAddress(String val) => address = val;
83
84 @action
88 - void setLogin (String val) => login = val;
85 + void setLogin(String val) => login = val;
86
87 @action
91 - void setPassword (String val) => password = val;
88 + void setPassword(String val) => password = val;
89
90 @action
94 - void setSSL (bool val) => useSSL = val;
91 + void setSSL(bool val) => useSSL = val;
92
93 @action
97 - void setTrusted (bool val) => trusted = val;
94 + void setTrusted(bool val) => trusted = val;
95
96 @action
97 Future<void> save({Node? editingNode, bool saveAsCurrent = false}) async {
@@ -109,11 +106,14 @@ abstract class NodeCreateOrEditViewModelBase with Store {
106 state = IsExecutingState();
107 if (editingNode != null) {
108 await _nodeSource.put(editingNode.key, node);
109 + } else if (existingNode(node) != null) {
110 + setAsCurrent(existingNode(node)!);
111 } else {
112 await _nodeSource.add(node);
113 + setAsCurrent(_nodeSource.values.last);
114 }
115 if (saveAsCurrent) {
116 - _settingsStore.nodes[_walletType] = node;
116 + setAsCurrent(node);
117 }
118
119 state = ExecutedSuccessfullyState();
@@ -124,14 +124,32 @@ abstract class NodeCreateOrEditViewModelBase with Store {
124
125 @action
126 Future<void> connect() async {
127 + final node = Node(
128 + uri: uri,
129 + type: _walletType,
130 + login: login,
131 + password: password,
132 + useSSL: useSSL,
133 + trusted: trusted);
134 try {
135 connectionState = IsExecutingState();
129 - final node =
130 - Node(uri: uri, type: _walletType, login: login, password: password);
136 final isAlive = await node.requestNode();
137 connectionState = ExecutedSuccessfullyState(payload: isAlive);
138 } catch (e) {
139 connectionState = FailureState(e.toString());
140 }
141 }
142 +
143 + Node? existingNode(Node node) {
144 + final nodes = _nodeSource.values.toList();
145 + nodes.forEach((item) {
146 + item.login ??= '';
147 + item.password ??= '';
148 + item.useSSL ??= false;
149 + });
150 + return nodes.firstWhereOrNull((item) => item == node);
151 + }
152 +
153 + @action
154 + void setAsCurrent(Node node) => _settingsStore.nodes[_walletType] = node;
155 }