From 317ef8f3e4414749569b74f83f4925b80d9c3400 Mon Sep 17 00:00:00 2001 From: OleksandrSobol Date: Tue, 16 Feb 2021 21:06:23 +0200 Subject: [PATCH] CAKE-278 | added isReceiveAmountEditable and isFixedRateMode to exchange_view_model.dart; fixed changeReceiveAmount() in the exchange_view_model.dart; applied StandardCheckbox to exchange_page.dart for checking fixed rate mode; applied alert dialog to exchange_page.dart when receive amount has focus and fixed rate mode is not checked; added reaction for isFixedRateMode in the exchange_page.dart; added key and changeValue() to standard_checkbox.dart --- lib/src/screens/exchange/exchange_page.dart | 57 +++++++++++++++++-- lib/src/widgets/standard_checkbox.dart | 7 ++- .../exchange/exchange_view_model.dart | 14 ++++- 3 files changed, 71 insertions(+), 7 deletions(-) diff --git a/lib/src/screens/exchange/exchange_page.dart b/lib/src/screens/exchange/exchange_page.dart index 55cd9c8ad..20b7a842f 100644 --- a/lib/src/screens/exchange/exchange_page.dart +++ b/lib/src/screens/exchange/exchange_page.dart @@ -1,6 +1,8 @@ import 'dart:ui'; import 'package:cake_wallet/entities/sync_status.dart'; import 'package:cake_wallet/entities/wallet_type.dart'; +import 'package:cake_wallet/exchange/changenow/changenow_exchange_provider.dart'; +import 'package:cake_wallet/src/widgets/standard_checkbox.dart'; import 'package:dotted_border/dotted_border.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; @@ -37,6 +39,7 @@ class ExchangePage extends BasePage { final ExchangeViewModel exchangeViewModel; final depositKey = GlobalKey(); final receiveKey = GlobalKey(); + final checkBoxKey = GlobalKey(); final _formKey = GlobalKey(); final _depositAmountFocus = FocusNode(); final _receiveAmountFocus = FocusNode(); @@ -240,9 +243,7 @@ class ExchangePage extends BasePage { ? exchangeViewModel.wallet.address : exchangeViewModel.receiveAddress, initialIsAmountEditable: exchangeViewModel - .provider is XMRTOExchangeProvider - ? true - : false, + .isReceiveAmountEditable, initialIsAddressEditable: exchangeViewModel .isReceiveAddressEnabled, @@ -271,6 +272,21 @@ class ExchangePage extends BasePage { ], ), ), + Padding( + padding: EdgeInsets.only(top: 12, left: 24), + child: Row( + mainAxisAlignment: MainAxisAlignment.start, + children: [ + StandardCheckbox( + key: checkBoxKey, + value: exchangeViewModel.isFixedRateMode, + caption: 'Fixed rate', // FIXME + onChanged: (value) => + exchangeViewModel.isFixedRateMode = value, + ), + ], + ) + ), Padding( padding: EdgeInsets.only(top: 30, left: 24, bottom: 24), child: Row( @@ -567,7 +583,7 @@ class ExchangePage extends BasePage { }); reaction((_) => exchangeViewModel.provider, (ExchangeProvider provider) { - provider is XMRTOExchangeProvider + provider is ChangeNowExchangeProvider ? receiveKey.currentState.isAmountEditable(isEditable: true) : receiveKey.currentState.isAmountEditable(isEditable: false); }); @@ -647,6 +663,39 @@ class ExchangePage extends BasePage { } }); + _receiveAmountFocus.addListener(() { + if (_receiveAmountFocus.hasFocus && !exchangeViewModel.isFixedRateMode) { + showPopUp( + context: context, + builder: (BuildContext context) { + return AlertWithTwoActions( + alertTitle: S.of(context).exchange, + alertContent: 'You will be able to enter receive amount when fixed rate is checked. Do you want to check fixed rate?', //FIXME + leftButtonText: S.of(context).cancel, + rightButtonText: S.of(context).ok, + actionLeftButton: () { + FocusScope.of(context).unfocus(); + receiveAmountController.text = ''; + Navigator.of(context).pop(); + }, + actionRightButton: () { + exchangeViewModel.isFixedRateMode = true; + checkBoxKey.currentState + .changeValue(exchangeViewModel.isFixedRateMode); + Navigator.of(context).pop(); + }); + }); + } + }); + + reaction((_) => exchangeViewModel.isFixedRateMode, (bool isFixedRateMode) { + if ((_receiveAmountFocus.hasFocus || + exchangeViewModel.isReceiveAmountEntered) && !isFixedRateMode) { + FocusScope.of(context).unfocus(); + receiveAmountController.text = ''; + } + }); + _isReactionsSet = true; } diff --git a/lib/src/widgets/standard_checkbox.dart b/lib/src/widgets/standard_checkbox.dart index 774acc9b7..64ae95acc 100644 --- a/lib/src/widgets/standard_checkbox.dart +++ b/lib/src/widgets/standard_checkbox.dart @@ -4,9 +4,10 @@ import 'package:flutter/material.dart'; class StandardCheckbox extends StatefulWidget { StandardCheckbox({ + Key key, @required this.value, this.caption = '', - @required this.onChanged}); + @required this.onChanged}) : super(key: key); final bool value; final String caption; @@ -24,6 +25,10 @@ class StandardCheckboxState extends State { String caption; Function(bool) onChanged; + void changeValue(bool newValue) { + setState(() => value = newValue); + } + @override Widget build(BuildContext context) { return GestureDetector( diff --git a/lib/view_model/exchange/exchange_view_model.dart b/lib/view_model/exchange/exchange_view_model.dart index 90c62593d..506df787b 100644 --- a/lib/view_model/exchange/exchange_view_model.dart +++ b/lib/view_model/exchange/exchange_view_model.dart @@ -58,6 +58,9 @@ abstract class ExchangeViewModelBase with Store { } }); + isReceiveAmountEditable = provider is ChangeNowExchangeProvider; + isFixedRateMode = false; + isReceiveAmountEntered = false; loadLimits(); } @@ -106,6 +109,12 @@ abstract class ExchangeViewModelBase with Store { @observable bool isReceiveAmountEntered; + @observable + bool isReceiveAmountEditable; + + @observable + bool isFixedRateMode; + @computed SyncStatus get status => wallet.syncStatus; @@ -127,6 +136,7 @@ abstract class ExchangeViewModelBase with Store { this.provider = provider; depositAmount = ''; receiveAmount = ''; + isReceiveAmountEditable = provider is ChangeNowExchangeProvider; loadLimits(); } @@ -160,8 +170,8 @@ abstract class ExchangeViewModelBase with Store { provider .calculateAmount( - from: depositCurrency, - to: receiveCurrency, + from: receiveCurrency, + to: depositCurrency, amount: _amount, isReceiveAmount: true) .then((amount) => _cryptoNumberFormat