1
+import 'package:another_flushbar/flushbar.dart';
2
+import 'package:cake_wallet/core/auth_service.dart';
3
+import 'package:cake_wallet/core/wallet_name_validator.dart';
4
+import 'package:cake_wallet/palette.dart';
5
+import 'package:cake_wallet/src/widgets/alert_with_one_action.dart';
6
+import 'package:cake_wallet/src/widgets/alert_with_two_actions.dart';
7
+import 'package:cake_wallet/utils/show_bar.dart';
8
+import 'package:cake_wallet/utils/show_pop_up.dart';
9
+import 'package:cake_wallet/view_model/wallet_list/wallet_edit_view_model.dart';
10
+import 'package:cake_wallet/view_model/wallet_list/wallet_list_item.dart';
11
+import 'package:cake_wallet/view_model/wallet_new_vm.dart';
12
+import 'package:flutter/material.dart';
13
+import 'package:cake_wallet/generated/i18n.dart';
14
+import 'package:cake_wallet/src/widgets/primary_button.dart';
15
+import 'package:cake_wallet/src/widgets/base_text_form_field.dart';
16
+import 'package:cake_wallet/src/screens/base_page.dart';
17
+import 'package:flutter_mobx/flutter_mobx.dart';
18
+
19
+class WalletEditPage extends BasePage {
20
+ WalletEditPage(
21
+ {required this.walletEditViewModel,
22
+ required this.editingWallet,
23
+ required this.walletNewVM,
24
+ required this.authService})
25
+ : _formKey = GlobalKey<FormState>(),
26
+ _labelController = TextEditingController(),
27
+ super() {
28
+ _labelController.text = editingWallet.name;
29
+ _labelController.addListener(() => walletEditViewModel.newName = _labelController.text);
30
+ }
31
+
32
+ final GlobalKey<FormState> _formKey;
33
+ final TextEditingController _labelController;
34
+
35
+ final WalletEditViewModel walletEditViewModel;
36
+ final WalletNewVM walletNewVM;
37
+ final WalletListItem editingWallet;
38
+ final AuthService authService;
39
+
40
+ @override
41
+ String get title => S.current.wallet_list_edit_wallet;
42
+
43
+ Flushbar<void>? _progressBar;
44
+
45
+ @override
46
+ Widget body(BuildContext context) {
47
+ return Form(
48
+ key: _formKey,
49
+ child: Container(
50
+ padding: EdgeInsets.all(24.0),
51
+ child: Column(
52
+ children: <Widget>[
53
+ Expanded(
54
+ child: Center(
55
+ child: BaseTextFormField(
56
+ controller: _labelController,
57
+ hintText: S.of(context).wallet_list_wallet_name,
58
+ validator: WalletNameValidator()))),
59
+ Observer(
60
+ builder: (_) {
61
+ final isLoading = walletEditViewModel.state is WalletEditRenamePending ||
62
+ walletEditViewModel.state is WalletEditDeletePending;
63
+
64
+ return Row(
65
+ children: <Widget>[
66
+ Flexible(
67
+ child: Container(
68
+ padding: EdgeInsets.only(right: 8.0),
69
+ child: LoadingPrimaryButton(
70
+ isDisabled: isLoading,
71
+ onPressed: () => _removeWallet(context),
72
+ text: S.of(context).delete,
73
+ color: Palette.red,
74
+ textColor: Colors.white),
75
+ ),
76
+ ),
77
+ Flexible(
78
+ child: Container(
79
+ padding: EdgeInsets.only(left: 8.0),
80
+ child: LoadingPrimaryButton(
81
+ onPressed: () async {
82
+ if (_formKey.currentState?.validate() ?? false) {
83
+ if (walletNewVM.nameExists(walletEditViewModel.newName)) {
84
+ showPopUp<void>(
85
+ context: context,
86
+ builder: (_) {
87
+ return AlertWithOneAction(
88
+ alertTitle: '',
89
+ alertContent: S.of(context).wallet_name_exists,
90
+ buttonText: S.of(context).ok,
91
+ buttonAction: () => Navigator.of(context).pop(),
92
+ );
93
+ },
94
+ );
95
+ } else {
96
+ try {
97
+ await walletEditViewModel.changeName(editingWallet);
98
+ Navigator.of(context).pop();
99
+ walletEditViewModel.resetState();
100
+ } catch (e) {}
101
+ }
102
+ }
103
+ },
104
+ text: S.of(context).save,
105
+ color: Theme.of(context).accentTextTheme.bodyLarge!.color!,
106
+ textColor: Colors.white,
107
+ isDisabled: walletEditViewModel.newName.isEmpty || isLoading,
108
+ ),
109
+ ),
110
+ )
111
+ ],
112
+ );
113
+ },
114
+ )
115
+ ],
116
+ ),
117
+ ),
118
+ );
119
+ }
120
+
121
+ Future<void> _removeWallet(BuildContext context) async {
122
+ authService.authenticateAction(context, onAuthSuccess: (isAuthenticatedSuccessfully) async {
123
+ if (!isAuthenticatedSuccessfully) {
124
+ return;
125
+ }
126
+
127
+ _onSuccessfulAuth(context);
128
+ });
129
+ }
130
+
131
+ void _onSuccessfulAuth(BuildContext context) async {
132
+ bool confirmed = false;
133
+
134
+ await showPopUp<void>(
135
+ context: context,
136
+ builder: (BuildContext dialogContext) {
137
+ return AlertWithTwoActions(
138
+ alertTitle: S.of(context).delete_wallet,
139
+ alertContent: S.of(context).delete_wallet_confirm_message(editingWallet.name),
140
+ leftButtonText: S.of(context).cancel,
141
+ rightButtonText: S.of(context).delete,
142
+ actionLeftButton: () => Navigator.of(dialogContext).pop(),
143
+ actionRightButton: () {
144
+ confirmed = true;
145
+ Navigator.of(dialogContext).pop();
146
+ });
147
+ });
148
+
149
+ if (confirmed) {
150
+ Navigator.of(context).pop();
151
+
152
+ try {
153
+ changeProcessText(context, S.of(context).wallet_list_removing_wallet(editingWallet.name));
154
+ await walletEditViewModel.remove(editingWallet);
155
+ hideProgressText();
156
+ } catch (e) {
157
+ changeProcessText(
158
+ context,
159
+ S.of(context).wallet_list_failed_to_remove(editingWallet.name, e.toString()),
160
+ );
161
+ }
162
+ }
163
+ }
164
+
165
+ void changeProcessText(BuildContext context, String text) {
166
+ _progressBar = createBar<void>(text, duration: null)..show(context);
167
+ }
168
+
169
+ Future<void> hideProgressText() async {
170
+ await Future.delayed(Duration(milliseconds: 50), () {
171
+ _progressBar?.dismiss();
172
+ _progressBar = null;
173
+ });
174
+ }
175
+}