From 4f280e103ee0ad7667cd8712eb3c6853bc6eb618 Mon Sep 17 00:00:00 2001 From: OleksandrSobol Date: Wed, 2 Dec 2020 20:26:41 +0200 Subject: [PATCH] CAKE-169 | added NodeSSL class with getter useSSl; applied useSSL in the connectToNode() method in the monero_wallet.dart; added dynamic parameter to ExecutedSuccessfullyState class; applied ExecuteState class to connect() method in the node_create_or_edit_view_model.dart; deleted connection_state.dart; deleted _isEffectsInstalled parameter from node_create_or_edit_page.dart --- lib/core/execution_state.dart | 6 +- lib/entities/node_ssl.dart | 11 +++ lib/monero/monero_wallet.dart | 4 +- .../nodes/node_create_or_edit_page.dart | 82 ++++++++----------- .../node_list/connection_state.dart | 17 ---- .../node_create_or_edit_view_model.dart | 11 ++- 6 files changed, 59 insertions(+), 72 deletions(-) create mode 100644 lib/entities/node_ssl.dart delete mode 100644 lib/view_model/node_list/connection_state.dart diff --git a/lib/core/execution_state.dart b/lib/core/execution_state.dart index d4e3faeec..18dc81030 100644 --- a/lib/core/execution_state.dart +++ b/lib/core/execution_state.dart @@ -4,7 +4,11 @@ class InitialExecutionState extends ExecutionState {} class IsExecutingState extends ExecutionState {} -class ExecutedSuccessfullyState extends ExecutionState {} +class ExecutedSuccessfullyState extends ExecutionState { + ExecutedSuccessfullyState({this.payload}); + + final dynamic payload; +} class FailureState extends ExecutionState { FailureState(this.error); diff --git a/lib/entities/node_ssl.dart b/lib/entities/node_ssl.dart new file mode 100644 index 000000000..4775d7304 --- /dev/null +++ b/lib/entities/node_ssl.dart @@ -0,0 +1,11 @@ +import 'package:cake_wallet/entities/node.dart'; + +class NodeSSL { + NodeSSL(this._node) : + _useSSL = _node.useSSL ?? false; + + final Node _node; + final bool _useSSL; + + bool get useSSL => _useSSL; +} \ No newline at end of file diff --git a/lib/monero/monero_wallet.dart b/lib/monero/monero_wallet.dart index 6c801b6e3..5dabd4805 100644 --- a/lib/monero/monero_wallet.dart +++ b/lib/monero/monero_wallet.dart @@ -1,5 +1,6 @@ import 'dart:async'; +import 'package:cake_wallet/entities/node_ssl.dart'; import 'package:cake_wallet/monero/monero_amount_format.dart'; import 'package:cake_wallet/monero/monero_transaction_creation_exception.dart'; import 'package:flutter/foundation.dart'; @@ -143,11 +144,12 @@ abstract class MoneroWalletBase extends WalletBase with Store { Future connectToNode({@required Node node}) async { try { syncStatus = ConnectingSyncStatus(); + final nodeSSL = NodeSSL(node); await monero_wallet.setupNode( address: node.uri, login: node.login, password: node.password, - useSSL: node.useSSL ?? false, + useSSL: nodeSSL.useSSL, isLightWallet: false); // FIXME: hardcoded value syncStatus = ConnectedSyncStatus(); } catch (e) { diff --git a/lib/src/screens/nodes/node_create_or_edit_page.dart b/lib/src/screens/nodes/node_create_or_edit_page.dart index 70dae96d1..6cfa67285 100644 --- a/lib/src/screens/nodes/node_create_or_edit_page.dart +++ b/lib/src/screens/nodes/node_create_or_edit_page.dart @@ -1,7 +1,7 @@ +import 'package:cake_wallet/core/execution_state.dart'; import 'package:cake_wallet/src/widgets/alert_with_one_action.dart'; import 'package:cake_wallet/src/widgets/standard_checkbox.dart'; import 'package:cake_wallet/utils/show_pop_up.dart'; -import 'package:cake_wallet/view_model/node_list/connection_state.dart'; import 'package:flutter/material.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter_mobx/flutter_mobx.dart'; @@ -14,7 +14,6 @@ import 'package:cake_wallet/src/widgets/base_text_form_field.dart'; import 'package:cake_wallet/src/screens/base_page.dart'; import 'package:cake_wallet/src/widgets/scollable_with_bottom_section.dart'; import 'package:cake_wallet/view_model/node_list/node_create_or_edit_view_model.dart'; -import 'package:cake_wallet/view_model/node_list/connection_state.dart'; class NodeCreateOrEditPage extends BasePage { NodeCreateOrEditPage(this.nodeCreateOrEditViewModel) @@ -65,8 +64,6 @@ class NodeCreateOrEditPage extends BasePage { final TextEditingController _loginController; final TextEditingController _passwordController; - bool _effectsInstalled = false; - @override String get title => S.current.node_new; @@ -74,7 +71,38 @@ class NodeCreateOrEditPage extends BasePage { @override Widget body(BuildContext context) { - _setEffects(context); + + reaction((_) => nodeCreateOrEditViewModel.connectionState, + (ExecutionState state) { + if (state is ExecutedSuccessfullyState) { + WidgetsBinding.instance.addPostFrameCallback((_) { + showPopUp( + context: context, + builder: (BuildContext context) => + AlertWithOneAction( + alertTitle: S.of(context).new_node_testing, + alertContent: state.payload as bool + ? S.of(context).node_connection_successful + : S.of(context).node_connection_failed, + buttonText: S.of(context).ok, + buttonAction: () => Navigator.of(context).pop())); + }); + } + + if (state is FailureState) { + WidgetsBinding.instance.addPostFrameCallback((_) { + showPopUp( + context: context, + builder: (BuildContext context) { + return AlertWithOneAction( + alertTitle: S.of(context).error, + alertContent: state.error, + buttonText: S.of(context).ok, + buttonAction: () => Navigator.of(context).pop()); + }); + }); + } + }); return Container( padding: EdgeInsets.only(left: 24, right: 24), @@ -166,7 +194,7 @@ class NodeCreateOrEditPage extends BasePage { await nodeCreateOrEditViewModel.connect(); }, isLoading: nodeCreateOrEditViewModel - .connectionState is IsConnectingState, + .connectionState is IsExecutingState, text: S.of(context).node_test, isDisabled: !nodeCreateOrEditViewModel.isReady, color: Colors.orange, @@ -189,51 +217,11 @@ class NodeCreateOrEditPage extends BasePage { textColor: Colors.white, isDisabled: (!nodeCreateOrEditViewModel.isReady)|| (nodeCreateOrEditViewModel - .connectionState is IsConnectingState), + .connectionState is IsExecutingState), ), )), ], )), )); } - - void _setEffects(BuildContext context) { - if (_effectsInstalled) { - return; - } - - reaction((_) => nodeCreateOrEditViewModel.connectionState, - (ConnectionToNodeState state) { - if (state is ConnectedSuccessfullyState) { - WidgetsBinding.instance.addPostFrameCallback((_) { - showPopUp( - context: context, - builder: (BuildContext context) => - AlertWithOneAction( - alertTitle: S.of(context).new_node_testing, - alertContent: state.isAlive - ? S.of(context).node_connection_successful - : S.of(context).node_connection_failed, - buttonText: S.of(context).ok, - buttonAction: () => Navigator.of(context).pop())); - }); - } - - if (state is FailureConnectedState) { - WidgetsBinding.instance.addPostFrameCallback((_) { - showPopUp( - context: context, - builder: (BuildContext context) { - return AlertWithOneAction( - alertTitle: S.of(context).error, - alertContent: state.error, - buttonText: S.of(context).ok, - buttonAction: () => Navigator.of(context).pop()); - }); - }); - } - }); - - _effectsInstalled = true; - } } diff --git a/lib/view_model/node_list/connection_state.dart b/lib/view_model/node_list/connection_state.dart deleted file mode 100644 index eb13e474c..000000000 --- a/lib/view_model/node_list/connection_state.dart +++ /dev/null @@ -1,17 +0,0 @@ -abstract class ConnectionToNodeState {} - -class InitialConnectionState extends ConnectionToNodeState {} - -class IsConnectingState extends ConnectionToNodeState {} - -class ConnectedSuccessfullyState extends ConnectionToNodeState { - ConnectedSuccessfullyState(this.isAlive); - - final bool isAlive; -} - -class FailureConnectedState extends ConnectionToNodeState { - FailureConnectedState(this.error); - - final String error; -} \ No newline at end of file diff --git a/lib/view_model/node_list/node_create_or_edit_view_model.dart b/lib/view_model/node_list/node_create_or_edit_view_model.dart index ce4404051..0a5e07266 100644 --- a/lib/view_model/node_list/node_create_or_edit_view_model.dart +++ b/lib/view_model/node_list/node_create_or_edit_view_model.dart @@ -1,5 +1,4 @@ import 'package:cake_wallet/core/execution_state.dart'; -import 'package:cake_wallet/view_model/node_list/connection_state.dart'; import 'package:hive/hive.dart'; import 'package:mobx/mobx.dart'; import 'package:cake_wallet/core/wallet_base.dart'; @@ -14,7 +13,7 @@ class NodeCreateOrEditViewModel = NodeCreateOrEditViewModelBase abstract class NodeCreateOrEditViewModelBase with Store { NodeCreateOrEditViewModelBase(this._nodeSource, this._wallet) : state = InitialExecutionState(), - connectionState = InitialConnectionState(), + connectionState = InitialExecutionState(), useSSL = false; @observable @@ -33,7 +32,7 @@ abstract class NodeCreateOrEditViewModelBase with Store { String password; @observable - ConnectionToNodeState connectionState; + ExecutionState connectionState; @observable bool useSSL; @@ -83,13 +82,13 @@ abstract class NodeCreateOrEditViewModelBase with Store { @action Future connect() async { try { - connectionState = IsConnectingState(); + connectionState = IsExecutingState(); final node = Node(uri: uri, type: _wallet.type, login: login, password: password); final isAlive = await node.requestNode(); - connectionState = ConnectedSuccessfullyState(isAlive); + connectionState = ExecutedSuccessfullyState(payload: isAlive); } catch (e) { - connectionState = FailureConnectedState(e.toString()); + connectionState = FailureState(e.toString()); } } }