CAKE-169 | created connection_state.dart; added connect() method and connectionState parameter to node_create_or_edit_view_model.dart; replaced Reset button to Connect button and added _setEffects() method to node_create_or_edit_page.dart; changed useSSL parameter from false to true in the connectToNode() method (monero_wallet.dart)

OleksandrSobol committed Nov 19, 2020 at 21:13 UTC 6cd6139fb382284503c40c5b286188617dc8e25a
4 files changed +97 -5
lib/monero/monero_wallet.dart
+1 -1
@@ -147,7 +147,7 @@ abstract class MoneroWalletBase extends WalletBase<MoneroBalance> with Store {
147 address: node.uri,
148 login: node.login,
149 password: node.password,
150 - useSSL: false,
150 + useSSL: true,
151 // FIXME: hardcoded value
152 isLightWallet: false); // FIXME: hardcoded value
153 syncStatus = ConnectedSyncStatus();
lib/src/screens/nodes/node_create_or_edit_page.dart
+60 -3
@@ -1,3 +1,6 @@
1 +import 'package:cake_wallet/src/widgets/alert_with_one_action.dart';
2 +import 'package:cake_wallet/utils/show_pop_up.dart';
3 +import 'package:cake_wallet/view_model/node_list/connection_state.dart';
4 import 'package:flutter/material.dart';
5 import 'package:flutter/cupertino.dart';
6 import 'package:flutter_mobx/flutter_mobx.dart';
@@ -10,6 +13,7 @@ import 'package:cake_wallet/src/widgets/base_text_form_field.dart';
13 import 'package:cake_wallet/src/screens/base_page.dart';
14 import 'package:cake_wallet/src/widgets/scollable_with_bottom_section.dart';
15 import 'package:cake_wallet/view_model/node_list/node_create_or_edit_view_model.dart';
16 +import 'package:cake_wallet/view_model/node_list/connection_state.dart';
17
18 class NodeCreateOrEditPage extends BasePage {
19 NodeCreateOrEditPage(this.nodeCreateOrEditViewModel)
@@ -60,6 +64,8 @@ class NodeCreateOrEditPage extends BasePage {
64 final TextEditingController _loginController;
65 final TextEditingController _passwordController;
66
67 + bool _effectsInstalled = false;
68 +
69 @override
70 String get title => S.current.node_new;
71
@@ -67,6 +73,8 @@ class NodeCreateOrEditPage extends BasePage {
73
74 @override
75 Widget body(BuildContext context) {
76 + _setEffects(context);
77 +
78 return Container(
79 padding: EdgeInsets.only(left: 24, right: 24),
80 child: ScrollableWithBottomSection(
@@ -133,9 +141,18 @@ class NodeCreateOrEditPage extends BasePage {
141 Flexible(
142 child: Container(
143 padding: EdgeInsets.only(right: 8.0),
136 - child: PrimaryButton(
137 - onPressed: () => nodeCreateOrEditViewModel.reset(),
138 - text: S.of(context).reset,
144 + child: LoadingPrimaryButton(
145 + onPressed: () async {
146 + if (!_formKey.currentState.validate()) {
147 + return;
148 + }
149 +
150 + await nodeCreateOrEditViewModel.connect();
151 + },
152 + isLoading: nodeCreateOrEditViewModel
153 + .connectionState is IsConnectingState,
154 + text: 'Connect',
155 + isDisabled: !nodeCreateOrEditViewModel.isReady,
156 color: Colors.orange,
157 textColor: Colors.white),
158 )),
@@ -161,4 +178,44 @@ class NodeCreateOrEditPage extends BasePage {
178 )),
179 ));
180 }
181 +
182 + void _setEffects(BuildContext context) {
183 + if (_effectsInstalled) {
184 + return;
185 + }
186 +
187 + reaction((_) => nodeCreateOrEditViewModel.connectionState,
188 + (ConnectionToNodeState state) {
189 + if (state is ConnectedSuccessfullyState) {
190 + WidgetsBinding.instance.addPostFrameCallback((_) {
191 + showPopUp<void>(
192 + context: context,
193 + builder: (BuildContext context) =>
194 + AlertWithOneAction(
195 + alertTitle: S.of(context).node_new,
196 + alertContent: state.isAlive
197 + ? 'Connected to node'
198 + : 'Not connected to node',
199 + buttonText: S.of(context).ok,
200 + buttonAction: () => Navigator.of(context).pop()));
201 + });
202 + }
203 +
204 + if (state is FailureConnectedState) {
205 + WidgetsBinding.instance.addPostFrameCallback((_) {
206 + showPopUp<void>(
207 + context: context,
208 + builder: (BuildContext context) {
209 + return AlertWithOneAction(
210 + alertTitle: S.of(context).error,
211 + alertContent: state.error,
212 + buttonText: S.of(context).ok,
213 + buttonAction: () => Navigator.of(context).pop());
214 + });
215 + });
216 + }
217 + });
218 +
219 + _effectsInstalled = true;
220 + }
221 }
lib/view_model/node_list/connection_state.dart new
+17
@@ -0,0 +1,17 @@
1 +abstract class ConnectionToNodeState {}
2 +
3 +class InitialConnectionState extends ConnectionToNodeState {}
4 +
5 +class IsConnectingState extends ConnectionToNodeState {}
6 +
7 +class ConnectedSuccessfullyState extends ConnectionToNodeState {
8 + ConnectedSuccessfullyState(this.isAlive);
9 +
10 + final bool isAlive;
11 +}
12 +
13 +class FailureConnectedState extends ConnectionToNodeState {
14 + FailureConnectedState(this.error);
15 +
16 + final String error;
17 +}
\ No newline at end of file
lib/view_model/node_list/node_create_or_edit_view_model.dart
+19 -1
@@ -1,4 +1,5 @@
1 import 'package:cake_wallet/core/execution_state.dart';
2 +import 'package:cake_wallet/view_model/node_list/connection_state.dart';
3 import 'package:hive/hive.dart';
4 import 'package:mobx/mobx.dart';
5 import 'package:cake_wallet/core/wallet_base.dart';
@@ -12,7 +13,8 @@ class NodeCreateOrEditViewModel = NodeCreateOrEditViewModelBase
13
14 abstract class NodeCreateOrEditViewModelBase with Store {
15 NodeCreateOrEditViewModelBase(this._nodeSource, this._wallet)
15 - : state = InitialExecutionState();
16 + : state = InitialExecutionState(),
17 + connectionState = InitialConnectionState();
18
19 @observable
20 ExecutionState state;
@@ -29,6 +31,9 @@ abstract class NodeCreateOrEditViewModelBase with Store {
31 @observable
32 String password;
33
34 + @observable
35 + ConnectionToNodeState connectionState;
36 +
37 @computed
38 bool get isReady =>
39 (address?.isNotEmpty ?? false) && (port?.isNotEmpty ?? false);
@@ -68,4 +73,17 @@ abstract class NodeCreateOrEditViewModelBase with Store {
73 state = FailureState(e.toString());
74 }
75 }
76 +
77 + @action
78 + Future<void> connect() async {
79 + try {
80 + connectionState = IsConnectingState();
81 + final node =
82 + Node(uri: uri, type: _wallet.type, login: login, password: password);
83 + final isAlive = await node.requestNode();
84 + connectionState = ConnectedSuccessfullyState(isAlive);
85 + } catch (e) {
86 + connectionState = FailureConnectedState(e.toString());
87 + }
88 + }
89 }