stack_wallet/lib/pages/exchange_view/confirm_change_now_send.dart

412 lines
17 KiB
Dart
Raw Normal View History

import 'dart:async';
2022-08-26 08:11:35 +00:00
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:stackwallet/models/exchange/response_objects/trade.dart';
2022-08-26 08:11:35 +00:00
import 'package:stackwallet/models/trade_wallet_lookup.dart';
import 'package:stackwallet/pages/pinpad_views/lock_screen_view.dart';
import 'package:stackwallet/pages/send_view/sub_widgets/sending_transaction_dialog.dart';
import 'package:stackwallet/pages/wallet_view/wallet_view.dart';
import 'package:stackwallet/providers/exchange/trade_sent_from_stack_lookup_provider.dart';
import 'package:stackwallet/providers/providers.dart';
import 'package:stackwallet/route_generator.dart';
import 'package:stackwallet/utilities/enums/coin_enum.dart';
import 'package:stackwallet/utilities/format.dart';
import 'package:stackwallet/utilities/text_styles.dart';
import 'package:stackwallet/utilities/theme/stack_colors.dart';
2022-08-26 08:11:35 +00:00
import 'package:stackwallet/widgets/custom_buttons/app_bar_icon_button.dart';
import 'package:stackwallet/widgets/rounded_container.dart';
import 'package:stackwallet/widgets/rounded_white_container.dart';
import 'package:stackwallet/widgets/stack_dialog.dart';
import 'package:uuid/uuid.dart';
class ConfirmChangeNowSendView extends ConsumerStatefulWidget {
const ConfirmChangeNowSendView({
Key? key,
required this.transactionInfo,
required this.walletId,
this.routeOnSuccessName = WalletView.routeName,
required this.trade,
}) : super(key: key);
static const String routeName = "/confirmChangeNowSend";
final Map<String, dynamic> transactionInfo;
final String walletId;
final String routeOnSuccessName;
final Trade trade;
2022-08-26 08:11:35 +00:00
@override
ConsumerState<ConfirmChangeNowSendView> createState() =>
_ConfirmChangeNowSendViewState();
}
class _ConfirmChangeNowSendViewState
extends ConsumerState<ConfirmChangeNowSendView> {
late final Map<String, dynamic> transactionInfo;
late final String walletId;
late final String routeOnSuccessName;
late final Trade trade;
2022-08-26 08:11:35 +00:00
Future<void> _attemptSend(BuildContext context) async {
unawaited(showDialog<void>(
2022-08-26 08:11:35 +00:00
context: context,
useSafeArea: false,
barrierDismissible: false,
builder: (context) {
return const SendingTransactionDialog();
},
));
2022-08-26 08:11:35 +00:00
final String note = transactionInfo["note"] as String? ?? "";
final manager =
ref.read(walletsChangeNotifierProvider).getManager(walletId);
try {
final txid = await manager.confirmSend(txData: transactionInfo);
unawaited(manager.refresh());
2022-08-26 08:11:35 +00:00
// save note
await ref
2022-08-26 08:11:35 +00:00
.read(notesServiceChangeNotifierProvider(walletId))
.editOrAddNote(txid: txid, note: note);
await ref.read(tradeSentFromStackLookupProvider).save(
tradeWalletLookup: TradeWalletLookup(
uuid: const Uuid().v1(),
txid: txid,
tradeId: trade.tradeId,
2022-08-26 08:11:35 +00:00
walletIds: [walletId],
),
);
// pop back to wallet
if (mounted) {
Navigator.of(context).popUntil(ModalRoute.withName(routeOnSuccessName));
}
} catch (e) {
// pop sending dialog
Navigator.of(context).pop();
2022-09-21 00:46:07 +00:00
await showDialog<dynamic>(
2022-08-26 08:11:35 +00:00
context: context,
useSafeArea: false,
barrierDismissible: true,
builder: (context) {
return StackDialog(
title: "Broadcast transaction failed",
message: e.toString(),
rightButton: TextButton(
style: Theme.of(context)
.extension<StackColors>()!
.getSecondaryEnabledButtonColor(context),
2022-08-26 08:11:35 +00:00
child: Text(
"Ok",
2022-09-22 22:17:21 +00:00
style: STextStyles.button(context).copyWith(
color: Theme.of(context)
.extension<StackColors>()!
.buttonTextSecondary,
2022-08-26 08:11:35 +00:00
),
),
onPressed: () {
Navigator.of(context).pop();
},
),
);
},
);
}
}
@override
void initState() {
transactionInfo = widget.transactionInfo;
walletId = widget.walletId;
routeOnSuccessName = widget.routeOnSuccessName;
trade = widget.trade;
super.initState();
}
@override
Widget build(BuildContext context) {
final managerProvider = ref.watch(walletsChangeNotifierProvider
.select((value) => value.getManagerProvider(walletId)));
return Scaffold(
2022-09-29 18:05:11 +00:00
backgroundColor: Theme.of(context).extension<StackColors>()!.background,
2022-08-26 08:11:35 +00:00
appBar: AppBar(
backgroundColor: Theme.of(context).extension<StackColors>()!.background,
2022-08-26 08:11:35 +00:00
leading: AppBarBackButton(
onPressed: () async {
// if (FocusScope.of(context).hasFocus) {
// FocusScope.of(context).unfocus();
// await Future<void>.delayed(Duration(milliseconds: 50));
// }
Navigator.of(context).pop();
},
),
title: Text(
"Confirm transaction",
2022-09-22 22:17:21 +00:00
style: STextStyles.navBarTitle(context),
2022-08-26 08:11:35 +00:00
),
),
body: LayoutBuilder(
builder: (builderContext, constraints) {
return Padding(
padding: const EdgeInsets.only(
left: 12,
top: 12,
right: 12,
),
child: SingleChildScrollView(
child: ConstrainedBox(
constraints: BoxConstraints(
minHeight: constraints.maxHeight - 24,
),
child: IntrinsicHeight(
child: Padding(
padding: const EdgeInsets.all(4),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text(
"Send ${ref.watch(managerProvider.select((value) => value.coin)).ticker}",
2022-09-22 22:17:21 +00:00
style: STextStyles.pageTitleH1(context),
2022-08-26 08:11:35 +00:00
),
const SizedBox(
height: 12,
),
RoundedWhiteContainer(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text(
"Send from",
2022-09-22 22:17:21 +00:00
style: STextStyles.smallMed12(context),
2022-08-26 08:11:35 +00:00
),
const SizedBox(
height: 4,
),
Text(
ref
.watch(walletsChangeNotifierProvider)
.getManager(walletId)
.walletName,
2022-09-22 22:17:21 +00:00
style: STextStyles.itemSubtitle12(context),
2022-08-26 08:11:35 +00:00
),
],
),
),
const SizedBox(
height: 12,
),
const SizedBox(
height: 12,
),
RoundedWhiteContainer(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text(
"${trade.exchangeName} address",
2022-09-22 22:17:21 +00:00
style: STextStyles.smallMed12(context),
2022-08-26 08:11:35 +00:00
),
const SizedBox(
height: 4,
),
Text(
"${transactionInfo["address"] ?? "ERROR"}",
2022-09-22 22:17:21 +00:00
style: STextStyles.itemSubtitle12(context),
2022-08-26 08:11:35 +00:00
),
],
),
),
const SizedBox(
height: 12,
),
RoundedWhiteContainer(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
"Amount",
2022-09-22 22:17:21 +00:00
style: STextStyles.smallMed12(context),
2022-08-26 08:11:35 +00:00
),
Text(
"${Format.satoshiAmountToPrettyString(
transactionInfo["recipientAmt"] as int,
ref.watch(
localeServiceChangeNotifierProvider
.select((value) => value.locale),
),
)} ${ref.watch(
managerProvider
.select((value) => value.coin),
).ticker}",
2022-09-22 22:17:21 +00:00
style: STextStyles.itemSubtitle12(context),
2022-08-26 08:11:35 +00:00
textAlign: TextAlign.right,
),
],
),
),
const SizedBox(
height: 12,
),
RoundedWhiteContainer(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
"Transaction fee",
2022-09-22 22:17:21 +00:00
style: STextStyles.smallMed12(context),
2022-08-26 08:11:35 +00:00
),
Text(
"${Format.satoshiAmountToPrettyString(
transactionInfo["fee"] as int,
ref.watch(
localeServiceChangeNotifierProvider
.select((value) => value.locale),
),
)} ${ref.watch(
managerProvider
.select((value) => value.coin),
).ticker}",
2022-09-22 22:17:21 +00:00
style: STextStyles.itemSubtitle12(context),
2022-08-26 08:11:35 +00:00
textAlign: TextAlign.right,
),
],
),
),
const SizedBox(
height: 12,
),
RoundedWhiteContainer(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text(
"Note",
2022-09-22 22:17:21 +00:00
style: STextStyles.smallMed12(context),
2022-08-26 08:11:35 +00:00
),
const SizedBox(
height: 4,
),
Text(
transactionInfo["note"] as String? ?? "",
2022-09-22 22:17:21 +00:00
style: STextStyles.itemSubtitle12(context),
2022-08-26 08:11:35 +00:00
),
],
),
),
const SizedBox(
height: 12,
),
RoundedWhiteContainer(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
"Trade ID",
2022-09-22 22:17:21 +00:00
style: STextStyles.smallMed12(context),
2022-08-26 08:11:35 +00:00
),
Text(
trade.tradeId,
2022-09-22 22:17:21 +00:00
style: STextStyles.itemSubtitle12(context),
2022-08-26 08:11:35 +00:00
textAlign: TextAlign.right,
),
],
),
),
const SizedBox(
height: 12,
),
RoundedContainer(
color: Theme.of(context)
.extension<StackColors>()!
.snackBarBackSuccess,
2022-08-26 08:11:35 +00:00
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
"Total amount",
style:
STextStyles.titleBold12(context).copyWith(
color: Theme.of(context)
.extension<StackColors>()!
.textConfirmTotalAmount,
),
2022-08-26 08:11:35 +00:00
),
Text(
"${Format.satoshiAmountToPrettyString(
(transactionInfo["fee"] as int) +
(transactionInfo["recipientAmt"] as int),
ref.watch(
localeServiceChangeNotifierProvider
.select((value) => value.locale),
),
)} ${ref.watch(
managerProvider
.select((value) => value.coin),
).ticker}",
style: STextStyles.itemSubtitle12(context)
.copyWith(
color: Theme.of(context)
.extension<StackColors>()!
.textConfirmTotalAmount,
),
2022-08-26 08:11:35 +00:00
textAlign: TextAlign.right,
),
],
),
),
const SizedBox(
height: 16,
),
const Spacer(),
TextButton(
style: Theme.of(context)
.extension<StackColors>()!
.getPrimaryEnabledButtonColor(context),
2022-08-26 08:11:35 +00:00
onPressed: () async {
final unlocked = await Navigator.push(
context,
RouteGenerator.getRoute(
shouldUseMaterialRoute:
RouteGenerator.useMaterialPageRoute,
builder: (_) => const LockscreenView(
showBackButton: true,
popOnSuccess: true,
routeOnSuccessArguments: true,
routeOnSuccess: "",
biometricsCancelButtonString: "CANCEL",
biometricsLocalizedReason:
"Authenticate to send transaction",
biometricsAuthenticationTitle:
"Confirm Transaction",
),
settings: const RouteSettings(
name: "/confirmsendlockscreen"),
),
);
if (unlocked is bool && unlocked && mounted) {
await _attemptSend(context);
2022-08-26 08:11:35 +00:00
}
},
child: Text(
"Send",
2022-09-22 22:17:21 +00:00
style: STextStyles.button(context),
2022-08-26 08:11:35 +00:00
),
),
],
),
),
),
),
),
);
},
),
);
}
}