2020-01-04 19:31:52 +00:00
|
|
|
import 'dart:ui';
|
2021-07-07 14:31:16 +00:00
|
|
|
import 'package:cake_wallet/entities/parsed_address.dart';
|
2022-01-26 15:44:15 +00:00
|
|
|
import 'package:cake_wallet/utils/debounce.dart';
|
2021-12-24 12:37:24 +00:00
|
|
|
import 'package:cw_core/sync_status.dart';
|
|
|
|
import 'package:cw_core/wallet_type.dart';
|
2021-07-07 13:50:55 +00:00
|
|
|
import 'package:cake_wallet/entities/parse_address_from_domain.dart';
|
2021-09-29 11:38:31 +00:00
|
|
|
import 'package:cake_wallet/src/screens/send/widgets/extract_address_from_parsed.dart';
|
2021-02-16 19:06:23 +00:00
|
|
|
import 'package:cake_wallet/src/widgets/standard_checkbox.dart';
|
2020-10-24 12:55:24 +00:00
|
|
|
import 'package:dotted_border/dotted_border.dart';
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_mobx/flutter_mobx.dart';
|
|
|
|
import 'package:keyboard_actions/keyboard_actions.dart';
|
|
|
|
import 'package:mobx/mobx.dart';
|
2020-09-22 19:26:20 +00:00
|
|
|
import 'package:cake_wallet/exchange/exchange_template.dart';
|
2020-10-24 12:55:24 +00:00
|
|
|
import 'package:cake_wallet/exchange/exchange_trade_state.dart';
|
|
|
|
import 'package:cake_wallet/exchange/limits_state.dart';
|
2020-09-22 19:26:20 +00:00
|
|
|
import 'package:cake_wallet/src/screens/base_page.dart';
|
2020-09-22 18:32:43 +00:00
|
|
|
import 'package:cake_wallet/src/widgets/alert_with_two_actions.dart';
|
2020-09-29 17:56:11 +00:00
|
|
|
import 'package:cake_wallet/src/widgets/keyboard_done_button.dart';
|
2020-09-22 18:32:43 +00:00
|
|
|
import 'package:cake_wallet/src/widgets/template_tile.dart';
|
2020-09-22 19:26:20 +00:00
|
|
|
import 'package:cake_wallet/src/widgets/trail_button.dart';
|
2020-09-25 15:32:44 +00:00
|
|
|
import 'package:cake_wallet/utils/show_pop_up.dart';
|
2020-09-22 19:26:20 +00:00
|
|
|
import 'package:cake_wallet/routes.dart';
|
|
|
|
import 'package:cake_wallet/generated/i18n.dart';
|
2021-12-24 12:37:24 +00:00
|
|
|
import 'package:cw_core/crypto_currency.dart';
|
2020-09-22 19:26:20 +00:00
|
|
|
import 'package:cake_wallet/src/screens/exchange/widgets/exchange_card.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/alert_with_one_action.dart';
|
|
|
|
import 'package:cake_wallet/view_model/exchange/exchange_view_model.dart';
|
|
|
|
import 'package:cake_wallet/core/address_validator.dart';
|
|
|
|
import 'package:cake_wallet/core/amount_validator.dart';
|
|
|
|
import 'package:cake_wallet/src/screens/exchange/widgets/present_provider_picker.dart';
|
2020-01-04 19:31:52 +00:00
|
|
|
|
|
|
|
class ExchangePage extends BasePage {
|
2020-07-31 15:29:21 +00:00
|
|
|
ExchangePage(this.exchangeViewModel);
|
2020-01-04 19:31:52 +00:00
|
|
|
|
2020-07-31 15:29:21 +00:00
|
|
|
final ExchangeViewModel exchangeViewModel;
|
2020-09-22 18:32:43 +00:00
|
|
|
final depositKey = GlobalKey<ExchangeCardState>();
|
|
|
|
final receiveKey = GlobalKey<ExchangeCardState>();
|
2021-02-16 19:06:23 +00:00
|
|
|
final checkBoxKey = GlobalKey<StandardCheckboxState>();
|
2020-09-22 18:32:43 +00:00
|
|
|
final _formKey = GlobalKey<FormState>();
|
2020-09-29 17:56:11 +00:00
|
|
|
final _depositAmountFocus = FocusNode();
|
2021-04-20 17:49:53 +00:00
|
|
|
final _depositAddressFocus = FocusNode();
|
2020-09-29 17:56:11 +00:00
|
|
|
final _receiveAmountFocus = FocusNode();
|
2021-04-20 17:49:53 +00:00
|
|
|
final _receiveAddressFocus = FocusNode();
|
2022-01-26 15:44:15 +00:00
|
|
|
final _receiveAmountDebounce = Debounce(Duration(milliseconds: 500));
|
|
|
|
final _depositAmountDebounce = Debounce(Duration(milliseconds: 500));
|
2020-09-22 18:32:43 +00:00
|
|
|
var _isReactionsSet = false;
|
2020-05-29 15:10:11 +00:00
|
|
|
|
|
|
|
@override
|
2020-07-31 15:29:21 +00:00
|
|
|
String get title => S.current.exchange;
|
2020-01-04 19:31:52 +00:00
|
|
|
|
|
|
|
@override
|
2020-08-21 20:26:23 +00:00
|
|
|
Color get titleColor => Colors.white;
|
2020-01-04 19:31:52 +00:00
|
|
|
|
|
|
|
@override
|
2021-05-10 17:58:05 +00:00
|
|
|
bool get resizeToAvoidBottomInset => false;
|
2020-08-21 20:26:23 +00:00
|
|
|
|
|
|
|
@override
|
2020-09-22 18:32:43 +00:00
|
|
|
bool get extendBodyBehindAppBar => true;
|
|
|
|
|
|
|
|
@override
|
|
|
|
AppBarStyle get appBarStyle => AppBarStyle.transparent;
|
2020-01-04 19:31:52 +00:00
|
|
|
|
|
|
|
@override
|
2020-07-31 15:29:21 +00:00
|
|
|
Widget middle(BuildContext context) =>
|
|
|
|
PresentProviderPicker(exchangeViewModel: exchangeViewModel);
|
2020-01-04 19:31:52 +00:00
|
|
|
|
|
|
|
@override
|
2020-09-29 17:56:11 +00:00
|
|
|
Widget trailing(BuildContext context) => TrailButton(
|
2021-01-05 18:31:03 +00:00
|
|
|
caption: S.of(context).reset,
|
|
|
|
onPressed: () {
|
2020-10-30 12:42:23 +00:00
|
|
|
_formKey.currentState.reset();
|
|
|
|
exchangeViewModel.reset();
|
2021-01-05 18:31:03 +00:00
|
|
|
});
|
2020-01-04 19:31:52 +00:00
|
|
|
|
|
|
|
@override
|
2020-09-22 18:32:43 +00:00
|
|
|
Widget body(BuildContext context) {
|
|
|
|
final arrowBottomPurple = Image.asset(
|
|
|
|
'assets/images/arrow_bottom_purple_icon.png',
|
|
|
|
color: Colors.white,
|
|
|
|
height: 8,
|
|
|
|
);
|
|
|
|
final arrowBottomCakeGreen = Image.asset(
|
|
|
|
'assets/images/arrow_bottom_cake_green.png',
|
|
|
|
color: Colors.white,
|
|
|
|
height: 8,
|
|
|
|
);
|
2020-08-21 20:26:23 +00:00
|
|
|
|
2020-09-22 18:32:43 +00:00
|
|
|
final depositWalletName =
|
2020-09-29 17:56:11 +00:00
|
|
|
exchangeViewModel.depositCurrency == CryptoCurrency.xmr
|
|
|
|
? exchangeViewModel.wallet.name
|
|
|
|
: null;
|
2020-09-22 18:32:43 +00:00
|
|
|
final receiveWalletName =
|
2020-09-29 17:56:11 +00:00
|
|
|
exchangeViewModel.receiveCurrency == CryptoCurrency.xmr
|
|
|
|
? exchangeViewModel.wallet.name
|
|
|
|
: null;
|
2020-09-22 18:32:43 +00:00
|
|
|
|
|
|
|
WidgetsBinding.instance
|
|
|
|
.addPostFrameCallback((_) => _setReactions(context, exchangeViewModel));
|
|
|
|
|
2020-09-29 17:56:11 +00:00
|
|
|
return KeyboardActions(
|
2022-01-26 15:44:15 +00:00
|
|
|
disableScroll: true,
|
2020-09-29 17:56:11 +00:00
|
|
|
config: KeyboardActionsConfig(
|
|
|
|
keyboardActionsPlatform: KeyboardActionsPlatform.IOS,
|
2021-01-05 18:31:03 +00:00
|
|
|
keyboardBarColor:
|
|
|
|
Theme.of(context).accentTextTheme.body2.backgroundColor,
|
2020-09-29 17:56:11 +00:00
|
|
|
nextFocus: false,
|
|
|
|
actions: [
|
|
|
|
KeyboardActionsItem(
|
|
|
|
focusNode: _depositAmountFocus,
|
|
|
|
toolbarButtons: [(_) => KeyboardDoneButton()]),
|
|
|
|
KeyboardActionsItem(
|
|
|
|
focusNode: _receiveAmountFocus,
|
|
|
|
toolbarButtons: [(_) => KeyboardDoneButton()])
|
|
|
|
]),
|
|
|
|
child: Container(
|
|
|
|
color: Theme.of(context).backgroundColor,
|
|
|
|
child: Form(
|
|
|
|
key: _formKey,
|
|
|
|
child: ScrollableWithBottomSection(
|
|
|
|
contentPadding: EdgeInsets.only(bottom: 24),
|
2021-02-23 17:36:03 +00:00
|
|
|
content: Observer(builder: (_) => Column(
|
2020-09-29 17:56:11 +00:00
|
|
|
children: <Widget>[
|
|
|
|
Container(
|
|
|
|
padding: EdgeInsets.only(bottom: 32),
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
borderRadius: BorderRadius.only(
|
|
|
|
bottomLeft: Radius.circular(24),
|
|
|
|
bottomRight: Radius.circular(24)),
|
|
|
|
gradient: LinearGradient(
|
|
|
|
colors: [
|
|
|
|
Theme.of(context).primaryTextTheme.body1.color,
|
|
|
|
Theme.of(context)
|
|
|
|
.primaryTextTheme
|
|
|
|
.body1
|
|
|
|
.decorationColor,
|
|
|
|
],
|
|
|
|
stops: [
|
|
|
|
0.35,
|
|
|
|
1.0
|
|
|
|
],
|
|
|
|
begin: Alignment.topLeft,
|
|
|
|
end: Alignment.bottomRight),
|
2020-09-22 18:32:43 +00:00
|
|
|
),
|
2020-09-29 17:56:11 +00:00
|
|
|
child: Column(
|
|
|
|
children: <Widget>[
|
|
|
|
Container(
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
borderRadius: BorderRadius.only(
|
|
|
|
bottomLeft: Radius.circular(24),
|
|
|
|
bottomRight: Radius.circular(24)),
|
|
|
|
gradient: LinearGradient(
|
|
|
|
colors: [
|
|
|
|
Theme.of(context)
|
|
|
|
.primaryTextTheme
|
|
|
|
.subtitle
|
|
|
|
.color,
|
|
|
|
Theme.of(context)
|
|
|
|
.primaryTextTheme
|
|
|
|
.subtitle
|
|
|
|
.decorationColor,
|
|
|
|
],
|
|
|
|
begin: Alignment.topLeft,
|
|
|
|
end: Alignment.bottomRight),
|
|
|
|
),
|
|
|
|
padding: EdgeInsets.fromLTRB(24, 100, 24, 32),
|
|
|
|
child: Observer(
|
|
|
|
builder: (_) => ExchangeCard(
|
2021-01-05 18:31:03 +00:00
|
|
|
hasAllAmount: exchangeViewModel.hasAllAmount,
|
|
|
|
allAmount: exchangeViewModel.hasAllAmount
|
|
|
|
? () => exchangeViewModel
|
2021-02-23 17:36:03 +00:00
|
|
|
.calculateDepositAllAmount()
|
2021-01-05 18:31:03 +00:00
|
|
|
: null,
|
2020-09-29 17:56:11 +00:00
|
|
|
amountFocusNode: _depositAmountFocus,
|
2021-04-20 17:49:53 +00:00
|
|
|
addressFocusNode: _depositAddressFocus,
|
2020-09-29 17:56:11 +00:00
|
|
|
key: depositKey,
|
|
|
|
title: S.of(context).you_will_send,
|
|
|
|
initialCurrency:
|
2021-02-23 17:36:03 +00:00
|
|
|
exchangeViewModel.depositCurrency,
|
2020-09-29 17:56:11 +00:00
|
|
|
initialWalletName: depositWalletName,
|
|
|
|
initialAddress:
|
2021-02-23 17:36:03 +00:00
|
|
|
exchangeViewModel.depositCurrency ==
|
|
|
|
exchangeViewModel.wallet.currency
|
2021-07-13 05:46:34 +00:00
|
|
|
? exchangeViewModel.wallet.walletAddresses.address
|
2021-02-23 17:36:03 +00:00
|
|
|
: exchangeViewModel.depositAddress,
|
2020-09-29 17:56:11 +00:00
|
|
|
initialIsAmountEditable: true,
|
|
|
|
initialIsAddressEditable:
|
2021-02-23 17:36:03 +00:00
|
|
|
exchangeViewModel.isDepositAddressEnabled,
|
2020-09-29 17:56:11 +00:00
|
|
|
isAmountEstimated: false,
|
2020-10-02 15:49:45 +00:00
|
|
|
hasRefundAddress: true,
|
2021-07-23 11:05:33 +00:00
|
|
|
isMoneroWallet: exchangeViewModel.isMoneroWallet,
|
2020-09-29 17:56:11 +00:00
|
|
|
currencies: CryptoCurrency.all,
|
2021-01-11 17:15:27 +00:00
|
|
|
onCurrencySelected: (currency) {
|
|
|
|
// FIXME: need to move it into view model
|
|
|
|
if (currency == CryptoCurrency.xmr &&
|
|
|
|
exchangeViewModel.wallet.type ==
|
|
|
|
WalletType.bitcoin) {
|
|
|
|
showPopUp<void>(
|
|
|
|
context: context,
|
|
|
|
builder: (dialogContext) {
|
|
|
|
return AlertWithOneAction(
|
|
|
|
alertTitle: S.of(context).error,
|
|
|
|
alertContent: S
|
|
|
|
.of(context)
|
|
|
|
.exchange_incorrect_current_wallet_for_xmr,
|
|
|
|
buttonText: S.of(context).ok,
|
|
|
|
buttonAction: () =>
|
|
|
|
Navigator.of(dialogContext)
|
|
|
|
.pop());
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
exchangeViewModel.changeDepositCurrency(
|
|
|
|
currency: currency);
|
|
|
|
},
|
2020-09-29 17:56:11 +00:00
|
|
|
imageArrow: arrowBottomPurple,
|
|
|
|
currencyButtonColor: Colors.transparent,
|
|
|
|
addressButtonsColor:
|
2021-02-23 17:36:03 +00:00
|
|
|
Theme.of(context).focusColor,
|
2020-09-29 17:56:11 +00:00
|
|
|
borderColor: Theme.of(context)
|
2020-09-22 18:32:43 +00:00
|
|
|
.primaryTextTheme
|
2020-09-29 17:56:11 +00:00
|
|
|
.body2
|
2020-09-22 18:32:43 +00:00
|
|
|
.color,
|
2020-09-29 17:56:11 +00:00
|
|
|
currencyValueValidator: AmountValidator(
|
|
|
|
type: exchangeViewModel.wallet.type),
|
|
|
|
addressTextFieldValidator: AddressValidator(
|
|
|
|
type: exchangeViewModel.depositCurrency),
|
2021-04-20 17:49:53 +00:00
|
|
|
onPushPasteButton: (context) async {
|
|
|
|
final domain =
|
|
|
|
exchangeViewModel.depositAddress;
|
2021-06-22 08:29:30 +00:00
|
|
|
final ticker = exchangeViewModel
|
|
|
|
.depositCurrency.title.toLowerCase();
|
2021-04-20 17:49:53 +00:00
|
|
|
exchangeViewModel.depositAddress =
|
2021-09-20 14:56:27 +00:00
|
|
|
await fetchParsedAddress(
|
2021-04-20 17:49:53 +00:00
|
|
|
context, domain, ticker);
|
|
|
|
},
|
2021-10-01 15:13:10 +00:00
|
|
|
onPushAddressBookButton: (context) async {
|
|
|
|
final domain =
|
|
|
|
exchangeViewModel.depositAddress;
|
|
|
|
final ticker = exchangeViewModel
|
|
|
|
.depositCurrency.title.toLowerCase();
|
|
|
|
exchangeViewModel.depositAddress =
|
|
|
|
await fetchParsedAddress(
|
|
|
|
context, domain, ticker);
|
|
|
|
},
|
2020-09-29 17:56:11 +00:00
|
|
|
),
|
|
|
|
),
|
2020-09-22 18:32:43 +00:00
|
|
|
),
|
2020-09-29 17:56:11 +00:00
|
|
|
Padding(
|
|
|
|
padding:
|
2021-02-23 17:36:03 +00:00
|
|
|
EdgeInsets.only(top: 29, left: 24, right: 24),
|
2020-09-29 17:56:11 +00:00
|
|
|
child: Observer(
|
|
|
|
builder: (_) => ExchangeCard(
|
2021-02-23 17:36:03 +00:00
|
|
|
amountFocusNode: _receiveAmountFocus,
|
2021-04-20 17:49:53 +00:00
|
|
|
addressFocusNode: _receiveAddressFocus,
|
2021-02-23 17:36:03 +00:00
|
|
|
key: receiveKey,
|
|
|
|
title: S.of(context).you_will_get,
|
|
|
|
initialCurrency:
|
|
|
|
exchangeViewModel.receiveCurrency,
|
|
|
|
initialWalletName: receiveWalletName,
|
|
|
|
initialAddress: exchangeViewModel
|
|
|
|
.receiveCurrency ==
|
|
|
|
exchangeViewModel.wallet.currency
|
2021-07-13 05:46:34 +00:00
|
|
|
? exchangeViewModel.wallet.walletAddresses.address
|
2021-02-23 17:36:03 +00:00
|
|
|
: exchangeViewModel.receiveAddress,
|
|
|
|
initialIsAmountEditable: exchangeViewModel
|
|
|
|
.isReceiveAmountEditable,
|
|
|
|
initialIsAddressEditable:
|
|
|
|
exchangeViewModel
|
|
|
|
.isReceiveAddressEnabled,
|
|
|
|
isAmountEstimated: true,
|
2021-07-23 11:05:33 +00:00
|
|
|
isMoneroWallet: exchangeViewModel.isMoneroWallet,
|
2021-04-06 17:11:26 +00:00
|
|
|
currencies:
|
|
|
|
exchangeViewModel.receiveCurrencies,
|
2021-02-23 17:36:03 +00:00
|
|
|
onCurrencySelected: (currency) =>
|
|
|
|
exchangeViewModel
|
|
|
|
.changeReceiveCurrency(
|
|
|
|
currency: currency),
|
|
|
|
imageArrow: arrowBottomCakeGreen,
|
|
|
|
currencyButtonColor: Colors.transparent,
|
|
|
|
addressButtonsColor:
|
|
|
|
Theme.of(context).focusColor,
|
|
|
|
borderColor: Theme.of(context)
|
|
|
|
.primaryTextTheme
|
|
|
|
.body2
|
|
|
|
.decorationColor,
|
|
|
|
currencyValueValidator: AmountValidator(
|
|
|
|
type: exchangeViewModel.wallet.type),
|
|
|
|
addressTextFieldValidator:
|
|
|
|
AddressValidator(
|
|
|
|
type: exchangeViewModel
|
|
|
|
.receiveCurrency),
|
2021-04-20 17:49:53 +00:00
|
|
|
onPushPasteButton: (context) async {
|
|
|
|
final domain =
|
|
|
|
exchangeViewModel.receiveAddress;
|
2021-06-22 08:29:30 +00:00
|
|
|
final ticker = exchangeViewModel
|
|
|
|
.receiveCurrency.title.toLowerCase();
|
2021-04-20 17:49:53 +00:00
|
|
|
exchangeViewModel.receiveAddress =
|
2021-09-20 14:56:27 +00:00
|
|
|
await fetchParsedAddress(
|
2021-04-20 17:49:53 +00:00
|
|
|
context, domain, ticker);
|
|
|
|
},
|
2021-10-01 15:13:10 +00:00
|
|
|
onPushAddressBookButton: (context) async {
|
|
|
|
final domain =
|
|
|
|
exchangeViewModel.receiveAddress;
|
|
|
|
final ticker = exchangeViewModel
|
|
|
|
.receiveCurrency.title.toLowerCase();
|
|
|
|
exchangeViewModel.receiveAddress =
|
|
|
|
await fetchParsedAddress(
|
|
|
|
context, domain, ticker);
|
|
|
|
},
|
2021-02-23 17:36:03 +00:00
|
|
|
)),
|
2020-09-29 17:56:11 +00:00
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Padding(
|
|
|
|
padding: EdgeInsets.only(top: 30, left: 24, bottom: 24),
|
|
|
|
child: Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.start,
|
|
|
|
children: <Widget>[
|
|
|
|
Text(
|
|
|
|
S.of(context).send_templates,
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 18,
|
|
|
|
fontWeight: FontWeight.w600,
|
|
|
|
color: Theme.of(context)
|
|
|
|
.primaryTextTheme
|
|
|
|
.display4
|
|
|
|
.color),
|
|
|
|
)
|
|
|
|
],
|
2020-09-22 18:32:43 +00:00
|
|
|
),
|
2020-09-29 17:56:11 +00:00
|
|
|
),
|
|
|
|
Container(
|
|
|
|
height: 40,
|
|
|
|
width: double.infinity,
|
|
|
|
padding: EdgeInsets.only(left: 24),
|
|
|
|
child: SingleChildScrollView(
|
|
|
|
scrollDirection: Axis.horizontal,
|
|
|
|
child: Row(
|
|
|
|
children: <Widget>[
|
|
|
|
GestureDetector(
|
|
|
|
onTap: () => Navigator.of(context)
|
|
|
|
.pushNamed(Routes.exchangeTemplate),
|
|
|
|
child: Container(
|
|
|
|
padding:
|
2021-02-23 17:36:03 +00:00
|
|
|
EdgeInsets.only(left: 1, right: 10),
|
2020-09-29 17:56:11 +00:00
|
|
|
child: DottedBorder(
|
|
|
|
borderType: BorderType.RRect,
|
|
|
|
dashPattern: [6, 4],
|
|
|
|
color: Theme.of(context)
|
|
|
|
.primaryTextTheme
|
|
|
|
.display2
|
|
|
|
.decorationColor,
|
|
|
|
strokeWidth: 2,
|
|
|
|
radius: Radius.circular(20),
|
|
|
|
child: Container(
|
|
|
|
height: 34,
|
|
|
|
width: 75,
|
|
|
|
padding: EdgeInsets.only(
|
|
|
|
left: 10, right: 10),
|
|
|
|
alignment: Alignment.center,
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
borderRadius: BorderRadius.all(
|
|
|
|
Radius.circular(20)),
|
|
|
|
color: Colors.transparent,
|
|
|
|
),
|
|
|
|
child: Text(
|
|
|
|
S.of(context).send_new,
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 14,
|
|
|
|
fontWeight: FontWeight.w600,
|
|
|
|
color: Theme.of(context)
|
|
|
|
.primaryTextTheme
|
|
|
|
.display3
|
|
|
|
.color),
|
|
|
|
),
|
|
|
|
)),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Observer(builder: (_) {
|
|
|
|
final templates = exchangeViewModel.templates;
|
2020-10-07 05:58:22 +00:00
|
|
|
final itemCount = templates.length;
|
2020-09-29 17:56:11 +00:00
|
|
|
|
|
|
|
return ListView.builder(
|
|
|
|
scrollDirection: Axis.horizontal,
|
|
|
|
shrinkWrap: true,
|
|
|
|
physics: NeverScrollableScrollPhysics(),
|
|
|
|
itemCount: itemCount,
|
|
|
|
itemBuilder: (context, index) {
|
|
|
|
final template = templates[index];
|
|
|
|
|
|
|
|
return TemplateTile(
|
|
|
|
key: UniqueKey(),
|
|
|
|
amount: template.amount,
|
|
|
|
from: template.depositCurrency,
|
|
|
|
to: template.receiveCurrency,
|
|
|
|
onTap: () {
|
2021-04-20 17:49:53 +00:00
|
|
|
applyTemplate(context,
|
2020-09-29 17:56:11 +00:00
|
|
|
exchangeViewModel, template);
|
|
|
|
},
|
|
|
|
onRemove: () {
|
|
|
|
showPopUp<void>(
|
|
|
|
context: context,
|
|
|
|
builder: (dialogContext) {
|
|
|
|
return AlertWithTwoActions(
|
|
|
|
alertTitle: S
|
|
|
|
.of(context)
|
|
|
|
.template,
|
|
|
|
alertContent: S
|
|
|
|
.of(context)
|
|
|
|
.confirm_delete_template,
|
|
|
|
rightButtonText:
|
2021-02-23 17:36:03 +00:00
|
|
|
S.of(context).delete,
|
2020-10-07 05:58:22 +00:00
|
|
|
leftButtonText:
|
2021-02-23 17:36:03 +00:00
|
|
|
S.of(context).cancel,
|
2020-10-07 05:58:22 +00:00
|
|
|
actionRightButton: () {
|
2020-09-29 17:56:11 +00:00
|
|
|
Navigator.of(
|
2021-02-23 17:36:03 +00:00
|
|
|
dialogContext)
|
2020-09-29 17:56:11 +00:00
|
|
|
.pop();
|
|
|
|
exchangeViewModel
|
2020-10-07 05:58:22 +00:00
|
|
|
.removeTemplate(
|
2021-02-23 17:36:03 +00:00
|
|
|
template:
|
|
|
|
template);
|
2020-09-29 17:56:11 +00:00
|
|
|
exchangeViewModel
|
2020-10-07 05:58:22 +00:00
|
|
|
.updateTemplate();
|
2020-09-29 17:56:11 +00:00
|
|
|
},
|
2020-10-07 05:58:22 +00:00
|
|
|
actionLeftButton: () =>
|
2020-09-29 17:56:11 +00:00
|
|
|
Navigator.of(
|
2021-02-23 17:36:03 +00:00
|
|
|
dialogContext)
|
2020-09-29 17:56:11 +00:00
|
|
|
.pop());
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
)))
|
|
|
|
],
|
2021-02-23 17:36:03 +00:00
|
|
|
)),
|
2020-09-29 17:56:11 +00:00
|
|
|
bottomSectionPadding:
|
|
|
|
EdgeInsets.only(left: 24, right: 24, bottom: 24),
|
|
|
|
bottomSection: Column(children: <Widget>[
|
|
|
|
Padding(
|
|
|
|
padding: EdgeInsets.only(bottom: 15),
|
|
|
|
child: Observer(builder: (_) {
|
2021-02-24 08:16:03 +00:00
|
|
|
final description = exchangeViewModel.isFixedRateMode
|
2020-10-30 17:28:20 +00:00
|
|
|
? S.of(context).amount_is_guaranteed
|
2020-09-29 17:56:11 +00:00
|
|
|
: S.of(context).amount_is_estimate;
|
|
|
|
return Center(
|
|
|
|
child: Text(
|
|
|
|
description,
|
2020-10-30 17:28:20 +00:00
|
|
|
textAlign: TextAlign.center,
|
2020-09-29 17:56:11 +00:00
|
|
|
style: TextStyle(
|
|
|
|
color: Theme.of(context)
|
2020-09-22 18:32:43 +00:00
|
|
|
.primaryTextTheme
|
2020-09-29 17:56:11 +00:00
|
|
|
.display4
|
2020-09-22 18:32:43 +00:00
|
|
|
.decorationColor,
|
2020-09-29 17:56:11 +00:00
|
|
|
fontWeight: FontWeight.w500,
|
|
|
|
fontSize: 12),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}),
|
2020-09-22 18:32:43 +00:00
|
|
|
),
|
2020-09-29 17:56:11 +00:00
|
|
|
Observer(
|
2021-01-05 18:31:03 +00:00
|
|
|
builder: (_) => LoadingPrimaryButton(
|
|
|
|
text: S.of(context).exchange,
|
|
|
|
onPressed: () {
|
|
|
|
if (_formKey.currentState.validate()) {
|
|
|
|
if ((exchangeViewModel.depositCurrency ==
|
|
|
|
CryptoCurrency.xmr) &&
|
|
|
|
(!(exchangeViewModel.status
|
|
|
|
is SyncedSyncStatus))) {
|
|
|
|
showPopUp<void>(
|
|
|
|
context: context,
|
|
|
|
builder: (BuildContext context) {
|
|
|
|
return AlertWithOneAction(
|
|
|
|
alertTitle: S.of(context).exchange,
|
|
|
|
alertContent: S
|
|
|
|
.of(context)
|
|
|
|
.exchange_sync_alert_content,
|
|
|
|
buttonText: S.of(context).ok,
|
|
|
|
buttonAction: () =>
|
|
|
|
Navigator.of(context).pop());
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
exchangeViewModel.createTrade();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
color: Theme.of(context).accentTextTheme.body2.color,
|
|
|
|
textColor: Colors.white,
|
|
|
|
isLoading:
|
|
|
|
exchangeViewModel.tradeState is TradeIsCreating)),
|
2020-09-29 17:56:11 +00:00
|
|
|
]),
|
|
|
|
)),
|
|
|
|
));
|
2020-08-21 20:26:23 +00:00
|
|
|
}
|
2020-09-22 18:32:43 +00:00
|
|
|
|
2021-04-20 17:49:53 +00:00
|
|
|
void applyTemplate(BuildContext context,
|
|
|
|
ExchangeViewModel exchangeViewModel, ExchangeTemplate template) async {
|
2020-09-22 18:32:43 +00:00
|
|
|
exchangeViewModel.changeDepositCurrency(
|
|
|
|
currency: CryptoCurrency.fromString(template.depositCurrency));
|
|
|
|
exchangeViewModel.changeReceiveCurrency(
|
|
|
|
currency: CryptoCurrency.fromString(template.receiveCurrency));
|
|
|
|
|
|
|
|
switch (template.provider) {
|
|
|
|
case 'ChangeNOW':
|
|
|
|
exchangeViewModel.changeProvider(
|
2021-02-19 08:37:30 +00:00
|
|
|
provider: exchangeViewModel.providerList[0]);
|
2020-09-22 18:32:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
exchangeViewModel.changeDepositAmount(amount: template.amount);
|
|
|
|
exchangeViewModel.depositAddress = template.depositAddress;
|
|
|
|
exchangeViewModel.receiveAddress = template.receiveAddress;
|
2020-10-12 16:47:01 +00:00
|
|
|
exchangeViewModel.isReceiveAmountEntered = false;
|
2021-02-19 08:37:30 +00:00
|
|
|
exchangeViewModel.isFixedRateMode = false;
|
2021-04-20 17:49:53 +00:00
|
|
|
|
|
|
|
var domain = template.depositAddress;
|
2021-06-22 08:29:30 +00:00
|
|
|
var ticker = template.depositCurrency.toLowerCase();
|
2021-04-20 17:49:53 +00:00
|
|
|
exchangeViewModel.depositAddress =
|
2021-09-20 14:56:27 +00:00
|
|
|
await fetchParsedAddress(context, domain, ticker);
|
2021-04-20 17:49:53 +00:00
|
|
|
|
|
|
|
domain = template.receiveAddress;
|
2021-06-22 08:29:30 +00:00
|
|
|
ticker = template.receiveCurrency.toLowerCase();
|
2021-04-20 17:49:53 +00:00
|
|
|
exchangeViewModel.receiveAddress =
|
2021-09-20 14:56:27 +00:00
|
|
|
await fetchParsedAddress(context, domain, ticker);
|
2020-09-22 18:32:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void _setReactions(
|
|
|
|
BuildContext context, ExchangeViewModel exchangeViewModel) {
|
|
|
|
if (_isReactionsSet) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
final depositAddressController = depositKey.currentState.addressController;
|
|
|
|
final depositAmountController = depositKey.currentState.amountController;
|
|
|
|
final receiveAddressController = receiveKey.currentState.addressController;
|
|
|
|
final receiveAmountController = receiveKey.currentState.amountController;
|
|
|
|
final limitsState = exchangeViewModel.limitsState;
|
|
|
|
|
2020-10-24 12:55:24 +00:00
|
|
|
if (limitsState is LimitsLoadedSuccessfully) {
|
|
|
|
final min = limitsState.limits.min != null
|
|
|
|
? limitsState.limits.min.toString()
|
|
|
|
: null;
|
|
|
|
final max = limitsState.limits.max != null
|
|
|
|
? limitsState.limits.max.toString()
|
|
|
|
: null;
|
2022-01-26 15:44:15 +00:00
|
|
|
final key = exchangeViewModel.isFixedRateMode
|
|
|
|
? receiveKey
|
|
|
|
: depositKey;
|
2020-10-24 12:55:24 +00:00
|
|
|
key.currentState.changeLimits(min: min, max: max);
|
|
|
|
}
|
2020-09-22 18:32:43 +00:00
|
|
|
|
|
|
|
_onCurrencyChange(
|
|
|
|
exchangeViewModel.receiveCurrency, exchangeViewModel, receiveKey);
|
|
|
|
_onCurrencyChange(
|
|
|
|
exchangeViewModel.depositCurrency, exchangeViewModel, depositKey);
|
|
|
|
|
|
|
|
reaction(
|
2020-09-29 17:56:11 +00:00
|
|
|
(_) => exchangeViewModel.wallet.name,
|
|
|
|
(String _) => _onWalletNameChange(
|
2020-09-22 18:32:43 +00:00
|
|
|
exchangeViewModel, exchangeViewModel.receiveCurrency, receiveKey));
|
|
|
|
|
|
|
|
reaction(
|
2020-09-29 17:56:11 +00:00
|
|
|
(_) => exchangeViewModel.wallet.name,
|
|
|
|
(String _) => _onWalletNameChange(
|
2020-09-22 18:32:43 +00:00
|
|
|
exchangeViewModel, exchangeViewModel.depositCurrency, depositKey));
|
|
|
|
|
|
|
|
reaction(
|
2020-09-29 17:56:11 +00:00
|
|
|
(_) => exchangeViewModel.receiveCurrency,
|
|
|
|
(CryptoCurrency currency) =>
|
2020-09-22 18:32:43 +00:00
|
|
|
_onCurrencyChange(currency, exchangeViewModel, receiveKey));
|
|
|
|
|
|
|
|
reaction(
|
2020-09-29 17:56:11 +00:00
|
|
|
(_) => exchangeViewModel.depositCurrency,
|
|
|
|
(CryptoCurrency currency) =>
|
2020-09-22 18:32:43 +00:00
|
|
|
_onCurrencyChange(currency, exchangeViewModel, depositKey));
|
|
|
|
|
|
|
|
reaction((_) => exchangeViewModel.depositAmount, (String amount) {
|
|
|
|
if (depositKey.currentState.amountController.text != amount) {
|
|
|
|
depositKey.currentState.amountController.text = amount;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
reaction((_) => exchangeViewModel.depositAddress, (String address) {
|
|
|
|
if (depositKey.currentState.addressController.text != address) {
|
|
|
|
depositKey.currentState.addressController.text = address;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
reaction((_) => exchangeViewModel.isDepositAddressEnabled,
|
2020-09-29 17:56:11 +00:00
|
|
|
(bool isEnabled) {
|
|
|
|
depositKey.currentState.isAddressEditable(isEditable: isEnabled);
|
|
|
|
});
|
2020-09-22 18:32:43 +00:00
|
|
|
|
|
|
|
reaction((_) => exchangeViewModel.receiveAmount, (String amount) {
|
|
|
|
if (receiveKey.currentState.amountController.text != amount) {
|
|
|
|
receiveKey.currentState.amountController.text = amount;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
reaction((_) => exchangeViewModel.receiveAddress, (String address) {
|
|
|
|
if (receiveKey.currentState.addressController.text != address) {
|
|
|
|
receiveKey.currentState.addressController.text = address;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
reaction((_) => exchangeViewModel.isReceiveAddressEnabled,
|
2020-09-29 17:56:11 +00:00
|
|
|
(bool isEnabled) {
|
|
|
|
receiveKey.currentState.isAddressEditable(isEditable: isEnabled);
|
|
|
|
});
|
2020-09-22 18:32:43 +00:00
|
|
|
|
2021-02-23 17:36:03 +00:00
|
|
|
reaction((_) => exchangeViewModel.isReceiveAmountEditable,
|
|
|
|
(bool isReceiveAmountEditable) {
|
|
|
|
receiveKey.currentState
|
|
|
|
.isAmountEditable(isEditable: isReceiveAmountEditable);
|
2020-09-25 19:55:41 +00:00
|
|
|
});
|
|
|
|
|
2020-10-24 12:55:24 +00:00
|
|
|
reaction((_) => exchangeViewModel.tradeState, (ExchangeTradeState state) {
|
|
|
|
if (state is TradeIsCreatedFailure) {
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
|
|
showPopUp<void>(
|
|
|
|
context: context,
|
|
|
|
builder: (BuildContext context) {
|
|
|
|
return AlertWithOneAction(
|
2020-12-03 13:47:13 +00:00
|
|
|
alertTitle: S.of(context).provider_error(state.title),
|
2020-10-24 12:55:24 +00:00
|
|
|
alertContent: state.error,
|
|
|
|
buttonText: S.of(context).ok,
|
|
|
|
buttonAction: () => Navigator.of(context).pop());
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (state is TradeIsCreatedSuccessfully) {
|
2020-11-19 16:48:09 +00:00
|
|
|
exchangeViewModel.reset();
|
2020-10-24 12:55:24 +00:00
|
|
|
Navigator.of(context).pushNamed(Routes.exchangeConfirm);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
reaction((_) => exchangeViewModel.limitsState, (LimitsState state) {
|
|
|
|
String min;
|
|
|
|
String max;
|
|
|
|
|
|
|
|
if (state is LimitsLoadedSuccessfully) {
|
|
|
|
min = state.limits.min != null ? state.limits.min.toString() : null;
|
|
|
|
max = state.limits.max != null ? state.limits.max.toString() : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state is LimitsLoadedFailure) {
|
|
|
|
min = '0';
|
|
|
|
max = '0';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state is LimitsIsLoading) {
|
|
|
|
min = '...';
|
|
|
|
max = '...';
|
|
|
|
}
|
|
|
|
|
2022-01-26 15:44:15 +00:00
|
|
|
if (exchangeViewModel.isFixedRateMode) {
|
|
|
|
depositKey.currentState.changeLimits(min: null, max: null);
|
|
|
|
receiveKey.currentState.changeLimits(min: min, max: max);
|
|
|
|
} else {
|
|
|
|
depositKey.currentState.changeLimits(min: min, max: max);
|
|
|
|
receiveKey.currentState.changeLimits(min: null, max: null);
|
|
|
|
}
|
2020-10-24 12:55:24 +00:00
|
|
|
});
|
2020-09-22 18:32:43 +00:00
|
|
|
|
|
|
|
depositAddressController.addListener(
|
2020-09-29 17:56:11 +00:00
|
|
|
() => exchangeViewModel.depositAddress = depositAddressController.text);
|
2020-09-22 18:32:43 +00:00
|
|
|
|
|
|
|
depositAmountController.addListener(() {
|
|
|
|
if (depositAmountController.text != exchangeViewModel.depositAmount) {
|
2022-01-26 15:44:15 +00:00
|
|
|
_depositAmountDebounce.run(() {
|
|
|
|
exchangeViewModel.changeDepositAmount(
|
|
|
|
amount: depositAmountController.text);
|
|
|
|
exchangeViewModel.isReceiveAmountEntered = false;
|
|
|
|
});
|
2020-09-22 18:32:43 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
receiveAddressController.addListener(
|
2020-09-29 17:56:11 +00:00
|
|
|
() => exchangeViewModel.receiveAddress = receiveAddressController.text);
|
2020-09-22 18:32:43 +00:00
|
|
|
|
|
|
|
receiveAmountController.addListener(() {
|
|
|
|
if (receiveAmountController.text != exchangeViewModel.receiveAmount) {
|
2022-01-26 15:44:15 +00:00
|
|
|
_receiveAmountDebounce.run(() {
|
|
|
|
exchangeViewModel.changeReceiveAmount(
|
|
|
|
amount: receiveAmountController.text);
|
|
|
|
exchangeViewModel.isReceiveAmountEntered = true;
|
|
|
|
});
|
2020-09-22 18:32:43 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-07-13 05:46:34 +00:00
|
|
|
reaction((_) => exchangeViewModel.wallet.walletAddresses.address,
|
|
|
|
(String address) {
|
2020-09-22 18:32:43 +00:00
|
|
|
if (exchangeViewModel.depositCurrency == CryptoCurrency.xmr) {
|
|
|
|
depositKey.currentState.changeAddress(address: address);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (exchangeViewModel.receiveCurrency == CryptoCurrency.xmr) {
|
|
|
|
receiveKey.currentState.changeAddress(address: address);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-04-20 17:49:53 +00:00
|
|
|
_depositAddressFocus.addListener(() async {
|
|
|
|
if (!_depositAddressFocus.hasFocus &&
|
|
|
|
depositAddressController.text.isNotEmpty) {
|
|
|
|
final domain = depositAddressController.text;
|
2021-06-22 08:29:30 +00:00
|
|
|
final ticker = exchangeViewModel.depositCurrency.title.toLowerCase();
|
2021-04-20 17:49:53 +00:00
|
|
|
exchangeViewModel.depositAddress =
|
2021-09-20 14:56:27 +00:00
|
|
|
await fetchParsedAddress(context, domain, ticker);
|
2021-04-20 17:49:53 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
_receiveAddressFocus.addListener(() async {
|
|
|
|
if (!_receiveAddressFocus.hasFocus &&
|
|
|
|
receiveAddressController.text.isNotEmpty) {
|
|
|
|
final domain = receiveAddressController.text;
|
2021-06-22 08:29:30 +00:00
|
|
|
final ticker = exchangeViewModel.receiveCurrency.title.toLowerCase();
|
2021-04-20 17:49:53 +00:00
|
|
|
exchangeViewModel.receiveAddress =
|
2021-09-20 14:56:27 +00:00
|
|
|
await fetchParsedAddress(context, domain, ticker);
|
2021-04-20 17:49:53 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-02-16 19:06:23 +00:00
|
|
|
_receiveAmountFocus.addListener(() {
|
2022-01-27 09:20:51 +00:00
|
|
|
exchangeViewModel.isFixedRateMode = true;
|
|
|
|
exchangeViewModel.changeReceiveAmount(
|
|
|
|
amount: receiveAmountController.text);
|
2021-02-16 19:06:23 +00:00
|
|
|
});
|
|
|
|
|
2022-01-27 09:20:51 +00:00
|
|
|
_depositAmountFocus.addListener(() {
|
|
|
|
exchangeViewModel.isFixedRateMode = false;
|
|
|
|
exchangeViewModel.changeDepositAmount(
|
|
|
|
amount: depositAmountController.text);
|
2021-02-16 19:06:23 +00:00
|
|
|
});
|
|
|
|
|
2020-09-22 18:32:43 +00:00
|
|
|
_isReactionsSet = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void _onCurrencyChange(CryptoCurrency currency,
|
|
|
|
ExchangeViewModel exchangeViewModel, GlobalKey<ExchangeCardState> key) {
|
|
|
|
final isCurrentTypeWallet = currency == exchangeViewModel.wallet.currency;
|
|
|
|
|
|
|
|
key.currentState.changeSelectedCurrency(currency);
|
|
|
|
key.currentState.changeWalletName(
|
|
|
|
isCurrentTypeWallet ? exchangeViewModel.wallet.name : null);
|
|
|
|
|
|
|
|
key.currentState.changeAddress(
|
2021-07-13 05:46:34 +00:00
|
|
|
address: isCurrentTypeWallet
|
|
|
|
? exchangeViewModel.wallet.walletAddresses.address : '');
|
2020-09-22 18:32:43 +00:00
|
|
|
|
|
|
|
key.currentState.changeAmount(amount: '');
|
|
|
|
}
|
|
|
|
|
|
|
|
void _onWalletNameChange(ExchangeViewModel exchangeViewModel,
|
|
|
|
CryptoCurrency currency, GlobalKey<ExchangeCardState> key) {
|
|
|
|
final isCurrentTypeWallet = currency == exchangeViewModel.wallet.currency;
|
|
|
|
|
|
|
|
if (isCurrentTypeWallet) {
|
|
|
|
key.currentState.changeWalletName(exchangeViewModel.wallet.name);
|
|
|
|
key.currentState.addressController.text =
|
2021-07-13 05:46:34 +00:00
|
|
|
exchangeViewModel.wallet.walletAddresses.address;
|
2020-09-22 18:32:43 +00:00
|
|
|
} else if (key.currentState.addressController.text ==
|
2021-07-13 05:46:34 +00:00
|
|
|
exchangeViewModel.wallet.walletAddresses.address) {
|
2020-09-22 18:32:43 +00:00
|
|
|
key.currentState.changeWalletName(null);
|
|
|
|
key.currentState.addressController.text = null;
|
|
|
|
}
|
|
|
|
}
|
2021-07-07 14:31:16 +00:00
|
|
|
|
2021-09-20 14:56:27 +00:00
|
|
|
Future<String> fetchParsedAddress(
|
2021-07-07 14:31:16 +00:00
|
|
|
BuildContext context, String domain, String ticker) async {
|
|
|
|
final parsedAddress = await parseAddressFromDomain(domain, ticker);
|
2021-09-29 11:38:31 +00:00
|
|
|
final address = await extractAddressFromParsed(context, parsedAddress);
|
2021-09-20 14:56:27 +00:00
|
|
|
return address;
|
2021-07-07 14:31:16 +00:00
|
|
|
}
|
2020-01-04 19:31:52 +00:00
|
|
|
}
|