-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathpay_send_payment_screen.dart
More file actions
468 lines (443 loc) · 16.5 KB
/
Copy pathpay_send_payment_screen.dart
File metadata and controls
468 lines (443 loc) · 16.5 KB
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
import 'package:bb_mobile/core/fees/domain/fees_entity.dart';
import 'package:bb_mobile/core/widgets/bottom_sheet/x.dart';
import 'package:bb_mobile/core/themes/app_theme.dart';
import 'package:bb_mobile/core/utils/amount_conversions.dart';
import 'package:bb_mobile/core/utils/amount_formatting.dart';
import 'package:bb_mobile/core/utils/build_context_x.dart';
import 'package:bb_mobile/core/widgets/buttons/button.dart';
import 'package:bb_mobile/core/widgets/loading/fading_linear_progress.dart';
import 'package:bb_mobile/core/widgets/loading/loading_line_content.dart';
import 'package:bb_mobile/core/widgets/scrollable_column.dart';
import 'package:bb_mobile/core/widgets/snackbar_utils.dart';
import 'package:bb_mobile/core/widgets/timers/countdown.dart';
import 'package:bb_mobile/features/pay/presentation/pay_bloc.dart';
import 'package:bb_mobile/features/pay/ui/widgets/pay_advanced_options_bottom_sheet.dart';
import 'package:bb_mobile/features/recipients/domain/value_objects/recipient_type.dart';
import 'package:bb_mobile/features/recipients/interface_adapters/presenters/models/recipient_view_model.dart';
import 'package:bb_mobile/generated/flutter_gen/assets.gen.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:gap/gap.dart';
class PaySendPaymentScreen extends StatelessWidget {
const PaySendPaymentScreen({super.key});
String _formatSinpePhoneNumber(String? phoneNumber) {
if (phoneNumber == null || phoneNumber.isEmpty) return 'N/A';
// Remove any existing formatting
final String cleanNumber = phoneNumber.replaceAll(RegExp(r'[^\d]'), '');
// Add +506 prefix
final String formattedNumber = '+506$cleanNumber';
// Add dashes every 4 digits after the prefix
if (cleanNumber.length >= 4) {
const String prefix = '+506';
final String number = cleanNumber;
final StringBuffer formatted = StringBuffer(prefix);
for (int i = 0; i < number.length; i += 4) {
final int end = (i + 4 < number.length) ? i + 4 : number.length;
formatted.write('-${number.substring(i, end)}');
}
return formatted.toString();
}
return formattedNumber;
}
@override
Widget build(BuildContext context) {
final isConfirmingPayment = context.select(
(PayBloc bloc) =>
bloc.state is PayPaymentState &&
(bloc.state as PayPaymentState).isConfirmingPayment,
);
final wallet = context.select(
(PayBloc bloc) =>
bloc.state is PayPaymentState
? (bloc.state as PayPaymentState).selectedWallet
: null,
);
final order = context.select(
(PayBloc bloc) =>
bloc.state is PayPaymentState
? (bloc.state as PayPaymentState).payOrder
: null,
);
final recipient = context.select(
(PayBloc bloc) =>
bloc.state is PayPaymentState
? (bloc.state as PayPaymentState).selectedRecipient
: null,
);
return Scaffold(
appBar: AppBar(
title: Image.asset(
Assets.logos.bbLogoSmall.path,
height: 32,
width: 32,
),
),
body: SafeArea(
child: ScrollableColumn(
padding: const EdgeInsets.symmetric(horizontal: 24.0),
children: [
FadingLinearProgress(
height: 3,
trigger: isConfirmingPayment,
backgroundColor: context.appColors.surface,
foregroundColor: context.appColors.primary,
),
const Gap(24.0),
Text(
context.loc.payConfirmPayment,
style: context.font.headlineMedium?.copyWith(
color: context.appColors.secondary,
),
),
const Gap(4.0),
Row(
mainAxisAlignment: .center,
children: [
Text(
context.loc.payPriceRefreshIn,
style: context.font.bodyMedium?.copyWith(
color: context.appColors.outline,
),
),
if (order != null)
Countdown(
until: order.confirmationDeadline,
onTimeout: () {
context.read<PayBloc>().add(
const PayEvent.orderRefreshTimePassed(),
);
},
),
],
),
const Gap(8.0),
_DetailRow(
title: context.loc.payOrderNumber,
value: order?.orderNumber.toString(),
copyValue: order?.orderNumber.toString(),
),
if (order?.paymentDescription != null &&
order!.paymentDescription!.isNotEmpty)
_DetailRow(
title: context.loc.payPaymentDescription,
value: order.paymentDescription,
),
_DetailRow(
title: context.loc.payRecipientType,
value:
recipient != null
? switch (recipient.type) {
// TODO: Use localization labels instead of hardcoded strings.
// CANADA types
RecipientType.interacEmailCad => 'Interac e-Transfer',
RecipientType.billPaymentCad => 'Bill Payment',
RecipientType.bankTransferCad => 'Bank Transfer',
// EUROPE types
RecipientType.sepaEur => 'SEPA Transfer',
// MEXICO types
RecipientType.speiClabeMxn => 'SPEI CLABE',
RecipientType.speiSmsMxn => 'SPEI SMS',
RecipientType.speiCardMxn => 'SPEI Card',
// COSTA RICA types
RecipientType.sinpeIbanUsd => 'SINPE IBAN (USD)',
RecipientType.sinpeIbanCrc => 'SINPE IBAN (CRC)',
RecipientType.sinpeMovilCrc => 'SINPE Móvil',
// ARGENTINA types
RecipientType.bankAccountArgentina => 'CBU/CVU Argentina',
RecipientType.pseColombia => 'Bank Account COP',
RecipientType.nequiColombia => 'Nequi',
}
: null,
),
_DetailRow(
title: context.loc.payRecipientName,
value: recipient?.displayName,
),
_DetailRow(
title: context.loc.payRecipientDetails,
value:
recipient != null ? _getRecipientInfoValue(recipient) : null,
),
const _Divider(),
_DetailRow(
title: context.loc.payPayinAmount,
value: order == null ? null : FormatAmount.btc(order.payinAmount),
),
_DetailRow(
title: context.loc.payPayoutAmount,
value:
order == null
? null
: FormatAmount.fiat(
order.payoutAmount,
order.payoutCurrency,
),
),
_DetailRow(
title: context.loc.payExchangeRate,
value:
order == null
? null
: FormatAmount.fiat(
order.exchangeRateAmount ??
order.payoutAmount / order.payinAmount,
order.exchangeRateCurrency ?? order.payoutCurrency,
),
),
const _Divider(),
_DetailRow(
title: context.loc.payPayFromWallet,
value:
wallet?.label ??
(wallet?.isDefault == true
? wallet?.isLiquid == true
? context.loc.payInstantPayments
: context.loc.paySecureBitcoinWallet
: ''),
),
if (wallet != null && !wallet.isLiquid) ...[
_DetailRow(
title: context.loc.payFeePriority,
value: context.loc.payFastest,
onTap: () {
debugPrint('Tapped Fee Priority');
},
),
],
_DetailRow(
title: context.loc.payNetworkFees,
value: context.select((PayBloc bloc) {
final state = bloc.state;
if (state is PayPaymentState && state.absoluteFees != null) {
return FormatAmount.btc(
ConvertAmount.satsToBtc(state.absoluteFees!),
);
}
return context.loc.payCalculating;
}),
),
const Spacer(),
_BottomButtons(
onContinuePressed: () {
context.read<PayBloc>().add(
const PayEvent.sendPaymentConfirmed(
feeSelection: FeeSelection.fastest,
customFee: null,
),
);
},
),
],
),
),
);
}
String? _getRecipientInfoValue(RecipientViewModel recipient) {
switch (recipient.type) {
case RecipientType.bankAccountArgentina:
case RecipientType.pseColombia:
return recipient.bankAccount;
case RecipientType.nequiColombia:
return recipient.phoneNumber;
case RecipientType.interacEmailCad:
return recipient.email;
case RecipientType.billPaymentCad:
return recipient.payeeName ??
recipient.payeeCode ??
recipient.payeeAccountNumber;
case RecipientType.bankTransferCad:
return '${recipient.institutionNumber}-${recipient.transitNumber}-${recipient.accountNumber}';
case RecipientType.sepaEur:
return recipient.iban;
case RecipientType.speiClabeMxn:
return recipient.clabe;
case RecipientType.speiSmsMxn:
return recipient.phoneNumber;
case RecipientType.speiCardMxn:
return recipient.debitcard;
case RecipientType.sinpeIbanUsd:
return recipient.iban;
case RecipientType.sinpeIbanCrc:
return recipient.iban;
case RecipientType.sinpeMovilCrc:
return _formatSinpePhoneNumber(recipient.phoneNumber);
}
}
}
class _DetailRow extends StatelessWidget {
final String title;
final String? value;
final void Function()? onTap;
final String? copyValue;
const _DetailRow({
required this.title,
required this.value,
this.onTap,
this.copyValue,
}) : super();
@override
Widget build(BuildContext context) {
final valueColor =
onTap == null
? context.appColors.secondary
: context.appColors.primary;
return Padding(
padding: const EdgeInsets.symmetric(vertical: 12.0),
child:
value == null
? const LoadingLineContent()
: Row(
mainAxisAlignment: .spaceBetween,
children: [
Text(
title,
style: context.font.bodyMedium?.copyWith(
color: context.appColors.onSurfaceVariant,
),
),
Expanded(
child:
onTap == null
? Row(
mainAxisAlignment: .end,
children: [
Flexible(
child: Text(
value!,
textAlign: .end,
maxLines: 2,
style: context.font.bodyMedium?.copyWith(
color: valueColor,
),
),
),
if (copyValue != null) ...[
const Gap(8),
GestureDetector(
onTap: () {
Clipboard.setData(
ClipboardData(text: copyValue!),
);
SnackBarUtils.showCopiedSnackBar(context);
},
child: Icon(
Icons.copy,
color: context.appColors.primary,
size: 16,
),
),
],
],
)
: GestureDetector(
onTap: onTap,
behavior: .opaque,
child: Row(
mainAxisAlignment: .end,
children: [
Flexible(
child: Text(
value!,
textAlign: .end,
maxLines: 2,
style: context.font.bodyMedium?.copyWith(
color: valueColor,
),
),
),
Icon(
Icons.chevron_right,
color: valueColor,
size: 20,
),
],
),
),
),
],
),
);
}
}
class _Divider extends StatelessWidget {
const _Divider();
@override
Widget build(BuildContext context) {
return Divider(color: context.appColors.secondaryFixedDim, height: 1);
}
}
class _BottomButtons extends StatelessWidget {
final VoidCallback onContinuePressed;
const _BottomButtons({required this.onContinuePressed}) : super();
@override
Widget build(BuildContext context) {
final isConfirmingPayment = context.select(
(PayBloc bloc) =>
bloc.state is PayPaymentState &&
(bloc.state as PayPaymentState).isConfirmingPayment,
);
final wallet = context.select(
(PayBloc bloc) =>
bloc.state is PayPaymentState
? (bloc.state as PayPaymentState).selectedWallet
: null,
);
return Column(
children: [
const _PayError(),
if (wallet != null && !wallet.isLiquid) ...[
BBButton.big(
label: context.loc.payAdvancedSettings,
onPressed: () {
BlurredBottomSheet.show(
context: context,
child: BlocProvider.value(
value: context.read<PayBloc>(),
child: const PayAdvancedOptionsBottomSheet(),
),
);
},
bgColor: context.appColors.transparent,
textColor: context.appColors.secondary,
outlined: true,
borderColor: context.appColors.secondary,
),
const Gap(16),
],
BBButton.big(
label: context.loc.payContinue,
disabled: isConfirmingPayment,
onPressed: onContinuePressed,
bgColor: context.appColors.secondary,
textColor: context.appColors.onSecondary,
),
],
);
}
}
class _PayError extends StatelessWidget {
const _PayError();
@override
Widget build(BuildContext context) {
final payError = context.select(
(PayBloc bloc) =>
bloc.state is PayPaymentState
? (bloc.state as PayPaymentState).error
: null,
);
if (payError == null) return const SizedBox.shrink();
return Center(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 16.0),
child: Text(
payError.toTranslated(context),
style: context.font.bodyMedium?.copyWith(
color: context.appColors.error,
),
textAlign: TextAlign.center,
),
),
);
}
}