-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcontact_detail_page.dart
322 lines (303 loc) · 11.7 KB
/
contact_detail_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
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/common/components/address_icon.dart';
import 'package:encointer_wallet/common/components/secondary_button_wide.dart';
import 'package:encointer_wallet/common/components/submit_button_secondary.dart';
import 'package:encointer_wallet/theme/theme.dart';
import 'package:encointer_wallet/models/index.dart';
import 'package:encointer_wallet/page/assets/transfer/transfer_page.dart';
import 'package:encointer_wallet/service/substrate_api/api.dart';
import 'package:encointer_wallet/service/tx/lib/tx.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/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 ContactDetailPage extends StatefulWidget {
const ContactDetailPage(this.accountData, {super.key});
static const String route = '/profile/contactDetail';
final AccountData accountData;
@override
State<ContactDetailPage> createState() => _ContactDetailPageState();
}
class _ContactDetailPageState extends State<ContactDetailPage> {
final TextEditingController _nameCtrl = TextEditingController();
late final AccountData account;
bool isEditing = false;
@override
void initState() {
account = widget.accountData;
_nameCtrl.text = widget.accountData.name;
super.initState();
}
@override
Widget build(BuildContext context) {
final l10n = context.l10n;
final store = context.watch<AppStore>();
final address = AddressUtils.pubKeyHexToAddress(account.pubKey, prefix: store.settings.endpoint.ss58!);
return Scaffold(
appBar: AppBar(
title: isEditing
? TextFormField(key: const Key(EWTestKeys.contactNameField), controller: _nameCtrl)
: Text(_nameCtrl.text),
actions: [
if (isEditing)
IconButton(
key: const Key(EWTestKeys.contactNameEditCheck),
icon: const Icon(Icons.check),
onPressed: () async {
if (_nameCtrl.text != widget.accountData.name) {
final contactData = {
'address': widget.accountData.address,
'name': _nameCtrl.text,
'memo': widget.accountData.memo,
'observation': widget.accountData.observation,
'pubKey': widget.accountData.pubKey,
};
await context.read<AppStore>().settings.updateContact(contactData);
}
setState(() {
isEditing = false;
});
},
)
else
IconButton(
key: const Key(EWTestKeys.contactNameEdit),
icon: const Icon(Iconsax.edit),
onPressed: () {
setState(() {
isEditing = true;
});
},
)
],
centerTitle: true,
backgroundColor: Colors.transparent,
shadowColor: Colors.transparent,
),
body: SafeArea(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
children: <Widget>[
Expanded(
child: ListView(
children: <Widget>[
const SizedBox(height: 30),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
AddressIcon(address, account.pubKey, size: 130),
],
),
const SizedBox(height: 20),
// The below is duplicate code of `accountManagePage`, but according to figma the design will
// change here.
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(Fmt.address(address)!, style: const TextStyle(fontSize: 20)),
IconButton(
icon: const Icon(Iconsax.copy),
color: context.colorScheme.secondary,
onPressed: () => UI.copyAndNotify(context, address),
),
],
),
],
),
),
EndorseButton(store, webApi, account),
const SizedBox(height: 16),
SecondaryButtonWide(
key: const Key(EWTestKeys.sendMoneyToAccount),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(Iconsax.send_sqaure_2),
const SizedBox(width: 12),
Text(
l10n.tokenSend(store.encointer.community?.symbol ?? 'null'),
style: context.titleLarge.copyWith(color: context.colorScheme.primary),
),
],
),
onPressed: () {
Navigator.of(context).pushNamed(
TransferPage.route,
arguments: TransferPageParams(
cid: context.read<AppStore>().encointer.chosenCid,
communitySymbol: context.read<AppStore>().encointer.community?.symbol,
recipientAddress: address,
label: _nameCtrl.text,
),
);
},
),
const SizedBox(height: 16),
SecondaryButtonWide(
onPressed: () => _removeItem(context, account, context.read<AppStore>()),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(Iconsax.trash),
const SizedBox(width: 12),
Text(l10n.contactDelete, style: context.titleLarge.copyWith(color: context.colorScheme.primary))
],
),
),
],
),
),
),
);
}
void _removeItem(BuildContext context, AccountData account, AppStore store) {
final l10n = context.l10n;
showCupertinoDialog<void>(
context: context,
builder: (BuildContext context) {
return CupertinoAlertDialog(
title: Text(l10n.contactDeleteWarn),
content: Text(Fmt.accountName(context, account)),
actions: <Widget>[
CupertinoButton(
child: Text(l10n.cancel),
onPressed: () => Navigator.of(context).pop(),
),
CupertinoButton(
child: Text(l10n.ok),
onPressed: () {
Navigator.of(context).pop();
store.settings.removeContact(account);
if (store.account.currentAccountPubKey == account.pubKey) {
webApi.account.changeCurrentAccount(fetchData: true);
}
Navigator.of(context).pop();
},
),
],
);
},
);
}
}
class EndorseButton extends StatelessWidget {
const EndorseButton(this.store, this.api, this.contact, {super.key});
final AppStore store;
final Api api;
final AccountData contact;
@override
Widget build(BuildContext context) {
final l10n = context.l10n;
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Observer(builder: (_) {
return store.encointer.community!.bootstrappers!.contains(store.account.currentAddress)
? FittedBox(
child: Row(children: [
Text(l10n.remainingNewbieTicketsAsBootStrapper),
Text(
' ${store.encointer.communityAccount?.numberOfNewbieTicketsForBootstrapper ?? 0}',
style: TextStyle(fontSize: 15, color: context.colorScheme.primary),
),
]),
)
: const SizedBox();
}),
Observer(builder: (_) {
return store.encointer.account != null && store.encointer.account!.verifiedReputations.isNotEmpty
? FittedBox(
child: Row(children: [
Text(l10n.remainingNewbieTicketsAsReputable),
Text(
' ${store.encointer.account?.numberOfNewbieTicketsForReputable ?? 0}',
style: TextStyle(fontSize: 15, color: context.colorScheme.primary),
),
]),
)
: !store.encointer.community!.bootstrappers!.contains(store.account.currentAddress)
? Text(l10n.onlyReputablesCanEndorseAttendGatheringToBecomeOne)
: const SizedBox();
}),
const SizedBox(height: 5),
Observer(builder: (_) {
return SubmitButtonSecondary(
key: const Key(EWTestKeys.tapEndorseButton),
onPressed: hasNewbieTickets() ? onPressed : null,
child: FittedBox(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(Iconsax.verify),
const SizedBox(width: 12),
Text(l10n.contactEndorse, style: context.titleLarge.copyWith(color: context.colorScheme.primary))
],
),
),
);
}),
],
);
}
bool hasNewbieTickets() {
if (store.encointer.account!.verifiedReputations.isNotEmpty &&
store.encointer.account!.numberOfNewbieTicketsForReputable > 0) {
return true;
}
// we have not chosen a community yet. May happen after changing the network.
if (store.encointer.community == null) return false;
if (store.encointer.community!.bootstrappers!.contains(store.account.currentAddress) &&
store.encointer.communityAccount!.numberOfNewbieTicketsForBootstrapper > 0) {
return true;
}
return false;
}
Future<void> onPressed(BuildContext context) async {
final community = store.encointer.community;
final bootstrappers = community?.bootstrappers;
final l10n = context.l10n;
final address = AddressUtils.pubKeyHexToAddress(contact.pubKey, prefix: store.settings.endpoint.ss58!);
if (bootstrappers != null && bootstrappers.contains(address)) {
await _popupDialog(context, l10n.cantEndorseBootstrapper);
} else if (store.encointer.currentPhase != CeremonyPhase.Registering) {
await _popupDialog(context, l10n.canEndorseInRegisteringPhaseOnly);
} else {
await submitEndorseNewcomer(
context,
store,
api,
store.account.getKeyringAccount(store.account.currentAccountPubKey!),
store.encointer.chosenCid!,
Address.decode(address),
txPaymentAsset: store.encointer.getTxPaymentAsset(store.encointer.chosenCid),
);
}
}
}
Future<void> _popupDialog(BuildContext context, String content) async {
return showCupertinoDialog<void>(
context: context,
builder: (BuildContext context) {
return CupertinoAlertDialog(
title: Container(),
content: Text(content),
actions: <Widget>[
CupertinoButton(
child: Text(context.l10n.ok),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}