-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathaccount_manage_page.dart
438 lines (402 loc) · 16.6 KB
/
account_manage_page.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
import 'package:encointer_wallet/page/profile/account/remark.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:iconsax/iconsax.dart';
import 'package:provider/provider.dart';
import 'package:encointer_wallet/models/faucet/faucet.dart';
import 'package:encointer_wallet/common/components/address_icon.dart';
import 'package:encointer_wallet/common/components/logo/community_icon.dart';
import 'package:encointer_wallet/theme/theme.dart';
import 'package:encointer_wallet/config.dart';
import 'package:encointer_wallet/utils/repository_provider.dart';
import 'package:encointer_wallet/page/profile/account/benefits.dart';
import 'package:encointer_wallet/page/profile/account/export_result_page.dart';
import 'package:encointer_wallet/page/profile/contacts/account_share_page.dart';
import 'package:encointer_wallet/service/log/log_service.dart';
import 'package:encointer_wallet/service/substrate_api/api.dart';
import 'package:encointer_wallet/modules/modules.dart';
import 'package:encointer_wallet/store/account/types/account_data.dart';
import 'package:encointer_wallet/store/app.dart';
import 'package:encointer_wallet/utils/format.dart';
import 'package:encointer_wallet/utils/input_validation.dart';
import 'package:encointer_wallet/l10n/l10.dart';
import 'package:encointer_wallet/utils/ui.dart';
import 'package:ew_keyring/ew_keyring.dart';
import 'package:ew_test_keys/ew_test_keys.dart';
class AccountManagePage extends StatefulWidget {
const AccountManagePage({super.key});
static const String route = '/profile/account';
@override
State<AccountManagePage> createState() => _AccountManagePageState();
}
enum AccountAction { delete, export }
class _AccountManagePageState extends State<AccountManagePage> {
TextEditingController? _nameCtrl;
bool _isEditingText = false;
late final AppStore _appStore;
Map<String, Faucet>? faucets;
@override
void initState() {
super.initState();
_appStore = context.read<AppStore>();
if (_appStore.encointer.chosenCid != null) webApi.encointer.getBootstrappers();
_init();
}
Future<void> _init() async {
final allFaucets = await webApi.encointer.getAllFaucetsWithAccount();
// show faucets we have reputation for and faucets for `chosenCid`.
final relevantCids = _appStore.encointer.account!.reputations.values.map((e) => e.communityIdentifier).toSet()
..add(_appStore.encointer.chosenCid!);
faucets = Map.fromEntries(allFaucets.entries.where((e) {
final whitelist = e.value.whitelist;
if (whitelist == null) {
// if the whitelist is null, all communities may access it.
return true;
} else {
return containsAny(whitelist, relevantCids.toList());
}
}));
setState(() {});
}
@override
void dispose() {
_nameCtrl!.dispose();
super.dispose();
}
void _onDeleteAccount(BuildContext context, AccountData accountToBeEdited) {
LoginDialog.verifyPinOrBioAuth(
context,
titleText: context.l10n.accountDelete,
onSuccess: (v) async {
await _appStore.account
.removeAccount(accountToBeEdited)
.then((_) => _appStore.loadAccountCache())
.then((_) => webApi.fetchAccountData());
if (_appStore.account.accountListAll.isEmpty) {
await context.read<LoginStore>().deleteAuthenticationData();
await Navigator.pushNamedAndRemoveUntil(context, CreateAccountEntryView.route, (route) => false);
} else {
Navigator.pop(context);
}
},
);
}
Widget _getBalanceEntryListTile(int index, String address) {
final h3 = context.titleLarge.copyWith(color: context.colorScheme.primary);
// Checked in `ListView` generator that `balanceEntries` are not null.
final cidFmt = _appStore.encointer.accountStores![address]!.balanceEntries.keys.elementAt(index);
final entry = _appStore.encointer.accountStores![address]!.balanceEntries[cidFmt]!;
final community = _appStore.encointer.communityStores?[cidFmt];
if (community == null || community.applyDemurrage == null) {
// Never happened, but we want to be defensive here to prevent a red screen.
Log.e('[AccountManagePage] Communities is null, even though we have a balance entry for it. Fatal app error.');
return Container();
}
if ((community.applyDemurrage!(entry) ?? 0) <= 0.0001 && cidFmt != _appStore.encointer.chosenCid!.toFmtString()) {
Log.p("[AccountManagePage] Don't display community with 0 balance");
return Container();
}
final isBootstrapper = _appStore.encointer.community!.bootstrappers != null &&
_appStore.encointer.community!.bootstrappers!.contains(address);
Log.d('_getBalanceEntryListTile: $community', 'AccountManagePage');
return ListTile(
contentPadding: const EdgeInsets.symmetric(),
leading: CommunityIcon(
store: _appStore,
isBootstrapper: isBootstrapper,
icon: CommunityCircleAvatar(community.icon),
),
title: Text(community.name!, style: h3),
subtitle: Text(community.symbol!, style: h3),
trailing: Text(
// Should never be null, but we still want to be defensive here.
'${community.applyDemurrage != null ? Fmt.doubleFormat(community.applyDemurrage!(entry)) : 0} ⵐ',
style: h3.copyWith(color: AppColors.encointerGrey),
),
);
}
Future<void> _showPasswordDialog(BuildContext context, AccountData accountToBeEdited) async {
await LoginDialog.verifyPinOrBioAuth(
context,
titleText: context.l10n.confirmPin,
autoCloseOnSuccess: false,
onSuccess: (password) async {
Navigator.of(context).pop();
final account = _appStore.account.getKeyringAccount(accountToBeEdited.pubKey);
await Navigator.of(context).pushNamed(ExportResultPage.route, arguments: {
'key': account.uri,
'type': '',
});
},
);
}
@override
Widget build(BuildContext context) {
final l10n = context.l10n;
final h3Grey = context.titleLarge.copyWith(fontSize: 19, color: AppColors.encointerGrey);
final isKeyboard = MediaQuery.of(context).viewInsets.bottom != 0;
final store = context.watch<AppStore>();
final appConfig = RepositoryProvider.of<AppConfig>(context);
final accountToBeEditedPubKey = ModalRoute.of(context)!.settings.arguments as String?;
final accountToBeEdited = store.account.getAccountData(accountToBeEditedPubKey);
final addressSS58 =
AddressUtils.pubKeyHexToAddress(accountToBeEditedPubKey!, prefix: store.settings.currentNetwork.ss58());
_nameCtrl = TextEditingController(text: accountToBeEdited.name);
_nameCtrl!.selection = TextSelection.fromPosition(TextPosition(offset: _nameCtrl!.text.length));
Widget benefits() {
if (faucets == null) {
return appConfig.isIntegrationTest ? const SizedBox.shrink() : const CupertinoActivityIndicator();
}
if (store.account.currentAccountPubKey! != accountToBeEditedPubKey) {
return Column(children: [
Text(l10n.benefits, style: h3Grey, textAlign: TextAlign.left),
Text(l10n.canUseFaucetOnlyWithCurrentAccount, style: h3Grey, textAlign: TextAlign.left),
]);
}
return Benefits(
store,
faucets: faucets!,
userAddress: Address(
pubkey: AddressUtils.pubKeyHexToPubKey(accountToBeEditedPubKey),
prefix: store.settings.currentNetwork.ss58(),
),
);
}
Widget remarks() {
return Remarks(
store,
userAddress: Address(
pubkey: AddressUtils.pubKeyHexToPubKey(accountToBeEditedPubKey),
prefix: store.settings.currentNetwork.ss58(),
),
);
}
return Observer(
builder: (_) => Scaffold(
appBar: AppBar(
leading: IconButton(
key: const Key(EWTestKeys.closeAccountManage),
onPressed: () => Navigator.pop(context),
icon: const Icon(Icons.close),
),
title: _isEditingText
? TextFormField(
key: const Key(EWTestKeys.accountNameField),
controller: _nameCtrl,
validator: (v) => InputValidation.validateAccountName(context, v, _appStore.account.accountList),
)
: Text(_nameCtrl!.text),
actions: <Widget>[
if (!_isEditingText)
IconButton(
key: const Key(EWTestKeys.accountNameEdit),
icon: const Icon(Iconsax.edit),
onPressed: () {
setState(() {
_isEditingText = true;
});
},
)
else
IconButton(
key: const Key(EWTestKeys.accountNameEditCheck),
icon: const Icon(Icons.check),
onPressed: () async {
await _appStore.account.updateAccountName(accountToBeEdited, _nameCtrl!.text.trim());
setState(() {
_isEditingText = false;
});
},
)
],
),
body: SafeArea(
child: Column(
children: [
Expanded(
child: SingleChildScrollView(
padding: const EdgeInsets.all(16),
child: Column(
children: <Widget>[
const SizedBox(height: 20),
if (!isKeyboard)
AddressIcon(
addressSS58,
accountToBeEditedPubKey,
size: 130,
),
Text(
addressSS58,
key: const Key(EWTestKeys.accountPublicKey),
style: const TextStyle(fontSize: 2, color: Colors.transparent),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
Fmt.address(addressSS58)!,
style: const TextStyle(fontSize: 20),
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
IconButton(
icon: const Icon(Iconsax.copy),
color: context.colorScheme.secondary,
onPressed: () => UI.copyAndNotify(context, addressSS58),
),
],
),
Text(l10n.communities, style: h3Grey, textAlign: TextAlign.left),
ListView.builder(
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
itemCount: store.encointer.accountStores!.containsKey(addressSS58)
? store.encointer.accountStores![addressSS58]?.balanceEntries.length ?? 0
: 0,
itemBuilder: (BuildContext context, int index) => _getBalanceEntryListTile(
index,
addressSS58,
),
),
benefits(),
remarks(),
const SizedBox(height: 20),
],
),
),
),
Padding(
padding: const EdgeInsets.all(16),
child: DecoratedBox(
decoration: BoxDecoration(
gradient: AppColors.primaryGradient(context),
borderRadius: BorderRadius.circular(20),
),
child: Row(
children: [
ElevatedButton(
key: const Key(EWTestKeys.goToAccountShare),
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.all(16),
backgroundColor: Colors.transparent,
foregroundColor: Colors.white,
shadowColor: Colors.transparent,
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
const Icon(Iconsax.share),
const SizedBox(width: 12),
Text(
l10n.accountShare,
style: context.titleLarge.copyWith(color: context.colorScheme.background, fontSize: 19),
),
],
),
onPressed: () =>
Navigator.pushNamed(context, AccountSharePage.route, arguments: accountToBeEditedPubKey),
),
const Spacer(),
PopupMenuButton<AccountAction>(
offset: const Offset(-10, -150),
icon: const Icon(
Iconsax.more,
key: Key(EWTestKeys.popupMenuAccountTrashExport),
color: Colors.white,
),
color: context.colorScheme.background,
padding: const EdgeInsets.all(20),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20),
),
onSelected: (AccountAction accountAction) {
return switch (accountAction) {
AccountAction.delete => _onDeleteAccount(context, accountToBeEdited),
AccountAction.export => _showPasswordDialog(context, accountToBeEdited),
};
},
itemBuilder: (BuildContext context) => [
AccountActionItemData(
accountAction: AccountAction.delete,
icon: Iconsax.trash,
title: l10n.deleteAccount,
),
AccountActionItemData(
accountAction: AccountAction.export, icon: Iconsax.export, title: l10n.exportAccount),
]
.map((AccountActionItemData data) => PopupMenuItem<AccountAction>(
key: Key(data.accountAction.name),
value: data.accountAction,
child: ListTileTheme(
textColor: context.colorScheme.secondary,
iconColor: context.colorScheme.secondary,
child: ListTile(
minLeadingWidth: 0,
title: Text(data.title),
leading: Icon(data.icon),
),
),
))
.toList(),
),
],
),
),
),
],
),
),
),
);
}
}
class AccountActionItemData {
const AccountActionItemData({
required this.title,
required this.accountAction,
required this.icon,
});
// in newer flutter versions you can put that stuff into the AccountAction enum and do not need an extra class
final String title;
final AccountAction accountAction;
final IconData icon;
}
class CommunityIcon extends StatelessWidget {
const CommunityIcon({
super.key,
required this.store,
required this.icon,
required this.isBootstrapper,
});
final AppStore store;
final Widget icon;
final bool isBootstrapper;
@override
Widget build(BuildContext context) {
return Stack(
children: [
SizedBox(
width: 50,
height: 50,
child: icon,
),
if (isBootstrapper)
const Positioned(
bottom: 0,
right: 0,
child: Icon(Iconsax.star, color: Colors.yellow),
)
],
);
}
}
/// Checks if any entry in list one is contained in another list.
bool containsAny(List<dynamic> list1, List<dynamic> list2) {
for (final entry in list1) {
if (list2.contains(entry)) {
return true;
}
}
return false;
}