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>
This commit is contained in:
Serhii 2023-04-14 21:55:12 +03:00 committed by GitHub
parent 375755919a
commit 3b69aa8686
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 58 additions and 21 deletions

View file

@ -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"

View file

@ -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;

View file

@ -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"

View file

@ -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"

View file

@ -105,7 +105,7 @@ class _AdvancedPrivacySettingsBodyState extends State<AdvancedPrivacySettingsBod
return;
}
widget.nodeViewModel.save(saveAsCurrent: true);
widget.nodeViewModel.save();
}
Navigator.pop(context);

View file

@ -4,13 +4,11 @@ import 'package:hive/hive.dart';
import 'package:mobx/mobx.dart';
import 'package:cw_core/node.dart';
import 'package:cw_core/wallet_type.dart';
import 'node_list_view_model.dart';
import 'package:collection/collection.dart';
part 'node_create_or_edit_view_model.g.dart';
class NodeCreateOrEditViewModel = NodeCreateOrEditViewModelBase
with _$NodeCreateOrEditViewModel;
class NodeCreateOrEditViewModel = NodeCreateOrEditViewModelBase with _$NodeCreateOrEditViewModel;
abstract class NodeCreateOrEditViewModelBase with Store {
NodeCreateOrEditViewModelBase(this._nodeSource, this._walletType, this._settingsStore)
@ -48,11 +46,10 @@ abstract class NodeCreateOrEditViewModelBase with Store {
bool trusted;
@computed
bool get isReady =>
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<void> 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<void> 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;
}