| 1 | import 'package:cake_wallet/core/execution_state.dart'; |
| 2 | import 'package:cake_wallet/entities/new_ui_entities/list_item/list_item_regular_row.dart'; |
| 3 | import 'package:cake_wallet/generated/i18n.dart'; |
| 4 | import 'package:cake_wallet/new-ui/widgets/keyboard_hide_overlay.dart'; |
| 5 | import 'package:cake_wallet/new-ui/widgets/receive_page/receive_top_bar.dart'; |
| 6 | import 'package:cake_wallet/src/screens/nodes/node_share_page.dart'; |
| 7 | import 'package:cake_wallet/src/screens/nodes/widgets/node_form.dart'; |
| 8 | import 'package:cake_wallet/src/widgets/alert_with_one_action.dart'; |
| 9 | import 'package:cake_wallet/src/widgets/alert_with_two_actions.dart'; |
| 10 | import 'package:cake_wallet/src/widgets/new_list_row/new_list_section.dart'; |
| 11 | import 'package:cake_wallet/utils/show_pop_up.dart'; |
| 12 | import 'package:cake_wallet/view_model/node_list/node_create_or_edit_view_model.dart'; |
| 13 | import 'package:cw_core/currency_for_wallet_type.dart'; |
| 14 | import 'package:cw_core/node.dart'; |
| 15 | import 'package:cw_core/wallet_type.dart'; |
| 16 | import 'package:flutter/cupertino.dart'; |
| 17 | import 'package:flutter/material.dart'; |
| 18 | import 'package:mobx/mobx.dart'; |
| 19 | |
| 20 | class NodeCreateOrEditPage extends StatefulWidget { |
| 21 | NodeCreateOrEditPage({ |
| 22 | required this.nodeCreateOrEditViewModel, |
| 23 | this.editingNode, |
| 24 | this.isSelected, |
| 25 | this.type, |
| 26 | }); |
| 27 | |
| 28 | final NodeCreateOrEditViewModel nodeCreateOrEditViewModel; |
| 29 | final Node? editingNode; |
| 30 | final bool? isSelected; |
| 31 | final WalletType? type; |
| 32 | |
| 33 | @override |
| 34 | State<NodeCreateOrEditPage> createState() => _NodeCreateOrEditPageState(); |
| 35 | } |
| 36 | |
| 37 | class _NodeCreateOrEditPageState extends State<NodeCreateOrEditPage> { |
| 38 | final _nodeFormKey = GlobalKey<NodeFormState>(); |
| 39 | |
| 40 | @override |
| 41 | void initState() { |
| 42 | super.initState(); |
| 43 | reaction( |
| 44 | (_) => widget.nodeCreateOrEditViewModel.connectionState, |
| 45 | (ExecutionState state) { |
| 46 | if (state is ExecutedSuccessfullyState) { |
| 47 | WidgetsBinding.instance.addPostFrameCallback( |
| 48 | (_) => showPopUp<void>( |
| 49 | context: context, |
| 50 | builder: (context) => AlertWithOneAction( |
| 51 | alertTitle: S.of(context).new_node_testing, |
| 52 | alertContent: state.payload as bool |
| 53 | ? S.of(context).node_connection_successful |
| 54 | : S.of(context).node_connection_failed, |
| 55 | buttonText: S.of(context).ok, |
| 56 | buttonAction: () => Navigator.of(context).pop(), |
| 57 | ), |
| 58 | ), |
| 59 | ); |
| 60 | } |
| 61 | |
| 62 | if (state is FailureState) { |
| 63 | WidgetsBinding.instance.addPostFrameCallback( |
| 64 | (_) => showPopUp<void>( |
| 65 | context: context, |
| 66 | builder: (context) => AlertWithOneAction( |
| 67 | alertTitle: S.of(context).error, |
| 68 | alertContent: state.error, |
| 69 | buttonText: S.of(context).ok, |
| 70 | buttonAction: () => Navigator.of(context).pop(), |
| 71 | ), |
| 72 | ), |
| 73 | ); |
| 74 | } |
| 75 | }, |
| 76 | ); |
| 77 | |
| 78 | reaction((_) => widget.nodeCreateOrEditViewModel.state, (state) async { |
| 79 | if (state is ExecutedSuccessfullyState) { |
| 80 | await Future.delayed(Duration(milliseconds: 100)); |
| 81 | if (mounted) Navigator.of(context).pop(widget.nodeCreateOrEditViewModel.editingNode); |
| 82 | } |
| 83 | }); |
| 84 | } |
| 85 | |
| 86 | @override |
| 87 | Widget build(BuildContext context) { |
| 88 | return Container( |
| 89 | color: Theme.of(context).colorScheme.surface, |
| 90 | child: SafeArea( |
| 91 | child: Column( |
| 92 | children: [ |
| 93 | ModalTopBar( |
| 94 | title: widget.editingNode != null ? S.current.edit_node : S.current.node_new, |
| 95 | leadingIcon: Icon(Icons.arrow_back_ios_new), |
| 96 | leadingSemanticLabel: S.current.seed_alert_back, |
| 97 | onLeadingPressed: Navigator.of(context).pop, |
| 98 | trailingIcon: Icon(Icons.check), |
| 99 | trailingSemanticLabel: S.current.save, |
| 100 | onTrailingPressed: () async { |
| 101 | if (_nodeFormKey.currentState != null && !_nodeFormKey.currentState!.validate()) { |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | await widget.nodeCreateOrEditViewModel |
| 106 | .save(saveAsCurrent: widget.isSelected ?? false); |
| 107 | }, |
| 108 | ), |
| 109 | Expanded( |
| 110 | child: KeyboardHideOverlay( |
| 111 | child: Container( |
| 112 | padding: const EdgeInsets.only(left: 18, right: 18), |
| 113 | child: Column(spacing: 18, children: [ |
| 114 | NodeForm( |
| 115 | key: _nodeFormKey, |
| 116 | nodeViewModel: widget.nodeCreateOrEditViewModel, |
| 117 | ), |
| 118 | NewListSections(sections: { |
| 119 | "": [ |
| 120 | if (widget.editingNode != null) |
| 121 | ListItemRegularRow( |
| 122 | keyValue: "share", |
| 123 | label: S.of(context).share_this_node, |
| 124 | foregroundColor: Theme.of(context).colorScheme.primary, |
| 125 | showArrow: false, |
| 126 | onTap: () { |
| 127 | Navigator.of(context).push(CupertinoPageRoute( |
| 128 | builder: (context) => NodeSharePage( |
| 129 | uri: widget.editingNode!.uri, |
| 130 | currency: walletTypeToCryptoCurrency(widget.type!)))); |
| 131 | }), |
| 132 | if (!(widget.editingNode == null || |
| 133 | !widget.nodeCreateOrEditViewModel.isReady || |
| 134 | widget.editingNode!.isBuiltin || |
| 135 | (widget.isSelected ?? false))) |
| 136 | ListItemRegularRow( |
| 137 | keyValue: "delete", |
| 138 | label: S.of(context).delete_this_node, |
| 139 | foregroundColor: Theme.of(context).colorScheme.errorContainer, |
| 140 | showArrow: false, |
| 141 | onTap: () async { |
| 142 | final confirmed = await showPopUp<bool>( |
| 143 | context: context, |
| 144 | builder: (context) => AlertWithTwoActions( |
| 145 | alertTitle: S.of(context).remove_node, |
| 146 | alertContent: S.of(context).remove_node_message, |
| 147 | rightButtonText: S.of(context).remove, |
| 148 | leftButtonText: S.of(context).cancel, |
| 149 | actionRightButton: () => Navigator.pop(context, true), |
| 150 | actionLeftButton: () => Navigator.pop(context, false), |
| 151 | ), |
| 152 | ) ?? |
| 153 | false; |
| 154 | |
| 155 | if (confirmed) { |
| 156 | await widget.nodeCreateOrEditViewModel |
| 157 | .delete(editingNode: widget.editingNode!); |
| 158 | Navigator.of(context).pop(); |
| 159 | } |
| 160 | }) |
| 161 | ] |
| 162 | }) |
| 163 | ]), |
| 164 | ), |
| 165 | ), |
| 166 | ), |
| 167 | ], |
| 168 | ), |
| 169 | ), |
| 170 | ); |
| 171 | } |
| 172 | } |