Allow setting the new added node as the current selected node

OmarHatem committed Dec 4, 2022 at 17:30 UTC 9eb8a442d813ece2f9f73b11142afc30faf96721
3 files changed +18 -8
lib/di.dart
+5 -2
@@ -500,8 +500,11 @@ Future setup(
500 getIt.registerFactory(() => OtherSettingsPage(getIt.get<OtherSettingsViewModel>()));
501
502 getIt.registerFactoryParam<NodeCreateOrEditViewModel, WalletType?, void>(
503 - (WalletType? type, _) =>
504 - NodeCreateOrEditViewModel(_nodeSource, type ?? getIt.get<AppStore>().wallet!.type));
503 + (WalletType? type, _) => NodeCreateOrEditViewModel(
504 + _nodeSource,
505 + type ?? getIt.get<AppStore>().wallet!.type,
506 + getIt.get<SettingsStore>(),
507 + ));
508
509 getIt.registerFactory(
510 () => NodeCreateOrEditPage(getIt.get<NodeCreateOrEditViewModel>()));
lib/src/screens/new_wallet/advanced_privacy_settings_page.dart
+4 -4
@@ -10,9 +10,9 @@ import 'package:cake_wallet/src/widgets/primary_button.dart';
10 import 'package:cake_wallet/src/widgets/scollable_with_bottom_section.dart';
11
12 class AdvancedPrivacySettingsPage extends BasePage {
13 - AdvancedPrivacySettingsPage(this.privacySettingsViewModel, this.nodeViewModel);
13 + AdvancedPrivacySettingsPage(this.advancedPrivacySettingsViewModel, this.nodeViewModel);
14
15 - final AdvancedPrivacySettingsViewModel privacySettingsViewModel;
15 + final AdvancedPrivacySettingsViewModel advancedPrivacySettingsViewModel;
16 final NodeCreateOrEditViewModel nodeViewModel;
17
18 @override
@@ -20,7 +20,7 @@ class AdvancedPrivacySettingsPage extends BasePage {
20
21 @override
22 Widget body(BuildContext context) =>
23 - AdvancedPrivacySettingsBody(privacySettingsViewModel, nodeViewModel);
23 + AdvancedPrivacySettingsBody(advancedPrivacySettingsViewModel, nodeViewModel);
24 }
25
26 class AdvancedPrivacySettingsBody extends StatefulWidget {
@@ -78,7 +78,7 @@ class _AdvancedPrivacySettingsBodyState extends State<AdvancedPrivacySettingsBod
78 children: [
79 LoadingPrimaryButton(
80 onPressed: () {
81 - widget.nodeViewModel.save();
81 + widget.nodeViewModel.save(saveAsCurrent: true);
82 Navigator.pop(context);
83 },
84 text: S.of(context).continue_text,
lib/view_model/node_list/node_create_or_edit_view_model.dart
+9 -2
@@ -1,4 +1,5 @@
1 import 'package:cake_wallet/core/execution_state.dart';
2 +import 'package:cake_wallet/store/settings_store.dart';
3 import 'package:hive/hive.dart';
4 import 'package:mobx/mobx.dart';
5 import 'package:cw_core/node.dart';
@@ -10,7 +11,7 @@ class NodeCreateOrEditViewModel = NodeCreateOrEditViewModelBase
11 with _$NodeCreateOrEditViewModel;
12
13 abstract class NodeCreateOrEditViewModelBase with Store {
13 - NodeCreateOrEditViewModelBase(this._nodeSource, this._walletType)
14 + NodeCreateOrEditViewModelBase(this._nodeSource, this._walletType, this._settingsStore)
15 : state = InitialExecutionState(),
16 connectionState = InitialExecutionState(),
17 useSSL = false,
@@ -63,6 +64,7 @@ abstract class NodeCreateOrEditViewModelBase with Store {
64
65 final WalletType _walletType;
66 final Box<Node> _nodeSource;
67 + final SettingsStore _settingsStore;
68
69 @action
70 void reset() {
@@ -75,13 +77,18 @@ abstract class NodeCreateOrEditViewModelBase with Store {
77 }
78
79 @action
78 - Future<void> save() async {
80 + Future<void> save({bool saveAsCurrent = false}) async {
81 try {
82 state = IsExecutingState();
83 final node =
84 Node(uri: uri, type: _walletType, login: login, password: password,
85 useSSL: useSSL, trusted: trusted);
86 await _nodeSource.add(node);
87 +
88 + if (saveAsCurrent) {
89 + _settingsStore.nodes[_walletType] = node;
90 + }
91 +
92 state = ExecutedSuccessfullyState();
93 } catch (e) {
94 state = FailureState(e.toString());