CakeWallet/lib/src/screens/send/send_page.dart

469 lines
20 KiB
Dart
Raw Normal View History

2020-08-25 19:32:40 +03:00
import 'dart:ui';
import 'package:cake_wallet/entities/fiat_currency.dart';
import 'package:cake_wallet/src/screens/dashboard/widgets/sync_indicator_icon.dart';
import 'package:cake_wallet/src/screens/send/widgets/send_card.dart';
import 'package:cake_wallet/src/widgets/alert_with_two_actions.dart';
2022-03-30 17:57:04 +02:00
import 'package:cake_wallet/src/widgets/picker.dart';
import 'package:cake_wallet/src/widgets/template_tile.dart';
import 'package:cake_wallet/view_model/send/output.dart';
import 'package:cake_wallet/view_model/settings/settings_view_model.dart';
2020-09-29 20:56:11 +03:00
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:mobx/mobx.dart';
import 'package:cake_wallet/routes.dart';
import 'package:cake_wallet/view_model/send/send_view_model.dart';
import 'package:cake_wallet/core/execution_state.dart';
import 'package:cake_wallet/src/screens/base_page.dart';
import 'package:cake_wallet/src/widgets/primary_button.dart';
import 'package:cake_wallet/src/widgets/scollable_with_bottom_section.dart';
import 'package:cake_wallet/src/widgets/trail_button.dart';
2020-09-25 18:32:44 +03:00
import 'package:cake_wallet/utils/show_pop_up.dart';
import 'package:cake_wallet/view_model/send/send_view_model_state.dart';
2020-01-04 21:31:52 +02:00
import 'package:cake_wallet/generated/i18n.dart';
import 'package:dotted_border/dotted_border.dart';
import 'package:cake_wallet/src/widgets/alert_with_one_action.dart';
import 'package:cake_wallet/src/screens/send/widgets/confirm_sending_alert.dart';
import 'package:smooth_page_indicator/smooth_page_indicator.dart';
2022-03-30 17:57:04 +02:00
import 'package:cw_core/crypto_currency.dart';
2020-08-25 19:32:40 +03:00
2020-01-04 21:31:52 +02:00
class SendPage extends BasePage {
SendPage({@required this.sendViewModel,@required this.settingsViewModel }) : _formKey = GlobalKey<FormState>(),fiatFromSettings = settingsViewModel.fiatCurrency;
2020-07-06 23:09:03 +03:00
final SendViewModel sendViewModel;
final SettingsViewModel settingsViewModel;
2020-10-02 20:28:29 +03:00
final GlobalKey<FormState> _formKey;
final controller = PageController(initialPage: 0);
final FiatCurrency fiatFromSettings ;
2020-09-21 22:01:45 +03:00
bool _effectsInstalled = false;
2020-07-06 23:09:03 +03:00
2020-01-08 14:26:34 +02:00
@override
2020-09-21 22:01:45 +03:00
String get title => S.current.send;
2020-01-08 14:26:34 +02:00
@override
Color get titleColor => Colors.white;
2020-01-08 14:26:34 +02:00
@override
bool get resizeToAvoidBottomInset => false;
2020-01-04 21:31:52 +02:00
@override
bool get extendBodyBehindAppBar => true;
2020-09-01 14:18:07 +03:00
2020-09-02 11:47:41 +03:00
@override
AppBarStyle get appBarStyle => AppBarStyle.transparent;
2020-09-02 11:47:41 +03:00
@override
void onClose(BuildContext context) {
settingsViewModel.setFiatCurrency(fiatFromSettings);
Navigator.of(context).pop();
}
@override
Widget middle(BuildContext context) => Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.only(right:8.0),
child: Observer(builder: (_) => SyncIndicatorIcon(isSynced: sendViewModel.isReadyForSend),),
),super.middle(context),
],
);
2020-09-02 11:47:41 +03:00
@override
Widget trailing(context) => Observer(builder: (_) {
2022-01-12 18:58:54 +01:00
return sendViewModel.isBatchSending
? TrailButton(
caption: S.of(context).remove,
onPressed: () {
var pageToJump = controller.page.round() - 1;
pageToJump = pageToJump > 0 ? pageToJump : 0;
final output = _defineCurrentOutput();
sendViewModel.removeOutput(output);
controller.jumpToPage(pageToJump);
})
: TrailButton(
caption: S.of(context).clear,
onPressed: () {
final output = _defineCurrentOutput();
_formKey.currentState.reset();
output.reset();
});
});
2020-09-02 11:47:41 +03:00
2020-09-21 22:01:45 +03:00
@override
Widget body(BuildContext context) {
_setEffects(context);
return Form(
key: _formKey,
child: ScrollableWithBottomSection(
contentPadding: EdgeInsets.only(bottom: 24),
content: Column(
children: <Widget>[
Container(
height: sendViewModel.isElectrumWallet ? 490 : 465,
child: Observer(
builder: (_) {
return PageView.builder(
scrollDirection: Axis.horizontal,
controller: controller,
itemCount: sendViewModel.outputs.length,
itemBuilder: (context, index) {
final output = sendViewModel.outputs[index];
return SendCard(
key: output.key,
output: output,
sendViewModel: sendViewModel,
);
2022-01-12 18:58:54 +01:00
});
},
2022-01-12 18:58:54 +01:00
)),
Padding(
2022-01-12 18:58:54 +01:00
padding:
EdgeInsets.only(top: 10, left: 24, right: 24, bottom: 10),
child: Container(
height: 10,
child: Observer(
builder: (_) {
final count = sendViewModel.outputs.length;
2022-01-12 18:58:54 +01:00
return count > 1
? SmoothPageIndicator(
2022-01-12 18:58:54 +01:00
controller: controller,
count: count,
effect: ScrollingDotsEffect(
spacing: 6.0,
radius: 6.0,
dotWidth: 6.0,
dotHeight: 6.0,
dotColor: Theme.of(context)
.primaryTextTheme
.display2
.backgroundColor,
activeDotColor: Theme.of(context)
.primaryTextTheme
.display3
.backgroundColor),
)
: Offstage();
2022-01-12 18:58:54 +01:00
},
),
),
),
2022-03-30 17:57:04 +02:00
if (sendViewModel.hasMultiRecipient)
Container(
height: 40,
width: double.infinity,
padding: EdgeInsets.only(left: 24),
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
2022-01-12 18:58:54 +01:00
child: Observer(
builder: (_) {
final templates = sendViewModel.templates;
final itemCount = templates.length;
2022-01-12 18:58:54 +01:00
return Row(
children: <Widget>[
GestureDetector(
onTap: () => Navigator.of(context)
.pushNamed(Routes.sendTemplate),
child: Container(
padding: EdgeInsets.only(left: 1, right: 10),
child: DottedBorder(
borderType: BorderType.RRect,
dashPattern: [6, 4],
color: Theme.of(context)
.primaryTextTheme
.headline2
.decorationColor,
strokeWidth: 2,
radius: Radius.circular(20),
child: Container(
height: 34,
padding: EdgeInsets.only(left: 10, right: 10),
alignment: Alignment.center,
decoration: BoxDecoration(
borderRadius:
BorderRadius.all(Radius.circular(20)),
color: Colors.transparent,
),
child: templates.length >= 1
? Icon(
Icons.add,
color: Theme.of(context)
.primaryTextTheme
.display3
.color,
)
: Text(
S.of(context).new_template,
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w600,
color: Theme.of(context)
.primaryTextTheme
.display3
.color,
),
),
),
),
),
),
ListView.builder(
scrollDirection: Axis.horizontal,
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemCount: itemCount,
itemBuilder: (context, index) {
final template = templates[index];
return TemplateTile(
key: UniqueKey(),
to: template.name,
amount: template.isCurrencySelected ? template.amount : template.amountFiat,
from: template.isCurrencySelected ? template.cryptoCurrency : template.fiatCurrency,
onTap: () async {
final fiatFromTemplate = FiatCurrency.all.singleWhere((element) => element.title == template.fiatCurrency);
final output = _defineCurrentOutput();
2022-01-12 18:58:54 +01:00
output.address = template.address;
if(template.isCurrencySelected){
output.setCryptoAmount(template.amount);
}else{
settingsViewModel.setFiatCurrency(fiatFromTemplate);
output.setFiatAmount(template.amountFiat);
}
output.resetParsedAddress();
await output.fetchParsedAddress(context);
},
onRemove: () {
showPopUp<void>(
2022-01-12 18:58:54 +01:00
context: context,
builder: (dialogContext) {
return AlertWithTwoActions(
alertTitle: S.of(context).template,
alertContent: S
.of(context)
.confirm_delete_template,
rightButtonText: S.of(context).delete,
leftButtonText: S.of(context).cancel,
actionRightButton: () {
Navigator.of(dialogContext).pop();
sendViewModel.sendTemplateViewModel
.removeTemplate(
template: template);
},
actionLeftButton: () =>
Navigator.of(dialogContext)
2022-01-12 18:58:54 +01:00
.pop());
},
);
},
);
2022-01-12 18:58:54 +01:00
},
),
],
);
},
),
),
)
],
),
bottomSectionPadding:
2022-01-12 18:58:54 +01:00
EdgeInsets.only(left: 24, right: 24, bottom: 24),
bottomSection: Column(
children: [
2022-03-30 17:57:04 +02:00
if (sendViewModel.hasCurrecyChanger)
Observer(builder: (_) =>
Padding(
padding: EdgeInsets.only(bottom: 12),
child: PrimaryButton(
onPressed: () => presentCurrencyPicker(context),
text: 'Change your asset (${sendViewModel.selectedCryptoCurrency})',
color: Colors.transparent,
textColor: Theme.of(context)
.accentTextTheme
.display2
.decorationColor,
)
)
),
if (sendViewModel.hasMultiRecipient)
Padding(
padding: EdgeInsets.only(bottom: 12),
child: PrimaryButton(
onPressed: () {
sendViewModel.addOutput();
Future.delayed(const Duration(milliseconds: 250), () {
controller.jumpToPage(sendViewModel.outputs.length - 1);
});
},
text: S.of(context).add_receiver,
color: Colors.transparent,
textColor: Theme.of(context)
.accentTextTheme
.display2
.decorationColor,
isDottedBorder: true,
borderColor: Theme.of(context)
.primaryTextTheme
.display2
.decorationColor,
)),
2022-01-12 18:58:54 +01:00
Observer(
builder: (_) {
return LoadingPrimaryButton(
onPressed: () async {
if (!_formKey.currentState.validate()) {
if (sendViewModel.outputs.length > 1) {
showErrorValidationAlert(context);
}
2022-01-12 18:58:54 +01:00
return;
}
2022-01-12 18:58:54 +01:00
final notValidItems = sendViewModel.outputs
.where((item) =>
item.address.isEmpty || item.cryptoAmount.isEmpty)
.toList();
2022-01-12 18:58:54 +01:00
if (notValidItems?.isNotEmpty ?? false) {
showErrorValidationAlert(context);
return;
}
2022-03-30 17:57:04 +02:00
2022-01-12 18:58:54 +01:00
await sendViewModel.createTransaction();
2021-11-02 09:17:24 +00:00
2022-01-12 18:58:54 +01:00
},
text: S.of(context).send,
color: Theme.of(context).accentTextTheme.body2.color,
textColor: Colors.white,
isLoading: sendViewModel.state is IsExecutingState ||
sendViewModel.state is TransactionCommitting,
isDisabled: !sendViewModel.isReadyForSend,
);
},
)
],
)),
);
2020-09-21 22:01:45 +03:00
}
void _setEffects(BuildContext context) {
if (_effectsInstalled) {
return;
}
reaction((_) => sendViewModel.state, (ExecutionState state) {
if (state is FailureState) {
2020-09-21 22:01:45 +03:00
WidgetsBinding.instance.addPostFrameCallback((_) {
2020-09-25 18:32:44 +03:00
showPopUp<void>(
2020-09-21 22:01:45 +03:00
context: context,
builder: (BuildContext context) {
return AlertWithOneAction(
alertTitle: S.of(context).error,
alertContent: state.error,
buttonText: S.of(context).ok,
buttonAction: () => Navigator.of(context).pop());
});
});
}
if (state is ExecutedSuccessfullyState) {
2020-09-21 22:01:45 +03:00
WidgetsBinding.instance.addPostFrameCallback((_) {
2020-09-25 18:32:44 +03:00
showPopUp<void>(
2020-09-21 22:01:45 +03:00
context: context,
builder: (BuildContext context) {
return ConfirmSendingAlert(
alertTitle: S.of(context).confirm_sending,
amount: S.of(context).send_amount,
amountValue:
2022-01-12 18:58:54 +01:00
sendViewModel.pendingTransaction.amountFormatted,
fiatAmountValue:
sendViewModel.pendingTransactionFiatAmount +
' ' +
sendViewModel.fiat.title,
2020-09-21 22:01:45 +03:00
fee: S.of(context).send_fee,
feeValue: sendViewModel.pendingTransaction.feeFormatted,
2022-01-12 18:58:54 +01:00
feeFiatAmount:
sendViewModel.pendingTransactionFeeFiatAmount +
' ' +
sendViewModel.fiat.title,
outputs: sendViewModel.outputs,
rightButtonText: S.of(context).ok,
leftButtonText: S.of(context).cancel,
actionRightButton: () {
2020-09-21 22:01:45 +03:00
Navigator.of(context).pop();
sendViewModel.commitTransaction();
2020-09-25 18:32:44 +03:00
showPopUp<void>(
2020-09-21 22:01:45 +03:00
context: context,
builder: (BuildContext context) {
return Observer(builder: (_) {
final state = sendViewModel.state;
2020-11-06 20:54:00 +02:00
if (state is FailureState) {
Navigator.of(context).pop();
}
2020-09-21 22:01:45 +03:00
if (state is TransactionCommitted) {
return AlertWithOneAction(
alertTitle: '',
alertContent: S.of(context).send_success(
2022-03-30 17:57:04 +02:00
sendViewModel.selectedCryptoCurrency.toString()),
buttonText: S.of(context).ok,
buttonAction: () =>
Navigator.of(context).pop());
2020-11-06 20:54:00 +02:00
}
return Offstage();
2020-09-21 22:01:45 +03:00
});
});
},
actionLeftButton: () => Navigator.of(context).pop());
2020-09-21 22:01:45 +03:00
});
});
}
if (state is TransactionCommitted) {
WidgetsBinding.instance.addPostFrameCallback((_) {
sendViewModel.clearOutputs();
2020-09-21 22:01:45 +03:00
});
}
});
_effectsInstalled = true;
}
Output _defineCurrentOutput() {
final itemCount = controller.page.round();
return sendViewModel.outputs[itemCount];
2020-09-21 22:01:45 +03:00
}
void showErrorValidationAlert(BuildContext context) async {
await showPopUp<void>(
context: context,
builder: (BuildContext context) {
return AlertWithOneAction(
alertTitle: S.of(context).error,
alertContent: 'Please, check receiver forms',
buttonText: S.of(context).ok,
2022-01-12 18:58:54 +01:00
buttonAction: () => Navigator.of(context).pop());
});
}
2022-03-30 17:57:04 +02:00
void presentCurrencyPicker(BuildContext context) async {
await showPopUp<CryptoCurrency>(
builder: (_) => Picker(
items: sendViewModel.currencies,
displayItem: (Object item) => item.toString(),
selectedAtIndex: sendViewModel.currencies.indexOf(sendViewModel.selectedCryptoCurrency),
title: S.of(context).please_select,
mainAxisAlignment: MainAxisAlignment.center,
onItemSelected: (CryptoCurrency cur) => sendViewModel.selectedCryptoCurrency = cur,
),
context: context);
}
2020-01-04 21:31:52 +02:00
}